FMResultSet.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. /** Types for columns in a result set.
  16. */
  17. typedef NS_ENUM(int, SqliteValueType) {
  18. SqliteValueTypeInteger = 1,
  19. SqliteValueTypeFloat = 2,
  20. SqliteValueTypeText = 3,
  21. SqliteValueTypeBlob = 4,
  22. SqliteValueTypeNull = 5
  23. };
  24. /** Represents the results of executing a query on an @c FMDatabase .
  25. See also
  26. - @c FMDatabase
  27. */
  28. @interface FMResultSet : NSObject
  29. @property (nonatomic, retain, nullable) FMDatabase *parentDB;
  30. ///-----------------
  31. /// @name Properties
  32. ///-----------------
  33. /** Executed query */
  34. @property (atomic, retain, nullable) NSString *query;
  35. /** `NSMutableDictionary` mapping column names to numeric index */
  36. @property (readonly) NSMutableDictionary *columnNameToIndexMap;
  37. /** `FMStatement` used by result set. */
  38. @property (atomic, retain, nullable) FMStatement *statement;
  39. ///------------------------------------
  40. /// @name Creating and closing a result set
  41. ///------------------------------------
  42. /** Close result set */
  43. - (void)close;
  44. ///---------------------------------------
  45. /// @name Iterating through the result set
  46. ///---------------------------------------
  47. /** Retrieve next row for result set.
  48. You must always invoke `next` or `nextWithError` before attempting to access the values returned in a query, even if you're only expecting one.
  49. @return @c YES if row successfully retrieved; @c NO if end of result set reached
  50. @see hasAnotherRow
  51. */
  52. - (BOOL)next;
  53. /** Retrieve next row for result set.
  54. You must always invoke `next` or `nextWithError` before attempting to access the values returned in a query, even if you're only expecting one.
  55. @param outErr A 'NSError' object to receive any error object (if any).
  56. @return 'YES' if row successfully retrieved; 'NO' if end of result set reached
  57. @see hasAnotherRow
  58. */
  59. - (BOOL)nextWithError:(NSError * _Nullable __autoreleasing *)outErr;
  60. /** Perform SQL statement.
  61. @return 'YES' if successful; 'NO' if not.
  62. @see hasAnotherRow
  63. */
  64. - (BOOL)step;
  65. /** Perform SQL statement.
  66. @param outErr A 'NSError' object to receive any error object (if any).
  67. @return 'YES' if successful; 'NO' if not.
  68. @see hasAnotherRow
  69. */
  70. - (BOOL)stepWithError:(NSError * _Nullable __autoreleasing *)outErr;
  71. /** Did the last call to `<next>` succeed in retrieving another row?
  72. @return 'YES' if there is another row; 'NO' if not.
  73. @see next
  74. @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 @c NO, whether there is another row of data or not.
  75. */
  76. - (BOOL)hasAnotherRow;
  77. ///---------------------------------------------
  78. /// @name Retrieving information from result set
  79. ///---------------------------------------------
  80. /** How many columns in result set
  81. @return Integer value of the number of columns.
  82. */
  83. @property (nonatomic, readonly) int columnCount;
  84. /** Column index for column name
  85. @param columnName @c NSString value of the name of the column.
  86. @return Zero-based index for column.
  87. */
  88. - (int)columnIndexForName:(NSString*)columnName;
  89. /** Column name for column index
  90. @param columnIdx Zero-based index for column.
  91. @return columnName @c NSString value of the name of the column.
  92. */
  93. - (NSString * _Nullable)columnNameForIndex:(int)columnIdx;
  94. /** Result set integer value for column.
  95. @param columnName @c NSString value of the name of the column.
  96. @return @c int value of the result set's column.
  97. */
  98. - (int)intForColumn:(NSString*)columnName;
  99. /** Result set integer value for column.
  100. @param columnIdx Zero-based index for column.
  101. @return @c int value of the result set's column.
  102. */
  103. - (int)intForColumnIndex:(int)columnIdx;
  104. /** Result set @c long value for column.
  105. @param columnName @c NSString value of the name of the column.
  106. @return @c long value of the result set's column.
  107. */
  108. - (long)longForColumn:(NSString*)columnName;
  109. /** Result set long value for column.
  110. @param columnIdx Zero-based index for column.
  111. @return @c long value of the result set's column.
  112. */
  113. - (long)longForColumnIndex:(int)columnIdx;
  114. /** Result set `long long int` value for column.
  115. @param columnName @c NSString value of the name of the column.
  116. @return `long long int` value of the result set's column.
  117. */
  118. - (long long int)longLongIntForColumn:(NSString*)columnName;
  119. /** Result set `long long int` value for column.
  120. @param columnIdx Zero-based index for column.
  121. @return `long long int` value of the result set's column.
  122. */
  123. - (long long int)longLongIntForColumnIndex:(int)columnIdx;
  124. /** Result set `unsigned long long int` value for column.
  125. @param columnName @c NSString value of the name of the column.
  126. @return `unsigned long long int` value of the result set's column.
  127. */
  128. - (unsigned long long int)unsignedLongLongIntForColumn:(NSString*)columnName;
  129. /** Result set `unsigned long long int` value for column.
  130. @param columnIdx Zero-based index for column.
  131. @return `unsigned long long int` value of the result set's column.
  132. */
  133. - (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx;
  134. /** Result set `BOOL` value for column.
  135. @param columnName @c NSString value of the name of the column.
  136. @return `BOOL` value of the result set's column.
  137. */
  138. - (BOOL)boolForColumn:(NSString*)columnName;
  139. /** Result set `BOOL` value for column.
  140. @param columnIdx Zero-based index for column.
  141. @return `BOOL` value of the result set's column.
  142. */
  143. - (BOOL)boolForColumnIndex:(int)columnIdx;
  144. /** Result set `double` value for column.
  145. @param columnName @c NSString value of the name of the column.
  146. @return `double` value of the result set's column.
  147. */
  148. - (double)doubleForColumn:(NSString*)columnName;
  149. /** Result set `double` value for column.
  150. @param columnIdx Zero-based index for column.
  151. @return `double` value of the result set's column.
  152. */
  153. - (double)doubleForColumnIndex:(int)columnIdx;
  154. /** Result set @c NSString value for column.
  155. @param columnName @c NSString value of the name of the column.
  156. @return String value of the result set's column.
  157. */
  158. - (NSString * _Nullable)stringForColumn:(NSString*)columnName;
  159. /** Result set @c NSString value for column.
  160. @param columnIdx Zero-based index for column.
  161. @return String value of the result set's column.
  162. */
  163. - (NSString * _Nullable)stringForColumnIndex:(int)columnIdx;
  164. /** Result set @c NSDate value for column.
  165. @param columnName @c NSString value of the name of the column.
  166. @return Date value of the result set's column.
  167. */
  168. - (NSDate * _Nullable)dateForColumn:(NSString*)columnName;
  169. /** Result set @c NSDate value for column.
  170. @param columnIdx Zero-based index for column.
  171. @return Date value of the result set's column.
  172. */
  173. - (NSDate * _Nullable)dateForColumnIndex:(int)columnIdx;
  174. /** Result set @c NSData value for column.
  175. This is useful when storing binary data in table (such as image or the like).
  176. @param columnName @c NSString value of the name of the column.
  177. @return Data value of the result set's column.
  178. */
  179. - (NSData * _Nullable)dataForColumn:(NSString*)columnName;
  180. /** Result set @c NSData value for column.
  181. @param columnIdx Zero-based index for column.
  182. @warning For zero length BLOBs, this will return `nil`. Use `typeForColumn` to determine whether this was really a zero
  183. length BLOB or `NULL`.
  184. @return Data value of the result set's column.
  185. */
  186. - (NSData * _Nullable)dataForColumnIndex:(int)columnIdx;
  187. /** Result set `(const unsigned char *)` value for column.
  188. @param columnName @c NSString value of the name of the column.
  189. @warning For zero length BLOBs, this will return `nil`. Use `typeForColumnIndex` to determine whether this was really a zero
  190. length BLOB or `NULL`.
  191. @return `(const unsigned char *)` value of the result set's column.
  192. */
  193. - (const unsigned char * _Nullable)UTF8StringForColumn:(NSString*)columnName;
  194. - (const unsigned char * _Nullable)UTF8StringForColumnName:(NSString*)columnName __deprecated_msg("Use UTF8StringForColumn instead");
  195. /** Result set `(const unsigned char *)` value for column.
  196. @param columnIdx Zero-based index for column.
  197. @return `(const unsigned char *)` value of the result set's column.
  198. */
  199. - (const unsigned char * _Nullable)UTF8StringForColumnIndex:(int)columnIdx;
  200. /** Result set object for column.
  201. @param columnName Name of the column.
  202. @return Either @c NSNumber , @c NSString , @c NSData , or @c NSNull . If the column was @c NULL , this returns `[NSNull null]` object.
  203. @see objectForKeyedSubscript:
  204. */
  205. - (id _Nullable)objectForColumn:(NSString*)columnName;
  206. - (id _Nullable)objectForColumnName:(NSString*)columnName __deprecated_msg("Use objectForColumn instead");
  207. /** Column type by column name.
  208. @param columnName Name of the column.
  209. @return The `SqliteValueType` of the value in this column.
  210. */
  211. - (SqliteValueType)typeForColumn:(NSString*)columnName;
  212. /** Column type by column index.
  213. @param columnIdx Index of the column.
  214. @return The `SqliteValueType` of the value in this column.
  215. */
  216. - (SqliteValueType)typeForColumnIndex:(int)columnIdx;
  217. /** Result set object for column.
  218. @param columnIdx Zero-based index for column.
  219. @return Either @c NSNumber , @c NSString , @c NSData , or @c NSNull . If the column was @c NULL , this returns `[NSNull null]` object.
  220. @see objectAtIndexedSubscript:
  221. */
  222. - (id _Nullable)objectForColumnIndex:(int)columnIdx;
  223. /** Result set object for column.
  224. 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:
  225. @code
  226. id result = rs[@"employee_name"];
  227. @endcode
  228. This simplified syntax is equivalent to calling:
  229. @code
  230. id result = [rs objectForKeyedSubscript:@"employee_name"];
  231. @endcode
  232. which is, it turns out, equivalent to calling:
  233. @code
  234. id result = [rs objectForColumnName:@"employee_name"];
  235. @endcode
  236. @param columnName @c NSString value of the name of the column.
  237. @return Either @c NSNumber , @c NSString , @c NSData , or @c NSNull . If the column was @c NULL , this returns `[NSNull null]` object.
  238. */
  239. - (id _Nullable)objectForKeyedSubscript:(NSString *)columnName;
  240. /** Result set object for column.
  241. 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:
  242. @code
  243. id result = rs[0];
  244. @endcode
  245. This simplified syntax is equivalent to calling:
  246. @code
  247. id result = [rs objectForKeyedSubscript:0];
  248. @endcode
  249. which is, it turns out, equivalent to calling:
  250. @code
  251. id result = [rs objectForColumnName:0];
  252. @endcode
  253. @param columnIdx Zero-based index for column.
  254. @return Either @c NSNumber , @c NSString , @c NSData , or @c NSNull . If the column was @c NULL , this returns `[NSNull null]` object.
  255. */
  256. - (id _Nullable)objectAtIndexedSubscript:(int)columnIdx;
  257. /** Result set @c NSData value for column.
  258. @param columnName @c NSString value of the name of the column.
  259. @return Data value of the result set's column.
  260. @warning If you are going to use this data after you iterate over the next row, or after you close the
  261. result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
  262. If you don't, you're going to be in a world of hurt when you try and use the data.
  263. */
  264. - (NSData * _Nullable)dataNoCopyForColumn:(NSString *)columnName NS_RETURNS_NOT_RETAINED;
  265. /** Result set @c NSData value for column.
  266. @param columnIdx Zero-based index for column.
  267. @return Data value of the result set's column.
  268. @warning If you are going to use this data after you iterate over the next row, or after you close the
  269. result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
  270. If you don't, you're going to be in a world of hurt when you try and use the data.
  271. */
  272. - (NSData * _Nullable)dataNoCopyForColumnIndex:(int)columnIdx NS_RETURNS_NOT_RETAINED;
  273. /** Is the column @c NULL ?
  274. @param columnIdx Zero-based index for column.
  275. @return @c YES if column is @c NULL ; @c NO if not @c NULL .
  276. */
  277. - (BOOL)columnIndexIsNull:(int)columnIdx;
  278. /** Is the column @c NULL ?
  279. @param columnName @c NSString value of the name of the column.
  280. @return @c YES if column is @c NULL ; @c NO if not @c NULL .
  281. */
  282. - (BOOL)columnIsNull:(NSString*)columnName;
  283. /** Returns a dictionary of the row results mapped to case sensitive keys of the column names.
  284. @warning The keys to the dictionary are case sensitive of the column names.
  285. */
  286. @property (nonatomic, readonly, nullable) NSDictionary *resultDictionary;
  287. /** Returns a dictionary of the row results
  288. @see resultDictionary
  289. @warning **Deprecated**: Please use `<resultDictionary>` instead. Also, beware that `<resultDictionary>` is case sensitive!
  290. */
  291. - (NSDictionary * _Nullable)resultDict __deprecated_msg("Use resultDictionary instead");
  292. ///-----------------------------
  293. /// @name Key value coding magic
  294. ///-----------------------------
  295. /** Performs `setValue` to yield support for key value observing.
  296. @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.
  297. */
  298. - (void)kvcMagic:(id)object;
  299. ///-----------------------------
  300. /// @name Binding values
  301. ///-----------------------------
  302. /// Bind array of values to prepared statement.
  303. ///
  304. /// @param array Array of values to bind to SQL statement.
  305. - (BOOL)bindWithArray:(NSArray*)array;
  306. /// Bind dictionary of values to prepared statement.
  307. ///
  308. /// @param dictionary Dictionary of values to bind to SQL statement.
  309. - (BOOL)bindWithDictionary:(NSDictionary *)dictionary;
  310. @end
  311. NS_ASSUME_NONNULL_END