Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
tpl: sql script removes quote
Browse files Browse the repository at this point in the history
  • Loading branch information
scbizu committed Apr 23, 2020
1 parent 13baa8c commit 88d7da1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 31 deletions.
15 changes: 11 additions & 4 deletions example/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions example/script/gen.script.mysql.blog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);

3 changes: 1 addition & 2 deletions example/script/gen.script.mysql.indexed.blog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';

3 changes: 1 addition & 2 deletions example/script/gen.script.mysql.office.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';

4 changes: 2 additions & 2 deletions example/script/gen.script.mysql.user.base.info.sql
Original file line number Diff line number Diff line change
@@ -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;

3 changes: 1 addition & 2 deletions example/script/gen.script.mysql.user.blogs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';

10 changes: 5 additions & 5 deletions example/script/gen.script.mysql.user.sql
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);

2 changes: 1 addition & 1 deletion tpl/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions tpl/script.mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
);
Expand All @@ -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 -}}
);
Expand All @@ -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}}

0 comments on commit 88d7da1

Please sign in to comment.