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

Add configuration option for dialects without multi row insert support #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/SQLKit/Query/SQLInsert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public struct SQLInsert: SQLExpression {

public func serialize(to serializer: inout SQLSerializer) {
let modifier = self.conflictStrategy?.queryModifier(for: serializer)

if !serializer.dialect.supportsMultiRowInsert && self.values.count > 1 {
serializer.database.logger.warning("Database does not support inserting multiple row in a single statement. You will need to rewrite as individual insert statements.")
}

serializer.statement {
$0.append("INSERT")
Expand Down
10 changes: 9 additions & 1 deletion Sources/SQLKit/SQLDialect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ public protocol SQLDialect {
/// "writer" lock on rows retrieved by a `SELECT` query. A `nil` value means the database doesn't
/// support exclusive locking requests, which causes the locking clause to be silently ignored.
var exclusiveSelectLockExpression: SQLExpression? { get }


/// `true` if the dialect supports inserting multiple rows with a single statement. If `false`,
/// the dialect requires separate statements for each insert.
///
/// INSERT INTO table(col1, col2) VALUES (val1, val2), (val3, val4)
///
/// Defaults to `true`.
var supportsMultiRowInsert: Bool { get }
}

/// Controls `ALTER TABLE` syntax.
Expand Down Expand Up @@ -322,4 +329,5 @@ extension SQLDialect {
public var unionFeatures: SQLUnionFeatures { [.union, .unionAll] }
public var sharedSelectLockExpression: SQLExpression? { nil }
public var exclusiveSelectLockExpression: SQLExpression? { nil }
public var supportsMultiRowInsert: Bool { true }
}