-
-
Notifications
You must be signed in to change notification settings - Fork 282
Import: choose source format (SQL/JSON) and batch JSON inserts #1548
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -196,6 +196,36 @@ struct SQLStatementGenerator { | |
| return ParameterizedStatement(sql: sql, parameters: bindParameters) | ||
| } | ||
|
|
||
| func insertStatement(columns insertColumns: [String], rows: [[PluginCellValue]]) | ||
| -> ParameterizedStatement? | ||
| { | ||
| guard !insertColumns.isEmpty, !rows.isEmpty, | ||
| rows.allSatisfy({ $0.count == insertColumns.count }) else { return nil } | ||
|
|
||
| var bindParameters: [Any?] = [] | ||
| let columnList = insertColumns.map(quoteIdentifierFn).joined(separator: ", ") | ||
| let rowTuples = rows.map { values -> String in | ||
| let placeholders = values.map { value -> String in | ||
| bindParameters.append(value.asAny) | ||
| return placeholder(at: bindParameters.count - 1) | ||
| }.joined(separator: ", ") | ||
| return "(\(placeholders))" | ||
| }.joined(separator: ", ") | ||
|
|
||
| let sql = | ||
| "INSERT INTO \(quoteIdentifierFn(tableName)) (\(columnList)) VALUES \(rowTuples)" | ||
|
Comment on lines
+215
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Oracle connections, JSON row imports now batch same-shaped rows into Useful? React with 👍 / 👎. |
||
|
|
||
| return ParameterizedStatement(sql: sql, parameters: bindParameters) | ||
| } | ||
|
|
||
| var maxBindParameters: Int { | ||
| switch databaseType { | ||
| case .sqlite: 32_766 | ||
| case .mssql: 2_100 | ||
| default: 65_535 | ||
| } | ||
| } | ||
|
|
||
| func deleteAllRowsStatement() -> String { | ||
| "DELETE FROM \(quoteIdentifierFn(tableName))" | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // | ||
| // ImportRouting.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| struct ImportFormatOption: Identifiable, Equatable { | ||
| let id: String | ||
| let name: String | ||
|
|
||
| var submenuLabel: String { | ||
| String(format: String(localized: "From %@\u{2026}"), name) | ||
| } | ||
|
|
||
| var standaloneLabel: String { | ||
| String(format: String(localized: "Import %@\u{2026}"), name) | ||
| } | ||
| } | ||
|
|
||
| enum ImportSheetRoute: Equatable { | ||
| case statement(formatId: String) | ||
| case rowMapping(formatId: String) | ||
| } | ||
|
|
||
| enum ImportRouting { | ||
| static func route(formatId: String, requiresTargetTable: Bool) -> ImportSheetRoute { | ||
| requiresTargetTable ? .rowMapping(formatId: formatId) : .statement(formatId: formatId) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
skipAndContinuemode this assumessink.insertRows(...)is all-or-nothing, butImportDataSinkAdapter.insertRowscan execute several statements for one batch (different column sets or bind-limit chunks) before a later statement throws. When that happens, the catch path replays every entry row-by-row, so any rows from earlier successful statements in the same batch are inserted a second time. This shows up with JSON rows that do not all have the same mapped columns and one later group has a bad value; either make the sink batch atomic for this path or only replay the rows that were not already committed.Useful? React with 👍 / 👎.