//
//  iSalesDB+JKDB.m
//  iSales-NPD
//
//  Created by Jack on 8/3/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "iSalesDB+JKDB.h"

@implementation iSalesDB (JKDB)

+ (NSDictionary *)jk_query:(NSString *)sql completion:(queryBlock)block {
    
    __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    sqlite3 *db = [iSalesDB get_db];
    NSString *sqlQuery = sql;
    sqlite3_stmt * statement;
    
    if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
        long count = 0;
        while (sqlite3_step(statement) == SQLITE_ROW) {
            block(statement,dic,&count);
        }
        
        sqlite3_finalize(statement);
    }
    sqlite3_close(db);

    
    return [dic copy];
}

+ (int)jk_execSql:(NSString *)sql withDatabase:(BOOL)database {
    int flag = RESULT_TRUE;
    if (database) {
        sqlite3 *db = [self get_db];
        if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, nil) != SQLITE_OK) {
            flag = RESULT_FALSE;
        }
    }
    
    return flag;
}

@end
