@@ -209,6 +209,10 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked
209209 . cancelQuery ,
210210 . materializedViews ,
211211 . dataCompare ,
212+ . rowWrites ,
213+ . cellExpressions ,
214+ . uuidCellText ,
215+ . repeatingPrimaryKey ,
212216 ]
213217 }
214218
@@ -391,86 +395,76 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked
391395
392396 // MARK: - DML Statement Generation
393397
394- func generateStatements (
398+ func generateRowWrites (
395399 table : String ,
400+ schema : String ? ,
396401 columns : [ String ] ,
397402 primaryKeyColumns : [ String ] ,
398403 changes : [ PluginRowChange ] ,
399404 insertedRowData : [ Int : [ PluginCellValue ] ] ,
400405 deletedRowIndices : Set < Int > ,
401406 insertedRowIndices : Set < Int >
402- ) -> [ ( statement : String , parameters : [ PluginCellValue ] ) ] ? {
403- var statements : [ ( statement : String , parameters : [ PluginCellValue ] ) ] = [ ]
404-
405- for change in changes {
406- switch change . type {
407- case . insert :
408- guard insertedRowIndices . contains ( change . rowIndex ) else { continue }
409- if let values = insertedRowData [ change . rowIndex ] {
410- if let stmt = generateClickHouseInsert ( table : table , columns : columns , values : values ) {
411- statements . append ( stmt )
412- }
413- }
414- case . update :
415- if let stmt = generateClickHouseUpdate ( table : table , columns : columns , change : change ) {
416- statements . append ( stmt )
417- }
418- case . delete :
419- guard deletedRowIndices . contains ( change . rowIndex ) else { continue }
420- if let stmt = generateClickHouseDelete ( table : table , columns : columns , change : change ) {
421- statements . append ( stmt )
422- }
423- }
424- }
425-
426- return statements . isEmpty ? nil : statements
407+ ) throws -> [ PluginRowWrite ] ? {
408+ try generateRowWrites (
409+ table : table , schema : schema , columns : columns , primaryKeyColumns : primaryKeyColumns , changes : changes ,
410+ insertedRowData : insertedRowData , deletedRowIndices : deletedRowIndices ,
411+ insertedRowIndices : insertedRowIndices , context : PluginRowWriteContext ( )
412+ )
427413 }
428414
429- private func generateClickHouseInsert (
415+ // The sorting key repeats, so it never names a row: every write matches the whole row (ClickHouseRowWrites).
416+ func generateRowWrites (
430417 table : String ,
418+ schema : String ? ,
431419 columns : [ String ] ,
432- values : [ PluginCellValue ]
433- ) -> ( statement : String , parameters : [ PluginCellValue ] ) ? {
434- var nonDefaultColumns : [ String ] = [ ]
435- var parameters : [ PluginCellValue ] = [ ]
436-
437- for ( index , value ) in values . enumerated ( ) {
438- if value . asText == " __DEFAULT__ " { continue }
439- guard index < columns . count else { continue }
440- nonDefaultColumns . append ( clickHouseQuotedIdentifier ( columns [ index ] ) )
441- parameters . append ( value )
442- }
443-
444- guard ! nonDefaultColumns . isEmpty else { return nil }
420+ primaryKeyColumns : [ String ] ,
421+ changes : [ PluginRowChange ] ,
422+ insertedRowData : [ Int : [ PluginCellValue ] ] ,
423+ deletedRowIndices : Set < Int > ,
424+ insertedRowIndices : Set < Int > ,
425+ context : PluginRowWriteContext
426+ ) throws -> [ PluginRowWrite ] ? {
427+ try ClickHouseRowWrites . writes (
428+ table : table ,
429+ schema : schema ,
430+ columns : columns ,
431+ changes : changes ,
432+ insertedRowData : insertedRowData ,
433+ deletedRowIndices : deletedRowIndices ,
434+ insertedRowIndices : insertedRowIndices ,
435+ context : context ,
436+ forms : rowWriteForms
437+ )
438+ }
445439
446- let columnList = nonDefaultColumns . joined ( separator : " , " )
447- let placeholders = parameters . map { _ in " ? " } . joined ( separator : " , " )
448- let sql = " INSERT INTO \( clickHouseQuotedIdentifier ( table ) ) ( \( columnList ) ) VALUES ( \( placeholders ) ) "
449- return ( statement : sql , parameters : parameters )
440+ // A lightweight UPDATE or DELETE when the user may not wait for a mutation: each finishes before it returns.
441+ private var rowWriteForms : ClickHouseRowWrites . Forms {
442+ ClickHouseRowWrites . Forms (
443+ update : { [ self ] table , assignments , predicate in
444+ rowUpdateStatement ( table : table , assignments : assignments , predicate : predicate )
445+ ?? " UPDATE \( table ) SET \( assignments ) WHERE \( predicate ) "
446+ } ,
447+ delete : { [ self ] table , predicate in
448+ rowDeleteStatement ( table : table , predicate : predicate ) ?? " DELETE FROM \( table ) WHERE \( predicate ) "
449+ }
450+ )
450451 }
451452
452- private func generateClickHouseUpdate (
453+ // No column is filled by the server unasked, so the row goes back with every value it had.
454+ func generateIdentityPreservingInsert (
453455 table : String ,
456+ schema : String ? ,
454457 columns : [ String ] ,
455- change : PluginRowChange
456- ) -> ( statement : String , parameters : [ PluginCellValue ] ) ? {
457- guard ! change . cellChanges . isEmpty else { return nil }
458-
459- let escapedTable = clickHouseQuotedIdentifier ( table )
460- var parameters : [ PluginCellValue ] = [ ]
461-
462- let setClauses = change . cellChanges . map { cellChange -> String in
463- let col = clickHouseQuotedIdentifier ( cellChange . columnName )
464- parameters . append ( cellChange . newValue )
465- return " \( col ) = ? "
466- } . joined ( separator : " , " )
467-
468- guard let whereClause = buildWhereClause (
469- columns : columns , change : change , parameters : & parameters
470- ) else { return nil }
471-
472- let sql = " ALTER TABLE \( escapedTable ) UPDATE \( setClauses ) WHERE \( whereClause ) "
473- return ( statement : sql , parameters : parameters )
458+ primaryKeyColumns : [ String ] ,
459+ rows : [ [ PluginCellValue ] ]
460+ ) -> [ ( statement : String , parameters : [ PluginCellValue ] ) ] ? {
461+ let target = ClickHouseRowWrites . qualified ( table : table , schema : schema )
462+ return rows . enumerated ( ) . map { index , row in
463+ let write = ClickHouseRowWrites . insert (
464+ into : target , columns : columns , values : row , expressions : [ ] , rowIndex : index
465+ )
466+ return ( write . statement , write . parameters )
467+ }
474468 }
475469
476470 // A plain UPDATE needs lightweight updates enabled on the table. A mutation returns before it applies unless
@@ -493,46 +487,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked
493487 serverProfile . allows ( . mutationsSync , " 1 " ) ? " SETTINGS mutations_sync = 1 " : nil
494488 }
495489
496- private func generateClickHouseDelete (
497- table : String ,
498- columns : [ String ] ,
499- change : PluginRowChange
500- ) -> ( statement : String , parameters : [ PluginCellValue ] ) ? {
501- let escapedTable = clickHouseQuotedIdentifier ( table )
502- var parameters : [ PluginCellValue ] = [ ]
503-
504- guard let whereClause = buildWhereClause (
505- columns : columns , change : change , parameters : & parameters
506- ) else { return nil }
507-
508- let sql = " ALTER TABLE \( escapedTable ) DELETE WHERE \( whereClause ) "
509- return ( statement : sql , parameters : parameters )
510- }
511-
512- private func buildWhereClause (
513- columns : [ String ] ,
514- change : PluginRowChange ,
515- parameters : inout [ PluginCellValue ]
516- ) -> String ? {
517- guard let originalRow = change . originalRow else { return nil }
518-
519- var conditions : [ String ] = [ ]
520- for ( index , columnName ) in columns . enumerated ( ) {
521- guard index < originalRow . count else { continue }
522- let col = clickHouseQuotedIdentifier ( columnName )
523- let value = originalRow [ index ]
524- if value . isNull {
525- conditions . append ( " \( col ) IS NULL " )
526- } else {
527- parameters . append ( value )
528- conditions . append ( " \( col ) = ? " )
529- }
530- }
531-
532- guard ! conditions . isEmpty else { return nil }
533- return conditions . joined ( separator : " AND " )
534- }
535-
536490 func cancelQuery ( ) throws {
537491 let queryId : String ?
538492 lock . lock ( )
0 commit comments