99#import  " FLEXSQLiteDatabaseManager.h" 
1010#import  " FLEXManager.h" 
1111#import  " NSArray+Functional.h" 
12+ #import  " FLEXSQLResult.h" 
1213#import  < sqlite3.h> 
1314
14- static  NSString  * const  QUERY_TABLENAMES_SQL  = @" SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" 
15+ static  NSString  * const  QUERY_TABLENAMES  = @" SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" 
1516
1617@interface  FLEXSQLiteDatabaseManager  ()
17- @property  (nonatomic ,  readonly ) sqlite3 *db;
18+ @property  (nonatomic ) sqlite3 *db;
1819@property  (nonatomic , copy ) NSString  *path;
1920@end 
2021
@@ -36,7 +37,7 @@ - (instancetype)initWithPath:(NSString *)path {
3637}
3738
3839- (BOOL )open  {
39-     if  (_db ) {
40+     if  (self. db ) {
4041        return  YES ;
4142    }
4243
@@ -57,9 +58,9 @@ - (BOOL)open {
5758
5859    return  YES ;
5960}
60- 
61+      
6162- (BOOL )close  {
62-     if  (!_db ) {
63+     if  (!self. db ) {
6364        return  YES ;
6465    }
6566
@@ -84,88 +85,85 @@ - (BOOL)close {
8485        }
8586    } while  (retry);
8687
87-     _db  = nil ;
88+     self. db  = nil ;
8889    return  YES ;
8990}
9091
9192- (NSArray <NSString *> *)queryAllTables  {
92-     return  [[self  executeQuery: QUERY_TABLENAMES_SQL]  flex_mapped: ^id (NSArray  *table, NSUInteger  idx) {
93+     return  [[self  executeStatement: QUERY_TABLENAMES].rows  flex_mapped: ^id (NSArray  *table, NSUInteger  idx) {
9394        return  table.firstObject ;
9495    }];
9596}
9697
97- - (NSArray <NSString *> *)queryAllColumnsWithTableName : (NSString  *)tableName  {
98+ - (NSArray <NSString *> *)queryAllColumnsOfTable : (NSString  *)tableName  {
9899    NSString  *sql = [NSString  stringWithFormat: @" PRAGMA table_info('%@ ')" 
99-     NSArray < NSDictionary  *> * results =   [self  executeQueryWithColumns :
100+     FLEXSQLResult * results = [self  executeStatement :
100101
101-     return  [results flex_mapped: ^id (NSDictionary  *column, NSUInteger  idx) {
102+     return  [results.keyedRows  flex_mapped: ^id (NSDictionary  *column, NSUInteger  idx) {
102103        return  column[@" name" 
103104    }];
104105}
105106
106- - (NSArray <NSArray *> *)queryAllDataWithTableName : (NSString  *)tableName  {
107-     return  [self  executeQuery :@" SELECT * FROM " 
107+ - (NSArray <NSArray *> *)queryAllDataInTable : (NSString  *)tableName  {
108+     return  [self  executeStatement :@" SELECT * FROM " 
108109        stringByAppendingString: tableName
109-     ]];
110+     ]]. rows ;
110111}
111112
112- #pragma mark  - Private
113- 
114- // / @return an array of rows, where each row is an array
115- // / containing the values of each column for that row
116- - (NSArray <NSArray *> *)executeQuery : (NSString  *)sql  {
113+ - (FLEXSQLResult *)executeStatement : (NSString  *)sql  {
117114    [self  open ];
118115
119-     NSMutableArray < NSArray  *> *results = [ NSMutableArray   array ] ;
116+     FLEXSQLResult *result =  nil ;
120117
121118    sqlite3_stmt *pstmt;
122119    if  (sqlite3_prepare_v2 (_db, sql.UTF8String , -1 , &pstmt, 0 ) == SQLITE_OK) {
123-         while  (sqlite3_step (pstmt) == SQLITE_ROW) {
124-             int  num_cols = sqlite3_data_count (pstmt);
125-             if  (num_cols > 0 ) {
126-                 int  columnCount = sqlite3_column_count (pstmt);
127-                 
128-                 [results addObject: [NSArray  flex_forEachUpTo: columnCount map: ^id (NSUInteger  i) {
120+         NSMutableArray <NSArray  *> *rows = [NSMutableArray  new ];
121+         
122+         //  Grab columns
123+         int  columnCount = sqlite3_column_count (pstmt);
124+         NSArray <NSString  *> *columns = [NSArray  flex_forEachUpTo: columnCount map: ^id (NSUInteger  i) {
125+             return  @(sqlite3_column_name (pstmt, (int )i));
126+         }];
127+         
128+         //  Execute statement
129+         int  status;
130+         while  ((status = sqlite3_step (pstmt)) == SQLITE_ROW) {
131+             //  Grab rows if this is a selection query
132+             int  dataCount = sqlite3_data_count (pstmt);
133+             if  (dataCount > 0 ) {
134+                 [rows addObject: [NSArray  flex_forEachUpTo: columnCount map: ^id (NSUInteger  i) {
129135                    return  [self  objectForColumnIndex: (int )i stmt: pstmt];
130136                }]];
131137            }
132138        }
133-     }
134-     
135-     [self  close ];
136-     return  results;
137- }
138- 
139- // / Like \c executeQuery: except that a list of dictionaries are returned,
140- // / where the keys are column names and the values are the data.
141- - (NSArray <NSDictionary *> *)executeQueryWithColumns : (NSString  *)sql  {
142-     [self  open ];
143-     
144-     NSMutableArray <NSDictionary  *> *results = [NSMutableArray  array ];
145-     
146-     sqlite3_stmt *pstmt;
147-     if  (sqlite3_prepare_v2 (_db, sql.UTF8String , -1 , &pstmt, 0 ) == SQLITE_OK) {
148-         while  (sqlite3_step (pstmt) == SQLITE_ROW) {
149-             int  num_cols = sqlite3_data_count (pstmt);
150-             if  (num_cols > 0 ) {
151-                 int  columnCount = sqlite3_column_count (pstmt);
152-                 
153-                 
154-                 NSMutableDictionary  *rowFields = [NSMutableDictionary  new ];
155-                 for  (int  i = 0 ; i < columnCount; i++) {
156-                     id  value = [self  objectForColumnIndex: (int )i stmt: pstmt];
157-                     rowFields[@(sqlite3_column_name (pstmt, i))] = value;
158-                 }
159-                 
160-                 [results addObject: rowFields];
139+         
140+         if  (status == SQLITE_DONE) {
141+             if  (rows.count ) {
142+                 //  We selected some rows
143+                 result = [FLEXSQLResult columns: columns rows: rows];
144+             } else  {
145+                 //  We executed a query like INSERT, UDPATE, or DELETE
146+                 int  rowsAffected = sqlite3_changes (_db);
147+                 NSString  *message = [NSString  stringWithFormat: @" %d  row(s) affected" 
148+                 result = [FLEXSQLResult message: message];
161149            }
150+         } else  {
151+             //  An error occured executing the query
152+             result = [FLEXSQLResult message: @(sqlite3_errmsg (_db) ?: " (Execution: empty error)" 
162153        }
154+     } else  {
155+         //  An error occurred creating the prepared statement
156+         result = [FLEXSQLResult message: @(sqlite3_errmsg (_db) ?: " (Prepared statement: empty error)" 
163157    }
164158
159+     sqlite3_finalize (pstmt);
165160    [self  close ];
166-     return  results ;
161+     return  result ;
167162}
168163
164+ 
165+ #pragma mark  - Private
166+ 
169167- (id )objectForColumnIndex : (int )columnIdx  stmt : (sqlite3_stmt*)stmt  {
170168    int  columnType = sqlite3_column_type (stmt, columnIdx);
171169
@@ -184,7 +182,7 @@ - (id)objectForColumnIndex:(int)columnIdx stmt:(sqlite3_stmt*)stmt {
184182            return  [self  stringForColumnIndex: columnIdx stmt: stmt] ?: NSNull .null ;
185183    }
186184}
187- 
185+                  
188186- (NSString  *)stringForColumnIndex : (int )columnIdx  stmt : (sqlite3_stmt *)stmt  {
189187    if  (sqlite3_column_type (stmt, columnIdx) == SQLITE_NULL || columnIdx < 0 ) {
190188        return  nil ;
0 commit comments