FMDatabase.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. #import "FMDatabase.h"
  2. #import "unistd.h"
  3. @implementation FMDatabase
  4. + (id)databaseWithPath:(NSString*)aPath {
  5. return [[[self alloc] initWithPath:aPath] autorelease];
  6. }
  7. - (id)initWithPath:(NSString*)aPath {
  8. self = [super init];
  9. if (self) {
  10. databasePath = [aPath copy];
  11. openResultSets = [[NSMutableSet alloc] init];
  12. db = 0x00;
  13. logsErrors = 0x00;
  14. crashOnErrors = 0x00;
  15. busyRetryTimeout = 0x00;
  16. }
  17. return self;
  18. }
  19. - (void)finalize {
  20. [self close];
  21. [super finalize];
  22. }
  23. - (void)dealloc {
  24. [self close];
  25. [openResultSets release];
  26. [cachedStatements release];
  27. [databasePath release];
  28. [super dealloc];
  29. }
  30. + (NSString*)sqliteLibVersion {
  31. return [NSString stringWithFormat:@"%s", sqlite3_libversion()];
  32. }
  33. - (NSString *)databasePath {
  34. return databasePath;
  35. }
  36. - (sqlite3*)sqliteHandle {
  37. return db;
  38. }
  39. - (BOOL)open {
  40. if (db) {
  41. return YES;
  42. }
  43. int err = sqlite3_open((databasePath ? [databasePath fileSystemRepresentation] : ":memory:"), &db );
  44. if(err != SQLITE_OK) {
  45. NSLog(@"error opening!: %d", err);
  46. return NO;
  47. }
  48. return YES;
  49. }
  50. #if SQLITE_VERSION_NUMBER >= 3005000
  51. - (BOOL)openWithFlags:(int)flags {
  52. int err = sqlite3_open_v2((databasePath ? [databasePath fileSystemRepresentation] : ":memory:"), &db, flags, NULL /* Name of VFS module to use */);
  53. if(err != SQLITE_OK) {
  54. NSLog(@"error opening!: %d", err);
  55. return NO;
  56. }
  57. return YES;
  58. }
  59. #endif
  60. - (BOOL)close {
  61. [self clearCachedStatements];
  62. [self closeOpenResultSets];
  63. if (!db) {
  64. return YES;
  65. }
  66. int rc;
  67. BOOL retry;
  68. int numberOfRetries = 0;
  69. do {
  70. retry = NO;
  71. rc = sqlite3_close(db);
  72. if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
  73. retry = YES;
  74. usleep(20);
  75. if (busyRetryTimeout && (numberOfRetries++ > busyRetryTimeout)) {
  76. NSLog(@"%s:%d", __FUNCTION__, __LINE__);
  77. NSLog(@"Database busy, unable to close");
  78. return NO;
  79. }
  80. }
  81. else if (SQLITE_OK != rc) {
  82. NSLog(@"error closing!: %d", rc);
  83. }
  84. }
  85. while (retry);
  86. db = nil;
  87. return YES;
  88. }
  89. - (void)clearCachedStatements {
  90. NSEnumerator *e = [cachedStatements objectEnumerator];
  91. FMStatement *cachedStmt;
  92. while ((cachedStmt = [e nextObject])) {
  93. [cachedStmt close];
  94. }
  95. [cachedStatements removeAllObjects];
  96. }
  97. - (void)closeOpenResultSets {
  98. //Copy the set so we don't get mutation errors
  99. NSSet *resultSets = [[openResultSets copy] autorelease];
  100. NSEnumerator *e = [resultSets objectEnumerator];
  101. NSValue *returnedResultSet = nil;
  102. while((returnedResultSet = [e nextObject])) {
  103. FMResultSet *rs = (FMResultSet *)[returnedResultSet pointerValue];
  104. if ([rs respondsToSelector:@selector(close)]) {
  105. [rs close];
  106. }
  107. }
  108. }
  109. - (void)resultSetDidClose:(FMResultSet *)resultSet {
  110. NSValue *setValue = [NSValue valueWithNonretainedObject:resultSet];
  111. [openResultSets removeObject:setValue];
  112. }
  113. - (FMStatement*)cachedStatementForQuery:(NSString*)query {
  114. return [cachedStatements objectForKey:query];
  115. }
  116. - (void)setCachedStatement:(FMStatement*)statement forQuery:(NSString*)query {
  117. if (!query) {
  118. return;
  119. }
  120. //NSLog(@"setting query: %@", query);
  121. query = [query copy]; // in case we got handed in a mutable string...
  122. [statement setQuery:query];
  123. [cachedStatements setObject:statement forKey:query];
  124. [query release];
  125. }
  126. - (BOOL)rekey:(NSString*)key {
  127. #ifdef SQLITE_HAS_CODEC
  128. if (!key) {
  129. return NO;
  130. }
  131. int rc = sqlite3_rekey(db, [key UTF8String], strlen([key UTF8String]));
  132. if (rc != SQLITE_OK) {
  133. NSLog(@"error on rekey: %d", rc);
  134. NSLog(@"%@", [self lastErrorMessage]);
  135. }
  136. return (rc == SQLITE_OK);
  137. #else
  138. return NO;
  139. #endif
  140. }
  141. - (BOOL)setKey:(NSString*)key {
  142. #ifdef SQLITE_HAS_CODEC
  143. if (!key) {
  144. return NO;
  145. }
  146. int rc = sqlite3_key(db, [key UTF8String], strlen([key UTF8String]));
  147. return (rc == SQLITE_OK);
  148. #else
  149. return NO;
  150. #endif
  151. }
  152. - (BOOL)goodConnection {
  153. if (!db) {
  154. return NO;
  155. }
  156. FMResultSet *rs = [self executeQuery:@"select name from sqlite_master where type='table'"];
  157. if (rs) {
  158. [rs close];
  159. return YES;
  160. }
  161. return NO;
  162. }
  163. - (void)compainAboutInUse {
  164. NSLog(@"The FMDatabase %@ is currently in use.", self);
  165. #ifndef NS_BLOCK_ASSERTIONS
  166. if (crashOnErrors) {
  167. NSAssert1(false, @"The FMDatabase %@ is currently in use.", self);
  168. }
  169. #endif
  170. }
  171. - (NSString*)lastErrorMessage {
  172. return [NSString stringWithUTF8String:sqlite3_errmsg(db)];
  173. }
  174. - (BOOL)hadError {
  175. int lastErrCode = [self lastErrorCode];
  176. return (lastErrCode > SQLITE_OK && lastErrCode < SQLITE_ROW);
  177. }
  178. - (int)lastErrorCode {
  179. return sqlite3_errcode(db);
  180. }
  181. - (sqlite_int64)lastInsertRowId {
  182. if (inUse) {
  183. [self compainAboutInUse];
  184. return NO;
  185. }
  186. [self setInUse:YES];
  187. sqlite_int64 ret = sqlite3_last_insert_rowid(db);
  188. [self setInUse:NO];
  189. return ret;
  190. }
  191. - (int)changes {
  192. if (inUse) {
  193. [self compainAboutInUse];
  194. return 0;
  195. }
  196. [self setInUse:YES];
  197. int ret = sqlite3_changes(db);
  198. [self setInUse:NO];
  199. return ret;
  200. }
  201. - (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
  202. if ((!obj) || ((NSNull *)obj == [NSNull null])) {
  203. sqlite3_bind_null(pStmt, idx);
  204. }
  205. // FIXME - someday check the return codes on these binds.
  206. else if ([obj isKindOfClass:[NSData class]]) {
  207. sqlite3_bind_blob(pStmt, idx, [obj bytes], (int)[obj length], SQLITE_STATIC);
  208. }
  209. else if ([obj isKindOfClass:[NSDate class]]) {
  210. sqlite3_bind_double(pStmt, idx, [obj timeIntervalSince1970]);
  211. }
  212. else if ([obj isKindOfClass:[NSNumber class]]) {
  213. if (strcmp([obj objCType], @encode(BOOL)) == 0) {
  214. sqlite3_bind_int(pStmt, idx, ([obj boolValue] ? 1 : 0));
  215. }
  216. else if (strcmp([obj objCType], @encode(int)) == 0) {
  217. sqlite3_bind_int64(pStmt, idx, [obj longValue]);
  218. }
  219. else if (strcmp([obj objCType], @encode(long)) == 0) {
  220. sqlite3_bind_int64(pStmt, idx, [obj longValue]);
  221. }
  222. else if (strcmp([obj objCType], @encode(long long)) == 0) {
  223. sqlite3_bind_int64(pStmt, idx, [obj longLongValue]);
  224. }
  225. else if (strcmp([obj objCType], @encode(float)) == 0) {
  226. sqlite3_bind_double(pStmt, idx, [obj floatValue]);
  227. }
  228. else if (strcmp([obj objCType], @encode(double)) == 0) {
  229. sqlite3_bind_double(pStmt, idx, [obj doubleValue]);
  230. }
  231. else {
  232. sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);
  233. }
  234. }
  235. else {
  236. sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);
  237. }
  238. }
  239. - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orVAList:(va_list)args {
  240. if (inUse) {
  241. [self compainAboutInUse];
  242. return nil;
  243. }
  244. [self setInUse:YES];
  245. FMResultSet *rs = nil;
  246. int rc = 0x00;;
  247. sqlite3_stmt *pStmt = 0x00;;
  248. FMStatement *statement = 0x00;
  249. if (traceExecution && sql) {
  250. NSLog(@"%@ executeQuery: %@", self, sql);
  251. }
  252. if (shouldCacheStatements) {
  253. statement = [self cachedStatementForQuery:sql];
  254. pStmt = statement ? [statement statement] : 0x00;
  255. }
  256. int numberOfRetries = 0;
  257. BOOL retry = NO;
  258. if (!pStmt) {
  259. do {
  260. retry = NO;
  261. rc = sqlite3_prepare_v2(db, [sql UTF8String], -1, &pStmt, 0);
  262. if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
  263. retry = YES;
  264. usleep(20);
  265. if (busyRetryTimeout && (numberOfRetries++ > busyRetryTimeout)) {
  266. NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);
  267. NSLog(@"Database busy");
  268. sqlite3_finalize(pStmt);
  269. [self setInUse:NO];
  270. return nil;
  271. }
  272. }
  273. else if (SQLITE_OK != rc) {
  274. if (logsErrors) {
  275. NSLog(@"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
  276. NSLog(@"DB Query: %@", sql);
  277. #ifndef NS_BLOCK_ASSERTIONS
  278. if (crashOnErrors) {
  279. NSAssert2(false, @"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
  280. }
  281. #endif
  282. }
  283. sqlite3_finalize(pStmt);
  284. [self setInUse:NO];
  285. return nil;
  286. }
  287. }
  288. while (retry);
  289. }
  290. id obj;
  291. int idx = 0;
  292. int queryCount = sqlite3_bind_parameter_count(pStmt); // pointed out by Dominic Yu (thanks!)
  293. while (idx < queryCount) {
  294. if (arrayArgs) {
  295. obj = [arrayArgs objectAtIndex:idx];
  296. }
  297. else {
  298. obj = va_arg(args, id);
  299. }
  300. if (traceExecution) {
  301. NSLog(@"obj: %@", obj);
  302. }
  303. idx++;
  304. [self bindObject:obj toColumn:idx inStatement:pStmt];
  305. }
  306. if (idx != queryCount) {
  307. NSLog(@"Error: the bind count is not correct for the # of variables (executeQuery)");
  308. sqlite3_finalize(pStmt);
  309. [self setInUse:NO];
  310. return nil;
  311. }
  312. [statement retain]; // to balance the release below
  313. if (!statement) {
  314. statement = [[FMStatement alloc] init];
  315. [statement setStatement:pStmt];
  316. if (shouldCacheStatements) {
  317. [self setCachedStatement:statement forQuery:sql];
  318. }
  319. }
  320. // the statement gets closed in rs's dealloc or [rs close];
  321. rs = [FMResultSet resultSetWithStatement:statement usingParentDatabase:self];
  322. [rs setQuery:sql];
  323. NSValue *openResultSet = [NSValue valueWithNonretainedObject:rs];
  324. [openResultSets addObject:openResultSet];
  325. statement.useCount = statement.useCount + 1;
  326. [statement release];
  327. [self setInUse:NO];
  328. return rs;
  329. }
  330. - (FMResultSet *)executeQuery:(NSString*)sql, ... {
  331. va_list args;
  332. va_start(args, sql);
  333. id result = [self executeQuery:sql withArgumentsInArray:nil orVAList:args];
  334. va_end(args);
  335. return result;
  336. }
  337. - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments {
  338. return [self executeQuery:sql withArgumentsInArray:arguments orVAList:nil];
  339. }
  340. - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orVAList:(va_list)args {
  341. if (inUse) {
  342. [self compainAboutInUse];
  343. return NO;
  344. }
  345. [self setInUse:YES];
  346. int rc = 0x00;
  347. sqlite3_stmt *pStmt = 0x00;
  348. FMStatement *cachedStmt = 0x00;
  349. if (traceExecution && sql) {
  350. NSLog(@"%@ executeUpdate: %@", self, sql);
  351. }
  352. if (shouldCacheStatements) {
  353. cachedStmt = [self cachedStatementForQuery:sql];
  354. pStmt = cachedStmt ? [cachedStmt statement] : 0x00;
  355. }
  356. int numberOfRetries = 0;
  357. BOOL retry = NO;
  358. if (!pStmt) {
  359. do {
  360. retry = NO;
  361. rc = sqlite3_prepare_v2(db, [sql UTF8String], -1, &pStmt, 0);
  362. if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
  363. retry = YES;
  364. usleep(20);
  365. if (busyRetryTimeout && (numberOfRetries++ > busyRetryTimeout)) {
  366. NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);
  367. NSLog(@"Database busy");
  368. sqlite3_finalize(pStmt);
  369. [self setInUse:NO];
  370. return NO;
  371. }
  372. }
  373. else if (SQLITE_OK != rc) {
  374. if (logsErrors) {
  375. NSLog(@"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
  376. NSLog(@"DB Query: %@", sql);
  377. #ifndef NS_BLOCK_ASSERTIONS
  378. if (crashOnErrors) {
  379. NSAssert2(false, @"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
  380. }
  381. #endif
  382. }
  383. sqlite3_finalize(pStmt);
  384. [self setInUse:NO];
  385. if (outErr) {
  386. *outErr = [NSError errorWithDomain:[NSString stringWithUTF8String:sqlite3_errmsg(db)] code:rc userInfo:nil];
  387. }
  388. return NO;
  389. }
  390. }
  391. while (retry);
  392. }
  393. id obj;
  394. int idx = 0;
  395. int queryCount = sqlite3_bind_parameter_count(pStmt);
  396. while (idx < queryCount) {
  397. if (arrayArgs) {
  398. obj = [arrayArgs objectAtIndex:idx];
  399. }
  400. else {
  401. obj = va_arg(args, id);
  402. }
  403. if (traceExecution) {
  404. NSLog(@"obj: %@", obj);
  405. }
  406. idx++;
  407. [self bindObject:obj toColumn:idx inStatement:pStmt];
  408. }
  409. if (idx != queryCount) {
  410. NSLog(@"Error: the bind count is not correct for the # of variables (%@) (executeUpdate)", sql);
  411. sqlite3_finalize(pStmt);
  412. [self setInUse:NO];
  413. return NO;
  414. }
  415. /* Call sqlite3_step() to run the virtual machine. Since the SQL being
  416. ** executed is not a SELECT statement, we assume no data will be returned.
  417. */
  418. numberOfRetries = 0;
  419. do {
  420. rc = sqlite3_step(pStmt);
  421. retry = NO;
  422. if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
  423. // this will happen if the db is locked, like if we are doing an update or insert.
  424. // in that case, retry the step... and maybe wait just 10 milliseconds.
  425. retry = YES;
  426. if (SQLITE_LOCKED == rc) {
  427. rc = sqlite3_reset(pStmt);
  428. if (rc != SQLITE_LOCKED) {
  429. NSLog(@"Unexpected result from sqlite3_reset (%d) eu", rc);
  430. }
  431. }
  432. usleep(20);
  433. if (busyRetryTimeout && (numberOfRetries++ > busyRetryTimeout)) {
  434. NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);
  435. NSLog(@"Database busy");
  436. retry = NO;
  437. }
  438. }
  439. else if (SQLITE_DONE == rc || SQLITE_ROW == rc) {
  440. // all is well, let's return.
  441. }
  442. else if (SQLITE_ERROR == rc) {
  443. NSLog(@"Error calling sqlite3_step (%d: %s) SQLITE_ERROR", rc, sqlite3_errmsg(db));
  444. NSLog(@"DB Query: %@", sql);
  445. }
  446. else if (SQLITE_MISUSE == rc) {
  447. // uh oh.
  448. NSLog(@"Error calling sqlite3_step (%d: %s) SQLITE_MISUSE", rc, sqlite3_errmsg(db));
  449. NSLog(@"DB Query: %@", sql);
  450. }
  451. else {
  452. // wtf?
  453. NSLog(@"Unknown error calling sqlite3_step (%d: %s) eu", rc, sqlite3_errmsg(db));
  454. NSLog(@"DB Query: %@", sql);
  455. }
  456. } while (retry);
  457. assert( rc!=SQLITE_ROW );
  458. if (shouldCacheStatements && !cachedStmt) {
  459. cachedStmt = [[FMStatement alloc] init];
  460. [cachedStmt setStatement:pStmt];
  461. [self setCachedStatement:cachedStmt forQuery:sql];
  462. [cachedStmt release];
  463. }
  464. if (cachedStmt) {
  465. cachedStmt.useCount = cachedStmt.useCount + 1;
  466. rc = sqlite3_reset(pStmt);
  467. }
  468. else {
  469. /* Finalize the virtual machine. This releases all memory and other
  470. ** resources allocated by the sqlite3_prepare() call above.
  471. */
  472. rc = sqlite3_finalize(pStmt);
  473. }
  474. [self setInUse:NO];
  475. return (rc == SQLITE_OK);
  476. }
  477. - (BOOL)executeUpdate:(NSString*)sql, ... {
  478. va_list args;
  479. va_start(args, sql);
  480. BOOL result = [self executeUpdate:sql error:nil withArgumentsInArray:nil orVAList:args];
  481. va_end(args);
  482. return result;
  483. }
  484. - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments {
  485. return [self executeUpdate:sql error:nil withArgumentsInArray:arguments orVAList:nil];
  486. }
  487. - (BOOL)update:(NSString*)sql error:(NSError**)outErr bind:(id)bindArgs, ... {
  488. va_list args;
  489. va_start(args, bindArgs);
  490. BOOL result = [self executeUpdate:sql error:outErr withArgumentsInArray:nil orVAList:args];
  491. va_end(args);
  492. return result;
  493. }
  494. - (BOOL)rollback {
  495. BOOL b = [self executeUpdate:@"ROLLBACK TRANSACTION;"];
  496. if (b) {
  497. inTransaction = NO;
  498. }
  499. return b;
  500. }
  501. - (BOOL)commit {
  502. BOOL b = [self executeUpdate:@"COMMIT TRANSACTION;"];
  503. if (b) {
  504. inTransaction = NO;
  505. }
  506. return b;
  507. }
  508. - (BOOL)beginDeferredTransaction {
  509. BOOL b = [self executeUpdate:@"BEGIN DEFERRED TRANSACTION;"];
  510. if (b) {
  511. inTransaction = YES;
  512. }
  513. return b;
  514. }
  515. - (BOOL)beginTransaction {
  516. BOOL b = [self executeUpdate:@"BEGIN EXCLUSIVE TRANSACTION;"];
  517. if (b) {
  518. inTransaction = YES;
  519. }
  520. return b;
  521. }
  522. - (BOOL)logsErrors {
  523. return logsErrors;
  524. }
  525. - (void)setLogsErrors:(BOOL)flag {
  526. logsErrors = flag;
  527. }
  528. - (BOOL)crashOnErrors {
  529. return crashOnErrors;
  530. }
  531. - (void)setCrashOnErrors:(BOOL)flag {
  532. crashOnErrors = flag;
  533. }
  534. - (BOOL)inUse {
  535. return inUse || inTransaction;
  536. }
  537. - (void)setInUse:(BOOL)b {
  538. inUse = b;
  539. }
  540. - (BOOL)inTransaction {
  541. return inTransaction;
  542. }
  543. - (void)setInTransaction:(BOOL)flag {
  544. inTransaction = flag;
  545. }
  546. - (BOOL)traceExecution {
  547. return traceExecution;
  548. }
  549. - (void)setTraceExecution:(BOOL)flag {
  550. traceExecution = flag;
  551. }
  552. - (BOOL)checkedOut {
  553. return checkedOut;
  554. }
  555. - (void)setCheckedOut:(BOOL)flag {
  556. checkedOut = flag;
  557. }
  558. - (int)busyRetryTimeout {
  559. return busyRetryTimeout;
  560. }
  561. - (void)setBusyRetryTimeout:(int)newBusyRetryTimeout {
  562. busyRetryTimeout = newBusyRetryTimeout;
  563. }
  564. - (BOOL)shouldCacheStatements {
  565. return shouldCacheStatements;
  566. }
  567. - (void)setShouldCacheStatements:(BOOL)value {
  568. shouldCacheStatements = value;
  569. if (shouldCacheStatements && !cachedStatements) {
  570. [self setCachedStatements:[NSMutableDictionary dictionary]];
  571. }
  572. if (!shouldCacheStatements) {
  573. [self setCachedStatements:nil];
  574. }
  575. }
  576. - (NSMutableDictionary *)cachedStatements {
  577. return cachedStatements;
  578. }
  579. - (void)setCachedStatements:(NSMutableDictionary *)value {
  580. if (cachedStatements != value) {
  581. [cachedStatements release];
  582. cachedStatements = [value retain];
  583. }
  584. }
  585. @end
  586. @implementation FMStatement
  587. - (void)finalize {
  588. [self close];
  589. [super finalize];
  590. }
  591. - (void)dealloc {
  592. [self close];
  593. [query release];
  594. [super dealloc];
  595. }
  596. - (void)close {
  597. if (statement) {
  598. sqlite3_finalize(statement);
  599. statement = 0x00;
  600. }
  601. }
  602. - (void)reset {
  603. if (statement) {
  604. sqlite3_reset(statement);
  605. }
  606. }
  607. - (sqlite3_stmt *)statement {
  608. return statement;
  609. }
  610. - (void)setStatement:(sqlite3_stmt *)value {
  611. statement = value;
  612. }
  613. - (NSString *)query {
  614. return query;
  615. }
  616. - (void)setQuery:(NSString *)value {
  617. if (query != value) {
  618. [query release];
  619. query = [value retain];
  620. }
  621. }
  622. - (long)useCount {
  623. return useCount;
  624. }
  625. - (void)setUseCount:(long)value {
  626. if (useCount != value) {
  627. useCount = value;
  628. }
  629. }
  630. - (NSString*)description {
  631. return [NSString stringWithFormat:@"%@ %ld hit(s) for query %@", [super description], useCount, query];
  632. }
  633. @end