2016-04-01 11:36:56 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-04-01 11:36:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ValueComparisonSign string
|
|
|
|
|
|
|
|
const (
|
|
|
|
LessThanComparisonSign ValueComparisonSign = "<"
|
|
|
|
LessThanOrEqualsComparisonSign = "<="
|
|
|
|
EqualsComparisonSign = "="
|
|
|
|
GreaterThanOrEqualsComparisonSign = ">="
|
|
|
|
GreaterThanComparisonSign = ">"
|
|
|
|
NotEqualsComparisonSign = "!="
|
|
|
|
)
|
|
|
|
|
|
|
|
// EscapeName will escape a db/table/column/... name by wrapping with backticks.
|
|
|
|
// It is not fool proof. I'm just trying to do the right thing here, not solving
|
|
|
|
// SQL injection issues, which should be irrelevant for this tool.
|
|
|
|
func EscapeName(name string) string {
|
|
|
|
if unquoted, err := strconv.Unquote(name); err == nil {
|
|
|
|
name = unquoted
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("`%s`", name)
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:52:23 +00:00
|
|
|
func fixArgType(arg interface{}, isUnsigned bool) interface{} {
|
|
|
|
if !isUnsigned {
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
// unsigned
|
|
|
|
if i, ok := arg.(int8); ok {
|
|
|
|
return uint8(i)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int16); ok {
|
|
|
|
return uint16(i)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int32); ok {
|
|
|
|
return uint32(i)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int64); ok {
|
2016-08-18 11:31:53 +00:00
|
|
|
return strconv.FormatUint(uint64(i), 10)
|
2016-08-17 04:52:23 +00:00
|
|
|
}
|
|
|
|
if i, ok := arg.(int); ok {
|
|
|
|
return uint(i)
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
func buildPreparedValues(length int) []string {
|
|
|
|
values := make([]string, length, length)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
values[i] = "?"
|
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
|
|
|
|
|
|
|
func duplicateNames(names []string) []string {
|
|
|
|
duplicate := make([]string, len(names), len(names))
|
|
|
|
copy(duplicate, names)
|
|
|
|
return duplicate
|
|
|
|
}
|
|
|
|
|
2016-04-01 11:36:56 +00:00
|
|
|
func BuildValueComparison(column string, value string, comparisonSign ValueComparisonSign) (result string, err error) {
|
|
|
|
if column == "" {
|
|
|
|
return "", fmt.Errorf("Empty column in GetValueComparison")
|
|
|
|
}
|
|
|
|
if value == "" {
|
|
|
|
return "", fmt.Errorf("Empty value in GetValueComparison")
|
|
|
|
}
|
|
|
|
comparison := fmt.Sprintf("(%s %s %s)", EscapeName(column), string(comparisonSign), value)
|
|
|
|
return comparison, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildEqualsComparison(columns []string, values []string) (result string, err error) {
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", fmt.Errorf("Got 0 columns in GetEqualsComparison")
|
|
|
|
}
|
|
|
|
if len(columns) != len(values) {
|
|
|
|
return "", fmt.Errorf("Got %d columns but %d values in GetEqualsComparison", len(columns), len(values))
|
|
|
|
}
|
|
|
|
comparisons := []string{}
|
|
|
|
for i, column := range columns {
|
|
|
|
value := values[i]
|
|
|
|
comparison, err := BuildValueComparison(column, value, EqualsComparisonSign)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
comparisons = append(comparisons, comparison)
|
|
|
|
}
|
|
|
|
result = strings.Join(comparisons, " and ")
|
|
|
|
result = fmt.Sprintf("(%s)", result)
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
func BuildEqualsPreparedComparison(columns []string) (result string, err error) {
|
|
|
|
values := buildPreparedValues(len(columns))
|
|
|
|
return BuildEqualsComparison(columns, values)
|
|
|
|
}
|
|
|
|
|
2016-04-14 11:37:56 +00:00
|
|
|
func BuildSetPreparedClause(columns []string) (result string, err error) {
|
|
|
|
if len(columns) == 0 {
|
|
|
|
return "", fmt.Errorf("Got 0 columns in BuildSetPreparedClause")
|
|
|
|
}
|
|
|
|
setTokens := []string{}
|
|
|
|
for _, column := range columns {
|
|
|
|
setTokens = append(setTokens, fmt.Sprintf("%s=?", EscapeName(column)))
|
|
|
|
}
|
|
|
|
return strings.Join(setTokens, ", "), nil
|
|
|
|
}
|
|
|
|
|
2016-04-05 17:50:49 +00:00
|
|
|
func BuildRangeComparison(columns []string, values []string, args []interface{}, comparisonSign ValueComparisonSign) (result string, explodedArgs []interface{}, err error) {
|
2016-04-01 11:36:56 +00:00
|
|
|
if len(columns) == 0 {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, fmt.Errorf("Got 0 columns in GetRangeComparison")
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
if len(columns) != len(values) {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, fmt.Errorf("Got %d columns but %d values in GetEqualsComparison", len(columns), len(values))
|
|
|
|
}
|
|
|
|
if len(columns) != len(args) {
|
|
|
|
return "", explodedArgs, fmt.Errorf("Got %d columns but %d args in GetEqualsComparison", len(columns), len(args))
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
includeEquals := false
|
|
|
|
if comparisonSign == LessThanOrEqualsComparisonSign {
|
|
|
|
comparisonSign = LessThanComparisonSign
|
|
|
|
includeEquals = true
|
|
|
|
}
|
|
|
|
if comparisonSign == GreaterThanOrEqualsComparisonSign {
|
|
|
|
comparisonSign = GreaterThanComparisonSign
|
|
|
|
includeEquals = true
|
|
|
|
}
|
|
|
|
comparisons := []string{}
|
|
|
|
|
|
|
|
for i, column := range columns {
|
|
|
|
//
|
|
|
|
value := values[i]
|
|
|
|
rangeComparison, err := BuildValueComparison(column, value, comparisonSign)
|
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, err
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
if len(columns[0:i]) > 0 {
|
|
|
|
equalitiesComparison, err := BuildEqualsComparison(columns[0:i], values[0:i])
|
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, err
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
comparison := fmt.Sprintf("(%s AND %s)", equalitiesComparison, rangeComparison)
|
|
|
|
comparisons = append(comparisons, comparison)
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, args[0:i]...)
|
|
|
|
explodedArgs = append(explodedArgs, args[i])
|
2016-04-01 11:36:56 +00:00
|
|
|
} else {
|
|
|
|
comparisons = append(comparisons, rangeComparison)
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, args[i])
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeEquals {
|
|
|
|
comparison, err := BuildEqualsComparison(columns, values)
|
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, nil
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
comparisons = append(comparisons, comparison)
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, args...)
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
result = strings.Join(comparisons, " or ")
|
|
|
|
result = fmt.Sprintf("(%s)", result)
|
2016-04-05 17:50:49 +00:00
|
|
|
return result, explodedArgs, nil
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 17:50:49 +00:00
|
|
|
func BuildRangePreparedComparison(columns []string, args []interface{}, comparisonSign ValueComparisonSign) (result string, explodedArgs []interface{}, err error) {
|
2016-04-11 15:27:16 +00:00
|
|
|
values := buildPreparedValues(len(columns))
|
2016-04-05 17:50:49 +00:00
|
|
|
return BuildRangeComparison(columns, values, args, comparisonSign)
|
2016-04-01 14:05:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 06:03:18 +00:00
|
|
|
func BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName string, sharedColumns []string, mappedSharedColumns []string, uniqueKey string, uniqueKeyColumns, rangeStartValues, rangeEndValues []string, rangeStartArgs, rangeEndArgs []interface{}, includeRangeStartValues bool, transactionalTable bool) (result string, explodedArgs []interface{}, err error) {
|
2016-04-01 11:36:56 +00:00
|
|
|
if len(sharedColumns) == 0 {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, fmt.Errorf("Got 0 shared columns in BuildRangeInsertQuery")
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
2016-04-01 14:05:17 +00:00
|
|
|
databaseName = EscapeName(databaseName)
|
|
|
|
originalTableName = EscapeName(originalTableName)
|
|
|
|
ghostTableName = EscapeName(ghostTableName)
|
2016-06-17 06:03:18 +00:00
|
|
|
|
|
|
|
mappedSharedColumns = duplicateNames(mappedSharedColumns)
|
|
|
|
for i := range mappedSharedColumns {
|
|
|
|
mappedSharedColumns[i] = EscapeName(mappedSharedColumns[i])
|
|
|
|
}
|
|
|
|
mappedSharedColumnsListing := strings.Join(mappedSharedColumns, ", ")
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
sharedColumns = duplicateNames(sharedColumns)
|
2016-04-01 14:05:17 +00:00
|
|
|
for i := range sharedColumns {
|
|
|
|
sharedColumns[i] = EscapeName(sharedColumns[i])
|
|
|
|
}
|
2016-04-01 11:36:56 +00:00
|
|
|
sharedColumnsListing := strings.Join(sharedColumns, ", ")
|
2016-06-17 06:03:18 +00:00
|
|
|
|
|
|
|
uniqueKey = EscapeName(uniqueKey)
|
2016-04-05 07:14:22 +00:00
|
|
|
var minRangeComparisonSign ValueComparisonSign = GreaterThanComparisonSign
|
|
|
|
if includeRangeStartValues {
|
|
|
|
minRangeComparisonSign = GreaterThanOrEqualsComparisonSign
|
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
rangeStartComparison, rangeExplodedArgs, err := BuildRangeComparison(uniqueKeyColumns, rangeStartValues, rangeStartArgs, minRangeComparisonSign)
|
2016-04-01 11:36:56 +00:00
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, err
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, rangeExplodedArgs...)
|
|
|
|
rangeEndComparison, rangeExplodedArgs, err := BuildRangeComparison(uniqueKeyColumns, rangeEndValues, rangeEndArgs, LessThanOrEqualsComparisonSign)
|
2016-04-01 11:36:56 +00:00
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, err
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, rangeExplodedArgs...)
|
2016-04-08 08:34:44 +00:00
|
|
|
transactionalClause := ""
|
|
|
|
if transactionalTable {
|
|
|
|
transactionalClause = "lock in share mode"
|
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
result = fmt.Sprintf(`
|
2016-05-16 09:09:17 +00:00
|
|
|
insert /* gh-ost %s.%s */ ignore into %s.%s (%s)
|
2016-04-01 11:36:56 +00:00
|
|
|
(select %s from %s.%s force index (%s)
|
2016-04-08 08:34:44 +00:00
|
|
|
where (%s and %s) %s
|
2016-04-01 11:36:56 +00:00
|
|
|
)
|
2016-06-17 06:03:18 +00:00
|
|
|
`, databaseName, originalTableName, databaseName, ghostTableName, mappedSharedColumnsListing,
|
2016-04-01 11:36:56 +00:00
|
|
|
sharedColumnsListing, databaseName, originalTableName, uniqueKey,
|
2016-04-08 08:34:44 +00:00
|
|
|
rangeStartComparison, rangeEndComparison, transactionalClause)
|
2016-04-05 17:50:49 +00:00
|
|
|
return result, explodedArgs, nil
|
2016-04-01 11:36:56 +00:00
|
|
|
}
|
2016-04-01 11:55:51 +00:00
|
|
|
|
2016-06-17 06:03:18 +00:00
|
|
|
func BuildRangeInsertPreparedQuery(databaseName, originalTableName, ghostTableName string, sharedColumns []string, mappedSharedColumns []string, uniqueKey string, uniqueKeyColumns []string, rangeStartArgs, rangeEndArgs []interface{}, includeRangeStartValues bool, transactionalTable bool) (result string, explodedArgs []interface{}, err error) {
|
2016-04-11 15:27:16 +00:00
|
|
|
rangeStartValues := buildPreparedValues(len(uniqueKeyColumns))
|
|
|
|
rangeEndValues := buildPreparedValues(len(uniqueKeyColumns))
|
2016-06-17 06:03:18 +00:00
|
|
|
return BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName, sharedColumns, mappedSharedColumns, uniqueKey, uniqueKeyColumns, rangeStartValues, rangeEndValues, rangeStartArgs, rangeEndArgs, includeRangeStartValues, transactionalTable)
|
2016-04-01 11:55:51 +00:00
|
|
|
}
|
2016-04-01 14:05:17 +00:00
|
|
|
|
2016-05-18 12:53:09 +00:00
|
|
|
func BuildUniqueKeyRangeEndPreparedQuery(databaseName, tableName string, uniqueKeyColumns []string, rangeStartArgs, rangeEndArgs []interface{}, chunkSize int64, includeRangeStartValues bool, hint string) (result string, explodedArgs []interface{}, err error) {
|
2016-04-01 14:05:17 +00:00
|
|
|
if len(uniqueKeyColumns) == 0 {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, fmt.Errorf("Got 0 columns in BuildUniqueKeyRangeEndPreparedQuery")
|
2016-04-01 14:05:17 +00:00
|
|
|
}
|
|
|
|
databaseName = EscapeName(databaseName)
|
2016-04-04 16:19:46 +00:00
|
|
|
tableName = EscapeName(tableName)
|
2016-04-01 14:05:17 +00:00
|
|
|
|
2016-05-18 12:53:09 +00:00
|
|
|
var startRangeComparisonSign ValueComparisonSign = GreaterThanComparisonSign
|
|
|
|
if includeRangeStartValues {
|
|
|
|
startRangeComparisonSign = GreaterThanOrEqualsComparisonSign
|
|
|
|
}
|
|
|
|
rangeStartComparison, rangeExplodedArgs, err := BuildRangePreparedComparison(uniqueKeyColumns, rangeStartArgs, startRangeComparisonSign)
|
2016-04-01 14:05:17 +00:00
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, err
|
2016-04-01 14:05:17 +00:00
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, rangeExplodedArgs...)
|
|
|
|
rangeEndComparison, rangeExplodedArgs, err := BuildRangePreparedComparison(uniqueKeyColumns, rangeEndArgs, LessThanOrEqualsComparisonSign)
|
2016-04-01 14:05:17 +00:00
|
|
|
if err != nil {
|
2016-04-05 17:50:49 +00:00
|
|
|
return "", explodedArgs, err
|
2016-04-01 14:05:17 +00:00
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
explodedArgs = append(explodedArgs, rangeExplodedArgs...)
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
uniqueKeyColumns = duplicateNames(uniqueKeyColumns)
|
2016-04-01 14:05:17 +00:00
|
|
|
uniqueKeyColumnAscending := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns))
|
|
|
|
uniqueKeyColumnDescending := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns))
|
|
|
|
for i := range uniqueKeyColumns {
|
|
|
|
uniqueKeyColumns[i] = EscapeName(uniqueKeyColumns[i])
|
|
|
|
uniqueKeyColumnAscending[i] = fmt.Sprintf("%s asc", uniqueKeyColumns[i])
|
|
|
|
uniqueKeyColumnDescending[i] = fmt.Sprintf("%s desc", uniqueKeyColumns[i])
|
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
result = fmt.Sprintf(`
|
2016-05-16 09:09:17 +00:00
|
|
|
select /* gh-ost %s.%s %s */ %s
|
2016-04-01 14:05:17 +00:00
|
|
|
from (
|
|
|
|
select
|
|
|
|
%s
|
|
|
|
from
|
|
|
|
%s.%s
|
|
|
|
where %s and %s
|
|
|
|
order by
|
|
|
|
%s
|
|
|
|
limit %d
|
|
|
|
) select_osc_chunk
|
|
|
|
order by
|
|
|
|
%s
|
|
|
|
limit 1
|
2016-04-05 17:50:49 +00:00
|
|
|
`, databaseName, tableName, hint, strings.Join(uniqueKeyColumns, ", "),
|
2016-04-04 16:19:46 +00:00
|
|
|
strings.Join(uniqueKeyColumns, ", "), databaseName, tableName,
|
2016-04-01 14:05:17 +00:00
|
|
|
rangeStartComparison, rangeEndComparison,
|
|
|
|
strings.Join(uniqueKeyColumnAscending, ", "), chunkSize,
|
|
|
|
strings.Join(uniqueKeyColumnDescending, ", "),
|
|
|
|
)
|
2016-04-05 17:50:49 +00:00
|
|
|
return result, explodedArgs, nil
|
2016-04-01 14:05:17 +00:00
|
|
|
}
|
2016-04-04 16:19:46 +00:00
|
|
|
|
|
|
|
func BuildUniqueKeyMinValuesPreparedQuery(databaseName, tableName string, uniqueKeyColumns []string) (string, error) {
|
|
|
|
return buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, uniqueKeyColumns, "asc")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildUniqueKeyMaxValuesPreparedQuery(databaseName, tableName string, uniqueKeyColumns []string) (string, error) {
|
|
|
|
return buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, uniqueKeyColumns, "desc")
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName string, uniqueKeyColumns []string, order string) (string, error) {
|
|
|
|
if len(uniqueKeyColumns) == 0 {
|
|
|
|
return "", fmt.Errorf("Got 0 columns in BuildUniqueKeyMinMaxValuesPreparedQuery")
|
|
|
|
}
|
|
|
|
databaseName = EscapeName(databaseName)
|
|
|
|
tableName = EscapeName(tableName)
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
uniqueKeyColumns = duplicateNames(uniqueKeyColumns)
|
2016-04-04 16:19:46 +00:00
|
|
|
uniqueKeyColumnOrder := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns))
|
|
|
|
for i := range uniqueKeyColumns {
|
|
|
|
uniqueKeyColumns[i] = EscapeName(uniqueKeyColumns[i])
|
|
|
|
uniqueKeyColumnOrder[i] = fmt.Sprintf("%s %s", uniqueKeyColumns[i], order)
|
|
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
2016-05-16 09:09:17 +00:00
|
|
|
select /* gh-ost %s.%s */ %s
|
2016-04-04 16:19:46 +00:00
|
|
|
from
|
|
|
|
%s.%s
|
|
|
|
order by
|
|
|
|
%s
|
|
|
|
limit 1
|
|
|
|
`, databaseName, tableName, strings.Join(uniqueKeyColumns, ", "),
|
|
|
|
databaseName, tableName,
|
|
|
|
strings.Join(uniqueKeyColumnOrder, ", "),
|
|
|
|
)
|
|
|
|
return query, nil
|
|
|
|
}
|
2016-04-11 15:27:16 +00:00
|
|
|
|
2016-04-14 11:37:56 +00:00
|
|
|
func BuildDMLDeleteQuery(databaseName, tableName string, tableColumns, uniqueKeyColumns *ColumnList, args []interface{}) (result string, uniqueKeyArgs []interface{}, err error) {
|
|
|
|
if len(args) != tableColumns.Len() {
|
2016-04-11 15:27:16 +00:00
|
|
|
return result, uniqueKeyArgs, fmt.Errorf("args count differs from table column count in BuildDMLDeleteQuery")
|
|
|
|
}
|
2016-04-14 11:37:56 +00:00
|
|
|
if uniqueKeyColumns.Len() == 0 {
|
|
|
|
return result, uniqueKeyArgs, fmt.Errorf("No unique key columns found in BuildDMLDeleteQuery")
|
|
|
|
}
|
2016-04-11 15:27:16 +00:00
|
|
|
for _, column := range uniqueKeyColumns.Names {
|
2016-04-14 11:37:56 +00:00
|
|
|
tableOrdinal := tableColumns.Ordinals[column]
|
2016-08-17 04:52:23 +00:00
|
|
|
arg := fixArgType(args[tableOrdinal], uniqueKeyColumns.IsUnsigned(column))
|
|
|
|
uniqueKeyArgs = append(uniqueKeyArgs, arg)
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
|
|
|
databaseName = EscapeName(databaseName)
|
|
|
|
tableName = EscapeName(tableName)
|
|
|
|
equalsComparison, err := BuildEqualsPreparedComparison(uniqueKeyColumns.Names)
|
2016-04-14 11:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, uniqueKeyArgs, err
|
|
|
|
}
|
2016-04-11 15:27:16 +00:00
|
|
|
result = fmt.Sprintf(`
|
2016-05-16 09:09:17 +00:00
|
|
|
delete /* gh-ost %s.%s */
|
2016-04-11 15:27:16 +00:00
|
|
|
from
|
|
|
|
%s.%s
|
|
|
|
where
|
|
|
|
%s
|
|
|
|
`, databaseName, tableName,
|
|
|
|
databaseName, tableName,
|
|
|
|
equalsComparison,
|
|
|
|
)
|
2016-04-14 11:37:56 +00:00
|
|
|
return result, uniqueKeyArgs, nil
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 14:00:15 +00:00
|
|
|
func BuildDMLInsertQuery(databaseName, tableName string, tableColumns, sharedColumns, mappedSharedColumns *ColumnList, args []interface{}) (result string, sharedArgs []interface{}, err error) {
|
2016-04-14 11:37:56 +00:00
|
|
|
if len(args) != tableColumns.Len() {
|
2016-04-11 15:27:16 +00:00
|
|
|
return result, args, fmt.Errorf("args count differs from table column count in BuildDMLInsertQuery")
|
|
|
|
}
|
2016-04-14 11:37:56 +00:00
|
|
|
if !sharedColumns.IsSubsetOf(tableColumns) {
|
2016-04-11 15:27:16 +00:00
|
|
|
return result, args, fmt.Errorf("shared columns is not a subset of table columns in BuildDMLInsertQuery")
|
|
|
|
}
|
|
|
|
if sharedColumns.Len() == 0 {
|
|
|
|
return result, args, fmt.Errorf("No shared columns found in BuildDMLInsertQuery")
|
|
|
|
}
|
|
|
|
databaseName = EscapeName(databaseName)
|
|
|
|
tableName = EscapeName(tableName)
|
|
|
|
|
2016-08-22 14:00:15 +00:00
|
|
|
for _, column := range mappedSharedColumns.Names {
|
2016-04-14 11:37:56 +00:00
|
|
|
tableOrdinal := tableColumns.Ordinals[column]
|
2016-08-22 14:00:15 +00:00
|
|
|
arg := fixArgType(args[tableOrdinal], mappedSharedColumns.IsUnsigned(column))
|
2016-08-17 04:52:23 +00:00
|
|
|
sharedArgs = append(sharedArgs, arg)
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 14:00:15 +00:00
|
|
|
mappedSharedColumnNames := duplicateNames(mappedSharedColumns.Names)
|
|
|
|
for i := range mappedSharedColumnNames {
|
|
|
|
mappedSharedColumnNames[i] = EscapeName(mappedSharedColumnNames[i])
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
2016-08-22 14:00:15 +00:00
|
|
|
preparedValues := buildPreparedValues(mappedSharedColumns.Len())
|
2016-04-11 15:27:16 +00:00
|
|
|
|
|
|
|
result = fmt.Sprintf(`
|
2016-05-16 09:09:17 +00:00
|
|
|
replace /* gh-ost %s.%s */ into
|
2016-04-11 15:27:16 +00:00
|
|
|
%s.%s
|
|
|
|
(%s)
|
|
|
|
values
|
|
|
|
(%s)
|
|
|
|
`, databaseName, tableName,
|
|
|
|
databaseName, tableName,
|
2016-08-22 14:00:15 +00:00
|
|
|
strings.Join(mappedSharedColumnNames, ", "),
|
2016-04-11 15:27:16 +00:00
|
|
|
strings.Join(preparedValues, ", "),
|
|
|
|
)
|
2016-04-14 11:37:56 +00:00
|
|
|
return result, sharedArgs, nil
|
|
|
|
}
|
|
|
|
|
2016-08-22 06:49:27 +00:00
|
|
|
func BuildDMLUpdateQuery(databaseName, tableName string, tableColumns, sharedColumns, mappedSharedColumns, uniqueKeyColumns *ColumnList, valueArgs, whereArgs []interface{}) (result string, sharedArgs, uniqueKeyArgs []interface{}, err error) {
|
2016-04-14 11:37:56 +00:00
|
|
|
if len(valueArgs) != tableColumns.Len() {
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, fmt.Errorf("value args count differs from table column count in BuildDMLUpdateQuery")
|
|
|
|
}
|
|
|
|
if len(whereArgs) != tableColumns.Len() {
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, fmt.Errorf("where args count differs from table column count in BuildDMLUpdateQuery")
|
|
|
|
}
|
|
|
|
if !sharedColumns.IsSubsetOf(tableColumns) {
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, fmt.Errorf("shared columns is not a subset of table columns in BuildDMLUpdateQuery")
|
|
|
|
}
|
|
|
|
if !uniqueKeyColumns.IsSubsetOf(sharedColumns) {
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, fmt.Errorf("unique key columns is not a subset of shared columns in BuildDMLUpdateQuery")
|
|
|
|
}
|
|
|
|
if sharedColumns.Len() == 0 {
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, fmt.Errorf("No shared columns found in BuildDMLUpdateQuery")
|
|
|
|
}
|
|
|
|
if uniqueKeyColumns.Len() == 0 {
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, fmt.Errorf("No unique key columns found in BuildDMLUpdateQuery")
|
|
|
|
}
|
|
|
|
databaseName = EscapeName(databaseName)
|
|
|
|
tableName = EscapeName(tableName)
|
|
|
|
|
2016-08-22 06:49:27 +00:00
|
|
|
for i, column := range sharedColumns.Names {
|
|
|
|
mappedColumn := mappedSharedColumns.Names[i]
|
2016-04-14 11:37:56 +00:00
|
|
|
tableOrdinal := tableColumns.Ordinals[column]
|
2016-08-22 06:49:27 +00:00
|
|
|
arg := fixArgType(valueArgs[tableOrdinal], mappedSharedColumns.IsUnsigned(mappedColumn))
|
2016-08-17 04:52:23 +00:00
|
|
|
sharedArgs = append(sharedArgs, arg)
|
2016-04-14 11:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, column := range uniqueKeyColumns.Names {
|
|
|
|
tableOrdinal := tableColumns.Ordinals[column]
|
2016-08-17 04:52:23 +00:00
|
|
|
arg := fixArgType(whereArgs[tableOrdinal], uniqueKeyColumns.IsUnsigned(column))
|
|
|
|
uniqueKeyArgs = append(uniqueKeyArgs, arg)
|
2016-04-14 11:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 06:49:27 +00:00
|
|
|
mappedSharedColumnNames := duplicateNames(mappedSharedColumns.Names)
|
|
|
|
for i := range mappedSharedColumnNames {
|
|
|
|
mappedSharedColumnNames[i] = EscapeName(mappedSharedColumnNames[i])
|
2016-04-14 11:37:56 +00:00
|
|
|
}
|
2016-08-22 06:49:27 +00:00
|
|
|
setClause, err := BuildSetPreparedClause(mappedSharedColumnNames)
|
2016-04-14 11:37:56 +00:00
|
|
|
|
|
|
|
equalsComparison, err := BuildEqualsPreparedComparison(uniqueKeyColumns.Names)
|
|
|
|
result = fmt.Sprintf(`
|
2016-05-16 09:09:17 +00:00
|
|
|
update /* gh-ost %s.%s */
|
2016-04-14 11:37:56 +00:00
|
|
|
%s.%s
|
|
|
|
set
|
|
|
|
%s
|
|
|
|
where
|
|
|
|
%s
|
|
|
|
`, databaseName, tableName,
|
|
|
|
databaseName, tableName,
|
|
|
|
setClause,
|
|
|
|
equalsComparison,
|
|
|
|
)
|
|
|
|
return result, sharedArgs, uniqueKeyArgs, nil
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|