Permalink
Browse files

Rename executeStatement to resetAndStepStatement. Add new executeStat…

…ement.

See CHANGES for details.
  • Loading branch information...
jverkoey committed Dec 17, 2009
1 parent 135c7b9 commit c7cf674ed1ade51c99eacb6a49f82dfebf4be351
Showing with 85 additions and 7 deletions.
  1. +10 −0 CHANGES
  2. +6 −1 src/ObjSqlite/ObjSqliteStatement.h
  3. +2 −5 src/ObjSqliteDB.m
  4. +23 −1 src/ObjSqliteStatement.m
  5. +44 −0 src/ObjSqliteTest.m
View
10 CHANGES
@@ -1,4 +1,14 @@
+Changes for December 16, 2009
+
+- [NEW] -[ObjSqliteStatement executeStatement] Allows you to execute multiple SQL statements from
+ one SQL query.
+
+- [RENAMED] Original executeStatement method now has different functionality. The original method
+ can be called with stepAndResetStatement.
+ -[ObjSqliteStatement executeStatement] renamed to
+ -[ObjSqliteStatement stepAndResetStatement]
+
Changes for December 8, 2009
- [NEW] -[ObjSqliteDB dbExists] Return TRUE if the database file exists.
@@ -46,6 +46,11 @@
- (BOOL) bindNullToColumn:(int)column;
+// Execution
+
+- (BOOL)executeStatement;
+
+
// Stepping
// @returns true if the step succeeded.
@@ -57,7 +62,7 @@
- (void) resetStatement;
// Step and reset
-- (BOOL) executeStatement;
+- (BOOL) stepAndResetStatement;
- (void) finalizeStatement;
View
@@ -65,7 +65,7 @@ - (int)lastErrorCode {
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)lastErrorMessage {
- return [NSString stringWithCString:sqlite3_errmsg(_db)];
+ return [NSString stringWithCString:sqlite3_errmsg(_db) encoding:NSUTF8StringEncoding];
}
@@ -98,10 +98,7 @@ - (sqlite3*)sqliteDB {
// DB's been opened, now create the tables if necessary.
if (needsCreation && nil != self.createStatement) {
- if ([self.createStatement step]) {
- [self.createStatement resetStatement];
-
- } else {
+ if (![self.createStatement executeStatement]) {
sqlite3_close(_db);
_db = nil;
}
View
@@ -125,6 +125,28 @@ - (BOOL)bindNullToColumn:(int)column {
}
+///////////////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////////////
+#pragma mark -
+#pragma mark Executement
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+- (BOOL)executeStatement {
+ BOOL result = YES;
+
+ @synchronized(_db) {
+ if (result) {
+ int code = sqlite3_exec(_db.sqliteDB, _sql, nil, nil, nil);
+
+ result = code == SQLITE_DONE || code == SQLITE_ROW || code == SQLITE_OK;
+ }
+ }
+
+ return result;
+}
+
+
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
@@ -198,7 +220,7 @@ - (void)finalizeStatement {
///////////////////////////////////////////////////////////////////////////////////////////////////
-- (BOOL)executeStatement {
+- (BOOL)stepAndResetStatement {
BOOL succeeded;
@synchronized(_db) {
succeeded = [self step];
View
@@ -196,5 +196,49 @@ - (void)testLoadNewDbWithDeleteStatement {
}
+///////////////////////////////////////////////////////////////////////////////////////////////////
+- (void)testLoadNewDbWithDeleteStepAndResetStatement {
+ ObjSqliteDB* db = [[ObjSqliteDB alloc] initWithPath:kTestDBPath];
+ db.createSQL = kTestCreateDB;
+
+ ObjSqliteStatement* statement;
+
+ statement = [[ObjSqliteStatement alloc] initWithSQL:kTestInsertDB db:db];
+ STAssertTrue([statement stepAndResetStatement], @"Failed statement: %@", db.lastErrorMessage);
+ [statement release];
+ statement = nil;
+
+ statement = [[ObjSqliteStatement alloc] initWithSQL:kTestDeleteDB db:db];
+ STAssertTrue([statement stepAndResetStatement], @"Failed statement: %@", db.lastErrorMessage);
+ [statement release];
+ statement = nil;
+
+ [db release];
+ db = nil;
+}
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+- (void)testLoadNewDbWithDeleteExecuteStatement {
+ ObjSqliteDB* db = [[ObjSqliteDB alloc] initWithPath:kTestDBPath];
+ db.createSQL = kTestCreateDB;
+
+ ObjSqliteStatement* statement;
+
+ statement = [[ObjSqliteStatement alloc] initWithSQL:kTestInsertDB db:db];
+ STAssertTrue([statement executeStatement], @"Failed statement: %@", db.lastErrorMessage);
+ [statement release];
+ statement = nil;
+
+ statement = [[ObjSqliteStatement alloc] initWithSQL:kTestDeleteDB db:db];
+ STAssertTrue([statement executeStatement], @"Failed statement: %@", db.lastErrorMessage);
+ [statement release];
+ statement = nil;
+
+ [db release];
+ db = nil;
+}
+
+
@end

0 comments on commit c7cf674

Please sign in to comment.