github.com/team-ide/go-dialect@v1.9.20/dialect/mapping.sql.go (about) 1 package dialect 2 3 // Mysql 数据库 SQL 4 func appendMysqlSql(mapping *SqlMapping) { 5 6 mapping.OwnersSelect = ` 7 8 SELECT 9 SCHEMA_NAME ownerName, 10 DEFAULT_CHARACTER_SET_NAME ownerCharacterSetName, 11 DEFAULT_COLLATION_NAME ownerCollationName 12 FROM information_schema.schemata 13 ORDER BY SCHEMA_NAME 14 ` 15 16 mapping.OwnerSelect = ` 17 18 SELECT 19 SCHEMA_NAME ownerName, 20 DEFAULT_CHARACTER_SET_NAME ownerCharacterSetName, 21 DEFAULT_COLLATION_NAME ownerCollationName 22 FROM information_schema.schemata 23 WHERE SCHEMA_NAME={sqlValuePack(ownerName)} 24 ` 25 26 mapping.OwnerCreate = ` 27 28 CREATE DATABASE [IF NOT EXISTS] {ownerNamePack} 29 [CHARACTER SET {ownerCharacterSetName}] 30 [COLLATE {ownerCollationName}] 31 ` 32 33 mapping.OwnerDelete = ` 34 35 DROP DATABASE IF EXISTS {ownerNamePack} 36 ` 37 38 mapping.TablesSelect = ` 39 40 SELECT 41 TABLE_NAME tableName, 42 TABLE_COMMENT tableComment, 43 TABLE_SCHEMA ownerName, 44 TABLE_COLLATION tableCollationName 45 FROM information_schema.tables 46 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 47 ORDER BY TABLE_NAME 48 ` 49 50 mapping.TableSelect = ` 51 52 SELECT 53 TABLE_NAME tableName, 54 TABLE_COMMENT tableComment, 55 TABLE_SCHEMA ownerName, 56 TABLE_COLLATION tableCollationName 57 FROM information_schema.tables 58 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 59 AND TABLE_NAME={sqlValuePack(tableName)} 60 ` 61 62 mapping.TableCreate = ` 63 64 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 65 { tableCreateColumnContent } 66 { tableCreatePrimaryKeyContent } 67 )[CHARACTER SET {tableCharacterSetName}] 68 ` 69 70 mapping.TableCreateColumn = ` 71 72 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} [COMMENT {sqlValuePack(columnComment)}] 73 ` 74 mapping.TableCreateColumnHasComment = true 75 76 mapping.TableCreatePrimaryKey = ` 77 78 PRIMARY KEY ({primaryKeysPack}) 79 ` 80 81 mapping.TableDelete = ` 82 83 DROP TABLE IF EXISTS [{ownerNamePack}.]{tableNamePack} 84 ` 85 86 mapping.TableComment = ` 87 88 ALTER TABLE [{ownerNamePack}.]{tableNamePack} COMMENT {sqlValuePack(tableComment)}` 89 90 mapping.TableRename = ` 91 92 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME AS {tableNamePack} 93 ` 94 95 mapping.ColumnsSelect = ` 96 97 SELECT 98 COLUMN_NAME columnName, 99 COLUMN_COMMENT columnComment, 100 COLUMN_DEFAULT columnDefault, 101 EXTRA columnExtra, 102 TABLE_NAME tableName, 103 TABLE_SCHEMA ownerName, 104 CHARACTER_SET_NAME columnCharacterSetName, 105 IS_NULLABLE isNullable, 106 DATA_TYPE columnDataType, 107 COLUMN_TYPE columnType, 108 DATETIME_PRECISION DATETIME_PRECISION, 109 NUMERIC_PRECISION NUMERIC_PRECISION, 110 NUMERIC_SCALE NUMERIC_SCALE, 111 CHARACTER_MAXIMUM_LENGTH CHARACTER_MAXIMUM_LENGTH 112 FROM information_schema.columns 113 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 114 AND TABLE_NAME={sqlValuePack(tableName)} 115 ` 116 117 mapping.ColumnSelect = ` 118 119 SELECT 120 COLUMN_NAME columnName, 121 COLUMN_COMMENT columnComment, 122 COLUMN_DEFAULT columnDefault, 123 EXTRA columnExtra, 124 TABLE_NAME tableName, 125 TABLE_SCHEMA ownerName, 126 CHARACTER_SET_NAME columnCharacterSetName, 127 IS_NULLABLE isNullable, 128 DATA_TYPE columnDataType, 129 COLUMN_TYPE columnType 130 FROM information_schema.columns 131 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 132 AND TABLE_NAME={sqlValuePack(tableName)} 133 AND COLUMN_NAME={sqlValuePack(columnName)} 134 ` 135 136 mapping.ColumnAdd = ` 137 138 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD COLUMN {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} [COMMENT {sqlValuePack(columnComment)}] [AFTER {columnAfterColumnPack}] 139 ` 140 141 mapping.ColumnDelete = ` 142 143 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 144 ` 145 146 mapping.ColumnComment = ` 147 ` 148 149 mapping.ColumnRename = ` 150 ` 151 152 mapping.ColumnUpdate = ` 153 154 ALTER TABLE [{ownerNamePack}.]{tableNamePack} CHANGE COLUMN {oldColumnNamePack} {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} [COMMENT {sqlValuePack(columnComment)}] [AFTER {columnAfterColumnPack}] 155 ` 156 mapping.ColumnUpdateHasComment = true 157 mapping.ColumnUpdateHasRename = true 158 mapping.ColumnUpdateHasAfter = true 159 160 mapping.ColumnAfter = ` 161 ` 162 163 mapping.PrimaryKeysSelect = ` 164 165 SELECT 166 t2.COLUMN_NAME columnName, 167 t1.TABLE_NAME tableName, 168 t1.TABLE_SCHEMA ownerName 169 FROM information_schema.table_constraints t1 170 LEFT JOIN information_schema.key_column_usage t2 171 ON (t2.CONSTRAINT_NAME=t1.CONSTRAINT_NAME AND t2.TABLE_SCHEMA=t1.TABLE_SCHEMA AND t2.TABLE_NAME=t1.TABLE_NAME) 172 WHERE t1.TABLE_SCHEMA={sqlValuePack(ownerName)} 173 AND t1.TABLE_NAME={sqlValuePack(tableName)} 174 AND t1.CONSTRAINT_TYPE='PRIMARY KEY' 175 ` 176 177 mapping.PrimaryKeyAdd = ` 178 179 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 180 ` 181 182 mapping.PrimaryKeyDelete = ` 183 184 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 185 ` 186 187 mapping.IndexesSelect = ` 188 189 SELECT 190 t1.INDEX_NAME indexName, 191 t1.COLUMN_NAME columnName, 192 t1.INDEX_COMMENT indexComment, 193 t1.NON_UNIQUE nonUnique, 194 t1.TABLE_NAME tableName, 195 t1.TABLE_SCHEMA ownerName, 196 t2.CONSTRAINT_TYPE 197 FROM information_schema.statistics t1 198 LEFT JOIN information_schema.table_constraints t2 199 ON (t2.CONSTRAINT_NAME=t1.INDEX_NAME AND t2.TABLE_SCHEMA=t1.TABLE_SCHEMA AND t2.TABLE_NAME=t1.TABLE_NAME) 200 WHERE t1.TABLE_SCHEMA={sqlValuePack(ownerName)} 201 AND t1.TABLE_NAME={sqlValuePack(tableName)} 202 AND (t2.CONSTRAINT_TYPE !='PRIMARY KEY' OR t2.CONSTRAINT_TYPE = '' OR t2.CONSTRAINT_TYPE IS NULL) 203 ` 204 205 mapping.IndexAdd = ` 206 207 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {indexType} [{indexNamePack}] ({columnNamesPack}) [COMMENT {sqlValuePack(indexComment)}] 208 ` 209 210 mapping.IndexDelete = ` 211 212 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP INDEX {indexNamePack} 213 ` 214 215 mapping.IndexNamePack = ` 216 ` 217 } 218 219 // Oracle 数据库 SQL 220 func appendOracleSql(mapping *SqlMapping) { 221 222 mapping.OwnersSelect = ` 223 224 SELECT 225 USERNAME ownerName 226 FROM DBA_USERS 227 ORDER BY USERNAME 228 ` 229 230 mapping.OwnerSelect = ` 231 232 SELECT 233 USERNAME ownerName 234 FROM DBA_USERS 235 WHERE USERNAME={sqlValuePack(ownerName)} 236 ` 237 238 mapping.OwnerCreate = ` 239 240 CREATE USER {ownerName} IDENTIFIED BY {doubleQuotationMarksPack(ownerPassword)}; 241 GRANT dba,resource,connect TO {ownerName}; 242 ` 243 244 mapping.OwnerDelete = ` 245 246 DROP USER {ownerName} CASCADE 247 ` 248 249 mapping.TablesSelect = ` 250 251 SELECT 252 TABLE_NAME tableName, 253 OWNER ownerName 254 FROM ALL_TABLES 255 WHERE OWNER={sqlValuePack(ownerName)} 256 ORDER BY TABLE_NAME ` 257 258 mapping.TableSelect = ` 259 260 SELECT 261 TABLE_NAME tableName, 262 OWNER ownerName 263 FROM ALL_TABLES 264 WHERE OWNER={sqlValuePack(ownerName)} 265 AND TABLE_NAME={sqlValuePack(tableName)} 266 ` 267 268 mapping.TableCreate = ` 269 270 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 271 { tableCreateColumnContent } 272 { tableCreatePrimaryKeyContent } 273 ) 274 ` 275 276 mapping.TableCreateColumn = ` 277 278 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 279 ` 280 281 mapping.TableCreatePrimaryKey = ` 282 283 PRIMARY KEY ({primaryKeysPack}) 284 ` 285 286 mapping.TableDelete = ` 287 288 DROP TABLE [{ownerNamePack}.]{tableNamePack} 289 ` 290 291 mapping.TableComment = ` 292 293 COMMENT ON TABLE [{ownerNamePack}.]{tableNamePack} IS {sqlValuePack(tableComment)} 294 ` 295 296 mapping.TableRename = ` 297 298 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME TO {tableNamePack} 299 ` 300 301 mapping.ColumnsSelect = ` 302 303 SELECT 304 t.COLUMN_NAME columnName, 305 t.DATA_DEFAULT columnDefault, 306 t.CHARACTER_SET_NAME columnCharacterSetName, 307 t.NULLABLE isNullable, 308 t.DATA_TYPE columnDataType, 309 t.DATA_LENGTH, 310 t.DATA_PRECISION, 311 t.DATA_SCALE, 312 tc.COMMENTS columnComment, 313 t.TABLE_NAME tableName, 314 t.OWNER ownerName 315 FROM ALL_TAB_COLUMNS t 316 LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME) 317 WHERE t.OWNER={sqlValuePack(ownerName)} 318 AND t.TABLE_NAME={sqlValuePack(tableName)} 319 ` 320 321 mapping.ColumnSelect = ` 322 323 SELECT 324 t.COLUMN_NAME columnName, 325 t.DATA_DEFAULT columnDefault, 326 t.CHARACTER_SET_NAME columnCharacterSetName, 327 t.NULLABLE isNullable, 328 t.DATA_TYPE columnDataType, 329 t.DATA_LENGTH, 330 t.DATA_PRECISION, 331 t.DATA_SCALE, 332 tc.COMMENTS columnComment, 333 t.TABLE_NAME tableName, 334 t.OWNER ownerName 335 FROM ALL_TAB_COLUMNS t 336 LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME) 337 WHERE t.OWNER={sqlValuePack(ownerName)} 338 AND t.TABLE_NAME={sqlValuePack(tableName)} 339 AND t.COLUMN_NAME={sqlValuePack(columnName)} 340 ` 341 342 mapping.ColumnAdd = ` 343 344 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 345 ` 346 347 mapping.ColumnDelete = ` 348 349 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 350 ` 351 352 mapping.ColumnComment = ` 353 354 COMMENT ON COLUMN [{ownerNamePack}.]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 355 ` 356 357 mapping.ColumnRename = ` 358 359 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 360 ` 361 362 mapping.ColumnUpdate = ` 363 364 ALTER TABLE [{ownerNamePack}.]{tableNamePack} MODIFY {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 365 ` 366 367 mapping.ColumnAfter = ` 368 ` 369 370 mapping.PrimaryKeysSelect = ` 371 372 SELECT 373 t1.COLUMN_NAME columnName, 374 t2.TABLE_NAME tableName, 375 t2.OWNER ownerName 376 FROM ALL_CONS_COLUMNS t1 377 LEFT JOIN ALL_CONSTRAINTS t2 ON (t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME) 378 WHERE t2.OWNER={sqlValuePack(ownerName)} 379 AND t2.TABLE_NAME={sqlValuePack(tableName)} 380 AND t2.CONSTRAINT_TYPE = 'P' 381 ` 382 383 mapping.PrimaryKeyAdd = ` 384 385 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 386 ` 387 388 mapping.PrimaryKeyDelete = ` 389 390 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 391 ` 392 393 mapping.IndexesSelect = ` 394 395 SELECT 396 t1.INDEX_NAME indexName, 397 t1.COLUMN_NAME columnName, 398 t1.TABLE_OWNER ownerName, 399 t1.TABLE_NAME tableName, 400 t2.UNIQUENESS 401 FROM ALL_IND_COLUMNS t1 402 LEFT JOIN ALL_INDEXES t2 ON (t2.INDEX_NAME = t1.INDEX_NAME) 403 LEFT JOIN ALL_CONSTRAINTS t3 ON (t3.CONSTRAINT_NAME = t1.INDEX_NAME) 404 WHERE t1.TABLE_OWNER={sqlValuePack(ownerName)} 405 AND t1.TABLE_NAME={sqlValuePack(tableName)} 406 AND (t3.CONSTRAINT_TYPE !='P' OR t3.CONSTRAINT_TYPE = '' OR t3.CONSTRAINT_TYPE IS NULL) 407 ` 408 409 mapping.IndexAdd = ` 410 411 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}.]{tableNamePack} ({columnNamesPack}) 412 ` 413 414 mapping.IndexDelete = ` 415 416 DROP INDEX {indexNamePack} 417 ` 418 419 mapping.IndexNamePack = ` 420 ` 421 } 422 423 // 达梦 数据库 SQL 424 func appendDmSql(mapping *SqlMapping) { 425 426 mapping.OwnersSelect = ` 427 428 SELECT 429 USERNAME ownerName 430 FROM DBA_USERS 431 ORDER BY USERNAME 432 ` 433 434 mapping.OwnerSelect = ` 435 436 SELECT 437 USERNAME ownerName 438 FROM DBA_USERS 439 WHERE USERNAME={sqlValuePack(ownerName)} 440 ` 441 442 mapping.OwnerCreate = ` 443 444 CREATE USER {doubleQuotationMarksPack(ownerName)} IDENTIFIED BY {doubleQuotationMarksPack(ownerPassword)}; 445 GRANT DBA TO {doubleQuotationMarksPack(ownerName)}; 446 ` 447 448 mapping.OwnerDelete = ` 449 450 DROP USER {ownerName} CASCADE 451 ` 452 453 mapping.TablesSelect = ` 454 455 SELECT 456 TABLE_NAME tableName, 457 OWNER ownerName 458 FROM ALL_TABLES 459 WHERE OWNER={sqlValuePack(ownerName)} 460 ORDER BY TABLE_NAME 461 ` 462 463 mapping.TableSelect = ` 464 465 SELECT 466 TABLE_NAME tableName, 467 OWNER ownerName 468 FROM ALL_TABLES 469 WHERE OWNER={sqlValuePack(ownerName)} 470 AND TABLE_NAME={sqlValuePack(tableName)} 471 ` 472 473 mapping.TableCreate = ` 474 475 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 476 { tableCreateColumnContent } 477 { tableCreatePrimaryKeyContent } 478 ) 479 ` 480 481 mapping.TableCreateColumn = ` 482 483 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 484 ` 485 486 mapping.TableCreatePrimaryKey = ` 487 488 PRIMARY KEY ({primaryKeysPack}) 489 ` 490 491 mapping.TableDelete = ` 492 493 DROP TABLE [{ownerNamePack}.]{tableNamePack} 494 ` 495 496 mapping.TableComment = ` 497 498 COMMENT ON TABLE [{ownerNamePack}.]{tableNamePack} IS {sqlValuePack(tableComment)} 499 ` 500 501 mapping.TableRename = ` 502 503 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME TO {tableNamePack} 504 ` 505 506 mapping.ColumnsSelect = ` 507 508 SELECT 509 t.COLUMN_NAME columnName, 510 t.DATA_DEFAULT columnDefault, 511 t.CHARACTER_SET_NAME columnCharacterSetName, 512 t.NULLABLE isNullable, 513 t.DATA_TYPE columnDataType, 514 t.DATA_LENGTH, 515 t.DATA_PRECISION, 516 t.DATA_SCALE, 517 tc.COMMENTS columnComment, 518 t.TABLE_NAME tableName, 519 t.OWNER ownerName 520 FROM ALL_TAB_COLUMNS t 521 LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME) 522 WHERE t.OWNER={sqlValuePack(ownerName)} 523 AND t.TABLE_NAME={sqlValuePack(tableName)} 524 ` 525 526 mapping.ColumnSelect = ` 527 528 SELECT 529 t.COLUMN_NAME columnName, 530 t.DATA_DEFAULT columnDefault, 531 t.CHARACTER_SET_NAME columnCharacterSetName, 532 t.NULLABLE isNullable, 533 t.DATA_TYPE columnDataType, 534 t.DATA_LENGTH, 535 t.DATA_PRECISION, 536 t.DATA_SCALE, 537 tc.COMMENTS columnComment, 538 t.TABLE_NAME tableName, 539 t.OWNER ownerName 540 FROM ALL_TAB_COLUMNS t 541 LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME) 542 WHERE t.OWNER={sqlValuePack(ownerName)} 543 AND t.TABLE_NAME={sqlValuePack(tableName)} 544 AND t.COLUMN_NAME={sqlValuePack(columnName)} 545 ` 546 547 mapping.ColumnAdd = ` 548 549 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 550 ` 551 552 mapping.ColumnDelete = ` 553 554 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 555 ` 556 557 mapping.ColumnComment = ` 558 559 COMMENT ON COLUMN [{ownerNamePack}.]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 560 ` 561 562 mapping.ColumnRename = ` 563 564 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 565 ` 566 567 mapping.ColumnUpdate = ` 568 569 ALTER TABLE [{ownerNamePack}.]{tableNamePack} MODIFY {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 570 ` 571 572 mapping.ColumnAfter = ` 573 ` 574 575 mapping.PrimaryKeysSelect = ` 576 577 SELECT 578 t1.COLUMN_NAME columnName, 579 t2.TABLE_NAME tableName, 580 t2.OWNER ownerName 581 FROM ALL_CONS_COLUMNS t1 582 LEFT JOIN ALL_CONSTRAINTS t2 ON (t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME) 583 WHERE t2.OWNER={sqlValuePack(ownerName)} 584 AND t2.TABLE_NAME={sqlValuePack(tableName)} 585 AND t2.CONSTRAINT_TYPE = 'P' 586 ` 587 588 mapping.PrimaryKeyAdd = ` 589 590 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 591 ` 592 593 mapping.PrimaryKeyDelete = ` 594 595 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 596 ` 597 598 mapping.IndexesSelect = ` 599 600 SELECT 601 t1.INDEX_NAME indexName, 602 t1.COLUMN_NAME columnName, 603 t1.TABLE_OWNER ownerName, 604 t1.TABLE_NAME tableName, 605 t2.UNIQUENESS 606 FROM ALL_IND_COLUMNS t1 607 LEFT JOIN ALL_INDEXES t2 ON (t2.INDEX_NAME = t1.INDEX_NAME) 608 LEFT JOIN ALL_CONSTRAINTS t3 ON (t3.CONSTRAINT_NAME = t1.INDEX_NAME) 609 WHERE t1.TABLE_OWNER={sqlValuePack(ownerName)} 610 AND t1.TABLE_NAME={sqlValuePack(tableName)} 611 AND (t3.CONSTRAINT_TYPE !='P' OR t3.CONSTRAINT_TYPE = '' OR t3.CONSTRAINT_TYPE IS NULL) 612 ` 613 614 mapping.IndexAdd = ` 615 616 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}.]{tableNamePack} ({columnNamesPack}) 617 ` 618 619 mapping.IndexDelete = ` 620 621 DROP INDEX {indexNamePack} 622 ` 623 624 mapping.IndexNamePack = ` 625 ` 626 } 627 628 // 金仓 数据库 SQL 629 func appendKingBaseSql(mapping *SqlMapping) { 630 631 mapping.OwnersSelect = ` 632 633 SELECT 634 SCHEMA_NAME ownerName 635 FROM information_schema.schemata 636 ORDER BY SCHEMA_NAME 637 ` 638 639 mapping.OwnerSelect = ` 640 641 SELECT 642 SCHEMA_NAME ownerName 643 FROM information_schema.schemata 644 WHERE SCHEMA_NAME={sqlValuePack(ownerName)} 645 ` 646 647 mapping.OwnerCreate = ` 648 649 CREATE USER {ownerName} WITH PASSWORD {sqlValuePack(ownerPassword)}; 650 CREATE SCHEMA {ownerName}; 651 GRANT USAGE ON SCHEMA {ownerName} TO {ownerName}; 652 GRANT ALL ON SCHEMA {ownerName} TO {ownerName}; 653 GRANT ALL ON ALL TABLES IN SCHEMA {ownerName} TO {ownerName}; 654 ` 655 656 mapping.OwnerDelete = ` 657 658 DROP SCHEMA IF EXISTS {ownerName} CASCADE; 659 DROP USER IF EXISTS {ownerName}; 660 ` 661 662 mapping.TablesSelect = ` 663 664 SELECT 665 TABLE_NAME tableName, 666 TABLE_SCHEMA ownerName 667 FROM information_schema.tables 668 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 669 ORDER BY TABLE_NAME 670 ` 671 672 mapping.TableSelect = ` 673 674 SELECT 675 TABLE_NAME tableName, 676 TABLE_SCHEMA ownerName 677 FROM information_schema.tables 678 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 679 AND TABLE_NAME={sqlValuePack(tableName)} 680 ` 681 682 mapping.TableCreate = ` 683 684 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 685 { tableCreateColumnContent } 686 { tableCreatePrimaryKeyContent } 687 ) 688 ` 689 690 mapping.TableCreateColumn = ` 691 692 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 693 ` 694 695 mapping.TableCreatePrimaryKey = ` 696 697 PRIMARY KEY ({primaryKeysPack}) 698 ` 699 700 mapping.TableDelete = ` 701 702 DROP TABLE [{ownerNamePack}.]{tableNamePack} 703 ` 704 705 mapping.TableComment = ` 706 707 COMMENT ON TABLE [{ownerNamePack}.]{tableNamePack} IS {sqlValuePack(tableComment)} 708 ` 709 710 mapping.TableRename = ` 711 712 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME TO {tableNamePack} 713 ` 714 715 mapping.ColumnsSelect = ` 716 717 718 SELECT 719 COLUMN_NAME columnName, 720 COLUMN_DEFAULT columnDefault, 721 TABLE_NAME tableName, 722 TABLE_SCHEMA ownerName, 723 IS_NULLABLE isNullable, 724 DATA_TYPE columnDataType, 725 DATETIME_PRECISION DATETIME_PRECISION, 726 NUMERIC_PRECISION NUMERIC_PRECISION, 727 NUMERIC_SCALE NUMERIC_SCALE, 728 CHARACTER_MAXIMUM_LENGTH CHARACTER_MAXIMUM_LENGTH 729 FROM information_schema.columns 730 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 731 AND TABLE_NAME={sqlValuePack(tableName)} 732 733 ` 734 735 mapping.ColumnSelect = ` 736 SELECT 737 COLUMN_NAME columnName, 738 COLUMN_DEFAULT columnDefault, 739 TABLE_NAME tableName, 740 TABLE_SCHEMA ownerName, 741 IS_NULLABLE isNullable, 742 DATA_TYPE columnDataType, 743 NUMERIC_PRECISION NUMERIC_PRECISION, 744 NUMERIC_SCALE NUMERIC_SCALE, 745 CHARACTER_MAXIMUM_LENGTH CHARACTER_MAXIMUM_LENGTH 746 FROM information_schema.columns 747 WHERE TABLE_SCHEMA={sqlValuePack(ownerName)} 748 AND TABLE_NAME={sqlValuePack(tableName)} 749 AND COLUMN_NAME={sqlValuePack(columnName)} 750 ` 751 752 mapping.ColumnAdd = ` 753 754 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 755 ` 756 757 mapping.ColumnDelete = ` 758 759 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 760 ` 761 762 mapping.ColumnComment = ` 763 764 COMMENT ON COLUMN [{ownerNamePack}.]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 765 ` 766 767 mapping.ColumnRename = ` 768 769 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 770 ` 771 772 mapping.ColumnUpdate = ` 773 774 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ALTER COLUMN {columnNamePack} TYPE {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 775 776 ` 777 778 mapping.ColumnAfter = ` 779 ` 780 781 mapping.PrimaryKeysSelect = ` 782 783 SELECT 784 t2.COLUMN_NAME columnName, 785 t1.TABLE_NAME tableName, 786 t1.TABLE_SCHEMA ownerName 787 FROM information_schema.table_constraints t1 788 LEFT JOIN information_schema.key_column_usage t2 789 ON (t2.CONSTRAINT_NAME=t1.CONSTRAINT_NAME AND t2.TABLE_SCHEMA=t1.TABLE_SCHEMA AND t2.TABLE_NAME=t1.TABLE_NAME) 790 WHERE t1.TABLE_SCHEMA={sqlValuePack(ownerName)} 791 AND t1.TABLE_NAME={sqlValuePack(tableName)} 792 AND t1.CONSTRAINT_TYPE='PRIMARY KEY' 793 ` 794 795 mapping.PrimaryKeyAdd = ` 796 797 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 798 ` 799 800 mapping.PrimaryKeyDelete = ` 801 802 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 803 ` 804 805 mapping.IndexesSelect = ` 806 807 SELECT 808 t1.INDEX_NAME indexName, 809 t1.COLUMN_NAME columnName, 810 t1.TABLE_OWNER ownerName, 811 t1.TABLE_NAME tableName, 812 t2.UNIQUENESS 813 FROM ALL_IND_COLUMNS t1 814 LEFT JOIN ALL_INDEXES t2 ON (t2.INDEX_NAME = t1.INDEX_NAME) 815 LEFT JOIN ALL_CONSTRAINTS t3 ON (t3.CONSTRAINT_NAME = t1.INDEX_NAME) 816 WHERE t1.INDEX_NAME IN( 817 SELECT INDEXNAME 818 FROM SYS_CATALOG.sys_indexes 819 WHERE SCHEMANAME={sqlValuePack(ownerName)} 820 AND TABLENAME={sqlValuePack(tableName)} 821 ) 822 AND t1.INDEX_NAME NOT IN( 823 SELECT 824 t1.CONSTRAINT_NAME 825 FROM information_schema.table_constraints t1 826 WHERE t1.TABLE_SCHEMA={sqlValuePack(ownerName)} 827 AND t1.TABLE_NAME={sqlValuePack(tableName)} 828 AND t1.CONSTRAINT_TYPE='PRIMARY KEY' 829 ) 830 ` 831 832 mapping.IndexAdd = ` 833 834 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}.]{tableNamePack} ({columnNamesPack}) 835 ` 836 837 mapping.IndexDelete = ` 838 839 DROP INDEX {indexNamePack} 840 ` 841 842 mapping.IndexNamePack = ` 843 ` 844 } 845 846 // 神通 数据库 SQL 847 func appendShenTongSql(mapping *SqlMapping) { 848 849 mapping.OwnersSelect = ` 850 851 SELECT 852 USERNAME ownerName 853 FROM DBA_USERS 854 ORDER BY USERNAME 855 ` 856 857 mapping.OwnerSelect = ` 858 859 SELECT 860 USERNAME ownerName 861 FROM DBA_USERS 862 WHERE USERNAME={sqlValuePack(ownerName)} 863 ` 864 865 mapping.OwnerCreate = ` 866 867 CREATE USER {ownerName} WITH PASSWORD {sqlValuePack(ownerPassword)}; 868 ` 869 870 mapping.OwnerDelete = ` 871 872 DROP USER {ownerName} CASCADE 873 ` 874 875 mapping.TablesSelect = ` 876 877 SELECT 878 TABLE_NAME tableName, 879 OWNER ownerName 880 FROM ALL_TABLES 881 WHERE OWNER={sqlValuePack(ownerName)} 882 ORDER BY TABLE_NAME 883 ` 884 885 mapping.TableSelect = ` 886 887 SELECT 888 TABLE_NAME tableName, 889 OWNER ownerName 890 FROM ALL_TABLES 891 WHERE OWNER={sqlValuePack(ownerName)} 892 AND TABLE_NAME={sqlValuePack(tableName)} 893 ` 894 895 mapping.TableCreate = ` 896 897 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 898 { tableCreateColumnContent } 899 { tableCreatePrimaryKeyContent } 900 ) 901 ` 902 903 mapping.TableCreateColumn = ` 904 905 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 906 ` 907 908 mapping.TableCreatePrimaryKey = ` 909 910 PRIMARY KEY ({primaryKeysPack}) 911 ` 912 913 mapping.TableDelete = ` 914 915 DROP TABLE [{ownerNamePack}.]{tableNamePack} 916 ` 917 918 mapping.TableComment = ` 919 920 COMMENT ON TABLE [{ownerNamePack}.]{tableNamePack} IS {sqlValuePack(tableComment)} 921 ` 922 923 mapping.TableRename = ` 924 925 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME TO {tableNamePack} 926 ` 927 928 mapping.ColumnsSelect = ` 929 930 SELECT 931 t.COLUMN_NAME columnName, 932 t.DATA_DEFAULT columnDefault, 933 t.CHARACTER_SET_NAME columnCharacterSetName, 934 t.NULLABLE isNullable, 935 t.DATA_TYPE columnDataType, 936 t.DATA_LENGTH, 937 t.DATA_PRECISION, 938 t.DATA_SCALE, 939 tc.COMMENTS columnComment, 940 t.TABLE_NAME tableName, 941 t.OWNER ownerName 942 FROM ALL_TAB_COLUMNS t 943 LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME) 944 WHERE t.OWNER={sqlValuePack(ownerName)} 945 AND t.TABLE_NAME={sqlValuePack(tableName)} 946 ` 947 948 mapping.ColumnSelect = ` 949 950 SELECT 951 t.COLUMN_NAME columnName, 952 t.DATA_DEFAULT columnDefault, 953 t.CHARACTER_SET_NAME columnCharacterSetName, 954 t.NULLABLE isNullable, 955 t.DATA_TYPE columnDataType, 956 t.DATA_LENGTH, 957 t.DATA_PRECISION, 958 t.DATA_SCALE, 959 tc.COMMENTS columnComment, 960 t.TABLE_NAME tableName, 961 t.OWNER ownerName 962 FROM ALL_TAB_COLUMNS t 963 LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME) 964 WHERE t.OWNER={sqlValuePack(ownerName)} 965 AND t.TABLE_NAME={sqlValuePack(tableName)} 966 AND t.COLUMN_NAME={sqlValuePack(columnName)} 967 ` 968 969 mapping.ColumnAdd = ` 970 971 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 972 ` 973 974 mapping.ColumnDelete = ` 975 976 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 977 ` 978 979 mapping.ColumnComment = ` 980 981 COMMENT ON COLUMN [{ownerNamePack}.]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 982 ` 983 984 mapping.ColumnRename = ` 985 986 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 987 ` 988 989 mapping.ColumnUpdate = ` 990 991 ALTER TABLE [{ownerNamePack}.]{tableNamePack} MODIFY {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 992 ` 993 994 mapping.ColumnAfter = ` 995 ` 996 997 mapping.PrimaryKeysSelect = ` 998 999 SELECT 1000 t1.COLUMN_NAME columnName, 1001 t2.TABLE_NAME tableName, 1002 t2.OWNER ownerName 1003 FROM ALL_CONS_COLUMNS t1 1004 LEFT JOIN ALL_CONSTRAINTS t2 ON (t2.CONSTRAINT_NAME = t1.CONSTRAINT_NAME) 1005 WHERE t2.OWNER={sqlValuePack(ownerName)} 1006 AND t2.TABLE_NAME={sqlValuePack(tableName)} 1007 AND t2.CONSTRAINT_TYPE = 'P' 1008 ` 1009 1010 mapping.PrimaryKeyAdd = ` 1011 1012 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 1013 ` 1014 1015 mapping.PrimaryKeyDelete = ` 1016 1017 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 1018 ` 1019 1020 mapping.IndexesSelect = ` 1021 1022 SELECT 1023 t1.INDEX_NAME indexName, 1024 t1.COLUMN_NAME columnName, 1025 t1.TABLE_OWNER ownerName, 1026 t1.TABLE_NAME tableName, 1027 t2.UNIQUENESS 1028 FROM ALL_IND_COLUMNS t1 1029 LEFT JOIN ALL_INDEXES t2 ON (t2.INDEX_NAME = t1.INDEX_NAME) 1030 LEFT JOIN ALL_CONSTRAINTS t3 ON (t3.CONSTRAINT_NAME = t1.INDEX_NAME) 1031 WHERE t1.TABLE_OWNER={sqlValuePack(ownerName)} 1032 AND t1.TABLE_NAME={sqlValuePack(tableName)} 1033 AND (t3.CONSTRAINT_TYPE !='P' OR t3.CONSTRAINT_TYPE = '' OR t3.CONSTRAINT_TYPE IS NULL) 1034 ` 1035 1036 mapping.IndexAdd = ` 1037 1038 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}.]{tableNamePack} ({columnNamesPack}) 1039 ` 1040 1041 mapping.IndexDelete = ` 1042 1043 DROP INDEX {indexNamePack} 1044 ` 1045 1046 mapping.IndexNamePack = ` 1047 ` 1048 } 1049 1050 // Sqlite 数据库 SQL 1051 func appendSqliteSql(mapping *SqlMapping) { 1052 1053 mapping.OwnersSelect = ` 1054 1055 SELECT 1056 name ownerName 1057 FROM pragma_database_list AS t_i 1058 ORDER BY name 1059 ` 1060 1061 mapping.OwnerSelect = ` 1062 1063 SELECT 1064 name ownerName 1065 FROM pragma_database_list AS t_i 1066 WHERE name={sqlValuePack(ownerName)} 1067 ` 1068 1069 mapping.OwnerCreate = ` 1070 ` 1071 1072 mapping.OwnerDelete = ` 1073 ` 1074 1075 mapping.TablesSelect = ` 1076 1077 SELECT 1078 name tableName, 1079 sql 1080 FROM sqlite_master 1081 WHERE type ='table' 1082 ORDER BY name 1083 ` 1084 1085 mapping.TableSelect = ` 1086 1087 SELECT 1088 name tableName, 1089 sql 1090 FROM sqlite_master 1091 WHERE type ='table' 1092 AND name={sqlValuePack(tableName)} 1093 ` 1094 1095 mapping.TableCreate = ` 1096 1097 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 1098 { tableCreateColumnContent } 1099 { tableCreatePrimaryKeyContent } 1100 ) 1101 ` 1102 1103 mapping.TableCreateColumn = ` 1104 1105 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1106 ` 1107 1108 mapping.TableCreatePrimaryKey = ` 1109 1110 PRIMARY KEY ({primaryKeysPack}) 1111 ` 1112 1113 mapping.TableDelete = ` 1114 1115 DROP TABLE IF EXISTS [{ownerName}.]{tableName} 1116 ` 1117 1118 mapping.TableComment = ` 1119 ` 1120 1121 mapping.TableRename = ` 1122 1123 ALTER TABLE [{ownerName}.]{oldTableName} RENAME AS {newTableName} 1124 ` 1125 1126 mapping.ColumnsSelect = ` 1127 1128 SELECT 1129 name columnName, 1130 dflt_value columnDefault, 1131 "notnull" isNotNull, 1132 type columnType 1133 FROM pragma_table_info({tableNamePack}) AS t_i 1134 ` 1135 1136 mapping.ColumnSelect = ` 1137 1138 SELECT 1139 name columnName, 1140 dflt_value columnDefault, 1141 "notnull" isNotNull, 1142 type columnType 1143 FROM pragma_table_info({tableNamePack}) AS t_i 1144 WHERE name={sqlValuePack(columnName)} 1145 ` 1146 1147 mapping.ColumnAdd = ` 1148 1149 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD COLUMN {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1150 ` 1151 1152 mapping.ColumnDelete = ` 1153 1154 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 1155 ` 1156 1157 mapping.ColumnComment = ` 1158 ` 1159 1160 mapping.ColumnRename = ` 1161 1162 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 1163 ` 1164 1165 mapping.ColumnUpdate = ` 1166 ` 1167 1168 mapping.ColumnAfter = ` 1169 ` 1170 1171 mapping.PrimaryKeysSelect = ` 1172 1173 SELECT 1174 a.name indexName, 1175 b.name columnName 1176 FROM pragma_index_list({tableNamePack}) AS a,pragma_index_info(a.name) b 1177 WHERE a.origin = "pk" 1178 ` 1179 1180 mapping.PrimaryKeyAdd = ` 1181 1182 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 1183 ` 1184 1185 mapping.PrimaryKeyDelete = ` 1186 1187 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 1188 ` 1189 1190 mapping.IndexesSelect = ` 1191 1192 SELECT 1193 a.name indexName, 1194 a."unique" isUnique, 1195 b.name columnName 1196 FROM pragma_index_list({tableNamePack}) AS a,pragma_index_info(a.name) b 1197 WHERE a.origin != "pk" 1198 ` 1199 1200 mapping.IndexAdd = ` 1201 1202 CREATE {indexType} [{indexNamePack}] ON {tableNamePack}({columnNamesPack}) 1203 ` 1204 1205 mapping.IndexDelete = ` 1206 1207 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP INDEX {indexNamePack} 1208 ` 1209 1210 mapping.IndexNamePack = ` 1211 ` 1212 } 1213 1214 // GBase 数据库 SQL 1215 func appendGBaseSql(mapping *SqlMapping) { 1216 1217 mapping.OwnersSelect = ` 1218 1219 select trim(name) as "ownerName" 1220 from sysmaster:sysdatabases 1221 ORDER BY name 1222 ` 1223 1224 mapping.OwnerSelect = ` 1225 1226 select trim(name) as "ownerName" 1227 from sysmaster:sysdatabases 1228 WHERE name={sqlValuePack(ownerName)} 1229 ` 1230 1231 mapping.OwnerCreate = ` 1232 ` 1233 1234 mapping.OwnerDelete = ` 1235 1236 DROP DATABASE {ownerName} 1237 ` 1238 1239 mapping.TablesSelect = ` 1240 1241 select trim(tabname) as "tableName" 1242 from {ownerNamePack}:systables 1243 ORDER BY tabname` 1244 1245 mapping.TableSelect = ` 1246 1247 select trim(tabname) as "tableName" 1248 from {ownerNamePack}:systables 1249 WHERE 1250 tabname={sqlValuePack(tableName)} 1251 ` 1252 1253 mapping.TableCreate = ` 1254 1255 CREATE TABLE [{ownerNamePack}:]{tableNamePack}( 1256 { tableCreateColumnContent } 1257 { tableCreatePrimaryKeyContent } 1258 ) 1259 ` 1260 1261 mapping.TableCreateColumn = ` 1262 1263 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1264 ` 1265 1266 mapping.TableCreatePrimaryKey = ` 1267 1268 PRIMARY KEY ({primaryKeysPack}) 1269 ` 1270 1271 mapping.TableDelete = ` 1272 1273 DROP TABLE [{ownerNamePack}:]{tableNamePack} 1274 ` 1275 1276 mapping.TableComment = ` 1277 1278 COMMENT ON TABLE [{ownerNamePack}:]{tableNamePack} IS {sqlValuePack(tableComment)} 1279 ` 1280 1281 mapping.TableRename = ` 1282 1283 ALTER TABLE [{ownerNamePack}:]{oldTableNamePack} RENAME TO {tableNamePack} 1284 ` 1285 1286 mapping.ColumnsSelect = ` 1287 1288 1289 select 1290 trim(t.tabname) as "tableName", 1291 trim(c.colname) as "columnName", 1292 trim(d.default) as "columnDefault", 1293 get_colname(c.coltype ,c.collength ,c.extended_id ,1 ) as "columnType", 1294 cs_null.constrtype as notNull, 1295 c.collength as "DATA_LENGTH", 1296 c.colno as sortNo 1297 from {ownerNamePack}:systables t 1298 left join {ownerNamePack}:syscolumns c on c.tabid = t.tabid 1299 left join {ownerNamePack}:sysdefaults d on (t.tabid = d.tabid and c.colno = d.colno) 1300 left join {ownerNamePack}:syscoldepend cd on (c.tabid = cd.tabid and c.colno = cd.colno) 1301 left join {ownerNamePack}:sysxtdtypes xt on c.extended_id = xt.extended_id 1302 left join {ownerNamePack}:sysseclabels e on c.seclabelid = e.seclabelid 1303 left join {ownerNamePack}:sysconstraints cs_null on (cd.tabid = cs_null.tabid and cd.constrid = cs_null.constrid and cs_null.constrtype = 'N') 1304 WHERE 1305 t.tabname={sqlValuePack(tableName)} 1306 ` 1307 1308 mapping.ColumnSelect = ` 1309 1310 1311 select 1312 trim(t.tabname) as "tableName", 1313 trim(c.colname) as "columnName", 1314 trim(d.default) as "columnDefault", 1315 get_colname(c.coltype ,c.collength ,c.extended_id ,1 ) as "columnType", 1316 cs_null.constrtype as notNull, 1317 c.collength as "DATA_LENGTH", 1318 c.colno as sortNo 1319 from {ownerNamePack}:systables t 1320 left join {ownerNamePack}:syscolumns c on c.tabid = t.tabid 1321 left join {ownerNamePack}:sysdefaults d on (t.tabid = d.tabid and c.colno = d.colno) 1322 left join {ownerNamePack}:syscoldepend cd on (c.tabid = cd.tabid and c.colno = cd.colno) 1323 left join {ownerNamePack}:sysxtdtypes xt on c.extended_id = xt.extended_id 1324 left join {ownerNamePack}:sysseclabels e on c.seclabelid = e.seclabelid 1325 left join {ownerNamePack}:sysconstraints cs_null on (cd.tabid = cs_null.tabid and cd.constrid = cs_null.constrid and cs_null.constrtype = 'N') 1326 WHERE 1327 t.tabname={sqlValuePack(tableName)} 1328 AND t.colname={sqlValuePack(columnName)} 1329 ` 1330 1331 mapping.ColumnAdd = ` 1332 1333 ALTER TABLE [{ownerNamePack}:]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1334 ` 1335 1336 mapping.ColumnDelete = ` 1337 1338 ALTER TABLE [{ownerNamePack}:]{tableNamePack} DROP COLUMN {columnNamePack} 1339 ` 1340 1341 mapping.ColumnComment = ` 1342 1343 COMMENT ON COLUMN [{ownerNamePack}:]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 1344 ` 1345 1346 mapping.ColumnRename = ` 1347 1348 ALTER TABLE [{ownerNamePack}:]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 1349 ` 1350 1351 mapping.ColumnUpdate = ` 1352 1353 ALTER TABLE [{ownerNamePack}:]{tableNamePack} MODIFY {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1354 ` 1355 1356 mapping.ColumnAfter = ` 1357 ` 1358 1359 mapping.PrimaryKeysSelect = ` 1360 ` 1361 1362 mapping.PrimaryKeyAdd = ` 1363 1364 ALTER TABLE [{ownerName}:]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 1365 ` 1366 1367 mapping.PrimaryKeyDelete = ` 1368 1369 ALTER TABLE [{ownerName}:]{tableName} DROP PRIMARY KEY 1370 ` 1371 1372 mapping.IndexesSelect = ` 1373 ` 1374 1375 mapping.IndexAdd = ` 1376 1377 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}:]{tableNamePack} ({columnNamesPack}) 1378 ` 1379 1380 mapping.IndexDelete = ` 1381 1382 DROP INDEX {indexNamePack} 1383 ` 1384 1385 mapping.IndexNamePack = ` 1386 ` 1387 } 1388 1389 // OpenGauss 数据库 SQL 1390 func appendOpenGaussSql(mapping *SqlMapping) { 1391 1392 mapping.OwnersSelect = ` 1393 1394 select schema_name "ownerName" 1395 from information_schema.schemata 1396 ORDER BY schema_name 1397 ` 1398 1399 mapping.OwnerSelect = ` 1400 1401 select schema_name "ownerName" 1402 from information_schema.schemata 1403 WHERE schema_name={sqlValuePack(ownerName)} 1404 ` 1405 1406 mapping.OwnerCreate = ` 1407 1408 CREATE USER {ownerName} WITH SYSADMIN PASSWORD {doubleQuotationMarksPack(ownerPassword)}; 1409 ` 1410 1411 mapping.OwnerDelete = ` 1412 1413 DROP USER IF EXISTS {ownerName} CASCADE 1414 ` 1415 1416 mapping.TablesSelect = ` 1417 1418 SELECT 1419 table_schema "ownerName", 1420 table_name "tableName" 1421 FROM information_schema.tables 1422 WHERE table_schema={sqlValuePack(ownerName)} 1423 ORDER BY table_name ` 1424 1425 mapping.TableSelect = ` 1426 1427 SELECT 1428 table_schema "ownerName", 1429 table_name "tableName" 1430 FROM information_schema.tables 1431 WHERE table_schema={sqlValuePack(ownerName)} 1432 AND table_name={sqlValuePack(tableName)} 1433 ` 1434 1435 mapping.TableCreate = ` 1436 1437 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 1438 { tableCreateColumnContent } 1439 { tableCreatePrimaryKeyContent } 1440 ) 1441 ` 1442 1443 mapping.TableCreateColumn = ` 1444 1445 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1446 ` 1447 1448 mapping.TableCreatePrimaryKey = ` 1449 1450 PRIMARY KEY ({primaryKeysPack}) 1451 ` 1452 1453 mapping.TableDelete = ` 1454 1455 DROP TABLE [{ownerNamePack}.]{tableNamePack} 1456 ` 1457 1458 mapping.TableComment = ` 1459 1460 COMMENT ON TABLE [{ownerNamePack}.]{tableNamePack} IS {sqlValuePack(tableComment)} 1461 ` 1462 1463 mapping.TableRename = ` 1464 1465 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME TO {tableNamePack} 1466 ` 1467 1468 mapping.ColumnsSelect = ` 1469 1470 SELECT 1471 table_schema "ownerName", 1472 table_name "tableName", 1473 column_name "columnName", 1474 column_default "columnDefault", 1475 is_nullable "isNullable", 1476 data_type "columnDataType", 1477 numeric_precision "NUMERIC_PRECISION", 1478 numeric_scale "NUMERIC_SCALE", 1479 character_maximum_length "CHARACTER_MAXIMUM_LENGTH" 1480 FROM information_schema.columns 1481 WHERE table_schema={sqlValuePack(ownerName)} 1482 AND table_name={sqlValuePack(tableName)} 1483 ` 1484 1485 mapping.ColumnSelect = ` 1486 1487 SELECT 1488 table_schema "ownerName", 1489 table_name "tableName", 1490 column_name "columnName", 1491 column_default "columnDefault", 1492 is_nullable "isNullable", 1493 data_type "columnDataType", 1494 numeric_precision "NUMERIC_PRECISION", 1495 numeric_scale "NUMERIC_SCALE", 1496 character_maximum_length "CHARACTER_MAXIMUM_LENGTH" 1497 FROM information_schema.columns 1498 WHERE table_schema={sqlValuePack(ownerName)} 1499 AND table_name={sqlValuePack(tableName)} 1500 AND column_name={sqlValuePack(columnName)} 1501 ` 1502 1503 mapping.ColumnAdd = ` 1504 1505 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1506 ` 1507 1508 mapping.ColumnDelete = ` 1509 1510 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 1511 ` 1512 1513 mapping.ColumnComment = ` 1514 1515 COMMENT ON COLUMN [{ownerNamePack}.]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 1516 ` 1517 1518 mapping.ColumnRename = ` 1519 1520 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 1521 ` 1522 1523 mapping.ColumnUpdate = ` 1524 1525 ALTER TABLE [{ownerNamePack}.]{tableNamePack} MODIFY {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1526 ` 1527 1528 mapping.ColumnAfter = ` 1529 ` 1530 1531 mapping.PrimaryKeysSelect = ` 1532 1533 select 1534 pg_attribute.attname as "columnName", 1535 pg_namespace.nspname as "ownerName", 1536 pg_class.relname as "tableName" 1537 from 1538 pg_constraint 1539 inner join pg_class on 1540 pg_constraint.conrelid = pg_class.oid 1541 inner join pg_attribute on 1542 pg_attribute.attrelid = pg_class.oid 1543 and pg_attribute.attnum = any(pg_constraint.conkey) 1544 inner join pg_type on 1545 pg_type.oid = pg_attribute.atttypid 1546 inner join pg_namespace on 1547 pg_namespace.oid = pg_class.relnamespace 1548 where 1549 pg_constraint.contype = 'p' 1550 AND pg_namespace.nspname={sqlValuePack(ownerName)} 1551 AND pg_class.relname={sqlValuePack(tableName)} 1552 ` 1553 1554 mapping.PrimaryKeyAdd = ` 1555 1556 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 1557 ` 1558 1559 mapping.PrimaryKeyDelete = ` 1560 1561 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 1562 ` 1563 1564 mapping.IndexesSelect = ` 1565 1566 SELECT 1567 na.nspname "ownerName", 1568 cl.relname "tableName", 1569 co.conname "indexName", 1570 co.contype, 1571 pg_indexes.indexdef, 1572 co.conkey, --主键字段排序码(pg_attribute.attnum) 1573 co.confkey --外键字段排序码(pg_attribute.attnum) 1574 FROM pg_catalog.pg_class cl 1575 join pg_catalog.pg_namespace na on cl.relnamespace = na.oid 1576 join pg_constraint co on co.conrelid = cl.oid 1577 join pg_indexes on pg_indexes.indexname = co.conname 1578 1579 WHERE co.contype!='p' 1580 AND na.nspname={sqlValuePack(ownerName)} 1581 AND cl.relname={sqlValuePack(tableName)} 1582 ` 1583 1584 mapping.IndexAdd = ` 1585 1586 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}.]{tableNamePack} ({columnNamesPack}) 1587 ` 1588 1589 mapping.IndexDelete = ` 1590 1591 DROP INDEX {indexNamePack} 1592 ` 1593 1594 mapping.IndexNamePack = ` 1595 ` 1596 } 1597 1598 // Postgresql 数据库 SQL 1599 func appendPostgresqlSql(mapping *SqlMapping) { 1600 1601 mapping.OwnersSelect = ` 1602 1603 select schema_name "ownerName" 1604 from information_schema.schemata 1605 ORDER BY schema_name 1606 ` 1607 1608 mapping.OwnerSelect = ` 1609 1610 select schema_name "ownerName" 1611 from information_schema.schemata 1612 WHERE schema_name={sqlValuePack(ownerName)} 1613 ` 1614 1615 mapping.OwnerCreate = ` 1616 1617 CREATE USER {ownerName} WITH SYSADMIN PASSWORD {doubleQuotationMarksPack(ownerPassword)}; 1618 ` 1619 1620 mapping.OwnerDelete = ` 1621 1622 DROP USER IF EXISTS {ownerName} CASCADE 1623 ` 1624 1625 mapping.TablesSelect = ` 1626 1627 SELECT 1628 table_schema "ownerName", 1629 table_name "tableName" 1630 FROM information_schema.tables 1631 WHERE table_schema={sqlValuePack(ownerName)} 1632 ORDER BY table_name ` 1633 1634 mapping.TableSelect = ` 1635 1636 SELECT 1637 table_schema "ownerName", 1638 table_name "tableName" 1639 FROM information_schema.tables 1640 WHERE table_schema={sqlValuePack(ownerName)} 1641 AND table_name={sqlValuePack(tableName)} 1642 ` 1643 1644 mapping.TableCreate = ` 1645 1646 CREATE TABLE [{ownerNamePack}.]{tableNamePack}( 1647 { tableCreateColumnContent } 1648 { tableCreatePrimaryKeyContent } 1649 ) 1650 ` 1651 1652 mapping.TableCreateColumn = ` 1653 1654 {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1655 ` 1656 1657 mapping.TableCreatePrimaryKey = ` 1658 1659 PRIMARY KEY ({primaryKeysPack}) 1660 ` 1661 1662 mapping.TableDelete = ` 1663 1664 DROP TABLE [{ownerNamePack}.]{tableNamePack} 1665 ` 1666 1667 mapping.TableComment = ` 1668 1669 COMMENT ON TABLE [{ownerNamePack}.]{tableNamePack} IS {sqlValuePack(tableComment)} 1670 ` 1671 1672 mapping.TableRename = ` 1673 1674 ALTER TABLE [{ownerNamePack}.]{oldTableNamePack} RENAME TO {tableNamePack} 1675 ` 1676 1677 mapping.ColumnsSelect = ` 1678 1679 SELECT 1680 table_schema "ownerName", 1681 table_name "tableName", 1682 column_name "columnName", 1683 column_default "columnDefault", 1684 is_nullable "isNullable", 1685 data_type "columnDataType", 1686 numeric_precision "NUMERIC_PRECISION", 1687 numeric_scale "NUMERIC_SCALE", 1688 character_maximum_length "CHARACTER_MAXIMUM_LENGTH" 1689 FROM information_schema.columns 1690 WHERE table_schema={sqlValuePack(ownerName)} 1691 AND table_name={sqlValuePack(tableName)} 1692 ` 1693 1694 mapping.ColumnSelect = ` 1695 1696 SELECT 1697 table_schema "ownerName", 1698 table_name "tableName", 1699 column_name "columnName", 1700 column_default "columnDefault", 1701 is_nullable "isNullable", 1702 data_type "columnDataType", 1703 numeric_precision "NUMERIC_PRECISION", 1704 numeric_scale "NUMERIC_SCALE", 1705 character_maximum_length "CHARACTER_MAXIMUM_LENGTH" 1706 FROM information_schema.columns 1707 WHERE table_schema={sqlValuePack(ownerName)} 1708 AND table_name={sqlValuePack(tableName)} 1709 AND column_name={sqlValuePack(columnName)} 1710 ` 1711 1712 mapping.ColumnAdd = ` 1713 1714 ALTER TABLE [{ownerNamePack}.]{tableNamePack} ADD {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1715 ` 1716 1717 mapping.ColumnDelete = ` 1718 1719 ALTER TABLE [{ownerNamePack}.]{tableNamePack} DROP COLUMN {columnNamePack} 1720 ` 1721 1722 mapping.ColumnComment = ` 1723 1724 COMMENT ON COLUMN [{ownerNamePack}.]{tableNamePack}.{columnNamePack} IS {sqlValuePack(columnComment)} 1725 ` 1726 1727 mapping.ColumnRename = ` 1728 1729 ALTER TABLE [{ownerNamePack}.]{tableNamePack} RENAME COLUMN {oldColumnNamePack} TO {columnNamePack} 1730 ` 1731 1732 mapping.ColumnUpdate = ` 1733 1734 ALTER TABLE [{ownerNamePack}.]{tableNamePack} MODIFY {columnNamePack} {columnTypePack} [DEFAULT {columnDefaultPack}] {columnNotNull(columnNotNull)} 1735 ` 1736 1737 mapping.ColumnAfter = ` 1738 ` 1739 1740 mapping.PrimaryKeysSelect = ` 1741 1742 select 1743 pg_attribute.attname as "columnName", 1744 pg_namespace.nspname as "ownerName", 1745 pg_class.relname as "tableName" 1746 from 1747 pg_constraint 1748 inner join pg_class on 1749 pg_constraint.conrelid = pg_class.oid 1750 inner join pg_attribute on 1751 pg_attribute.attrelid = pg_class.oid 1752 and pg_attribute.attnum = any(pg_constraint.conkey) 1753 inner join pg_type on 1754 pg_type.oid = pg_attribute.atttypid 1755 inner join pg_namespace on 1756 pg_namespace.oid = pg_class.relnamespace 1757 where 1758 pg_constraint.contype = 'p' 1759 AND pg_namespace.nspname={sqlValuePack(ownerName)} 1760 AND pg_class.relname={sqlValuePack(tableName)} 1761 ` 1762 1763 mapping.PrimaryKeyAdd = ` 1764 1765 ALTER TABLE [{ownerName}.]{tableName} ADD PRIMARY KEY ({columnNamesPack}) 1766 ` 1767 1768 mapping.PrimaryKeyDelete = ` 1769 1770 ALTER TABLE [{ownerName}.]{tableName} DROP PRIMARY KEY 1771 ` 1772 1773 mapping.IndexesSelect = ` 1774 1775 SELECT 1776 na.nspname "ownerName", 1777 cl.relname "tableName", 1778 co.conname "indexName", 1779 co.contype, 1780 pg_indexes.indexdef, 1781 co.conkey, --主键字段排序码(pg_attribute.attnum) 1782 co.confkey --外键字段排序码(pg_attribute.attnum) 1783 FROM pg_catalog.pg_class cl 1784 join pg_catalog.pg_namespace na on cl.relnamespace = na.oid 1785 join pg_constraint co on co.conrelid = cl.oid 1786 join pg_indexes on pg_indexes.indexname = co.conname 1787 1788 WHERE co.contype!='p' 1789 AND na.nspname={sqlValuePack(ownerName)} 1790 AND cl.relname={sqlValuePack(tableName)} 1791 ` 1792 1793 mapping.IndexAdd = ` 1794 1795 CREATE {indexType} [{indexNamePack}] ON [{ownerNamePack}.]{tableNamePack} ({columnNamesPack}) 1796 ` 1797 1798 mapping.IndexDelete = ` 1799 1800 DROP INDEX {indexNamePack} 1801 ` 1802 1803 mapping.IndexNamePack = ` 1804 ` 1805 } 1806 1807 // DB2 数据库 SQL 1808 func appendDb2Sql(mapping *SqlMapping) { 1809 }