Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- //
- // SpySqlite.m
- // sqlx
- //
- // Created by Scott D. Yelich on 8/21/08.
- // Copyright 2008 n/a. All rights reserved.
- //
- */
- #import "SpySqlite.h"
- @implementation SpySqlite
- @synthesize delegate, headers, data;
- - (id) init {
- self = [super init];
- if (self != nil) {
- headers = [[NSMutableArray alloc] init];
- data = [[NSMutableArray alloc] init];
- filePath = nil;
- }
- return self;
- }
- - (int)reOpenDatabase {
- [self close];
- return [self openDatabase:filePath];
- }
- - (int)openDatabase:(NSString *)dbName {
- int rc = 0;
- if (nil == dbName) {
- dbName = [NSString stringWithFormat:@":memory:"];
- }
- if (rc = sqlite3_open([dbName UTF8String], &database)) {
- errorCode = rc;
- return rc;
- }
- /*
- // I decided that using the busy_handler allowed for more control. So
- // this is left commented out.
- if (rc = sqlite3_busy_timeout(_db, 25)) { // 25ms?
- throw Xql3Exception(2, "set busy timeout");
- }
- */
- /*
- if (rc = sqlite3_busy_handler(_db, Xql3::busy, this)) {
- throw Xql3Exception(3, "set busy handler");
- }
- */
- filePath = [dbName copy];
- return rc;
- }
- -(int)close {
- int rc = 0;
- if (database) {
- if (sqlStatement) {
- rc = sqlite3_finalize(sqlStatement); // check rc?
- }
- if (rc = sqlite3_close(database) ) {
- // error!
- }
- database = nil;
- }
- return rc;
- }
- - (int)xSql:(NSString *)sql {
- int rc = SQLITE_OK;
- if ([sql length] == 0 ) {
- return rc;
- }
- int i;
- int ncols;
- sqlStatement = nil;
- const char * zSql;
- const char * zLeftover = [sql UTF8String];
- // clear out the old results ...
- [headers removeAllObjects];
- [data removeAllObjects]; // in case errors out
- while (1) { // loop for all sql statements ...
- sqlStatement = nil;
- zSql = zLeftover;
- if (!zSql[0]) {
- break;
- }
- // NSLog(@"preparing zsql[%s] db[%i] zleft[%s]", zSql, (int)database, zLeftover);
- if ( SQLITE_OK != (rc = sqlite3_prepare_v2(database, zSql, -1, &sqlStatement, &zLeftover))) {
- // NSLog(@"preparing error and now zsql[%s] db[%i] zleft[%s]", zSql, (int)database, zLeftover);
- // NSLog(@"NOT OK in prepare rc[%i]!", rc);
- error = YES;
- return rc;
- }
- if (!sqlStatement) { // parsed blank or comment?
- // NSLog(@"sql is blank or comment, skipping (to next)");
- continue;
- }
- ncols = sqlite3_column_count(sqlStatement);
- // NSLog(@"sql cols=%i", ncols);
- while (1) { // loop for all results of an individual statement ...
- rc = sqlite3_step(sqlStatement);
- // NSMutableArray
- if (SQLITE_ROW == rc) {
- if (0 == [headers count]) {
- for(i=0; i<ncols; i++) {
- [headers addObject:[NSString stringWithFormat:@"%s", sqlite3_column_name(sqlStatement, i)]];
- }
- }
- [data removeAllObjects];
- for(i=0; i<ncols; i++){
- if ( sqlite3_column_bytes(sqlStatement, i) > 0 ) {
- [data addObject:[NSString stringWithFormat:@"%s", sqlite3_column_text(sqlStatement, i)]];
- } else {
- [data addObject:[NSString stringWithFormat:@""]];
- }
- }
- // NSLog(@"callback...");
- [delegate xSqlCallbackWithHeaders:headers andData:data];
- } else {
- if ( SQLITE_DONE != rc ) {
- // NSLog(@"NOT OK in step, skipping ...");
- }
- rc = sqlite3_finalize(sqlStatement);
- sqlStatement = nil;
- zSql = zLeftover; // advance to next statement ... or error?
- while (isspace((unsigned char)zSql[0])) zSql++;
- break;
- }
- }
- }
- if (sqlStatement) sqlite3_finalize(sqlStatement);
- sqlStatement = nil;
- return rc;
- }
- @end
Add Comment
Please, Sign In to add comment