FMResultSet.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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` 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. /** Did the last call to `<next>` succeed in retrieving another row?
  56. @return `YES` if the last call to `<next>` succeeded in retrieving another record; `NO` if not.
  57. @see next
  58. @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.
  59. */
  60. - (BOOL)hasAnotherRow;
  61. ///---------------------------------------------
  62. /// @name Retrieving information from result set
  63. ///---------------------------------------------
  64. /** How many columns in result set
  65. @return Integer value of the number of columns.
  66. */
  67. - (int)columnCount;
  68. /** Column index for column name
  69. @param columnName `NSString` value of the name of the column.
  70. @return Zero-based index for column.
  71. */
  72. - (int)columnIndexForName:(NSString*)columnName;
  73. /** Column name for column index
  74. @param columnIdx Zero-based index for column.
  75. @return columnName `NSString` value of the name of the column.
  76. */
  77. - (NSString*)columnNameForIndex:(int)columnIdx;
  78. /** Result set integer value for column.
  79. @param columnName `NSString` value of the name of the column.
  80. @return `int` value of the result set's column.
  81. */
  82. - (int)intForColumn:(NSString*)columnName;
  83. /** Result set integer value for column.
  84. @param columnIdx Zero-based index for column.
  85. @return `int` value of the result set's column.
  86. */
  87. - (int)intForColumnIndex:(int)columnIdx;
  88. /** Result set `long` value for column.
  89. @param columnName `NSString` value of the name of the column.
  90. @return `long` value of the result set's column.
  91. */
  92. - (long)longForColumn:(NSString*)columnName;
  93. /** Result set long value for column.
  94. @param columnIdx Zero-based index for column.
  95. @return `long` value of the result set's column.
  96. */
  97. - (long)longForColumnIndex:(int)columnIdx;
  98. /** Result set `long long int` value for column.
  99. @param columnName `NSString` value of the name of the column.
  100. @return `long long int` value of the result set's column.
  101. */
  102. - (long long int)longLongIntForColumn:(NSString*)columnName;
  103. /** Result set `long long int` value for column.
  104. @param columnIdx Zero-based index for column.
  105. @return `long long int` value of the result set's column.
  106. */
  107. - (long long int)longLongIntForColumnIndex:(int)columnIdx;
  108. /** Result set `unsigned long long int` value for column.
  109. @param columnName `NSString` value of the name of the column.
  110. @return `unsigned long long int` value of the result set's column.
  111. */
  112. - (unsigned long long int)unsignedLongLongIntForColumn:(NSString*)columnName;
  113. /** Result set `unsigned long long int` value for column.
  114. @param columnIdx Zero-based index for column.
  115. @return `unsigned long long int` value of the result set's column.
  116. */
  117. - (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx;
  118. /** Result set `BOOL` value for column.
  119. @param columnName `NSString` value of the name of the column.
  120. @return `BOOL` value of the result set's column.
  121. */
  122. - (BOOL)boolForColumn:(NSString*)columnName;
  123. /** Result set `BOOL` value for column.
  124. @param columnIdx Zero-based index for column.
  125. @return `BOOL` value of the result set's column.
  126. */
  127. - (BOOL)boolForColumnIndex:(int)columnIdx;
  128. /** Result set `double` value for column.
  129. @param columnName `NSString` value of the name of the column.
  130. @return `double` value of the result set's column.
  131. */
  132. - (double)doubleForColumn:(NSString*)columnName;
  133. /** Result set `double` value for column.
  134. @param columnIdx Zero-based index for column.
  135. @return `double` value of the result set's column.
  136. */
  137. - (double)doubleForColumnIndex:(int)columnIdx;
  138. /** Result set `NSString` value for column.
  139. @param columnName `NSString` value of the name of the column.
  140. @return `NSString` value of the result set's column.
  141. */
  142. - (NSString*)stringForColumn:(NSString*)columnName;
  143. /** Result set `NSString` value for column.
  144. @param columnIdx Zero-based index for column.
  145. @return `NSString` value of the result set's column.
  146. */
  147. - (NSString*)stringForColumnIndex:(int)columnIdx;
  148. /** Result set `NSDate` value for column.
  149. @param columnName `NSString` value of the name of the column.
  150. @return `NSDate` value of the result set's column.
  151. */
  152. - (NSDate*)dateForColumn:(NSString*)columnName;
  153. /** Result set `NSDate` value for column.
  154. @param columnIdx Zero-based index for column.
  155. @return `NSDate` value of the result set's column.
  156. */
  157. - (NSDate*)dateForColumnIndex:(int)columnIdx;
  158. /** Result set `NSData` value for column.
  159. This is useful when storing binary data in table (such as image or the like).
  160. @param columnName `NSString` value of the name of the column.
  161. @return `NSData` value of the result set's column.
  162. */
  163. - (NSData*)dataForColumn:(NSString*)columnName;
  164. /** Result set `NSData` value for column.
  165. @param columnIdx Zero-based index for column.
  166. @return `NSData` value of the result set's column.
  167. */
  168. - (NSData*)dataForColumnIndex:(int)columnIdx;
  169. /** Result set `(const unsigned char *)` value for column.
  170. @param columnName `NSString` value of the name of the column.
  171. @return `(const unsigned char *)` value of the result set's column.
  172. */
  173. - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName;
  174. /** Result set `(const unsigned char *)` value for column.
  175. @param columnIdx Zero-based index for column.
  176. @return `(const unsigned char *)` value of the result set's column.
  177. */
  178. - (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx;
  179. /** Result set object for column.
  180. @param columnName `NSString` value of the name of the column.
  181. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  182. @see objectForKeyedSubscript:
  183. */
  184. - (id)objectForColumnName:(NSString*)columnName;
  185. /** Result set object for column.
  186. @param columnIdx Zero-based index for column.
  187. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  188. @see objectAtIndexedSubscript:
  189. */
  190. - (id)objectForColumnIndex:(int)columnIdx;
  191. /** Result set object for column.
  192. 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:
  193. id result = rs[@"employee_name"];
  194. This simplified syntax is equivalent to calling:
  195. id result = [rs objectForKeyedSubscript:@"employee_name"];
  196. which is, it turns out, equivalent to calling:
  197. id result = [rs objectForColumnName:@"employee_name"];
  198. @param columnName `NSString` value of the name of the column.
  199. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  200. */
  201. - (id)objectForKeyedSubscript:(NSString *)columnName;
  202. /** Result set object for column.
  203. 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:
  204. id result = rs[0];
  205. This simplified syntax is equivalent to calling:
  206. id result = [rs objectForKeyedSubscript:0];
  207. which is, it turns out, equivalent to calling:
  208. id result = [rs objectForColumnName:0];
  209. @param columnIdx Zero-based index for column.
  210. @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
  211. */
  212. - (id)objectAtIndexedSubscript:(int)columnIdx;
  213. /** Result set `NSData` value for column.
  214. @param columnName `NSString` value of the name of the column.
  215. @return `NSData` value of the result set's column.
  216. @warning If you are going to use this data after you iterate over the next row, or after you close the
  217. result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
  218. If you don't, you're going to be in a world of hurt when you try and use the data.
  219. */
  220. - (NSData*)dataNoCopyForColumn:(NSString*)columnName NS_RETURNS_NOT_RETAINED;
  221. /** Result set `NSData` value for column.
  222. @param columnIdx Zero-based index for column.
  223. @return `NSData` value of the result set's column.
  224. @warning If you are going to use this data after you iterate over the next row, or after you close the
  225. result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
  226. If you don't, you're going to be in a world of hurt when you try and use the data.
  227. */
  228. - (NSData*)dataNoCopyForColumnIndex:(int)columnIdx NS_RETURNS_NOT_RETAINED;
  229. /** Is the column `NULL`?
  230. @param columnIdx Zero-based index for column.
  231. @return `YES` if column is `NULL`; `NO` if not `NULL`.
  232. */
  233. - (BOOL)columnIndexIsNull:(int)columnIdx;
  234. /** Is the column `NULL`?
  235. @param columnName `NSString` value of the name of the column.
  236. @return `YES` if column is `NULL`; `NO` if not `NULL`.
  237. */
  238. - (BOOL)columnIsNull:(NSString*)columnName;
  239. /** Returns a dictionary of the row results mapped to case sensitive keys of the column names.
  240. @returns `NSDictionary` of the row results.
  241. @warning The keys to the dictionary are case sensitive of the column names.
  242. */
  243. - (NSDictionary*)resultDictionary;
  244. /** Returns a dictionary of the row results
  245. @see resultDictionary
  246. @warning **Deprecated**: Please use `<resultDictionary>` instead. Also, beware that `<resultDictionary>` is case sensitive!
  247. */
  248. - (NSDictionary*)resultDict __attribute__ ((deprecated));
  249. ///-----------------------------
  250. /// @name Key value coding magic
  251. ///-----------------------------
  252. /** Performs `setValue` to yield support for key value observing.
  253. @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.
  254. */
  255. - (void)kvcMagic:(id)object;
  256. @end