vitess.io/vitess@v0.16.2/go/vt/external/golib/sqlutils/sqlite_dialect.go (about)

     1  /*
     2     Copyright 2017 GitHub Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  /*
    18  	This file has been copied over from VTOrc package
    19  */
    20  
    21  // What's this about?
    22  // This is a brute-force regular-expression based conversion from MySQL syntax to sqlite3 syntax.
    23  // It is NOT meant to be a general purpose solution and is only expected & confirmed to run on
    24  // queries issued by orchestrator. There are known limitations to this design.
    25  // It's not even pretty.
    26  // In fact...
    27  // Well, it gets the job done at this time. Call it debt.
    28  
    29  package sqlutils
    30  
    31  import (
    32  	"regexp"
    33  )
    34  
    35  var sqlite3CreateTableConversions = []regexpMap{
    36  	rmap(`(?i) (character set|charset) [\S]+`, ``),
    37  	rmap(`(?i)int unsigned`, `int`),
    38  	rmap(`(?i)int[\s]*[(][\s]*([0-9]+)[\s]*[)] unsigned`, `int`),
    39  	rmap(`(?i)engine[\s]*=[\s]*(innodb|myisam|ndb|memory|tokudb)`, ``),
    40  	rmap(`(?i)DEFAULT CHARSET[\s]*=[\s]*[\S]+`, ``),
    41  	rmap(`(?i)[\S]*int( not null|) auto_increment`, `integer`),
    42  	rmap(`(?i)comment '[^']*'`, ``),
    43  	rmap(`(?i)after [\S]+`, ``),
    44  	rmap(`(?i)alter table ([\S]+) add (index|key) ([\S]+) (.+)`, `create index ${3}_${1} on $1 $4`),
    45  	rmap(`(?i)alter table ([\S]+) add unique (index|key) ([\S]+) (.+)`, `create unique index ${3}_${1} on $1 $4`),
    46  	rmap(`(?i)([\S]+) enum[\s]*([(].*?[)])`, `$1 text check($1 in $2)`),
    47  	rmap(`(?i)([\s\S]+[/][*] sqlite3-skip [*][/][\s\S]+)`, ``),
    48  	rmap(`(?i)timestamp default current_timestamp`, `timestamp default ('')`),
    49  	rmap(`(?i)timestamp not null default current_timestamp`, `timestamp not null default ('')`),
    50  
    51  	rmap(`(?i)add column (.*int) not null[\s]*$`, `add column $1 not null default 0`),
    52  	rmap(`(?i)add column (.* text) not null[\s]*$`, `add column $1 not null default ''`),
    53  	rmap(`(?i)add column (.* varchar.*) not null[\s]*$`, `add column $1 not null default ''`),
    54  }
    55  
    56  var sqlite3InsertConversions = []regexpMap{
    57  	rmap(`(?i)insert ignore ([\s\S]+) on duplicate key update [\s\S]+`, `insert or ignore $1`),
    58  	rmap(`(?i)insert ignore`, `insert or ignore`),
    59  	rmap(`(?i)now[(][)]`, `datetime('now')`),
    60  	rmap(`(?i)insert into ([\s\S]+) on duplicate key update [\s\S]+`, `replace into $1`),
    61  }
    62  
    63  var sqlite3GeneralConversions = []regexpMap{
    64  	rmap(`(?i)now[(][)][\s]*[-][\s]*interval [?] ([\w]+)`, `datetime('now', printf('-%d $1', ?))`),
    65  	rmap(`(?i)now[(][)][\s]*[+][\s]*interval [?] ([\w]+)`, `datetime('now', printf('+%d $1', ?))`),
    66  	rmap(`(?i)now[(][)][\s]*[-][\s]*interval ([0-9.]+) ([\w]+)`, `datetime('now', '-${1} $2')`),
    67  	rmap(`(?i)now[(][)][\s]*[+][\s]*interval ([0-9.]+) ([\w]+)`, `datetime('now', '+${1} $2')`),
    68  
    69  	rmap(`(?i)[=<>\s]([\S]+[.][\S]+)[\s]*[-][\s]*interval [?] ([\w]+)`, ` datetime($1, printf('-%d $2', ?))`),
    70  	rmap(`(?i)[=<>\s]([\S]+[.][\S]+)[\s]*[+][\s]*interval [?] ([\w]+)`, ` datetime($1, printf('+%d $2', ?))`),
    71  
    72  	rmap(`(?i)unix_timestamp[(][)]`, `strftime('%s', 'now')`),
    73  	rmap(`(?i)unix_timestamp[(]([^)]+)[)]`, `strftime('%s', $1)`),
    74  	rmap(`(?i)now[(][)]`, `datetime('now')`),
    75  	rmap(`(?i)cast[(][\s]*([\S]+) as signed[\s]*[)]`, `cast($1 as integer)`),
    76  
    77  	rmap(`(?i)\bconcat[(][\s]*([^,)]+)[\s]*,[\s]*([^,)]+)[\s]*[)]`, `($1 || $2)`),
    78  	rmap(`(?i)\bconcat[(][\s]*([^,)]+)[\s]*,[\s]*([^,)]+)[\s]*,[\s]*([^,)]+)[\s]*[)]`, `($1 || $2 || $3)`),
    79  
    80  	rmap(`(?i) rlike `, ` like `),
    81  
    82  	rmap(`(?i)create index([\s\S]+)[(][\s]*[0-9]+[\s]*[)]([\s\S]+)`, `create index ${1}${2}`),
    83  	rmap(`(?i)drop index ([\S]+) on ([\S]+)`, `drop index if exists $1`),
    84  }
    85  
    86  var (
    87  	sqlite3IdentifyCreateTableStatement = regexp.MustCompile(regexpSpaces(`(?i)^[\s]*create table`))
    88  	sqlite3IdentifyCreateIndexStatement = regexp.MustCompile(regexpSpaces(`(?i)^[\s]*create( unique|) index`))
    89  	sqlite3IdentifyDropIndexStatement   = regexp.MustCompile(regexpSpaces(`(?i)^[\s]*drop index`))
    90  	sqlite3IdentifyAlterTableStatement  = regexp.MustCompile(regexpSpaces(`(?i)^[\s]*alter table`))
    91  	sqlite3IdentifyInsertStatement      = regexp.MustCompile(regexpSpaces(`(?i)^[\s]*(insert|replace)`))
    92  )
    93  
    94  func IsInsert(statement string) bool {
    95  	return sqlite3IdentifyInsertStatement.MatchString(statement)
    96  }
    97  
    98  func IsCreateTable(statement string) bool {
    99  	return sqlite3IdentifyCreateTableStatement.MatchString(statement)
   100  }
   101  
   102  func IsCreateIndex(statement string) bool {
   103  	return sqlite3IdentifyCreateIndexStatement.MatchString(statement)
   104  }
   105  
   106  func IsDropIndex(statement string) bool {
   107  	return sqlite3IdentifyDropIndexStatement.MatchString(statement)
   108  }
   109  
   110  func IsAlterTable(statement string) bool {
   111  	return sqlite3IdentifyAlterTableStatement.MatchString(statement)
   112  }
   113  
   114  func ToSqlite3CreateTable(statement string) string {
   115  	return applyConversions(statement, sqlite3CreateTableConversions)
   116  }
   117  
   118  func ToSqlite3Insert(statement string) string {
   119  	return applyConversions(statement, sqlite3InsertConversions)
   120  }
   121  
   122  func ToSqlite3Dialect(statement string) (translated string) {
   123  	if IsCreateTable(statement) {
   124  		return ToSqlite3CreateTable(statement)
   125  	}
   126  	if IsAlterTable(statement) {
   127  		return ToSqlite3CreateTable(statement)
   128  	}
   129  	statement = applyConversions(statement, sqlite3GeneralConversions)
   130  	if IsInsert(statement) {
   131  		return ToSqlite3Insert(statement)
   132  	}
   133  	return statement
   134  }