FMResultSet.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #import <Foundation/Foundation.h>
  2. #import "sqlite3.h"
  3. #ifndef __has_feature // Optional.
  4. #define __has_feature(x) 0 // Compatibility with non-clang compilers.
  5. #endif
  6. #ifndef NS_RETURNS_NOT_RETAINED
  7. #if __has_feature(attribute_ns_returns_not_retained)
  8. #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
  9. #else
  10. #define NS_RETURNS_NOT_RETAINED
  11. #endif
  12. #endif
  13. @class FMDatabase;
  14. @class FMStatement;
  15. /** Represents the results of executing a query on an `<FMDatabase>`.
  16. ### See also
  17. - `<FMDatabase>`
  18. */
  19. @interface FMResultSet : NSObject {
  20. FMDatabase *_parentDB;
  21. FMStatement *_statement;
  22. NSString *_query;
  23. NSMutableDictionary *_columnNameToIndexMap;
  24. }
  25. ///-----------------
  26. /// @name Properties
  27. ///-----------------
  28. /** Executed query */
  29. @property (atomic, retain) NSString *query;
  30. /** `NSMutableDictionary` mapping column names to numeric index */
  31. @property (readonly) NSMutableDictionary *columnNameToIndexMap;
  32. /** `FMStatement` used by result set. */
  33. @property (atomic, retain) FMStatement *statement;
  34. ///------------------------------------
  35. /// @name Creating and closing database
  36. ///------------------------------------
  37. /** Create result set from `<FMStatement>`
  38. @param statement A `<FMStatement>` to be performed
  39. @param aDB A `<FMDatabase>` to be used
  40. @return A `FMResultSet` on success; `nil` on failure
  41. */
  42. + (instancetype)resultSetWithStatement:(FMStatement *)statement usingParentDatabase:(FMDatabase*)aDB;
  43. /** Close result set */
  44. - (void)close;
  45. - (void)setParentDB:(FMDatabase *)newDb;
  46. ///---------------------------------------
  47. /// @name Iterating through the result set
  48. ///---------------------------------------
  49. /** Retrieve next row for result set.
  50. You must always invoke `next` or `nextWithError` before attempting to access the values returned in a query, even if you're only expecting one.
  51. @return `YES` if row successfully retrieved; `NO` if end of result set reached
  52. @see hasAnotherRow
  53. */
  54. - (BOOL)next;
  55. /** Retrieve next row for result set.
  56. You must always invoke `next` or `nextWithError` before attempting to access the values returned in a query, even if you're only expecting one.
  57. @param outErr A 'NSError' object to receive any error object (if any).
  58. @return 'YES' if row successfully retrieved; 'NO' if end of result set reached
  59. @see hasAnotherRow
  60. */
  61. - (BOOL)nextWithError:(NSError **)outErr;
  62. /** Did the last call to `<next>` succeed in retrieving another row?
  63. @return `YES` if the last call to `<next>` succeeded in retrieving another record; `NO` if not.
  64. @see next
  65. @warning The `hasAnotherRow` method must follow a call to `<next>`. If the previous database interaction was something other than a call to `next`, then this method may return `NO`, whether there is another row of data or not.
  66. */
  67. - (BOOL)hasAnotherRow;
  68. ///---------------------------------------------
  69. /// @name Retrieving information from result set
  70. ///---------------------------------------------
  71. /** How many columns in result set
  72. @return Integer value of the number of columns.
  73. */
  74. - (int)columnCount;
  75. /** Column index for column name
  76. @param columnName `NSString` value of the name of the column.
  77. @return Zero-based index for column.
  78. */
  79. - (int)columnIndexForName:(NSString*)columnName;
  80. /** Column name for column index
  81. @param columnIdx Zero-based index for column.
  82. @return columnName `NSString` value of the name of the column.
  83. */
  84. - (NSString*)columnNameForIndex:(int)columnIdx;
  85. /** Result set integer value for column.
  86. @param columnName `NSString` value of the name of the column.
  87. @return `int` value of the result set's column.
  88. */
  89. - (int)intForColumn:(NSString*)columnName;
  90. /** Result set integer value for column.
  91. @param columnIdx Zero-based index for column.
  92. @return `int` value of the result set's column.
  93. */
  94. - (int)intForColumnIndex:(int)columnIdx;
  95. /** Result set `long` value for column.
  96. @param columnName `NSString` value of the name of the column.
  97. @return `long` value of the result set's column.
  98. */
  99. - (long)longForColumn:(NSString*)columnName;
  100. /** Result set long value for column.
  101. @param columnIdx Zero-based index for column.
  102. @return `long` value of the result set's column.
  103. */
  104. - (long)longForColumnIndex:(int)columnIdx;
  105. /** Result set `long long int` value for column.
  106. @param columnName `NSString` value of the name of the column.
  107. @return `long long int` value of the result set's column.
  108. */
  109. - (long long int)longLongIntForColumn:(NSString*)columnName;
  110. /** Result set `long long int` value for column.
  111. @param columnIdx Zero-based index for column.
  112. @return `long long int` value of the result set's column.
  113. */
  114. - (long long int)longLongIntForColumnIndex:(int)columnIdx;
  115. /** Result set `unsigned long long int` value for column.
  116. @param columnName `NSString` value of the name of the column.
  117. @return `unsigned long long int` value of the result set's column.
  118. */
  119. - (unsigned long long int)unsignedLongLongIntForColumn:(NSString*)columnName;
  120. /** Result set `unsigned long long int` value for column.
  121. @param columnIdx Zero-based index for column.
  122. @return `unsigned long long int` value of the result set's column.
  123. */
  124. - (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx;
  125. /** Result set `BOOL` value for column.
  126. @param columnName `NSString` value of the name of the column.
  127. @return `BOOL` value of the result set's column.
  128. */
  129. - (BOOL)boolForColumn:(NSString*)columnName;
  130. /** Result set `BOOL` value for column.
  131. @param columnIdx Zero-based index for column.
  132. @return `BOOL` value of the result set's column.
  133. */
  134. - (BOOL)boolForColumnIndex:(int)columnIdx;
  135. /** Result set `double` value for column.
  136. @param columnName `NSString` value of the name of the column.
  137. @return `double` value of the result set's column.
  138. */
  139. - (double)doubleForColumn:(NSString*)columnName;
  140. /** Result set `double` value for column.
  141. @param columnIdx Zero-based index for column.
  142. @return `double` value of the result set's column.
  143. */
  144. - (double)doubleForColumnIndex:(int)columnIdx;
  145. /** Result set `NSString` value for column.
  146. @param columnName `NSString` value of the name of the column.
  147. @return `NSString` value of the result set's column.
  148. */
  149. - (NSString*)stringForColumn:(NSString*)columnName;
  150. /** Result set `NSString` value for column.
  151. @param columnIdx Zero-based index for column.
  152. @return `NSString` value of the result set's column.
  153. */
  154. - (NSString*)stringForColumnIndex:(int)columnIdx;
  155. /** Result set `NSDate` value for column.
  156. @param columnName `NSString` value of the name of the column.
  157. @return `NSDate` value of the result set's column.
  158. */
  159. - (NSDate*)dateForColumn:(NSString*)columnName;
  160. /** Result set `NSDate` value for column.
  161. @param columnIdx Zero-based index for column.
  162. @return `NSDate` value of the result set's column.
  163. */
  164. - (NSDate*)dateForColumnIndex:(int)columnIdx;
  165. /** Result set `NSData` value for column.
  166. This is useful when storing binary data in table (such as image or the like).
  167. @param columnName `NSString` value of the name of the column.
  168. @return `NSData` value of the result set's column.
  169. */
  170. - (NSData*)dataForColumn:(NSString*)columnName;
  171. /** Result set `NSData` value for column.
  172. @param columnIdx Zero-based index for column.
  173. @return `NSData` value of the result set's column.
  174. */
  175. - (NSData*)dataForColumnIndex:(int)columnIdx;
  176. /** Result set `(const unsigned char *)` value for column.
  177. @param columnName `NSString` value of the name of the column.
  178. @return `(const unsigned char *)` value of the result set's column.
  179. */
  180. - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName;
  181. /** Result set `(const unsigned char *)` value for column.
  182. @param columnIdx Zero-based index for column.
  183. @return `(const unsigned char *)` value of the result set's column.
  184. */
  185. - (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx;
  186. /** Result set object for column.
  187. @param columnName `NSString` value of the name of the column.
  188. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  189. @see objectForKeyedSubscript:
  190. */
  191. - (id)objectForColumnName:(NSString*)columnName;
  192. /** Result set object for column.
  193. @param columnIdx Zero-based index for column.
  194. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  195. @see objectAtIndexedSubscript:
  196. */
  197. - (id)objectForColumnIndex:(int)columnIdx;
  198. /** Result set object for column.
  199. This method allows the use of the "boxed" syntax supported in Modern Objective-C. For example, by defining this method, the following syntax is now supported:
  200. id result = rs[@"employee_name"];
  201. This simplified syntax is equivalent to calling:
  202. id result = [rs objectForKeyedSubscript:@"employee_name"];
  203. which is, it turns out, equivalent to calling:
  204. id result = [rs objectForColumnName:@"employee_name"];
  205. @param columnName `NSString` value of the name of the column.
  206. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  207. */
  208. - (id)objectForKeyedSubscript:(NSString *)columnName;
  209. /** Result set object for column.
  210. This method allows the use of the "boxed" syntax supported in Modern Objective-C. For example, by defining this method, the following syntax is now supported:
  211. id result = rs[0];
  212. This simplified syntax is equivalent to calling:
  213. id result = [rs objectForKeyedSubscript:0];
  214. which is, it turns out, equivalent to calling:
  215. id result = [rs objectForColumnName:0];
  216. @param columnIdx Zero-based index for column.
  217. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  218. */
  219. - (id)objectAtIndexedSubscript:(int)columnIdx;
  220. /** Result set `NSData` value for column.
  221. @param columnName `NSString` value of the name of the column.
  222. @return `NSData` value of the result set's column.
  223. @warning If you are going to use this data after you iterate over the next row, or after you close the
  224. result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
  225. If you don't, you're going to be in a world of hurt when you try and use the data.
  226. */
  227. - (NSData*)dataNoCopyForColumn:(NSString*)columnName NS_RETURNS_NOT_RETAINED;
  228. /** Result set `NSData` value for column.
  229. @param columnIdx Zero-based index for column.
  230. @return `NSData` value of the result set's column.
  231. @warning If you are going to use this data after you iterate over the next row, or after you close the
  232. result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
  233. If you don't, you're going to be in a world of hurt when you try and use the data.
  234. */
  235. - (NSData*)dataNoCopyForColumnIndex:(int)columnIdx NS_RETURNS_NOT_RETAINED;
  236. /** Is the column `NULL`?
  237. @param columnIdx Zero-based index for column.
  238. @return `YES` if column is `NULL`; `NO` if not `NULL`.
  239. */
  240. - (BOOL)columnIndexIsNull:(int)columnIdx;
  241. /** Is the column `NULL`?
  242. @param columnName `NSString` value of the name of the column.
  243. @return `YES` if column is `NULL`; `NO` if not `NULL`.
  244. */
  245. - (BOOL)columnIsNull:(NSString*)columnName;
  246. /** Returns a dictionary of the row results mapped to case sensitive keys of the column names.
  247. @returns `NSDictionary` of the row results.
  248. @warning The keys to the dictionary are case sensitive of the column names.
  249. */
  250. - (NSDictionary*)resultDictionary;
  251. /** Returns a dictionary of the row results
  252. @see resultDictionary
  253. @warning **Deprecated**: Please use `<resultDictionary>` instead. Also, beware that `<resultDictionary>` is case sensitive!
  254. */
  255. - (NSDictionary*)resultDict __attribute__ ((deprecated));
  256. ///-----------------------------
  257. /// @name Key value coding magic
  258. ///-----------------------------
  259. /** Performs `setValue` to yield support for key value observing.
  260. @param object The object for which the values will be set. This is the key-value-coding compliant object that you might, for example, observe.
  261. */
  262. - (void)kvcMagic:(id)object;
  263. @end