From 88d7da1fee98b6908c9a6dbb494c81effa0717a1 Mon Sep 17 00:00:00 2001 From: scnace Date: Thu, 23 Apr 2020 14:02:48 +0800 Subject: [PATCH] tpl: sql script removes quote --- example/model/model_test.go | 15 +++++++++++---- example/script/gen.script.mysql.blog.sql | 5 ++--- .../script/gen.script.mysql.indexed.blog.sql | 3 +-- example/script/gen.script.mysql.office.sql | 3 +-- .../gen.script.mysql.user.base.info.sql | 4 ++-- .../script/gen.script.mysql.user.blogs.sql | 3 +-- example/script/gen.script.mysql.user.sql | 10 +++++----- tpl/bindata.go | 2 +- tpl/script.mysql.sql | 19 +++++++++---------- 9 files changed, 33 insertions(+), 31 deletions(-) diff --git a/example/model/model_test.go b/example/model/model_test.go index 7ea9285..1db4f9e 100644 --- a/example/model/model_test.go +++ b/example/model/model_test.go @@ -23,13 +23,20 @@ func runInitSQLScript() error { return err } for _, f := range fs { - query, err := ioutil.ReadFile(filepath.Join(dir, f.Name())) + plain, err := ioutil.ReadFile(filepath.Join(dir, f.Name())) if err != nil { return err } - log.Printf("EXEC SCRIPT: \nFILENAME:%s \nPLAIN QUERY: %s", f.Name(), string(query)) - if _, err := MySQL().Exec(string(query)); err != nil { - return err + log.Printf("EXEC SCRIPT: \nFILENAME:%s \nPLAIN QUERY: %s", f.Name(), string(plain)) + queries := strings.Split(string(plain), ";") + for _, q := range queries { + if strings.TrimSpace(q) == "" { + continue + } + log.Printf("EXEC QUERY: %s", q) + if _, err := MySQL().Exec(q); err != nil { + return err + } } } return nil diff --git a/example/script/gen.script.mysql.blog.sql b/example/script/gen.script.mysql.blog.sql index 5234122..a2ab8cb 100644 --- a/example/script/gen.script.mysql.blog.sql +++ b/example/script/gen.script.mysql.blog.sql @@ -8,7 +8,6 @@ CREATE TABLE `blogs` ( `readed` INT(11) NOT NULL DEFAULT '0', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY(`id`,`user_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'blogs'; -CREATE INDEX `status_of_blog_idx` ON `blogs`(`status`); + PRIMARY KEY(`id`,`user_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'blogs'; +CREATE INDEX status_of_blog_idx ON blogs(status); diff --git a/example/script/gen.script.mysql.indexed.blog.sql b/example/script/gen.script.mysql.indexed.blog.sql index e3babd0..6635be9 100644 --- a/example/script/gen.script.mysql.indexed.blog.sql +++ b/example/script/gen.script.mysql.indexed.blog.sql @@ -8,6 +8,5 @@ CREATE TABLE `indexed_blog` ( `readed` INT(11) NOT NULL DEFAULT '0', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY(`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'indexed_blog'; + PRIMARY KEY(`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'indexed_blog'; diff --git a/example/script/gen.script.mysql.office.sql b/example/script/gen.script.mysql.office.sql index d91d827..fbaa190 100644 --- a/example/script/gen.script.mysql.office.sql +++ b/example/script/gen.script.mysql.office.sql @@ -9,6 +9,5 @@ CREATE TABLE `testCRUD` ( `update_by` VARCHAR(100) NOT NULL DEFAULT '', `create_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY(`office_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'testCRUD'; + PRIMARY KEY(`office_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'testCRUD'; diff --git a/example/script/gen.script.mysql.user.base.info.sql b/example/script/gen.script.mysql.user.base.info.sql index 93133ea..4d060db 100644 --- a/example/script/gen.script.mysql.user.base.info.sql +++ b/example/script/gen.script.mysql.user.base.info.sql @@ -1,4 +1,4 @@ -DROP VIEW IF EXISTS `user_base_info`; -CREATE VIEW `user_base_info` AS SELECT `id`,`name`,`mailbox`,`sex` FROM users; +DROP VIEW IF EXISTS user_base_info; +CREATE VIEW user_base_info AS SELECT `id`,`name`,`mailbox`,`sex` FROM users; diff --git a/example/script/gen.script.mysql.user.blogs.sql b/example/script/gen.script.mysql.user.blogs.sql index bd5dd09..889e925 100644 --- a/example/script/gen.script.mysql.user.blogs.sql +++ b/example/script/gen.script.mysql.user.blogs.sql @@ -2,6 +2,5 @@ CREATE TABLE `user_blogs` ( `user_id` INT(11) NOT NULL DEFAULT '0', `blog_id` INT(11) NOT NULL DEFAULT '0', - PRIMARY KEY(`user_id`,`blog_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'user_blogs'; + PRIMARY KEY(`user_id`,`blog_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'user_blogs'; diff --git a/example/script/gen.script.mysql.user.sql b/example/script/gen.script.mysql.user.sql index 135c958..3f02290 100644 --- a/example/script/gen.script.mysql.user.sql +++ b/example/script/gen.script.mysql.user.sql @@ -1,6 +1,7 @@ CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, + `sub_id` INT(11) NOT NULL DEFAULT '0', `name` VARCHAR(100) NOT NULL DEFAULT '', `mailbox` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '邮箱', `sex` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1', @@ -14,9 +15,8 @@ CREATE TABLE `users` ( `created_at` BIGINT(20) NOT NULL DEFAULT '0', `updated_at` BIGINT(20) NOT NULL DEFAULT '0', `deleted_at` BIGINT(20) NULL , - PRIMARY KEY(`id`), - UNIQUE KEY `uniq_mailbox_password_of_user_uk` (`mailbox`,`password`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '用户表'; -CREATE INDEX `sex_of_user_idx` ON `users`(`sex`); -CREATE INDEX `age_of_user_rng` ON `users`(`age`); + PRIMARY KEY(`id`,`sub_id`,`name`), + UNIQUE KEY `uniq_mailbox_password_of_user_uk` (`mailbox`,`password`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '用户表'; +CREATE INDEX sex_of_user_idx ON users(sex); +CREATE INDEX age_of_user_rng ON users(age); diff --git a/tpl/bindata.go b/tpl/bindata.go index 85f3bbd..db8b230 100644 --- a/tpl/bindata.go +++ b/tpl/bindata.go @@ -951,7 +951,7 @@ func tplRelationZsetSyncGogo() (*asset, error) { return a, nil } -var _tplScriptMysqlSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x94\x41\x6f\x9b\x30\x14\xc7\xcf\xe1\x53\x3c\x45\x91\x1a\xa4\x14\x69\xd3\x0e\xd3\xa2\x1c\x28\x38\xab\xd5\x94\x34\x81\x74\xdd\x69\x90\x62\x26\x4f\xd8\x34\x40\xb4\x45\xcc\xdf\x7d\xb2\xc1\x09\x90\xa8\xdd\x61\xc7\xdd\xfc\xec\xff\x7b\xcf\xfe\xff\x9e\x5c\x55\xd7\x10\x93\x84\x72\x02\xc3\xe2\x39\xa7\x2f\xa5\xc5\x0e\xc5\x2e\x1d\x0a\x21\x8f\x46\xd9\xf6\x07\x7c\x9a\x81\x05\xd7\x42\x18\x72\x87\x26\xc0\x89\xda\xb7\xdc\x6d\x10\x6d\x53\x02\xc3\xa1\x10\x86\xb3\x46\x76\x80\x20\xb0\x6f\x16\x08\xc2\xaa\x6a\x2b\x84\x08\x61\x6c\x0c\x64\x7a\x1e\xf1\xef\x04\x46\x74\x02\xa3\x84\x92\x34\x96\xc5\x95\x74\x2e\xa3\x42\x08\x29\xab\x8f\x2c\x7f\xb5\x70\xb2\x74\xcf\x38\x0c\xf5\x9d\x26\x75\x15\xc2\xe3\x46\x29\x53\x1f\x72\xca\xa2\xfc\x70\x47\x0e\x97\x52\xce\xfa\xee\x39\xdd\xed\xc9\xb1\xf1\x46\x85\x85\x16\xca\xf7\x65\xa5\x56\x59\xb7\x51\x71\x2a\xaf\xfa\x6f\x3c\xbc\xda\x20\xb8\x43\x5f\x21\x94\xa2\x6f\x55\xa5\xc5\x5e\xc4\x08\xfc\x86\xe7\x88\x91\xf4\x3d\x8f\x98\x7e\xf8\xd9\xcb\x55\xf3\x26\xa9\x7e\xb8\xf2\x77\x30\xd0\x57\x20\x3b\x18\x47\x71\x0c\x23\x0a\xef\x4c\x18\xa7\x84\xf7\xf4\xa6\x4e\x18\x84\x8a\x53\x72\xde\x5c\x2a\x42\x5d\x93\xa4\x05\xf9\xcb\x94\xc9\x31\x87\xc7\x4d\x4a\x27\x32\x7b\x0c\xf4\xd2\x04\xe4\x7d\xc6\x1e\x9a\x61\xce\x33\xf7\x06\x5c\x34\xb7\x37\x8b\x00\x9c\x5b\x7b\xed\xa3\x60\xb6\x2f\x93\x8f\x6c\xfb\x01\x9c\xe5\xfd\x3d\xf2\x02\xb8\x6a\xf0\x39\x19\x63\x84\x97\x42\x5c\x4d\x0d\xa3\xe7\x14\xe5\x31\xf9\x75\x44\x85\x65\xa4\x50\xb5\x49\x29\x4d\x1f\x94\x9e\x47\xec\xb9\xe8\x49\xcd\x63\x2d\xbb\x88\x68\xe9\x5d\x98\xd8\x0b\x03\xab\x2e\x52\xd7\xe9\x50\x7b\x05\x5a\x5b\x7d\x64\xf6\x26\xb2\x3e\xb1\xb7\x81\x75\x08\xb5\x03\x73\x6a\x9c\x10\x9d\x56\xaf\x1b\xbd\x96\x07\xff\x7d\xfe\x07\x3e\xf7\x1c\xef\xfc\x9e\x8f\x94\xfc\xac\x3f\x4f\x77\xbd\x7c\x80\x47\x8c\xbe\x00\x9e\x03\x7a\xc2\x7e\xe0\xb7\x9c\x92\x3a\x21\xc2\xa9\xf6\x5a\x09\xcf\x8e\xc1\xf6\xa1\xd9\xc3\xec\x25\xcb\x4b\x7f\xb5\x10\x62\xda\xbd\x41\xbd\xf8\x13\x00\x00\xff\xff\x43\xe2\xee\x26\xf5\x05\x00\x00") +var _tplScriptMysqlSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x94\x51\x6f\x9b\x3e\x14\xc5\x9f\xc3\xa7\xb8\x42\x91\x1a\xa4\x14\xe9\xff\xd7\x1e\xa6\x45\x79\xa0\xe0\xac\x56\x53\xd2\x04\xd2\x75\x4f\x83\x14\x33\x79\xc2\xa6\x01\xa2\x2d\x62\xfe\xee\x93\x0d\x4e\x08\xc9\xa2\x49\x7b\xdd\x9b\x2f\xfe\x9d\x7b\xed\x73\x2c\xea\xfa\x16\x12\x92\x52\x4e\xc0\x2c\x5f\x0b\xfa\x56\xd9\x6c\x5f\x6e\x33\x53\x08\xb9\x35\xcc\x37\xdf\xe0\xc3\x14\x6c\xb8\x15\xc2\x90\x5f\x68\x0a\x9c\xa8\xef\xb6\xb7\x09\xe3\x4d\x46\xc0\x34\x85\x30\xdc\x15\x72\x42\x04\xa1\x73\x37\x47\x10\xd5\x75\x97\x10\x22\x82\x91\x31\x90\xf2\x22\xe6\x5f\x09\x0c\xe9\x18\x86\x29\x25\x59\x22\x9b\x2b\x74\x26\xab\x52\x08\x89\x35\x5b\x76\xb0\x9c\xbb\x79\xb6\x63\x1c\x4c\x7d\xa6\x71\xd3\x85\xf0\xa4\x25\xa5\xf4\xa9\xa0\x2c\x2e\xf6\x0f\x64\x7f\x49\x72\x36\x77\xc7\xe9\x76\x47\x0e\x83\xd7\xaa\x2c\x35\x28\xef\x97\x57\x9a\xb2\xef\xe3\xf2\xd8\x5e\xcd\x5f\xfb\x78\xb9\x46\xf0\x80\x3e\x43\x24\xa1\x2f\x75\xad\x61\x3f\x66\x04\x7e\xc2\x6b\xcc\x48\xf6\x3f\x8f\x99\xbe\xf8\xd9\xcd\xd5\xf0\x56\xd4\x5c\x5c\xf9\x3b\x18\xe8\x23\x90\x2d\x8c\xe2\x24\x81\x21\x85\xff\x2c\x18\x65\x84\xf7\x78\x4b\x0b\x06\x91\xca\x29\x3d\x1f\x2e\x89\x48\xf7\x24\x59\x49\xfe\x50\x32\x3e\x68\x78\xd2\x4a\x4e\x2a\xab\x97\x41\xbb\xb4\x00\xf9\x1f\xb1\x8f\xa6\x98\xf3\xdc\xbb\x03\x0f\xcd\x9c\xf5\x3c\x04\xf7\xde\x59\x05\x28\x9c\xee\xaa\xf4\x3d\xdb\xbc\x03\x77\xf1\xf8\x88\xfc\x10\x6e\xda\xf4\xdc\x9c\x31\xc2\x2b\x21\x6e\x26\x86\xd1\x33\x8a\xf2\x84\xfc\x38\x24\x85\x65\xa5\x92\xea\x06\xa5\x98\x7e\x4e\xfa\x39\x62\xdf\x43\x2f\x50\xd7\x2d\x75\x21\x20\x58\xf8\xd0\x7f\xad\x17\xde\xaa\x3a\x44\xd3\xe4\x24\xb0\x2b\x79\x75\x69\xab\x9b\xef\xef\x9c\x37\xce\xa3\xba\x86\x8f\xfb\xc1\x74\x0b\x6b\x62\x1c\x43\x3a\xae\xae\x1b\xbc\x92\x1b\xff\xfc\xfd\x0b\x7f\x7b\x4e\x9f\xfc\x2c\x9f\x29\xf9\xde\xfc\x2b\xbd\xd5\xe2\x09\x9e\x31\xfa\x04\x78\x06\xe8\x05\x07\x61\x70\xf4\x48\x62\x42\x4c\xb4\xc3\x0a\xeb\x6d\x82\x73\xe0\x31\x7b\xcb\x8b\x2a\x58\xce\xa5\xe4\x64\x7a\xb3\xf8\x15\x00\x00\xff\xff\x7a\xcc\x2d\xab\xe0\x05\x00\x00") func tplScriptMysqlSqlBytes() ([]byte, error) { return bindataRead( diff --git a/tpl/script.mysql.sql b/tpl/script.mysql.sql index 240ba8b..ee7e84d 100644 --- a/tpl/script.mysql.sql +++ b/tpl/script.mysql.sql @@ -17,17 +17,16 @@ CREATE TABLE `{{$obj.DbTable}}` ( {{- end -}} ) {{- end}} - {{- end}} -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '{{$obj.Comment}}'; + {{- end}}) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '{{$obj.Comment}}'; {{- range $i, $index := $obj.Indexes}} {{- if not $index.HasPrimaryKey}} -CREATE INDEX `{{$index.Name | camel2name}}` ON `{{$obj.DbTable}}`( +CREATE INDEX {{$index.Name | camel2name}} ON {{$obj.DbTable}}( {{- range $i, $f := $index.Fields -}} {{- if eq (add $i 1) (len $index.Fields) -}} - `{{- $f.Name | camel2name -}}` + {{- $f.Name | camel2name -}} {{- else -}} - `{{- $f.Name | camel2name -}}`, + {{- $f.Name | camel2name -}}, {{- end -}} {{- end -}} ); @@ -36,12 +35,12 @@ CREATE INDEX `{{$index.Name | camel2name}}` ON `{{$obj.DbTable}}`( {{- range $i, $index := $obj.Ranges}} {{- if not $index.HasPrimaryKey}} -CREATE INDEX `{{$index.Name | camel2name}}` ON `{{$obj.DbTable}}`( +CREATE INDEX {{$index.Name | camel2name}} ON {{$obj.DbTable}}( {{- range $i, $f := $index.Fields -}} {{- if eq (add $i 1) (len $index.Fields) -}} - `{{- $f.Name | camel2name -}}` + {{- $f.Name | camel2name -}} {{- else -}} - `{{- $f.Name | camel2name -}}`, + {{- $f.Name | camel2name -}}, {{- end -}} {{- end -}} ); @@ -50,8 +49,8 @@ CREATE INDEX `{{$index.Name | camel2name}}` ON `{{$obj.DbTable}}`( {{- end}} {{- if ne $obj.DbView ""}} -DROP VIEW IF EXISTS `{{$obj.DbView}}`; -CREATE VIEW `{{$obj.DbView}}` AS {{$obj.ImportSQL}}; +DROP VIEW IF EXISTS {{$obj.DbView}}; +CREATE VIEW {{$obj.DbView}} AS {{$obj.ImportSQL}}; {{- end}} {{end}}