From 41c3d15963d2a3a0eb6b7dc5990690ea34309d6d Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Fri, 1 Apr 2016 13:55:51 +0200 Subject: [PATCH] building prepared queries (with positional params) --- go/sql/builder.go | 10 ++++++++++ go/sql/builder_test.go | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/go/sql/builder.go b/go/sql/builder.go index d1086bb..4b5e4a4 100644 --- a/go/sql/builder.go +++ b/go/sql/builder.go @@ -136,3 +136,13 @@ func BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName strin rangeStartComparison, rangeEndComparison) return query, nil } + +func BuildRangeInsertPreparedQuery(databaseName, originalTableName, ghostTableName string, sharedColumns []string, uniqueKey string, uniqueKeyColumns []string) (string, error) { + rangeStartValues := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns)) + rangeEndValues := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns)) + for i := range uniqueKeyColumns { + rangeStartValues[i] = "?" + rangeEndValues[i] = "?" + } + return BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName, sharedColumns, uniqueKey, uniqueKeyColumns, rangeStartValues, rangeEndValues) +} diff --git a/go/sql/builder_test.go b/go/sql/builder_test.go index 9994842..8373f3b 100644 --- a/go/sql/builder_test.go +++ b/go/sql/builder_test.go @@ -155,3 +155,24 @@ func TestBuildRangeInsertQuery(t *testing.T) { test.S(t).ExpectEquals(normalizeQuery(query), normalizeQuery(expected)) } } + +func TestBuildRangeInsertPreparedQuery(t *testing.T) { + databaseName := "mydb" + originalTableName := "tbl" + ghostTableName := "ghost" + sharedColumns := []string{"id", "name", "position"} + { + uniqueKey := "name_position_uidx" + uniqueKeyColumns := []string{"name", "position"} + + query, err := BuildRangeInsertPreparedQuery(databaseName, originalTableName, ghostTableName, sharedColumns, uniqueKey, uniqueKeyColumns) + test.S(t).ExpectNil(err) + expected := ` + insert /* gh-osc mydb.tbl */ ignore into mydb.ghost (id, name, position) + (select id, name, position from mydb.tbl force index (name_position_uidx) + where (((name > ?) or (((name = ?)) AND (position > ?)) or ((name = ?) and (position = ?))) and ((name < ?) or (((name = ?)) AND (position < ?)) or ((name = ?) and (position = ?)))) + ) + ` + test.S(t).ExpectEquals(normalizeQuery(query), normalizeQuery(expected)) + } +}