github.com/team-ide/go-dialect@v1.9.20/dialect/back/dialect_default.go (about) 1 package back 2 3 import ( 4 "errors" 5 "strconv" 6 "strings" 7 "sync" 8 ) 9 10 func NewDefaultDialect(dialectType *Type) *DefaultDialect { 11 12 return &DefaultDialect{ 13 columnTypeInfoCache: make(map[string]*ColumnTypeInfo), 14 funcTypeInfoCache: make(map[string]*FuncTypeInfo), 15 dialectType: dialectType, 16 } 17 } 18 19 type DefaultDialect struct { 20 columnTypeInfoList []*ColumnTypeInfo 21 columnTypeInfoCache map[string]*ColumnTypeInfo 22 columnTypeInfoCacheLock sync.Mutex 23 24 funcTypeInfoList []*FuncTypeInfo 25 funcTypeInfoCache map[string]*FuncTypeInfo 26 funcTypeInfoCacheLock sync.Mutex 27 28 dialectType *Type 29 } 30 31 func (this_ *DefaultDialect) DialectType() (dialectType *Type) { 32 dialectType = this_.dialectType 33 return 34 } 35 36 func (this_ *DefaultDialect) GetColumnTypeInfos() (columnTypeInfoList []*ColumnTypeInfo) { 37 columnTypeInfoList = this_.columnTypeInfoList 38 return 39 } 40 41 func (this_ *DefaultDialect) AddColumnTypeInfo(columnTypeInfo *ColumnTypeInfo) { 42 this_.columnTypeInfoCacheLock.Lock() 43 defer this_.columnTypeInfoCacheLock.Unlock() 44 45 key := strings.ToLower(columnTypeInfo.Name) 46 find := this_.columnTypeInfoCache[key] 47 this_.columnTypeInfoCache[key] = columnTypeInfo 48 if find == nil { 49 this_.columnTypeInfoList = append(this_.columnTypeInfoList, columnTypeInfo) 50 } else { 51 var list = this_.columnTypeInfoList 52 var newList []*ColumnTypeInfo 53 for _, one := range list { 54 if one == find { 55 newList = append(newList, columnTypeInfo) 56 } else { 57 newList = append(newList, one) 58 } 59 } 60 this_.columnTypeInfoList = newList 61 } 62 63 return 64 } 65 func (this_ *DefaultDialect) GetColumnTypeInfo(typeName string) (columnTypeInfo *ColumnTypeInfo, err error) { 66 this_.columnTypeInfoCacheLock.Lock() 67 defer this_.columnTypeInfoCacheLock.Unlock() 68 69 key := strings.ToLower(typeName) 70 columnTypeInfo = this_.columnTypeInfoCache[key] 71 if columnTypeInfo == nil { 72 err = errors.New("dialect [" + this_.DialectType().Name + "] not support type [" + typeName + "]") 73 return 74 } 75 return 76 } 77 func (this_ *DefaultDialect) FormatColumnType(column *ColumnModel) (columnType string, err error) { 78 columnTypeInfo, err := this_.GetColumnTypeInfo(column.Type) 79 if err != nil { 80 return 81 } 82 columnType = columnTypeInfo.FormatColumnType(column.Length, column.Decimal) 83 return 84 } 85 func (this_ *DefaultDialect) FormatDefaultValue(column *ColumnModel) (defaultValue string) { 86 defaultValue = "DEFAULT " 87 if column.DefaultCurrentTimestamp || column.OnUpdateCurrentTimestamp { 88 if column.DefaultCurrentTimestamp { 89 defaultValue += " TO_TIMESTAMP('20190101 00:00:00.000000','yyyyMMdd HH24:mi:ss.ff6') " 90 } 91 return 92 } 93 defaultValue += this_.PackValueForSql(nil, column.Default) 94 return 95 } 96 func (this_ *DefaultDialect) ToColumnTypeInfo(columnType string) (columnTypeInfo *ColumnTypeInfo, length, decimal int, err error) { 97 typeName := columnType 98 if strings.Contains(columnType, "(") { 99 typeName = columnType[0:strings.Index(columnType, "(")] 100 lengthStr := columnType[strings.Index(columnType, "(")+1 : strings.Index(columnType, ")")] 101 if strings.Contains(lengthStr, ",") { 102 length, _ = strconv.Atoi(lengthStr[0:strings.Index(lengthStr, ",")]) 103 decimal, _ = strconv.Atoi(lengthStr[strings.Index(lengthStr, ",")+1:]) 104 } else { 105 length, _ = strconv.Atoi(lengthStr) 106 } 107 } 108 columnTypeInfo, err = this_.GetColumnTypeInfo(typeName) 109 if err != nil { 110 return 111 } 112 return 113 } 114 115 func (this_ *DefaultDialect) GeFuncTypeInfos() (funcTypeInfoList []*FuncTypeInfo) { 116 funcTypeInfoList = this_.funcTypeInfoList 117 return 118 } 119 120 func (this_ *DefaultDialect) AddFuncTypeInfo(funcTypeInfo *FuncTypeInfo) { 121 this_.funcTypeInfoCacheLock.Lock() 122 defer this_.funcTypeInfoCacheLock.Unlock() 123 124 key := strings.ToLower(funcTypeInfo.Name) 125 find := this_.funcTypeInfoCache[key] 126 this_.funcTypeInfoCache[key] = funcTypeInfo 127 if find == nil { 128 this_.funcTypeInfoList = append(this_.funcTypeInfoList, funcTypeInfo) 129 } else { 130 var list = this_.funcTypeInfoList 131 var newList []*FuncTypeInfo 132 for _, one := range list { 133 if one == find { 134 newList = append(newList, funcTypeInfo) 135 } else { 136 newList = append(newList, one) 137 } 138 } 139 this_.funcTypeInfoList = newList 140 } 141 142 return 143 } 144 func (this_ *DefaultDialect) GetFuncTypeInfo(funcName string) (funcTypeInfo *FuncTypeInfo, err error) { 145 this_.funcTypeInfoCacheLock.Lock() 146 defer this_.funcTypeInfoCacheLock.Unlock() 147 148 key := strings.ToLower(funcName) 149 funcTypeInfo = this_.funcTypeInfoCache[key] 150 if funcTypeInfo == nil { 151 err = errors.New("dialect [" + this_.DialectType().Name + "] not support func [" + funcName + "]") 152 return 153 } 154 return 155 } 156 func (this_ *DefaultDialect) FormatFunc(funcStr string) (res string, err error) { 157 funcName := funcStr[:strings.Index(funcStr, "(")] 158 159 funcTypeInfo, err := this_.GetFuncTypeInfo(funcName) 160 if err != nil { 161 return 162 } 163 res = funcTypeInfo.Format + funcStr[strings.Index(funcStr, "("):] 164 return 165 } 166 167 func (this_ *DefaultDialect) PackOwner(ownerName string) string { 168 return packingName(`"`, ownerName) 169 } 170 171 func (this_ *DefaultDialect) PackTable(tableName string) string { 172 return packingName(`"`, tableName) 173 } 174 175 func (this_ *DefaultDialect) PackColumn(columnName string) string { 176 return packingName(`"`, columnName) 177 } 178 179 func (this_ *DefaultDialect) PackColumns(columnNames []string) string { 180 return packingNames(`"`, columnNames) 181 } 182 183 func (this_ *DefaultDialect) PackValueForSql(column *ColumnModel, value interface{}) string { 184 var columnTypeInfo *ColumnTypeInfo 185 if column != nil { 186 columnTypeInfo, _ = this_.GetColumnTypeInfo(column.Type) 187 } 188 return packingValue(columnTypeInfo, `'`, `'`, value) 189 } 190 191 func (this_ *DefaultDialect) IsSqlEnd(sqlStr string) (isSqlEnd bool) { 192 if !strings.HasSuffix(strings.TrimSpace(sqlStr), ";") { 193 return 194 } 195 cacheKey := UUID() 196 sqlCache := sqlStr 197 sqlCache = strings.ReplaceAll(sqlCache, `''`, `|-`+cacheKey+`-|`) 198 sqlCache = strings.ReplaceAll(sqlCache, `""`, `|--`+cacheKey+`--|`) 199 200 var inStringLevel int 201 var inStringPack byte 202 var thisChar byte 203 var lastChar byte 204 205 var stringPackChars = []byte{'"', '\''} 206 for i := 0; i < len(sqlCache); i++ { 207 thisChar = sqlCache[i] 208 if i > 0 { 209 lastChar = sqlCache[i-1] 210 } 211 212 // inStringLevel == 0 表示 不在 字符串 包装 中 213 if thisChar == ';' && inStringLevel == 0 { 214 } else { 215 packCharIndex := BytesIndex(stringPackChars, thisChar) 216 if packCharIndex >= 0 { 217 // inStringLevel == 0 表示 不在 字符串 包装 中 218 if inStringLevel == 0 { 219 inStringPack = stringPackChars[packCharIndex] 220 // 字符串包装层级 +1 221 inStringLevel++ 222 } else { 223 if thisChar != inStringPack { 224 } else if lastChar == '\\' { // 如果有转义符号 类似 “\'”,“\"” 225 } else if lastChar == inStringPack { 226 // 如果 前一个字符 与字符串包装字符一致 227 } else { 228 // 字符串包装层级 -1 229 inStringLevel-- 230 } 231 } 232 } 233 } 234 235 } 236 isSqlEnd = inStringLevel == 0 237 return 238 } 239 func (this_ *DefaultDialect) SqlSplit(sqlStr string) (sqlList []string) { 240 cacheKey := UUID() 241 sqlCache := sqlStr 242 sqlCache = strings.ReplaceAll(sqlCache, `''`, `|-`+cacheKey+`-|`) 243 sqlCache = strings.ReplaceAll(sqlCache, `""`, `|--`+cacheKey+`--|`) 244 245 var list []string 246 var beg int 247 248 var inStringLevel int 249 var inStringPack byte 250 var thisChar byte 251 var lastChar byte 252 253 var stringPackChars = []byte{'"', '\''} 254 for i := 0; i < len(sqlCache); i++ { 255 thisChar = sqlCache[i] 256 if i > 0 { 257 lastChar = sqlCache[i-1] 258 } 259 260 // inStringLevel == 0 表示 不在 字符串 包装 中 261 if thisChar == ';' && inStringLevel == 0 { 262 if i > 0 { 263 list = append(list, sqlCache[beg:i]) 264 } 265 beg = i + 1 266 } else { 267 packCharIndex := BytesIndex(stringPackChars, thisChar) 268 if packCharIndex >= 0 { 269 // inStringLevel == 0 表示 不在 字符串 包装 中 270 if inStringLevel == 0 { 271 inStringPack = stringPackChars[packCharIndex] 272 // 字符串包装层级 +1 273 inStringLevel++ 274 } else { 275 if thisChar != inStringPack { 276 } else if lastChar == '\\' { // 如果有转义符号 类似 “\'”,“\"” 277 } else if lastChar == inStringPack { 278 // 如果 前一个字符 与字符串包装字符一致 279 } else { 280 // 字符串包装层级 -1 281 inStringLevel-- 282 } 283 } 284 } 285 } 286 287 } 288 list = append(list, sqlCache[beg:]) 289 for _, sqlOne := range list { 290 sqlOne = strings.TrimSpace(sqlOne) 291 if sqlOne == "" { 292 continue 293 } 294 sqlOne = strings.ReplaceAll(sqlOne, `|-`+cacheKey+`-|`, `''`) 295 sqlOne = strings.ReplaceAll(sqlOne, `|--`+cacheKey+`--|`, `""`) 296 sqlList = append(sqlList, sqlOne) 297 } 298 return 299 } 300 func (this_ *DefaultDialect) OwnerModel(data map[string]interface{}) (owner *OwnerModel, err error) { 301 return 302 } 303 func (this_ *DefaultDialect) OwnersSelectSql() (sql string, err error) { 304 err = errors.New("dialect [" + this_.DialectType().Name + "] not support owner select sql") 305 return 306 } 307 func (this_ *DefaultDialect) OwnerCreateSql(owner *OwnerModel) (sqlList []string, err error) { 308 err = errors.New("dialect [" + this_.DialectType().Name + "] not support owner create sql") 309 310 return 311 } 312 func (this_ *DefaultDialect) OwnerDeleteSql(ownerName string) (sqlList []string, err error) { 313 err = errors.New("dialect [" + this_.DialectType().Name + "] not support owner delete sql") 314 return 315 } 316 func (this_ *DefaultDialect) OwnerChangeSql(ownerName string) (sql string, err error) { 317 err = errors.New("dialect [" + this_.DialectType().Name + "] not support owner change sql") 318 return 319 } 320 321 func (this_ *DefaultDialect) TableModel(data map[string]interface{}) (table *TableModel, err error) { 322 return 323 } 324 func (this_ *DefaultDialect) TablesSelectSql(ownerName string) (sql string, err error) { 325 err = errors.New("dialect [" + this_.DialectType().Name + "] not support table select") 326 return 327 } 328 func (this_ *DefaultDialect) TableSelectSql(ownerName string, tableName string) (sql string, err error) { 329 err = errors.New("dialect [" + this_.DialectType().Name + "] not support table select") 330 return 331 } 332 func (this_ *DefaultDialect) TableCreateSql(ownerName string, table *TableModel) (sqlList []string, err error) { 333 334 createTableSql := `CREATE TABLE ` 335 336 if ownerName != "" { 337 createTableSql += this_.PackOwner(ownerName) + "." 338 } 339 createTableSql += this_.PackTable(table.Name) 340 341 createTableSql += `(` 342 createTableSql += "\n" 343 primaryKeys := "" 344 if len(table.ColumnList) > 0 { 345 for _, column := range table.ColumnList { 346 var columnSql = this_.PackColumn(column.Name) 347 348 var columnType string 349 columnType, err = this_.FormatColumnType(column) 350 if err != nil { 351 return 352 } 353 columnSql += " " + columnType 354 355 if column.Default != "" { 356 columnSql += ` ` + this_.FormatDefaultValue(column) 357 } 358 if column.NotNull { 359 columnSql += ` NOT NULL` 360 } 361 362 if column.PrimaryKey { 363 primaryKeys += "" + column.Name + "," 364 } 365 createTableSql += "\t" + columnSql + ",\n" 366 } 367 } 368 if primaryKeys != "" { 369 primaryKeys = strings.TrimSuffix(primaryKeys, ",") 370 createTableSql += "\tPRIMARY KEY (" + this_.PackColumns(strings.Split(primaryKeys, ",")) + ")" 371 } 372 373 createTableSql = strings.TrimSuffix(createTableSql, ",\n") 374 createTableSql += "\n" 375 376 createTableSql += `)` 377 378 sqlList = append(sqlList, createTableSql) 379 380 // 添加注释 381 if table.Comment != "" { 382 var sqlList_ []string 383 sqlList_, err = this_.TableCommentSql(ownerName, table.Name, table.Comment) 384 if err != nil { 385 return 386 } 387 sqlList = append(sqlList, sqlList_...) 388 } 389 if len(table.ColumnList) > 0 { 390 for _, one := range table.ColumnList { 391 if one.Comment == "" { 392 continue 393 } 394 var sqlList_ []string 395 sqlList_, err = this_.ColumnCommentSql(ownerName, table.Name, one.Name, one.Comment) 396 if err != nil { 397 return 398 } 399 sqlList = append(sqlList, sqlList_...) 400 } 401 } 402 403 if len(table.IndexList) > 0 { 404 for _, one := range table.IndexList { 405 var sqlList_ []string 406 sqlList_, err = this_.IndexAddSql(ownerName, table.Name, one) 407 if err != nil { 408 return 409 } 410 sqlList = append(sqlList, sqlList_...) 411 } 412 } 413 return 414 } 415 func (this_ *DefaultDialect) TableCommentSql(ownerName string, tableName string, comment string) (sqlList []string, err error) { 416 sql := "COMMENT ON TABLE " 417 if ownerName != "" { 418 sql += this_.PackOwner(ownerName) + "." 419 } 420 sql += "" + this_.PackTable(tableName) 421 sql += " IS " + this_.PackValueForSql(nil, comment) 422 sqlList = append(sqlList, sql) 423 return 424 } 425 func (this_ *DefaultDialect) TableRenameSql(ownerName string, oldTableName string, newTableName string) (sqlList []string, err error) { 426 sql := "ALTER TABLE " 427 if ownerName != "" { 428 sql += this_.PackOwner(ownerName) + "." 429 } 430 sql += "" + this_.PackTable(oldTableName) 431 sql += " RENAME TO " 432 sql += "" + this_.PackTable(newTableName) 433 sqlList = append(sqlList, sql) 434 return 435 } 436 func (this_ *DefaultDialect) TableDeleteSql(ownerName string, tableName string) (sqlList []string, err error) { 437 var sql string 438 sql = `DROP TABLE ` 439 if ownerName != "" { 440 sql += this_.PackOwner(ownerName) + "." 441 } 442 sql += this_.PackTable(tableName) 443 sqlList = append(sqlList, sql) 444 return 445 } 446 447 func (this_ *DefaultDialect) ColumnModel(data map[string]interface{}) (table *ColumnModel, err error) { 448 return 449 } 450 func (this_ *DefaultDialect) ColumnsSelectSql(ownerName string, tableName string) (sql string, err error) { 451 err = errors.New("dialect [" + this_.DialectType().Name + "] not support columns select") 452 return 453 } 454 func (this_ *DefaultDialect) ColumnSelectSql(ownerName string, tableName string, columnName string) (sql string, err error) { 455 err = errors.New("dialect [" + this_.DialectType().Name + "] not support column select") 456 return 457 } 458 func (this_ *DefaultDialect) ColumnAddSql(ownerName string, tableName string, column *ColumnModel) (sqlList []string, err error) { 459 var columnType string 460 columnType, err = this_.FormatColumnType(column) 461 if err != nil { 462 return 463 } 464 465 var sql string 466 sql = `ALTER TABLE ` 467 468 if ownerName != "" { 469 sql += this_.PackOwner(ownerName) + "." 470 } 471 sql += this_.PackTable(tableName) 472 473 sql += ` ADD ` 474 sql += this_.PackColumn(column.Name) 475 sql += ` ` + columnType + `` 476 if column.Default != "" { 477 sql += ` ` + this_.FormatDefaultValue(column) 478 } 479 if column.NotNull { 480 sql += ` NOT NULL` 481 } 482 sql += `` 483 484 sqlList = append(sqlList, sql) 485 486 if column.Comment != "" { 487 var sqlList_ []string 488 sqlList_, err = this_.ColumnCommentSql(ownerName, tableName, column.Name, column.Comment) 489 if err != nil { 490 return 491 } 492 sqlList = append(sqlList, sqlList_...) 493 } 494 495 return 496 } 497 func (this_ *DefaultDialect) ColumnCommentSql(ownerName string, tableName string, columnName string, comment string) (sqlList []string, err error) { 498 sql := "COMMENT ON COLUMN " 499 if ownerName != "" { 500 sql += this_.PackOwner(ownerName) + "." 501 } 502 sql += "" + this_.PackTable(tableName) 503 sql += "." + this_.PackColumn(columnName) 504 sql += " IS " + this_.PackValueForSql(nil, comment) 505 sqlList = append(sqlList, sql) 506 return 507 } 508 func (this_ *DefaultDialect) ColumnRenameSql(ownerName string, tableName string, oldColumnName string, newColumnName string) (sqlList []string, err error) { 509 var sql string 510 sql = `ALTER TABLE ` 511 512 if ownerName != "" { 513 sql += this_.PackOwner(ownerName) + "." 514 } 515 sql += this_.PackTable(tableName) 516 517 sql += ` RENAME COLUMN ` 518 sql += this_.PackColumn(oldColumnName) 519 sql += ` TO ` 520 sql += this_.PackColumn(newColumnName) 521 522 sqlList = append(sqlList, sql) 523 return 524 } 525 func (this_ *DefaultDialect) ColumnUpdateSql(ownerName string, tableName string, oldColumn *ColumnModel, newColumn *ColumnModel) (sqlList []string, err error) { 526 527 if oldColumn.Name != newColumn.Name { 528 var sqlList_ []string 529 sqlList_, err = this_.ColumnRenameSql(ownerName, tableName, oldColumn.Name, newColumn.Name) 530 if err != nil { 531 return 532 } 533 sqlList = append(sqlList, sqlList_...) 534 } 535 536 if oldColumn.Type != newColumn.Type || 537 oldColumn.Length != newColumn.Length || 538 oldColumn.Decimal != newColumn.Decimal || 539 oldColumn.Default != newColumn.Default || 540 oldColumn.NotNull != newColumn.NotNull || 541 oldColumn.BeforeColumn != newColumn.BeforeColumn { 542 var columnType string 543 columnType, err = this_.FormatColumnType(newColumn) 544 if err != nil { 545 return 546 } 547 548 var sql string 549 sql = `ALTER TABLE ` 550 551 if ownerName != "" { 552 sql += this_.PackOwner(ownerName) + "." 553 } 554 sql += this_.PackTable(tableName) 555 556 sql += ` MODIFY ` 557 sql += this_.PackColumn(newColumn.Name) 558 sql += ` ` + columnType + `` 559 if newColumn.Default == "" { 560 sql += " DEFAULT NULL" 561 } else { 562 sql += " " + this_.FormatDefaultValue(newColumn) 563 } 564 if newColumn.NotNull { 565 sql += ` NOT NULL` 566 } 567 sql += `` 568 569 sqlList = append(sqlList, sql) 570 } 571 572 return 573 } 574 func (this_ *DefaultDialect) ColumnDeleteSql(ownerName string, tableName string, columnName string) (sqlList []string, err error) { 575 var sql string 576 sql = `ALTER TABLE ` 577 578 if ownerName != "" { 579 sql += this_.PackOwner(ownerName) + "." 580 } 581 sql += this_.PackTable(tableName) 582 583 sql += ` DROP COLUMN ` 584 sql += this_.PackColumn(columnName) 585 586 sqlList = append(sqlList, sql) 587 return 588 } 589 590 func (this_ *DefaultDialect) PrimaryKeyModel(data map[string]interface{}) (primaryKey *PrimaryKeyModel, err error) { 591 return 592 } 593 func (this_ *DefaultDialect) PrimaryKeysSelectSql(ownerName string, tableName string) (sql string, err error) { 594 err = errors.New("dialect [" + this_.DialectType().Name + "] not support primaryKeys select") 595 return 596 } 597 func (this_ *DefaultDialect) PrimaryKeyAddSql(ownerName string, tableName string, primaryKeys []string) (sqlList []string, err error) { 598 sql := "ALTER TABLE " 599 if ownerName != "" { 600 sql += this_.PackOwner(ownerName) + "." 601 } 602 sql += "" + this_.PackTable(tableName) 603 604 sql += ` ADD PRIMARY KEY ` 605 606 sql += "(" + this_.PackColumns(primaryKeys) + ")" 607 608 sqlList = append(sqlList, sql) 609 return 610 } 611 func (this_ *DefaultDialect) PrimaryKeyDeleteSql(ownerName string, tableName string) (sqlList []string, err error) { 612 sql := "ALTER TABLE " 613 if ownerName != "" { 614 sql += this_.PackOwner(ownerName) + "." 615 } 616 sql += "" + this_.PackTable(tableName) 617 618 sql += ` DROP PRIMARY KEY ` 619 620 sqlList = append(sqlList, sql) 621 return 622 } 623 624 func (this_ *DefaultDialect) IndexModel(data map[string]interface{}) (index *IndexModel, err error) { 625 return 626 } 627 func (this_ *DefaultDialect) IndexesSelectSql(ownerName string, tableName string) (sql string, err error) { 628 err = errors.New("dialect [" + this_.DialectType().Name + "] not support indexes select") 629 return 630 } 631 func (this_ *DefaultDialect) IndexAddSql(ownerName string, tableName string, index *IndexModel) (sqlList []string, err error) { 632 sql := "CREATE " 633 switch strings.ToUpper(index.Type) { 634 case "UNIQUE": 635 sql += "UNIQUE INDEX" 636 case "": 637 sql += "INDEX" 638 default: 639 err = errors.New("dialect [" + this_.DialectType().Name + "] not support index type [" + index.Type + "]") 640 return 641 } 642 643 sql += " " + this_.PackColumn(index.Name) + "" 644 645 sql += " ON " 646 if ownerName != "" { 647 sql += this_.PackOwner(ownerName) + "." 648 } 649 sql += "" + this_.PackTable(tableName) 650 651 sql += "(" + this_.PackColumns(index.Columns) + ")" 652 653 sqlList = append(sqlList, sql) 654 return 655 } 656 func (this_ *DefaultDialect) IndexRenameSql(ownerName string, tableName string, oldIndexName string, newIndexName string) (sqlList []string, err error) { 657 var sql string 658 sql = `ALTER INDEX ` 659 660 sql += this_.PackColumn(oldIndexName) 661 sql += ` RENAME TO ` 662 sql += this_.PackColumn(newIndexName) 663 664 sqlList = append(sqlList, sql) 665 return 666 } 667 func (this_ *DefaultDialect) IndexDeleteSql(ownerName string, tableName string, indexName string) (sqlList []string, err error) { 668 sql := "DROP INDEX " 669 sql += "" + this_.PackColumn(indexName) 670 671 sqlList = append(sqlList, sql) 672 return 673 } 674 675 func (this_ *DefaultDialect) InsertSql(insert *InsertModel) (sqlList []string, err error) { 676 677 sql := "INSERT INTO " 678 if insert.OwnerName != "" { 679 sql += this_.PackOwner(insert.OwnerName) + "." 680 } 681 sql += "" + this_.PackTable(insert.TableName) 682 683 sql += "(" + this_.PackColumns(insert.Columns) + ")" 684 sql += ` VALUES ` 685 686 for rowIndex, row := range insert.Rows { 687 if rowIndex > 0 { 688 sql += `, ` 689 } 690 sql += `( ` 691 692 for valueIndex, value := range row { 693 if valueIndex > 0 { 694 sql += `, ` 695 } 696 switch value.Type { 697 case ValueTypeString: 698 sql += this_.PackValueForSql(nil, value.Value) 699 break 700 case ValueTypeNumber: 701 sql += value.Value 702 break 703 case ValueTypeFunc: 704 705 var funcStr = value.Value 706 funcStr, err = this_.FormatFunc(funcStr) 707 if err != nil { 708 return 709 } 710 sql += funcStr 711 break 712 } 713 } 714 715 sql += `) ` 716 } 717 718 sqlList = append(sqlList, sql) 719 return 720 } 721 722 func (this_ *DefaultDialect) InsertDataListSql(ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}) (sqlList []string, batchSqlList []string, err error) { 723 var batchSqlCache = make(map[string]string) 724 var batchSqlIndexCache = make(map[string]int) 725 var columnNames []string 726 for _, one := range columnList { 727 columnNames = append(columnNames, one.Name) 728 } 729 for _, data := range dataList { 730 var columnList_ []string 731 var values = "(" 732 for _, column := range columnList { 733 str := this_.PackValueForSql(column, data[column.Name]) 734 if strings.EqualFold(str, "null") { 735 continue 736 } 737 columnList_ = append(columnList_, column.Name) 738 values += str + ", " 739 } 740 values = strings.TrimSuffix(values, ", ") 741 values += ")" 742 743 insertSqlInfo := "INSERT INTO " 744 if ownerName != "" { 745 insertSqlInfo += this_.PackOwner(ownerName) + "." 746 } 747 insertSqlInfo += this_.PackTable(tableName) 748 insertSqlInfo += " (" 749 insertSqlInfo += this_.PackColumns(columnList_) 750 insertSqlInfo += ") VALUES " 751 752 sqlList = append(sqlList, insertSqlInfo+values) 753 754 key := strings.Join(columnList_, ",") 755 find, ok := batchSqlCache[key] 756 if ok { 757 find += ",\n" + values 758 batchSqlCache[key] = find 759 batchSqlList[batchSqlIndexCache[key]] = find 760 } else { 761 find = insertSqlInfo + "\n" + values 762 batchSqlIndexCache[key] = len(batchSqlCache) 763 batchSqlCache[key] = find 764 batchSqlList = append(batchSqlList, find) 765 } 766 } 767 return 768 }