2016-04-01 11:36:56 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
|
|
|
See https://github.com/github/gh-osc/blob/master/LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-01 14:05:17 +00:00
|
|
|
values := make([]string, len(columns), len(columns))
|
|
|
|
for i := range columns {
|
|
|
|
values[i] = "?"
|
|
|
|
}
|
2016-04-05 17:50:49 +00:00
|
|
|
return BuildRangeComparison(columns, values, args, comparisonSign)
|
2016-04-01 14:05:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 08:34:44 +00:00
|
|
|
func BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName string, sharedColumns []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)
|
|
|
|
for i := range sharedColumns {
|
|
|
|
sharedColumns[i] = EscapeName(sharedColumns[i])
|
|
|
|
}
|
|
|
|
uniqueKey = EscapeName(uniqueKey)
|
|
|
|
|
2016-04-01 11:36:56 +00:00
|
|
|
sharedColumnsListing := strings.Join(sharedColumns, ", ")
|
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-04-01 11:36:56 +00:00
|
|
|
insert /* gh-osc %s.%s */ ignore into %s.%s (%s)
|
|
|
|
(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
|
|
|
)
|
|
|
|
`, databaseName, originalTableName, databaseName, ghostTableName, sharedColumnsListing,
|
|
|
|
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-04-08 08:34:44 +00:00
|
|
|
func BuildRangeInsertPreparedQuery(databaseName, originalTableName, ghostTableName string, sharedColumns []string, uniqueKey string, uniqueKeyColumns []string, rangeStartArgs, rangeEndArgs []interface{}, includeRangeStartValues bool, transactionalTable bool) (result string, explodedArgs []interface{}, err error) {
|
2016-04-01 11:55:51 +00:00
|
|
|
rangeStartValues := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns))
|
|
|
|
rangeEndValues := make([]string, len(uniqueKeyColumns), len(uniqueKeyColumns))
|
|
|
|
for i := range uniqueKeyColumns {
|
|
|
|
rangeStartValues[i] = "?"
|
|
|
|
rangeEndValues[i] = "?"
|
|
|
|
}
|
2016-04-08 08:34:44 +00:00
|
|
|
return BuildRangeInsertQuery(databaseName, originalTableName, ghostTableName, sharedColumns, 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-04-08 08:34:44 +00:00
|
|
|
func BuildUniqueKeyRangeEndPreparedQuery(databaseName, tableName string, uniqueKeyColumns []string, rangeStartArgs, rangeEndArgs []interface{}, chunkSize int64, 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-04-05 17:50:49 +00:00
|
|
|
rangeStartComparison, rangeExplodedArgs, err := BuildRangePreparedComparison(uniqueKeyColumns, rangeStartArgs, GreaterThanComparisonSign)
|
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-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(`
|
|
|
|
select /* gh-osc %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)
|
|
|
|
|
|
|
|
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(`
|
|
|
|
select /* gh-osc %s.%s */ %s
|
|
|
|
from
|
|
|
|
%s.%s
|
|
|
|
order by
|
|
|
|
%s
|
|
|
|
limit 1
|
|
|
|
`, databaseName, tableName, strings.Join(uniqueKeyColumns, ", "),
|
|
|
|
databaseName, tableName,
|
|
|
|
strings.Join(uniqueKeyColumnOrder, ", "),
|
|
|
|
)
|
|
|
|
return query, nil
|
|
|
|
}
|