github.com/astaxie/beego@v1.12.3/orm/db_sqlite.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package orm
    16  
    17  import (
    18  	"database/sql"
    19  	"fmt"
    20  	"reflect"
    21  	"time"
    22  )
    23  
    24  // sqlite operators.
    25  var sqliteOperators = map[string]string{
    26  	"exact":       "= ?",
    27  	"iexact":      "LIKE ? ESCAPE '\\'",
    28  	"contains":    "LIKE ? ESCAPE '\\'",
    29  	"icontains":   "LIKE ? ESCAPE '\\'",
    30  	"gt":          "> ?",
    31  	"gte":         ">= ?",
    32  	"lt":          "< ?",
    33  	"lte":         "<= ?",
    34  	"eq":          "= ?",
    35  	"ne":          "!= ?",
    36  	"startswith":  "LIKE ? ESCAPE '\\'",
    37  	"endswith":    "LIKE ? ESCAPE '\\'",
    38  	"istartswith": "LIKE ? ESCAPE '\\'",
    39  	"iendswith":   "LIKE ? ESCAPE '\\'",
    40  }
    41  
    42  // sqlite column types.
    43  var sqliteTypes = map[string]string{
    44  	"auto":            "integer NOT NULL PRIMARY KEY AUTOINCREMENT",
    45  	"pk":              "NOT NULL PRIMARY KEY",
    46  	"bool":            "bool",
    47  	"string":          "varchar(%d)",
    48  	"string-char":     "character(%d)",
    49  	"string-text":     "text",
    50  	"time.Time-date":  "date",
    51  	"time.Time":       "datetime",
    52  	"int8":            "tinyint",
    53  	"int16":           "smallint",
    54  	"int32":           "integer",
    55  	"int64":           "bigint",
    56  	"uint8":           "tinyint unsigned",
    57  	"uint16":          "smallint unsigned",
    58  	"uint32":          "integer unsigned",
    59  	"uint64":          "bigint unsigned",
    60  	"float64":         "real",
    61  	"float64-decimal": "decimal",
    62  }
    63  
    64  // sqlite dbBaser.
    65  type dbBaseSqlite struct {
    66  	dbBase
    67  }
    68  
    69  var _ dbBaser = new(dbBaseSqlite)
    70  
    71  // override base db read for update behavior as SQlite does not support syntax
    72  func (d *dbBaseSqlite) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string, isForUpdate bool) error {
    73  	if isForUpdate {
    74  		DebugLog.Println("[WARN] SQLite does not support SELECT FOR UPDATE query, isForUpdate param is ignored and always as false to do the work")
    75  	}
    76  	return d.dbBase.Read(q, mi, ind, tz, cols, false)
    77  }
    78  
    79  // get sqlite operator.
    80  func (d *dbBaseSqlite) OperatorSQL(operator string) string {
    81  	return sqliteOperators[operator]
    82  }
    83  
    84  // generate functioned sql for sqlite.
    85  // only support DATE(text).
    86  func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) {
    87  	if fi.fieldType == TypeDateField {
    88  		*leftCol = fmt.Sprintf("DATE(%s)", *leftCol)
    89  	}
    90  }
    91  
    92  // unable updating joined record in sqlite.
    93  func (d *dbBaseSqlite) SupportUpdateJoin() bool {
    94  	return false
    95  }
    96  
    97  // max int in sqlite.
    98  func (d *dbBaseSqlite) MaxLimit() uint64 {
    99  	return 9223372036854775807
   100  }
   101  
   102  // get column types in sqlite.
   103  func (d *dbBaseSqlite) DbTypes() map[string]string {
   104  	return sqliteTypes
   105  }
   106  
   107  // get show tables sql in sqlite.
   108  func (d *dbBaseSqlite) ShowTablesQuery() string {
   109  	return "SELECT name FROM sqlite_master WHERE type = 'table'"
   110  }
   111  
   112  // get columns in sqlite.
   113  func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) {
   114  	query := d.ins.ShowColumnsQuery(table)
   115  	rows, err := db.Query(query)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	columns := make(map[string][3]string)
   121  	for rows.Next() {
   122  		var tmp, name, typ, null sql.NullString
   123  		err := rows.Scan(&tmp, &name, &typ, &null, &tmp, &tmp)
   124  		if err != nil {
   125  			return nil, err
   126  		}
   127  		columns[name.String] = [3]string{name.String, typ.String, null.String}
   128  	}
   129  
   130  	return columns, nil
   131  }
   132  
   133  // get show columns sql in sqlite.
   134  func (d *dbBaseSqlite) ShowColumnsQuery(table string) string {
   135  	return fmt.Sprintf("pragma table_info('%s')", table)
   136  }
   137  
   138  // check index exist in sqlite.
   139  func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool {
   140  	query := fmt.Sprintf("PRAGMA index_list('%s')", table)
   141  	rows, err := db.Query(query)
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  	defer rows.Close()
   146  	for rows.Next() {
   147  		var tmp, index sql.NullString
   148  		rows.Scan(&tmp, &index, &tmp, &tmp, &tmp)
   149  		if name == index.String {
   150  			return true
   151  		}
   152  	}
   153  	return false
   154  }
   155  
   156  // create new sqlite dbBaser.
   157  func newdbBaseSqlite() dbBaser {
   158  	b := new(dbBaseSqlite)
   159  	b.ins = b
   160  	return b
   161  }