@@ -247,95 +247,103 @@ - (NSMutableArray *)sqlite3ExecQuery:(sqlite3 *)ppDb sql:(NSString *)sql{
247
247
248
248
NSMutableArray <NSDictionary *> *tables = [NSMutableArray array ];
249
249
// 将结果返回,保存为字典
250
-
251
- NSMutableArray *resArray = [self getObjName: sql ppDb: ppDb];
250
+ __block NSMutableArray *resArray = [self getObjName: sql ppDb: ppDb];
252
251
253
252
// 替换SELECT * FROM user 中星号
254
253
// SELECT * FROM user WHERE name = '...'
255
254
// SELECT user.name as paintinglite_name,user.age as paintinglite_age FROM user WHERE paintinglite_name = '...' and paintinglite_age = '...';
256
255
// SELECT * FROM user WHERE age < 40 ORDER BY name DESC
257
256
sql = [self replaceStar: ppDb resArray: resArray sql: sql];
258
- NSString *optAndStatusStr = [sql stringByAppendingString: @" | " ];
259
- @synchronized (self) {
260
- if (sqlite3_prepare_v2 (ppDb, [sql UTF8String ], -1 , &_stmt, nil ) == SQLITE_OK){
257
+
258
+ dispatch_semaphore_t signal = dispatch_semaphore_create (0 );
259
+
260
+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
261
+ NSString *optAndStatusStr = [sql stringByAppendingString: @" | " ];
262
+
263
+ if (sqlite3_prepare_v2 (ppDb, [sql UTF8String ], -1 , &(self->_stmt ), nil ) == SQLITE_OK){
261
264
// 查询成功
262
- while (sqlite3_step (_stmt) == SQLITE_ROW) {
263
- @autoreleasepool {
264
- NSMutableDictionary <id ,id > *queryDict = [NSMutableDictionary dictionary ];
265
- // resArray是一个含有表字段的数据,根据resArray获得值加入
266
- for (unsigned int i = 0 ; i < resArray.count ; i++) {
267
- // 取出来的值,然后判断类型,进行类型分类
268
- /* *
269
- SQLITE_INTEGER 1
270
- SQLITE_FLOAT 2
271
- SQLITE3_TEXT 3
272
- SQLITE_BLOB 4
273
- SQLITE_NULL 5
274
- */
275
- @autoreleasepool {
276
- id value = nil ;
277
- value = [self caseTheType: sqlite3_column_type (_stmt, i) index :i value: value];
278
- [queryDict setValue: value forKey: resArray[i]];
279
- }
265
+ while (sqlite3_step (self->_stmt ) == SQLITE_ROW) {
266
+ NSMutableDictionary <id ,id > *queryDict = [NSMutableDictionary dictionary ];
267
+ // resArray是一个含有表字段的数据,根据resArray获得值加入
268
+ for (unsigned int i = 0 ; i < resArray.count ; i++) {
269
+ // 取出来的值,然后判断类型,进行类型分类
270
+ @autoreleasepool {
271
+ id value = nil ;
272
+ value = [self caseTheType: sqlite3_column_type (self ->_stmt, i) index :i value: value];
273
+ [queryDict setValue: value forKey: resArray[i]];
280
274
}
281
- [tables addObject: queryDict];
282
275
}
276
+ [tables addObject: queryDict];
283
277
}
284
278
optAndStatusStr = [optAndStatusStr stringByAppendingString: @" success" ];
285
279
}else {
286
280
// 执行失败
287
281
optAndStatusStr = [optAndStatusStr stringByAppendingString: @" error" ];
288
282
}
289
- }
290
-
291
- // 写入缓存
292
- [self .cache addDatabaseOptionsCache: optAndStatusStr];
293
-
283
+
284
+ // 写入缓存
285
+ [self .cache addDatabaseOptionsCache: optAndStatusStr];
286
+
287
+ dispatch_semaphore_signal (signal );
288
+ });
289
+
290
+ dispatch_semaphore_wait (signal ,DISPATCH_TIME_FOREVER);
291
+
294
292
sqlite3_finalize (_stmt);
295
293
296
294
return tables;
297
295
}
298
296
299
297
#pragma mark - 替代*操作
300
298
- (NSString *__nonnull)replaceStar : (sqlite3 *)ppDb resArray : (NSMutableArray *)resArray sql : (NSString *__nonnull)sql {
301
- NSString *tableInfoStr = NULL ;
302
- NSString *tableName = NULL ;
303
- NSString *tempSql = [sql uppercaseString ];
304
- if ([tempSql containsString: @" *" ]) {
305
- if ([tempSql containsString: Paintinglite_Sqlite3_WHERE]) {
306
- tableName = [[[sql componentsSeparatedByString: [NSString stringWithFormat: @" %@ " ,Paintinglite_Sqlite3_WHERE]][0 ] componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
307
- tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
308
- }else if ([tempSql containsString: Paintinglite_Sqlite3_ORDER]){
309
- tableName = [[[sql componentsSeparatedByString: [NSString stringWithFormat: @" %@ " ,Paintinglite_Sqlite3_ORDER]][0 ] componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
310
- tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
311
- }else if ([tempSql containsString: Paintinglite_Sqlite3_LIMIT]){
312
- tableName = [[[sql componentsSeparatedByString: [NSString stringWithFormat: @" %@ " ,Paintinglite_Sqlite3_LIMIT]][0 ] componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
313
- tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
314
- }else {
315
- tableName = [[sql componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
316
- tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
317
- }
318
-
319
- // 去除换行和空格
320
- tableInfoStr = [[tableInfoStr stringByReplacingOccurrencesOfString: @" \n " withString: @" " ] stringByReplacingOccurrencesOfString: Paintinglite_Sqlite3_SPACE withString: @" " ];
321
- tableInfoStr = [tableInfoStr substringWithRange: NSMakeRange (1 , tableInfoStr.length - 2 )];
322
-
323
- // SELECT name,age FROM user
324
- // 每一个数组增加user.
325
- NSString *str = [NSString stringWithFormat: @" %@ ." ,tableName];
326
- NSString *resStr = [NSString string ];
299
+ __block NSString *tableInfoStr = NULL ;
300
+ __block NSString *tableName = NULL ;
301
+ __block NSString *tempSql = [sql uppercaseString ];
302
+
303
+ dispatch_semaphore_t signal = dispatch_semaphore_create (0 );
304
+
305
+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
327
306
328
- NSUInteger i = 0 ;
329
- NSArray <NSString *> *tableInfoArray = [tableInfoStr componentsSeparatedByString: @" ," ];
330
- for (NSString *field in tableInfoArray) {
331
- resStr = (i == tableInfoArray.count - 1 ) ? [resStr stringByAppendingString: [NSString stringWithFormat: @" %@ as %@ " ,[str stringByAppendingString: field],[NSString stringWithFormat: @" painting_%@ " ,field]]] : [resStr stringByAppendingString: [NSString stringWithFormat: @" %@ as %@ ," ,[str stringByAppendingString: field],[NSString stringWithFormat: @" painting_%@ " ,field]]];
332
- i++;
307
+ if ([tempSql containsString: @" *" ]) {
308
+ if ([tempSql containsString: Paintinglite_Sqlite3_WHERE]) {
309
+ tableName = [[[sql componentsSeparatedByString: [NSString stringWithFormat: @" %@ " ,Paintinglite_Sqlite3_WHERE]][0 ] componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
310
+ tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
311
+ }else if ([tempSql containsString: Paintinglite_Sqlite3_ORDER]){
312
+ tableName = [[[sql componentsSeparatedByString: [NSString stringWithFormat: @" %@ " ,Paintinglite_Sqlite3_ORDER]][0 ] componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
313
+ tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
314
+ }else if ([tempSql containsString: Paintinglite_Sqlite3_LIMIT]){
315
+ tableName = [[[sql componentsSeparatedByString: [NSString stringWithFormat: @" %@ " ,Paintinglite_Sqlite3_LIMIT]][0 ] componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
316
+ tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
317
+ }else {
318
+ tableName = [[sql componentsSeparatedByString: Paintinglite_Sqlite3_SPACE]lastObject];
319
+ tableInfoStr = [NSString stringWithFormat: @" %@ " ,[self getTableInfo: ppDb tableName: tableName]];
320
+ }
321
+
322
+ // 去除换行和空格
323
+ tableInfoStr = [[tableInfoStr stringByReplacingOccurrencesOfString: @" \n " withString: @" " ] stringByReplacingOccurrencesOfString: Paintinglite_Sqlite3_SPACE withString: @" " ];
324
+ tableInfoStr = [tableInfoStr substringWithRange: NSMakeRange (1 , tableInfoStr.length - 2 )];
325
+
326
+ // SELECT name,age FROM user
327
+ // 每一个数组增加user.
328
+ NSString *str = [NSString stringWithFormat: @" %@ ." ,tableName];
329
+ NSString *resStr = [NSString string ];
330
+
331
+ NSUInteger i = 0 ;
332
+ NSArray <NSString *> *tableInfoArray = [tableInfoStr componentsSeparatedByString: @" ," ];
333
+ for (NSString *field in tableInfoArray) {
334
+ resStr = (i == tableInfoArray.count - 1 ) ? [resStr stringByAppendingString: [NSString stringWithFormat: @" %@ as %@ " ,[str stringByAppendingString: field],[NSString stringWithFormat: @" painting_%@ " ,field]]] : [resStr stringByAppendingString: [NSString stringWithFormat: @" %@ as %@ ," ,[str stringByAppendingString: field],[NSString stringWithFormat: @" painting_%@ " ,field]]];
335
+ i++;
336
+ }
337
+
338
+ tempSql = [sql stringByReplacingOccurrencesOfString: @" *" withString: resStr];
333
339
}
334
340
335
- sql = [sql stringByReplacingOccurrencesOfString: @" * " withString: resStr] ;
336
- }
341
+ dispatch_semaphore_signal ( signal ) ;
342
+ });
337
343
338
- return sql;
344
+ dispatch_semaphore_wait (signal , DISPATCH_TIME_FOREVER);
345
+
346
+ return (sql = tempSql);
339
347
}
340
348
341
349
#pragma mark - 类名执行
0 commit comments