github.com/team-ide/go-dialect@v1.9.20/dialect/back/dialect_sqlite.go (about) 1 package back 2 3 import ( 4 "errors" 5 "strings" 6 ) 7 8 func NewSqliteDialect() *SqliteDialect { 9 10 res := &SqliteDialect{ 11 DefaultDialect: NewDefaultDialect(SqliteType), 12 } 13 res.init() 14 return res 15 } 16 17 type SqliteDialect struct { 18 *DefaultDialect 19 } 20 21 func (this_ *SqliteDialect) init() { 22 /** 数值类型 **/ 23 /** 24 MySQL 支持所有标准 SQL 数值数据类型。 25 这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL 和 NUMERIC),以及近似数值数据类型(FLOAT、REAL 和 DOUBLE PRECISION)。 26 关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词。 27 BIT数据类型保存位字段值,并且支持 MyISAM、MEMORY、InnoDB 和 BDB表。 28 作为 SQL 标准的扩展,MySQL 也支持整数类型 TINYINT、MEDIUMINT 和 BIGINT。下面的表显示了需要的每个整数类型的存储和范围。 29 30 如果不设置长度,会有默认的长度 31 长度代表了显示的最大宽度,如果不够会用0在左边填充,但必须搭配zerofill 使用! 32 例如: 33 INT(7) 括号中7不是指范围,范围是由数据类型决定的,只是代表显示结果的宽度 34 */ 35 36 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BIT", TypeFormat: "BIT($l)", HasLength: false, IsNumber: true}) 37 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYINT", TypeFormat: "TINYINT($l)", HasLength: true, IsNumber: true}) 38 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SMALLINT", TypeFormat: "SMALLINT($l)", HasLength: true, IsNumber: true}) 39 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMINT", TypeFormat: "MEDIUMINT($l)", HasLength: true, IsNumber: true}) 40 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT", TypeFormat: "INT($l)", HasLength: true, IsNumber: true}) 41 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INTEGER", TypeFormat: "INTEGER($l)", HasLength: true, IsNumber: true}) 42 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BIGINT", TypeFormat: "BIGINT($l)", HasLength: true, IsNumber: true}) 43 44 /** 小数 **/ 45 46 /** 47 M:整数部位+小数部位 48 D:小数部位 49 如果超过范围,则插入临界值 50 M和D都可以省略 51 如果是DECIMAL,则M默认为10,D默认为0 52 如果是FLOAT和DOUBLE,则会根据插入的数值的精度来决定精度 53 定点型的精确度较高,如果要求插入数值的精度较高如货币运算等则考虑使用 54 原则:所选择的类型越简单越好,能保存数值的类型越小越好 55 */ 56 57 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "FLOAT", TypeFormat: "FLOAT($l, $d)", HasLength: true, HasDecimal: true, IsNumber: true}) 58 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DOUBLE", TypeFormat: "DOUBLE($l, $d)", HasLength: true, HasDecimal: true, IsNumber: true}) 59 60 /** 61 DECIMAL。浮点数类型和定点数类型都可以用(M,N)来表示。其中,M称为精度,表示总共的位数;N称为标度,表示小数的位数.DECIMAL若不指定精度则默认为(10,0) 62 不论是定点数还是浮点数类型,如果用户指定的精度超出精度范围,则会四舍五入 63 */ 64 65 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DEC", TypeFormat: "DEC($l, $d)", HasLength: true, IsNumber: true}) 66 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DECIMAL", TypeFormat: "DOUBLE($l, $d)", HasLength: true, IsNumber: true}) 67 68 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NUMBER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true}) 69 70 /** 日期/时间类型 **/ 71 /** 72 表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。 73 每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值。 74 TIMESTAMP类型有专有的自动更新特性,将在后面描述。 75 DATE: 76 (1)以‘YYYY-MM-DD’或者‘YYYYMMDD’字符串格式表示的日期,取值范围为‘1000-01-01’~‘9999-12-3’。例如,输入‘2012-12-31’或者‘20121231’,插入数据库的日期都为2012-12-31。 77 (2)以‘YY-MM-DD’或者‘YYMMDD’字符串格式表示的日期,在这里YY表示两位的年值。包含两位年值的日期会令人模糊,因为不知道世纪。MySQL使用以下规则解释两位年值:‘00~69’范围的年值转换为‘2000~2069’;‘70~99’范围的年值转换为‘1970~1999’。例如,输入‘12-12-31’,插入数据库的日期为2012-12-31;输入‘981231’,插入数据的日期为1998-12-31。 78 (3)以YY-MM-DD或者YYMMDD数字格式表示的日期,与前面相似,00~69范围的年值转换为2000~2069,70~99范围的年值转换为1970~1999。例如,输入12-12-31插入数据库的日期为2012-12-31;输入981231,插入数据的日期为1998-12-31 79 */ 80 81 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "YEAR", TypeFormat: "YEAR", IsDateTime: true}) 82 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIME", TypeFormat: "TIME", IsDateTime: true}) 83 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DATE", TypeFormat: "DATE", IsDateTime: true}) 84 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DATETIME", TypeFormat: "DATETIME", IsDateTime: true}) 85 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIMESTAMP", TypeFormat: "TIMESTAMP", IsDateTime: true}) 86 87 /** 字符串类型 **/ 88 /** 89 字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。该节描述了这些类型如何工作以及如何在查询中使用这些类型 90 91 注意:char(n) 和 varchar(n) 中括号中 n 代表字符的个数,并不代表字节个数,比如 CHAR(30) 就可以存储 30 个字符。 92 CHAR 和 VARCHAR 类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。 93 BINARY 和 VARBINARY 类似于 CHAR 和 VARCHAR,不同的是它们包含二进制字符串而不要非二进制字符串。也就是说,它们包含字节字符串而不是字符字符串。这说明它们没有字符集,并且排序和比较基于列值字节的数值值。 94 BLOB 是一个二进制大对象,可以容纳可变数量的数据。有 4 种 BLOB 类型:TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。它们区别在于可容纳存储范围不同。 95 有 4 种 TEXT 类型:TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。对应的这 4 种 BLOB 类型,可存储的最大长度不同,可根据实际情况选择。 96 */ 97 98 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "CHAR", TypeFormat: "CHAR($l)", HasLength: true, IsString: true}) 99 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "VARCHAR", TypeFormat: "VARCHAR($l)", HasLength: true, IsString: true}) 100 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYTEXT", TypeFormat: "TINYTEXT($l)", HasLength: true, IsString: true}) 101 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TEXT", TypeFormat: "TEXT($l)", HasLength: true, IsString: true}) 102 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMTEXT", TypeFormat: "MEDIUMTEXT($l)", HasLength: true, IsString: true}) 103 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "LONGTEXT", TypeFormat: "LONGTEXT", HasLength: false, IsString: true}) 104 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "ENUM", TypeFormat: "ENUM($l)", HasLength: true, IsString: true}) 105 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYBLOB", TypeFormat: "TINYBLOB($l)", HasLength: true, IsString: true}) 106 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BLOB", TypeFormat: "BLOB($l)", HasLength: true, IsString: true}) 107 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMBLOB", TypeFormat: "MEDIUMBLOB($l)", HasLength: true, IsString: true}) 108 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "LONGBLOB", TypeFormat: "LONGBLOB", HasLength: false, IsString: true}) 109 110 this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SET", TypeFormat: "VARCHAR(100)", HasLength: true, IsString: true}) 111 112 this_.AddFuncTypeInfo(&FuncTypeInfo{Name: "md5", Format: "md5"}) 113 } 114 115 func (this_ *SqliteDialect) OwnerModel(data map[string]interface{}) (owner *OwnerModel, err error) { 116 if data == nil { 117 return 118 } 119 owner = &OwnerModel{} 120 if data["name"] != nil { 121 owner.Name = data["name"].(string) 122 } 123 return 124 } 125 func (this_ *SqliteDialect) OwnersSelectSql() (sql string, err error) { 126 sql = `SELECT name FROM pragma_database_list AS t_i ORDER BY name ` 127 return 128 } 129 func (this_ *SqliteDialect) OwnerSelectSql(ownerName string) (sql string, err error) { 130 sql = `SELECT name FROM pragma_database_list AS t_i ORDER BY name ` 131 return 132 } 133 func (this_ *SqliteDialect) OwnerCreateSql(owner *OwnerModel) (sqlList []string, err error) { 134 135 return 136 } 137 func (this_ *SqliteDialect) OwnerDeleteSql(ownerName string) (sqlList []string, err error) { 138 139 return 140 } 141 func (this_ *SqliteDialect) TableModel(data map[string]interface{}) (table *TableModel, err error) { 142 if data == nil { 143 return 144 } 145 table = &TableModel{} 146 if data["name"] != nil { 147 table.Name = data["name"].(string) 148 } 149 if data["sql"] != nil { 150 table.Sql = data["sql"].(string) 151 } 152 return 153 } 154 func (this_ *SqliteDialect) TablesSelectSql(ownerName string) (sql string, err error) { 155 sql = `SELECT name,sql FROM sqlite_master WHERE type ='table' ` 156 sql += `ORDER BY name` 157 return 158 } 159 func (this_ *SqliteDialect) TableSelectSql(ownerName string, tableName string) (sql string, err error) { 160 sql = `SELECT name,sql FROM sqlite_master WHERE type ='table' ` 161 sql += `AND name='` + tableName + `' ` 162 sql += `ORDER BY name` 163 return 164 } 165 func (this_ *SqliteDialect) TableCreateSql(ownerName string, table *TableModel) (sqlList []string, err error) { 166 167 createTableSql := `CREATE TABLE ` 168 169 if ownerName != "" { 170 createTableSql += this_.PackOwner(ownerName) + "." 171 } 172 createTableSql += this_.PackTable(table.Name) 173 174 createTableSql += `(` 175 createTableSql += "\n" 176 primaryKeys := "" 177 if len(table.ColumnList) > 0 { 178 for _, column := range table.ColumnList { 179 var columnSql = this_.PackColumn(column.Name) 180 181 var columnType string 182 columnType, err = this_.FormatColumnType(column) 183 if err != nil { 184 return 185 } 186 columnSql += " " + columnType 187 188 if column.Default != "" { 189 columnSql += ` ` + this_.FormatDefaultValue(column) 190 } 191 if column.NotNull { 192 columnSql += ` NOT NULL` 193 } 194 195 if column.PrimaryKey { 196 primaryKeys += "" + column.Name + "," 197 } 198 createTableSql += "\t" + columnSql + ",\n" 199 } 200 } 201 if primaryKeys != "" { 202 primaryKeys = strings.TrimSuffix(primaryKeys, ",") 203 createTableSql += "\tPRIMARY KEY (" + this_.PackColumns(strings.Split(primaryKeys, ",")) + ")" 204 } 205 206 createTableSql = strings.TrimSuffix(createTableSql, ",\n") 207 createTableSql += "\n" 208 209 createTableSql += `)` 210 211 sqlList = append(sqlList, createTableSql) 212 213 if len(table.IndexList) > 0 { 214 for _, one := range table.IndexList { 215 var sqlList_ []string 216 sqlList_, err = this_.IndexAddSql(ownerName, table.Name, one) 217 if err != nil { 218 return 219 } 220 sqlList = append(sqlList, sqlList_...) 221 } 222 } 223 return 224 } 225 func (this_ *SqliteDialect) TableCommentSql(ownerName string, tableName string, comment string) (sqlList []string, err error) { 226 227 return 228 } 229 func (this_ *SqliteDialect) TableDeleteSql(ownerName string, tableName string) (sqlList []string, err error) { 230 var sql string 231 sql = `DROP TABLE ` 232 233 if ownerName != "" { 234 sql += this_.PackOwner(ownerName) + "." 235 } 236 sql += this_.PackTable(tableName) 237 sqlList = append(sqlList, sql) 238 return 239 } 240 func (this_ *SqliteDialect) ColumnModel(data map[string]interface{}) (column *ColumnModel, err error) { 241 if data == nil { 242 return 243 } 244 column = &ColumnModel{} 245 if data["name"] != nil { 246 column.Name = data["name"].(string) 247 } 248 if data["dflt_value"] != nil { 249 column.Default = GetStringValue(data["dflt_value"]) 250 } 251 if GetStringValue(data["dflt_value"]) == "1" { 252 column.PrimaryKey = true 253 } 254 if GetStringValue(data["notnull"]) == "1" { 255 column.NotNull = true 256 } 257 258 var columnTypeInfo *ColumnTypeInfo 259 if data["type"] != nil { 260 columnType := data["type"].(string) 261 columnTypeInfo, column.Length, column.Decimal, err = this_.ToColumnTypeInfo(columnType) 262 if err != nil { 263 return 264 } 265 column.Type = columnTypeInfo.Name 266 } 267 return 268 } 269 func (this_ *SqliteDialect) ColumnsSelectSql(ownerName string, tableName string) (sql string, err error) { 270 sql = `SELECT name,dflt_value,"notnull",type FROM pragma_table_info("` + tableName + `") AS t_i ` 271 return 272 } 273 func (this_ *SqliteDialect) ColumnAddSql(ownerName string, tableName string, column *ColumnModel) (sqlList []string, err error) { 274 var columnType string 275 columnType, err = this_.FormatColumnType(column) 276 if err != nil { 277 return 278 } 279 280 var sql string 281 sql = `ALTER TABLE ` 282 283 if ownerName != "" { 284 sql += this_.PackOwner(ownerName) + "." 285 } 286 sql += this_.PackTable(tableName) 287 288 sql += ` ADD COLUMN ` 289 sql += this_.PackColumn(column.Name) 290 sql += ` ` + columnType + `` 291 if column.Default != "" { 292 sql += ` ` + this_.FormatDefaultValue(column) 293 } 294 if column.NotNull { 295 sql += ` NOT NULL` 296 } 297 sql += `` 298 299 sqlList = append(sqlList, sql) 300 301 return 302 } 303 func (this_ *SqliteDialect) ColumnCommentSql(ownerName string, tableName string, columnName string, comment string) (sqlList []string, err error) { 304 305 return 306 } 307 func (this_ *SqliteDialect) ColumnRenameSql(ownerName string, tableName string, oldName string, newName string) (sqlList []string, err error) { 308 var sql string 309 sql = `ALTER TABLE ` 310 311 if ownerName != "" { 312 sql += this_.PackOwner(ownerName) + "." 313 } 314 sql += this_.PackTable(tableName) 315 316 sql += ` RENAME COLUMN ` 317 sql += this_.PackColumn(oldName) 318 sql += ` TO ` 319 sql += this_.PackColumn(newName) 320 321 sqlList = append(sqlList, sql) 322 return 323 } 324 func (this_ *SqliteDialect) ColumnUpdateSql(ownerName string, tableName string, oldColumn *ColumnModel, newColumn *ColumnModel) (sqlList []string, err error) { 325 326 return 327 } 328 func (this_ *SqliteDialect) ColumnDeleteSql(ownerName string, tableName string, columnName string) (sqlList []string, err error) { 329 var sql string 330 sql = `ALTER TABLE ` 331 332 //if ownerName != "" { 333 // sql += this_.PackOwner(ownerName) + "." 334 //} 335 sql += this_.PackTable(tableName) 336 337 sql += ` DROP COLUMN ` 338 sql += this_.PackColumn(columnName) 339 340 sqlList = append(sqlList, sql) 341 return 342 } 343 344 func (this_ *SqliteDialect) PrimaryKeyModel(data map[string]interface{}) (primaryKey *PrimaryKeyModel, err error) { 345 if data == nil { 346 return 347 } 348 primaryKey = &PrimaryKeyModel{} 349 if data["columnName"] != nil { 350 primaryKey.ColumnName = data["columnName"].(string) 351 } 352 return 353 } 354 func (this_ *SqliteDialect) PrimaryKeysSelectSql(ownerName string, tableName string) (sql string, err error) { 355 sql = `select a.name,b.name columnName from pragma_index_list('` + tableName + `') as a,pragma_index_info(a.name) b WHERE a.origin="pk" ` 356 return 357 } 358 359 func (this_ *SqliteDialect) IndexModel(data map[string]interface{}) (index *IndexModel, err error) { 360 if data == nil { 361 return 362 } 363 index = &IndexModel{} 364 if data["name"] != nil { 365 index.Name = data["name"].(string) 366 } 367 if data["columnName"] != nil { 368 index.ColumnName = data["columnName"].(string) 369 } 370 if GetStringValue(data["unique"]) == "1" { 371 index.Type = "unique" 372 } 373 return 374 } 375 func (this_ *SqliteDialect) IndexesSelectSql(ownerName string, tableName string) (sql string, err error) { 376 sql = `select a.name,a."unique",b.name columnName from pragma_index_list('` + tableName + `') as a,pragma_index_info(a.name) b WHERE a.origin!="pk" ` 377 378 return 379 } 380 func (this_ *SqliteDialect) IndexAddSql(ownerName string, tableName string, index *IndexModel) (sqlList []string, err error) { 381 sql := "CREATE " 382 switch strings.ToUpper(index.Type) { 383 case "UNIQUE": 384 sql += "UNIQUE INDEX" 385 case "": 386 sql += "INDEX" 387 default: 388 err = errors.New("dialect [" + this_.DialectType().Name + "] not support index type [" + index.Type + "]") 389 return 390 } 391 392 sql += " " + this_.PackColumn(index.Name) + "" 393 394 sql += " ON " 395 //if ownerName != "" { 396 // sql += this_.PackOwner(ownerName) + "." 397 //} 398 sql += "" + this_.PackTable(tableName) 399 400 sql += "(" + this_.PackColumns(index.Columns) + ")" 401 402 sqlList = append(sqlList, sql) 403 return 404 }