Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improvements for SQLite RETURNING clause #395

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ - (int)key:(const void *)pKey nKey:(int)nKey {
return sqlite3_key(db, pKey, nKey);
}

- (int)last_insert_rowid {
int64_t lastRowId = sqlite3_last_insert_rowid(db);
return (int)lastRowId;
}

@end

@implementation EncryptedDatabaseProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -396,13 +396,13 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = GTGKNDBD23;
DEVELOPMENT_TEAM = WPX9KRKA8B;
INFOPLIST_FILE = "$(SRCROOT)/RudderSampleAppSwift/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.rudderstack.ios.test.swift;
PRODUCT_BUNDLE_IDENTIFIER = com.rudderstack.ios.swift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "RudderSampleAppSwift/RudderSampleAppSwift-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand All @@ -417,13 +417,13 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = GTGKNDBD23;
DEVELOPMENT_TEAM = WPX9KRKA8B;
INFOPLIST_FILE = "$(SRCROOT)/RudderSampleAppSwift/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.rudderstack.ios.test.swift;
PRODUCT_BUNDLE_IDENTIFIER = com.rudderstack.ios.swift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "RudderSampleAppSwift/RudderSampleAppSwift-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class EncryptedDatabase: RSDatabase {
func key(_ pKey: UnsafeRawPointer?, nKey: Int32) -> Int32 {
return sqlite3_key(db, pKey, nKey)
}

func last_insert_rowid() -> Int32 {
return Int32(sqlite3_last_insert_rowid(db))
}
}

class EncryptedDatabaseProvider: RSDatabaseProvider {
Expand Down
14 changes: 7 additions & 7 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- MetricsReporter (1.1.0):
- RSCrashReporter (~> 1.0.0)
- RudderKit (~> 1.4.0)
- MetricsReporter (1.1.1):
- RSCrashReporter (= 1.0.0)
- RudderKit (= 1.4.0)
- RSCrashReporter (1.0.0)
- Rudder (1.20.0):
- MetricsReporter (= 1.1.0)
- MetricsReporter (= 1.1.1)
- RudderKit (1.4.0)
- SQLCipher (4.5.4):
- SQLCipher/standard (= 4.5.4)
Expand All @@ -31,12 +31,12 @@ EXTERNAL SOURCES:
:path: "."

SPEC CHECKSUMS:
MetricsReporter: d7f4da199ecceefb2e3a49a0b45b288fbadaca1f
MetricsReporter: d4265e0ac833e2c0078bbe044b3f9dbe29f53a48
RSCrashReporter: 7e26b51ac816e967acb58fa458040946a93a9e65
Rudder: 6d1dc8dd6901d503666002ecc9973c2c11d29a89
Rudder: d9e8730c891325da05ac4c454db09bc449d3b9ff
RudderKit: d9d6997696e1642b753d8bdf94e57af643a68f03
SQLCipher: 905b145f65f349f26da9e60a19901ad24adcd381

PODFILE CHECKSUM: b6937cee06e0633464427ff0d975d40e17419e9f

COCOAPODS: 1.12.0
COCOAPODS: 1.13.0
2 changes: 1 addition & 1 deletion Rudder.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Pod::Spec.new do |s|

s.source_files = 'Sources/**/*.{h,m}'

s.dependency 'MetricsReporter', '1.1.0'
s.dependency 'MetricsReporter', '1.1.1'
end
198 changes: 99 additions & 99 deletions Rudder.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Sources/Classes/Headers/Public/RSDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef int (*callback)(void * _Nullable, int, char * _Nullable * _Nullable, cha
- (const unsigned char *)column_text:(void * _Nullable)pStmt i:(int)i;

- (int)key:(const void * _Nullable)pKey nKey:(int)nKey;
- (int)last_insert_rowid;

@end

Expand Down
34 changes: 26 additions & 8 deletions Sources/Classes/RSDBPersistentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@implementation RSDBPersistentManager {
NSLock* lock;
id<RSDatabase> database;
BOOL isReturnClauseSupported;
}

- (instancetype)initWithDBEncryption:(RSDBEncryption * __nullable)dbEncryption {
Expand All @@ -36,6 +37,12 @@ - (instancetype)initWithDBEncryption:(RSDBEncryption * __nullable)dbEncryption {
self->lock = [[NSLock alloc] init];
self->database = [[self getDatabaseProvider:dbEncryption] getDatabase];
[self createDB:dbEncryption];
isReturnClauseSupported = [self doesReturnClauseExists];
if(isReturnClauseSupported) {
[RSLogger logVerbose:@"RSDBPersistentManager: init: SQLiteVersion is >=3.35.0, hence return clause can be used"];
} else {
[RSLogger logVerbose:@"RSDBPersistentManager: init: SQLiteVersion is <3.35.0, hence return clause cannot be used"];
}
}
return self;
}
Expand Down Expand Up @@ -313,23 +320,34 @@ -(void) createEventsTableWithVersion:(int) version {
}

- (NSNumber*)saveEvent:(NSString *)message {
NSString *insertSQLString = [[NSString alloc] initWithFormat:@"INSERT INTO %@ (%@, %@) VALUES ('%@', %ld) RETURNING %@;", TABLE_EVENTS, COL_MESSAGE, COL_UPDATED, [message stringByReplacingOccurrencesOfString:@"'" withString:@"''"], [RSUtils getTimeStampLong], COL_ID];

NSString *insertSQLString;
if(isReturnClauseSupported) {
insertSQLString = [[NSString alloc] initWithFormat:@"INSERT INTO %@ (%@, %@) VALUES ('%@', %ld) RETURNING %@;", TABLE_EVENTS, COL_MESSAGE, COL_UPDATED, [message stringByReplacingOccurrencesOfString:@"'" withString:@"''"], [RSUtils getTimeStampLong], COL_ID];
} else {
insertSQLString = [[NSString alloc] initWithFormat:@"INSERT INTO %@ (%@, %@) VALUES ('%@', %ld);", TABLE_EVENTS, COL_MESSAGE, COL_UPDATED, [message stringByReplacingOccurrencesOfString:@"'" withString:@"''"], [RSUtils getTimeStampLong]];
}
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDBPersistentManager: saveEventSQL: %@", insertSQLString]];

const char* insertSQL = [insertSQLString UTF8String];
int rowId = -1;
void *insertStmt = nil;
[self->lock lock];
if ([database prepare_v2:insertSQL nBytes:-1 ppStmt:&insertStmt pzTail:NULL] == SQLITE_OK) {
if ([database step:insertStmt] == SQLITE_ROW) {
// table created
[RSLogger logDebug:@"RSDBPersistentManager: saveEvent: Successfully inserted event to table"];
rowId = [database column_int:insertStmt i:0];
if(isReturnClauseSupported) {
if ([database step:insertStmt] == SQLITE_ROW) {
// table created
[RSLogger logDebug:@"RSDBPersistentManager: saveEvent: Successfully inserted event to table"];
rowId = sqlite3_column_int(insertStmt, 0);
} else {
[RSLogger logError:@"RSDBPersistentManager: saveEvent: Failed to insert the event"];
}
[database finalize:insertStmt];
} else {
[RSLogger logError:@"RSDBPersistentManager: saveEvent: Failed to insert the event"];
if([database step:insertStmt] == SQLITE_DONE) {
[database finalize:insertStmt];
rowId = [database last_insert_rowid];
}
}
[database finalize:insertStmt];
} else {
[RSLogger logError:@"RSDBPersistentManager: saveEvent: SQLite Command Preparation Failed"];
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Classes/RSDefaultDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ - (int)key:(const void *)pKey nKey:(int)nKey {
return -1;
}

- (int)last_insert_rowid {
int64_t lastRowId = sqlite3_last_insert_rowid(db);
return (int)lastRowId;
}

@end
Loading