2015-01-20 21:15:48 +08:00
|
|
|
package migrator
|
2015-01-19 17:44:16 +08:00
|
|
|
|
2018-05-10 22:54:21 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-05 16:00:40 +08:00
|
|
|
"github.com/mattn/go-sqlite3"
|
2020-04-01 21:57:21 +08:00
|
|
|
"xorm.io/xorm"
|
2018-05-10 22:54:21 +08:00
|
|
|
)
|
2015-02-24 18:46:34 +08:00
|
|
|
|
2015-01-19 17:44:16 +08:00
|
|
|
type Sqlite3 struct {
|
|
|
|
BaseDialect
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:54:21 +08:00
|
|
|
func NewSqlite3Dialect(engine *xorm.Engine) *Sqlite3 {
|
2015-01-19 17:44:16 +08:00
|
|
|
d := Sqlite3{}
|
|
|
|
d.BaseDialect.dialect = &d
|
2018-05-10 22:54:21 +08:00
|
|
|
d.BaseDialect.engine = engine
|
2015-01-19 17:44:16 +08:00
|
|
|
d.BaseDialect.driverName = SQLITE
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
2015-01-20 21:44:37 +08:00
|
|
|
func (db *Sqlite3) SupportEngine() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-01-19 17:44:16 +08:00
|
|
|
func (db *Sqlite3) Quote(name string) string {
|
|
|
|
return "`" + name + "`"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Sqlite3) AutoIncrStr() string {
|
|
|
|
return "AUTOINCREMENT"
|
|
|
|
}
|
|
|
|
|
2016-09-23 14:07:14 +08:00
|
|
|
func (db *Sqlite3) BooleanStr(value bool) string {
|
|
|
|
if value {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:41:41 +08:00
|
|
|
func (db *Sqlite3) DateTimeFunc(value string) string {
|
|
|
|
return "datetime(" + value + ")"
|
|
|
|
}
|
|
|
|
|
2015-01-19 17:44:16 +08:00
|
|
|
func (db *Sqlite3) SqlType(c *Column) string {
|
|
|
|
switch c.Type {
|
|
|
|
case DB_Date, DB_DateTime, DB_TimeStamp, DB_Time:
|
|
|
|
return DB_DateTime
|
|
|
|
case DB_TimeStampz:
|
|
|
|
return DB_Text
|
|
|
|
case DB_Char, DB_Varchar, DB_NVarchar, DB_TinyText, DB_Text, DB_MediumText, DB_LongText:
|
2015-01-20 21:15:48 +08:00
|
|
|
return DB_Text
|
2015-01-19 17:44:16 +08:00
|
|
|
case DB_Bit, DB_TinyInt, DB_SmallInt, DB_MediumInt, DB_Int, DB_Integer, DB_BigInt, DB_Bool:
|
|
|
|
return DB_Integer
|
|
|
|
case DB_Float, DB_Double, DB_Real:
|
|
|
|
return DB_Real
|
|
|
|
case DB_Decimal, DB_Numeric:
|
|
|
|
return DB_Numeric
|
|
|
|
case DB_TinyBlob, DB_Blob, DB_MediumBlob, DB_LongBlob, DB_Bytea, DB_Binary, DB_VarBinary:
|
|
|
|
return DB_Blob
|
|
|
|
case DB_Serial, DB_BigSerial:
|
|
|
|
c.IsPrimaryKey = true
|
|
|
|
c.IsAutoIncrement = true
|
|
|
|
c.Nullable = false
|
2015-01-20 21:15:48 +08:00
|
|
|
return DB_Integer
|
2015-01-19 17:44:16 +08:00
|
|
|
default:
|
|
|
|
return c.Type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 04:47:45 +08:00
|
|
|
func (db *Sqlite3) IndexCheckSql(tableName, indexName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{tableName, indexName}
|
|
|
|
sql := "SELECT 1 FROM " + db.Quote("sqlite_master") + " WHERE " + db.Quote("type") + "='index' AND " + db.Quote("tbl_name") + "=? AND " + db.Quote("name") + "=?"
|
|
|
|
return sql, args
|
2015-01-19 17:44:16 +08:00
|
|
|
}
|
2015-02-24 18:46:34 +08:00
|
|
|
|
|
|
|
func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
|
|
|
|
quote := db.Quote
|
|
|
|
//var unique string
|
|
|
|
idxName := index.XName(tableName)
|
|
|
|
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
|
|
|
}
|
2018-05-10 22:54:21 +08:00
|
|
|
|
|
|
|
func (db *Sqlite3) CleanDB() error {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-27 18:07:43 +08:00
|
|
|
|
2019-06-13 21:36:09 +08:00
|
|
|
func (db *Sqlite3) isThisError(err error, errcode int) bool {
|
2018-09-27 18:07:43 +08:00
|
|
|
if driverErr, ok := err.(sqlite3.Error); ok {
|
2019-06-13 21:36:09 +08:00
|
|
|
if int(driverErr.ExtendedCode) == errcode {
|
2018-09-27 18:07:43 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2019-06-13 21:36:09 +08:00
|
|
|
|
2020-04-20 21:48:38 +08:00
|
|
|
func (db *Sqlite3) ErrorMessage(err error) string {
|
|
|
|
if driverErr, ok := err.(sqlite3.Error); ok {
|
|
|
|
return driverErr.Error()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:36:09 +08:00
|
|
|
func (db *Sqlite3) IsUniqueConstraintViolation(err error) bool {
|
|
|
|
return db.isThisError(err, int(sqlite3.ErrConstraintUnique))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Sqlite3) IsDeadlock(err error) bool {
|
|
|
|
return false // No deadlock
|
|
|
|
}
|