FMResultSet.h 12 KB

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