PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/fmdb/FMDatabaseAdditions.m

http://github.com/petewarden/iPhoneTracker
Objective C | 114 lines | 65 code | 26 blank | 23 comment | 3 complexity | b473dd005b6974a301c88ff5b74e2aff MD5 | raw file
  1. //
  2. // FMDatabaseAdditions.m
  3. // fmkit
  4. //
  5. // Created by August Mueller on 10/30/05.
  6. // Copyright 2005 Flying Meat Inc.. All rights reserved.
  7. //
  8. #import "FMDatabase.h"
  9. #import "FMDatabaseAdditions.h"
  10. @implementation FMDatabase (FMDatabaseAdditions)
  11. #define RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(type, sel) \
  12. va_list args; \
  13. va_start(args, query); \
  14. FMResultSet *resultSet = [self executeQuery:query withArgumentsInArray:0x00 orVAList:args]; \
  15. va_end(args); \
  16. if (![resultSet next]) { return (type)0; } \
  17. type ret = [resultSet sel:0]; \
  18. [resultSet close]; \
  19. [resultSet setParentDB:nil]; \
  20. return ret;
  21. - (NSString*)stringForQuery:(NSString*)query, ... {
  22. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSString *, stringForColumnIndex);
  23. }
  24. - (int)intForQuery:(NSString*)query, ... {
  25. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(int, intForColumnIndex);
  26. }
  27. - (long)longForQuery:(NSString*)query, ... {
  28. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(long, longForColumnIndex);
  29. }
  30. - (BOOL)boolForQuery:(NSString*)query, ... {
  31. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(BOOL, boolForColumnIndex);
  32. }
  33. - (double)doubleForQuery:(NSString*)query, ... {
  34. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(double, doubleForColumnIndex);
  35. }
  36. - (NSData*)dataForQuery:(NSString*)query, ... {
  37. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSData *, dataForColumnIndex);
  38. }
  39. - (NSDate*)dateForQuery:(NSString*)query, ... {
  40. RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSDate *, dateForColumnIndex);
  41. }
  42. //check if table exist in database (patch from OZLB)
  43. - (BOOL)tableExists:(NSString*)tableName {
  44. BOOL returnBool;
  45. //lower case table name
  46. tableName = [tableName lowercaseString];
  47. //search in sqlite_master table if table exists
  48. FMResultSet *rs = [self executeQuery:@"select [sql] from sqlite_master where [type] = 'table' and lower(name) = ?", tableName];
  49. //if at least one next exists, table exists
  50. returnBool = [rs next];
  51. //close and free object
  52. [rs close];
  53. return returnBool;
  54. }
  55. //get table with list of tables: result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING]
  56. //check if table exist in database (patch from OZLB)
  57. - (FMResultSet*)getSchema {
  58. //result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING]
  59. FMResultSet *rs = [self executeQuery:@"SELECT type, name, tbl_name, rootpage, sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type != 'meta' AND name NOT LIKE 'sqlite_%' ORDER BY tbl_name, type DESC, name"];
  60. return rs;
  61. }
  62. //get table schema: result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER]
  63. - (FMResultSet*)getTableSchema:(NSString*)tableName {
  64. //result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER]
  65. FMResultSet *rs = [self executeQuery:[NSString stringWithFormat: @"PRAGMA table_info(%@)", tableName]];
  66. return rs;
  67. }
  68. //check if column exist in table
  69. - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName {
  70. BOOL returnBool = NO;
  71. //lower case table name
  72. tableName = [tableName lowercaseString];
  73. //lower case column name
  74. columnName = [columnName lowercaseString];
  75. //get table schema
  76. FMResultSet *rs = [self getTableSchema: tableName];
  77. //check if column is present in table schema
  78. while ([rs next]) {
  79. if ([[[rs stringForColumn:@"name"] lowercaseString] isEqualToString: columnName]) {
  80. returnBool = YES;
  81. break;
  82. }
  83. }
  84. //close and free object
  85. [rs close];
  86. return returnBool;
  87. }
  88. @end