Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow Django like template SQL #29

Merged
merged 4 commits into from
May 29, 2023
Merged

allow Django like template SQL #29

merged 4 commits into from
May 29, 2023

Conversation

mashiike
Copy link
Owner

@mashiike mashiike commented May 29, 2023

It seems that there is a need to templateize the SQL specified in task.
So, although it is simple, it can be templateized with Go Tamplate.
For the time being, we will not use a complicated template engine that can define macros.

example

CREATE DATABASE IF NOT EXISTS {{ must_env `ENV` }}_mysqlbatch;
USE {{ must_env `ENV` }}_mysqlbatch;
DROP TABLE IF EXISTS {{ var `relation` `hoge` }};
CREATE TABLE {{ var `relation` `hoge` }} (
    id INTEGER auto_increment,
    name VARCHAR(191),
    age INTEGER,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `name` (`name`)
);
INSERT IGNORE INTO {{ var `relation` `hoge` }}(name) VALUES(CONCAT(SUBSTRING(MD5(RAND()), 1, 40),'@example.com'));
INSERT IGNORE INTO {{ var `relation` `hoge` }}(name) SELECT (CONCAT(SUBSTRING(MD5(RAND()), 1, 40),'@example.com')) FROM {{ var `relation` `hoge` }};
INSERT IGNORE INTO {{ var `relation` `hoge` }}(name, age) SELECT (CONCAT(SUBSTRING(MD5(RAND()), 1, 40),'@example.com')),RAND() FROM {{ var `relation` `hoge` }};
INSERT IGNORE INTO {{ var `relation` `hoge` }}(name, age) SELECT (CONCAT(SUBSTRING(MD5(RAND()), 1, 40),'@example.com')),RAND() FROM {{ var `relation` `hoge` }};
INSERT IGNORE INTO {{ var `relation` `hoge` }}(name, age) SELECT (CONCAT(SUBSTRING(MD5(RAND()), 1, 40),'@example.com')),RAND() FROM {{ var `relation` `hoge` }};
SELECT * FROM {{ var `relation` `hoge` }} WHERE age is NOT NULL LIMIT {{ must_var `limit` }};
$ go run cmd/mysqlbatch/main.go -h 127.0.0.1 --port 3306 -p mysqlbatch < testdata/test_template.sql --var limit=5 --var relation=hoge
2023/05/29 16:30:16 SELECT * FROM hoge WHERE age is NOT NULL LIMIT 5
+----+----------------------------------------------+-----+
| ID |                     NAME                     | AGE |
+----+----------------------------------------------+-----+
|  3 | 6b2d194aa6dfbe5886f089370c495af1@example.com |   1 |
|  4 | a998323e72b5582cf86c260f4d5c0f0a@example.com |   0 |
|  6 | 14c5b540fae3b60c26e703aaf58b4e13@example.com |   1 |
|  7 | cec2abd710bbce6cc899099d529b47b6@example.com |   1 |
|  8 | 17ac7a29e132612c2964ba2d78f7a3ca@example.com |   0 |
+----+----------------------------------------------+-----+


2023/05/29 16:30:16 DB time when the last SQL was executed: 2023-05-29 16:30:16 +0000 UTC

@mashiike mashiike merged commit 9af7f5e into main May 29, 2023
1 check passed
@mashiike mashiike changed the title allow Go template SQL allow Django like template SQL May 29, 2023
@github-actions github-actions bot mentioned this pull request May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant