From 68eee4790ff0d7430e91a7e3663a45fe2d747d88 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 1 Oct 2020 23:13:36 +0000 Subject: [PATCH] SL3Connection: Add -[executeStatement:] FossilOrigin-Name: 02992f86b3c02a7784a12c4b9a7ffa7b70614999a07b799c567755bde3d24cea --- src/SL3Connection.h | 3 ++- src/SL3Connection.m | 16 ++++++++++++++-- src/SL3PreparedStatement.m | 2 +- .../SL3ExecuteStatementFailedException.h | 6 +----- .../SL3ExecuteStatementFailedException.m | 12 ------------ 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/SL3Connection.h b/src/SL3Connection.h index bf44bf2..328499a 100644 --- a/src/SL3Connection.h +++ b/src/SL3Connection.h @@ -34,7 +34,7 @@ OF_ASSUME_NONNULL_BEGIN #ifdef SL3_PUBLIC_IVARS @public #endif - sqlite3 *_db; + sqlite3 *_conn; } + (instancetype)connectionWithPath: (OFString *)path; @@ -44,6 +44,7 @@ OF_ASSUME_NONNULL_BEGIN - (instancetype)initWithPath: (OFString *)path flags: (int)flags OF_DESIGNATED_INITIALIZER; - (SL3PreparedStatement *)prepareStatement: (OFConstantString *)SQL; +- (void)executeStatement: (OFConstantString *)SQL; @end OF_ASSUME_NONNULL_END diff --git a/src/SL3Connection.m b/src/SL3Connection.m index ba73a2e..732eb3b 100644 --- a/src/SL3Connection.m +++ b/src/SL3Connection.m @@ -23,6 +23,7 @@ #import "SL3Connection.h" #import "SL3PreparedStatement.h" +#import "SL3ExecuteStatementFailedException.h" #import "SL3OpenFailedException.h" @implementation SL3Connection @@ -50,7 +51,8 @@ self = [super init]; @try { - int code = sqlite3_open_v2(path.UTF8String, &_db, flags, NULL); + int code = sqlite3_open_v2(path.UTF8String, &_conn, flags, + NULL); if (code != SQLITE_OK) @throw [SL3OpenFailedException exceptionWithPath: path @@ -66,7 +68,7 @@ - (void)dealloc { - sqlite3_close(_db); + sqlite3_close(_conn); [super dealloc]; } @@ -77,4 +79,14 @@ sl3_initWithConnection: self SQLStatement: SQL] autorelease]; } + +- (void)executeStatement: (OFConstantString *)SQL +{ + int code = sqlite3_exec(_conn, SQL.UTF8String, NULL, NULL, NULL); + + if (code != SQLITE_OK) + @throw [SL3ExecuteStatementFailedException + exceptionWithConnection: self + errorCode: code]; +} @end diff --git a/src/SL3PreparedStatement.m b/src/SL3PreparedStatement.m index e7683cb..8fa6d34 100644 --- a/src/SL3PreparedStatement.m +++ b/src/SL3PreparedStatement.m @@ -42,7 +42,7 @@ releaseObject(void *object) self = [super init]; @try { - int code = sqlite3_prepare_v2(connection->_db, + int code = sqlite3_prepare_v2(connection->_conn, SQLStatement.UTF8String, SQLStatement.UTF8StringLength, &_stmt, NULL); diff --git a/src/exceptions/SL3ExecuteStatementFailedException.h b/src/exceptions/SL3ExecuteStatementFailedException.h index ab86d37..32562ff 100644 --- a/src/exceptions/SL3ExecuteStatementFailedException.h +++ b/src/exceptions/SL3ExecuteStatementFailedException.h @@ -33,14 +33,10 @@ OF_ASSUME_NONNULL_BEGIN @property (readonly, nonatomic) SL3PreparedStatement *statement; -+ (instancetype)exceptionWithConnection: (nullable SL3Connection *)connection - errorCode: (int)errorCode OF_UNAVAILABLE; + (instancetype)exceptionWithStatement: (SL3PreparedStatement *)statement errorCode: (int)errorCode; -- (instancetype)initWithConnection: (nullable SL3Connection *)connection - errorCode: (int)errorCode OF_UNAVAILABLE; - (instancetype)initWithStatement: (SL3PreparedStatement *)statement - errorCode: (int)errorCode OF_DESIGNATED_INITIALIZER; + errorCode: (int)errorCode; @end OF_ASSUME_NONNULL_END diff --git a/src/exceptions/SL3ExecuteStatementFailedException.m b/src/exceptions/SL3ExecuteStatementFailedException.m index df15a91..2b6ebfd 100644 --- a/src/exceptions/SL3ExecuteStatementFailedException.m +++ b/src/exceptions/SL3ExecuteStatementFailedException.m @@ -25,12 +25,6 @@ @implementation SL3ExecuteStatementFailedException @synthesize statement = _statement; -+ (instancetype)exceptionWithConnection: (SL3Connection *)connection - errorCode: (int)errorCode -{ - OF_UNRECOGNIZED_SELECTOR -} - + (instancetype)exceptionWithStatement: (SL3PreparedStatement *)statement errorCode: (int)errorCode { @@ -38,12 +32,6 @@ errorCode: errorCode] autorelease]; } -- (instancetype)initWithConnection: (SL3Connection *)connection - errorCode: (int)errorCode -{ - OF_INVALID_INIT_METHOD -} - - (instancetype)initWithStatement: (SL3PreparedStatement *)statement errorCode: (int)errorCode {