1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 12:21:27 +00:00
Beego/orm/db_sqlite.go

152 lines
3.9 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-07-30 12:32:38 +00:00
package orm
import (
2013-08-27 04:33:27 +00:00
"database/sql"
"fmt"
)
2014-01-17 09:25:17 +00:00
// sqlite operators.
var sqliteOperators = map[string]string{
"exact": "= ?",
"iexact": "LIKE ? ESCAPE '\\'",
"contains": "LIKE ? ESCAPE '\\'",
"icontains": "LIKE ? ESCAPE '\\'",
"gt": "> ?",
"gte": ">= ?",
"lt": "< ?",
"lte": "<= ?",
2015-06-09 02:18:21 +00:00
"eq": "= ?",
"ne": "!= ?",
"startswith": "LIKE ? ESCAPE '\\'",
"endswith": "LIKE ? ESCAPE '\\'",
"istartswith": "LIKE ? ESCAPE '\\'",
"iendswith": "LIKE ? ESCAPE '\\'",
}
2014-01-17 09:25:17 +00:00
// sqlite column types.
2013-08-19 14:37:39 +00:00
var sqliteTypes = map[string]string{
"auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT",
2013-08-19 14:37:39 +00:00
"pk": "NOT NULL PRIMARY KEY",
"bool": "bool",
"string": "varchar(%d)",
2017-11-30 10:12:49 +00:00
"string-char": "character(%d)",
2013-08-19 14:37:39 +00:00
"string-text": "text",
"time.Time-date": "date",
"time.Time": "datetime",
"int8": "tinyint",
"int16": "smallint",
"int32": "integer",
"int64": "bigint",
"uint8": "tinyint unsigned",
"uint16": "smallint unsigned",
"uint32": "integer unsigned",
"uint64": "bigint unsigned",
"float64": "real",
"float64-decimal": "decimal",
}
2014-01-17 09:25:17 +00:00
// sqlite dbBaser.
2013-07-30 12:32:38 +00:00
type dbBaseSqlite struct {
dbBase
}
var _ dbBaser = new(dbBaseSqlite)
2014-01-17 09:25:17 +00:00
// get sqlite operator.
2015-09-12 13:46:43 +00:00
func (d *dbBaseSqlite) OperatorSQL(operator string) string {
return sqliteOperators[operator]
}
2014-01-17 09:25:17 +00:00
// generate functioned sql for sqlite.
// only support DATE(text).
func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) {
if fi.fieldType == TypeDateField {
*leftCol = fmt.Sprintf("DATE(%s)", *leftCol)
}
}
2014-01-17 09:25:17 +00:00
// unable updating joined record in sqlite.
func (d *dbBaseSqlite) SupportUpdateJoin() bool {
return false
}
2014-01-17 09:25:17 +00:00
// max int in sqlite.
func (d *dbBaseSqlite) MaxLimit() uint64 {
return 9223372036854775807
}
2014-01-17 09:25:17 +00:00
// get column types in sqlite.
2013-08-19 14:37:39 +00:00
func (d *dbBaseSqlite) DbTypes() map[string]string {
return sqliteTypes
}
2014-01-17 09:25:17 +00:00
// get show tables sql in sqlite.
2013-08-27 04:33:27 +00:00
func (d *dbBaseSqlite) ShowTablesQuery() string {
return "SELECT name FROM sqlite_master WHERE type = 'table'"
}
2014-01-17 09:25:17 +00:00
// get columns in sqlite.
2013-08-27 04:33:27 +00:00
func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) {
query := d.ins.ShowColumnsQuery(table)
rows, err := db.Query(query)
if err != nil {
return nil, err
}
columns := make(map[string][3]string)
for rows.Next() {
var tmp, name, typ, null sql.NullString
err := rows.Scan(&tmp, &name, &typ, &null, &tmp, &tmp)
if err != nil {
return nil, err
}
columns[name.String] = [3]string{name.String, typ.String, null.String}
}
return columns, nil
}
2014-01-17 09:25:17 +00:00
// get show columns sql in sqlite.
2013-08-27 04:33:27 +00:00
func (d *dbBaseSqlite) ShowColumnsQuery(table string) string {
return fmt.Sprintf("pragma table_info('%s')", table)
}
2014-01-17 09:25:17 +00:00
// check index exist in sqlite.
2013-08-27 04:33:27 +00:00
func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool {
query := fmt.Sprintf("PRAGMA index_list('%s')", table)
rows, err := db.Query(query)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var tmp, index sql.NullString
rows.Scan(&tmp, &index, &tmp, &tmp, &tmp)
2013-08-27 04:33:27 +00:00
if name == index.String {
return true
}
}
return false
}
2014-01-17 09:25:17 +00:00
// create new sqlite dbBaser.
2013-07-30 12:32:38 +00:00
func newdbBaseSqlite() dbBaser {
b := new(dbBaseSqlite)
b.ins = b
return b
}