Skip to content

Change insert methods return types #10

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions Sources/Insert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension PostgresStORM {

/// Insert function where the suppled data is in [(String, Any)] format.
@discardableResult
public func insert(_ data: [(String, Any)]) throws -> Any {
public func insert(_ data: [(String, Any)]) throws -> Any? {

var keys = [String]()
var vals = [String]()
Expand All @@ -31,7 +31,7 @@ extension PostgresStORM {
}

/// Insert function where the suppled data is in [String: Any] format.
public func insert(_ data: [String: Any]) throws -> Any {
public func insert(_ data: [String: Any]) throws -> Any? {

var keys = [String]()
var vals = [String]()
Expand All @@ -47,10 +47,10 @@ extension PostgresStORM {
throw StORMError.error("\(error)")
}
}


/// Insert function where the suppled data is in matching arrays of columns and parameter values.
public func insert(cols: [String], params: [Any]) throws -> Any {
public func insert(cols: [String], params: [Any]) throws -> Any? {
let (idname, _) = firstAsKey()
do {
return try insert(cols: cols, params: params, idcolumn: idname)
Expand All @@ -62,7 +62,7 @@ extension PostgresStORM {


/// Insert function where the suppled data is in matching arrays of columns and parameter values, as well as specifying the name of the id column.
public func insert(cols: [String], params: [Any], idcolumn: String) throws -> Any {
public func insert(cols: [String], params: [Any], idcolumn: String) throws -> Any? {

var paramString = [String]()
var substString = [String]()
Expand All @@ -78,7 +78,11 @@ extension PostgresStORM {

do {
let response = try exec(str, params: paramString)
return parseRows(response)[0].data[idcolumn.lowercased()]!
let parsedRows = parseRows(response)
guard parsedRows.count > 0 else {
return nil
}
return parsedRows[0].data[idcolumn.lowercased()]!
} catch {
LogFile.error("Error: \(error)", logFile: "./StORMlog.txt")
self.error = StORMError.error("\(error)")
Expand Down