-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,354 additions
and
81 deletions.
There are no files selected for viewing
636 changes: 636 additions & 0 deletions
636
ios/DatabaseFramework/DatabaseFramework/DatabaseFramework.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
...atabaseFramework/DatabaseFramework.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...ork/DatabaseFramework.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
10 changes: 10 additions & 0 deletions
10
...atabaseFramework/DatabaseFramework/DatabaseFramework.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...ork/DatabaseFramework/DatabaseFramework.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
13 changes: 13 additions & 0 deletions
13
...DatabaseFramework/DatabaseFramework/DatabaseFramework.docc/DatabaseFramework.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# ``DatabaseFramework`` | ||
|
||
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@--> | ||
|
||
## Overview | ||
|
||
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@--> | ||
|
||
## Topics | ||
|
||
### <!--@START_MENU_TOKEN@-->Group<!--@END_MENU_TOKEN@--> | ||
|
||
- <!--@START_MENU_TOKEN@-->``Symbol``<!--@END_MENU_TOKEN@--> |
18 changes: 18 additions & 0 deletions
18
ios/DatabaseFramework/DatabaseFramework/DatabaseFramework/DatabaseFramework.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// DatabaseFramework.h | ||
// DatabaseFramework | ||
// | ||
// Created by jigar fumakiya on 25/09/23. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
//! Project version number for DatabaseFramework. | ||
FOUNDATION_EXPORT double DatabaseFrameworkVersionNumber; | ||
|
||
//! Project version string for DatabaseFramework. | ||
FOUNDATION_EXPORT const unsigned char DatabaseFrameworkVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <DatabaseFramework/PublicHeader.h> | ||
|
||
|
250 changes: 250 additions & 0 deletions
250
ios/DatabaseFramework/DatabaseFramework/DatabaseFramework/Db/Database.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
// | ||
// DB.swift | ||
// Runner | ||
// | ||
// Created by jigar fumakiya on 28/07/23. | ||
// | ||
|
||
import Foundation | ||
import Internalsdk | ||
import SQLite | ||
|
||
public struct DatabaseFactory { | ||
public static func getDbManager(databasePath: String) throws -> MinisqlDBProtocol { | ||
guard !databasePath.isEmpty else { | ||
throw NSError(domain: "DatabasePathError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Database path cannot be blank"]) | ||
} | ||
let connection = try! Connection(databasePath) | ||
return DatabaseManager(database: connection) | ||
} | ||
} | ||
|
||
class DatabaseManager: NSObject, MinisqlDBProtocol { | ||
private let db: Connection | ||
private var currentTransaction: TransactionManager? | ||
|
||
init(database: Connection) { | ||
self.db = database | ||
} | ||
|
||
// Static function to get an instance of DatabaseManager | ||
// Expose to Client | ||
static func getDbManager(databasePath: String) throws -> MinisqlDBProtocol { | ||
guard !databasePath.isEmpty else { | ||
throw NSError(domain: "DatabasePathError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Database path cannot be blank"]) | ||
} | ||
let connection = try! Connection(databasePath) | ||
return DatabaseManager(database: connection) | ||
} | ||
|
||
public func begin() throws -> MinisqlTxProtocol { | ||
currentTransaction = TransactionManager(database: db) | ||
return currentTransaction! | ||
} | ||
|
||
public func close()throws { | ||
//Automatically manages the database connections | ||
} | ||
|
||
public func exec(_ query: String?, args: MinisqlValuesProtocol?) throws -> MinisqlResultProtocol { | ||
guard let query = query, let args = args else { | ||
throw NSError(domain: "ArgumentError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Query or arguments are nil"]) | ||
} | ||
let bindings = ValueUtil.toBindingsArray(args) | ||
let statement = try db.prepare(query) | ||
|
||
try statement.run(bindings) | ||
return QueryResult(changes: db.totalChanges) | ||
} | ||
|
||
public func query(_ query: String?, args: MinisqlValuesProtocol?) throws -> MinisqlRowsProtocol { | ||
|
||
guard let query = query, let args = args else { | ||
throw NSError(domain: "ArgumentError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Query or arguments are nil"]) | ||
} | ||
|
||
let bindings = ValueUtil.toBindingsArray(args) | ||
let statement = try db.prepare(query) | ||
|
||
var rows: [Statement.Element] = [] | ||
|
||
try statement.run(bindings).forEach { row in | ||
rows.append(row) | ||
} | ||
return RowData(rows: rows) | ||
} | ||
} | ||
|
||
class TransactionManager: NSObject, MinisqlTxProtocol { | ||
let database: Connection | ||
private var savepointName: String? | ||
|
||
init(database: Connection) { | ||
self.database = database | ||
} | ||
|
||
private func begin() throws { | ||
savepointName = "Savepoint\(UUID().uuidString)" | ||
if let savepointName = savepointName { | ||
try database.run("SAVEPOINT '\(savepointName)'") | ||
} | ||
} | ||
|
||
func commit() throws { | ||
if let savepointName = savepointName { | ||
try database.run("RELEASE '\(savepointName)'") | ||
} | ||
savepointName = nil | ||
} | ||
|
||
func rollback() throws { | ||
if let savepointName = savepointName { | ||
try database.run("ROLLBACK TO SAVEPOINT '\(savepointName)'") | ||
try database.run("RELEASE '\(savepointName)'") | ||
} | ||
savepointName = nil | ||
} | ||
|
||
func exec(_ query: String?, args: MinisqlValuesProtocol?) throws -> MinisqlResultProtocol { | ||
guard let query = query, let args = args else { | ||
throw NSError(domain: "ArgumentError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Query or arguments are nil"]) } | ||
|
||
let bindings = ValueUtil.toBindingsArray(args) | ||
let statement = try database.prepare(query) | ||
// Start a transaction if none has been started yet | ||
if savepointName == nil { | ||
try begin() | ||
} | ||
|
||
try statement.run(bindings) | ||
return QueryResult(changes: database.totalChanges) | ||
} | ||
|
||
func query(_ query: String?, args: MinisqlValuesProtocol?) throws -> MinisqlRowsProtocol { | ||
guard let query = query, let args = args else { | ||
throw NSError(domain: "ArgumentError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Query or arguments are nil"]) } | ||
|
||
let bindings = ValueUtil.toBindingsArray(args) | ||
let statement = try database.prepare(query) | ||
// Start a transaction if none has been started yet | ||
if savepointName == nil { | ||
try begin() | ||
} | ||
|
||
var rows: [Statement.Element] = [] | ||
|
||
for row in try statement.run(bindings) { | ||
rows.append(row) | ||
} | ||
|
||
return RowData(rows: rows) | ||
} | ||
} | ||
|
||
class QueryResult: NSObject, MinisqlResultProtocol { | ||
|
||
let changes: Int | ||
|
||
init(changes: Int) { | ||
self.changes = changes | ||
} | ||
|
||
func lastInsertId(_ ret0_: UnsafeMutablePointer<Int64>?) throws -> Void { | ||
ret0_?.pointee = Int64(changes) | ||
} | ||
|
||
func rowsAffected(_ ret0_: UnsafeMutablePointer<Int64>?) throws -> Void { | ||
ret0_?.pointee = Int64(changes) | ||
} | ||
} | ||
|
||
class RowData: NSObject, MinisqlRowsProtocol { | ||
let rows: [Statement.Element] | ||
var currentIndex: Int = -1 | ||
private let syncQueue = DispatchQueue(label: "com.lantern.RowData.syncQueue") | ||
|
||
|
||
init(rows: [Statement.Element]) { | ||
self.rows = rows | ||
} | ||
|
||
func close() throws { | ||
// Not sure what to put here | ||
} | ||
|
||
func next() -> Bool { | ||
currentIndex += 1 | ||
return currentIndex < rows.count | ||
} | ||
|
||
/** | ||
This method scans the current row and converts its values to `MinisqlValue` objects. | ||
This method assumes that `values` is an object that supports setting values by index, and `rows` is an array of arrays where each inner array represents a row from a database and contains values of type `Binding`. | ||
- Parameter values: An object that conforms to `MinisqlValuesProtocol`. This object will be populated with the values from the current row, converted to `MinisqlValue` objects. | ||
- Throws: An `NSError` if `values` is `nil` or if `currentIndex` is outside the bounds of the `rows` array. | ||
- Note: This method updates `currentIndex` to point to the next row. If there are no more rows, `next()` will return `false`. | ||
*/ | ||
func scan(_ values: MinisqlValuesProtocol?) throws { | ||
try syncQueue.sync { | ||
if values == nil { | ||
logger.log("Error: values is nil") | ||
throw NSError(domain: "Scan method failed", code: 0, userInfo: [NSLocalizedDescriptionKey: "Values object is nil"]) | ||
} | ||
if currentIndex >= rows.count { | ||
logger.log("Error: currentIndex \(currentIndex) is out of bounds") | ||
throw NSError(domain: "Scan method failed", code: 0, userInfo: [NSLocalizedDescriptionKey: "Current index is out of bounds"]) | ||
} | ||
let currentRow = rows[currentIndex] | ||
for (index, value) in currentRow.enumerated() { | ||
let miniSqlValue = values!.get(index)! | ||
ValueUtil.setValueFromBinding(binding: value!, value: miniSqlValue) | ||
} | ||
} | ||
} | ||
} | ||
|
||
public struct ValueArrayFactory { | ||
public static func createValueArrayHandler(values: [MinisqlValue]) -> MinisqlValuesProtocol { | ||
return ValueArrayHandler(values: values) | ||
} | ||
} | ||
|
||
class ValueArrayHandler: NSObject, MinisqlValuesProtocol { | ||
|
||
var values: [MinisqlValue] | ||
|
||
init(values: [MinisqlValue]) { | ||
self.values = values | ||
} | ||
|
||
public func get(_ index: Int) -> MinisqlValue? { | ||
guard index < values.count else { | ||
print("Error: Index out of bounds while trying to get value.") | ||
return nil | ||
} | ||
return values[index] | ||
} | ||
|
||
func len() -> Int { | ||
return values.count | ||
} | ||
|
||
func set(_ index: Int, value: MinisqlValue?) { | ||
guard index < values.count else { | ||
print("Error: Index out of bounds while trying to set value.") | ||
return | ||
} | ||
|
||
guard let value = value else { | ||
print("Error: Attempted to set nil value.") | ||
return | ||
} | ||
|
||
values[index] = value | ||
} | ||
} | ||
extension Date { | ||
static var currentTimeStamp: Int64{ | ||
return Int64(Date().timeIntervalSince1970 * 1000) | ||
} | ||
} |
Oops, something went wrong.