Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (88 sloc) 3.973 kb
280d9f9 @fileability Big improvements in startup time
authored
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
9 #import "FMDatabase.h"
10 #import "FMDatabaseAdditions.h"
11
12 @implementation FMDatabase (FMDatabaseAdditions)
13
14 #define RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(type, sel) \
15 va_list args; \
16 va_start(args, query); \
17 FMResultSet *resultSet = [self executeQuery:query withArgumentsInArray:0x00 orVAList:args]; \
18 va_end(args); \
19 if (![resultSet next]) { return (type)0; } \
20 type ret = [resultSet sel:0]; \
21 [resultSet close]; \
22 [resultSet setParentDB:nil]; \
23 return ret;
24
25
26 - (NSString*)stringForQuery:(NSString*)query, ... {
27 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSString *, stringForColumnIndex);
28 }
29
30 - (int)intForQuery:(NSString*)query, ... {
31 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(int, intForColumnIndex);
32 }
33
34 - (long)longForQuery:(NSString*)query, ... {
35 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(long, longForColumnIndex);
36 }
37
38 - (BOOL)boolForQuery:(NSString*)query, ... {
39 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(BOOL, boolForColumnIndex);
40 }
41
42 - (double)doubleForQuery:(NSString*)query, ... {
43 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(double, doubleForColumnIndex);
44 }
45
46 - (NSData*)dataForQuery:(NSString*)query, ... {
47 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSData *, dataForColumnIndex);
48 }
49
50 - (NSDate*)dateForQuery:(NSString*)query, ... {
51 RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSDate *, dateForColumnIndex);
52 }
53
54
55 //check if table exist in database (patch from OZLB)
56 - (BOOL)tableExists:(NSString*)tableName {
57
58 BOOL returnBool;
59 //lower case table name
60 tableName = [tableName lowercaseString];
61 //search in sqlite_master table if table exists
62 FMResultSet *rs = [self executeQuery:@"select [sql] from sqlite_master where [type] = 'table' and lower(name) = ?", tableName];
63 //if at least one next exists, table exists
64 returnBool = [rs next];
65 //close and free object
66 [rs close];
67
68 return returnBool;
69 }
70
71 //get table with list of tables: result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING]
72 //check if table exist in database (patch from OZLB)
73 - (FMResultSet*)getSchema {
74
75 //result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING]
76 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"];
77
78 return rs;
79 }
80
81 //get table schema: result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER]
82 - (FMResultSet*)getTableSchema:(NSString*)tableName {
83
84 //result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER]
85 FMResultSet *rs = [self executeQuery:[NSString stringWithFormat: @"PRAGMA table_info(%@)", tableName]];
86
87 return rs;
88 }
89
90
91 //check if column exist in table
92 - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName {
93
94 BOOL returnBool = NO;
95 //lower case table name
96 tableName = [tableName lowercaseString];
97 //lower case column name
98 columnName = [columnName lowercaseString];
99 //get table schema
100 FMResultSet *rs = [self getTableSchema: tableName];
101 //check if column is present in table schema
102 while ([rs next]) {
103 if ([[[rs stringForColumn:@"name"] lowercaseString] isEqualToString: columnName]) {
104 returnBool = YES;
105 break;
106 }
107 }
108 //close and free object
109 [rs close];
110
111 return returnBool;
112 }
113
114 @end
Something went wrong with that request. Please try again.