Skip to content

Commit 3cc32e2

Browse files
IwarkDaniel Perez
authored andcommitted
Surround keys with quotes (#1)
Fix bug with column names needing to be quoted. Thanks @Iwark !
1 parent ca4bd0a commit 3cc32e2

File tree

7 files changed

+70
-44
lines changed

7 files changed

+70
-44
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ Here is an example in JSON:
7979
"keys": ["name", "country_id"],
8080
"data": [{
8181
"country_id": 1,
82-
"name": "Ile de france"
82+
"name": "Ile de france",
83+
"order": 10
8384
}, {
8485
"country_id": 2,
85-
"name": "Tokyo"
86+
"name": "Tokyo",
87+
"order": 20
8688
}]
8789
},
8890
"prefectures": [{

db/tables.mysql.sql

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
DROP TABLE IF EXISTS countries;
22
CREATE TABLE countries(
3-
id integer primary key auto_increment,
4-
name varchar(255)
3+
`id` integer primary key auto_increment,
4+
`name` varchar(255)
55
);
66

77
DROP TABLE IF EXISTS regions;
88
CREATE TABLE regions(
9-
id integer primary key auto_increment,
10-
name varchar(255),
11-
country_id int
9+
`id` integer primary key auto_increment,
10+
`name` varchar(255),
11+
`country_id` int,
12+
`order` int
1213
);
1314

14-
CREATE INDEX regions_country_id ON regions (country_id);
15+
CREATE INDEX regions_country_id ON regions (`country_id`);
1516

1617
DROP TABLE IF EXISTS prefectures;
1718
CREATE TABLE prefectures(
18-
id integer primary key auto_increment,
19-
name varchar(255),
20-
region_id int
19+
`id` integer primary key auto_increment,
20+
`name` varchar(255),
21+
`region_id` int
2122
);
2223

23-
CREATE INDEX prefectures_region_id ON prefectures (region_id);
24+
CREATE INDEX prefectures_region_id ON prefectures (`region_id`);
2425

25-
INSERT INTO countries (name) values ('france');
26-
INSERT INTO regions (name, country_id) values ('Tokyo', 2);
26+
INSERT INTO countries (`name`) values ('france');
27+
INSERT INTO regions (`name`, `country_id`, `order`) values ('Tokyo', 2, 10);

db/tables.postgres.sql

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
DROP TABLE IF EXISTS countries;
22
CREATE TABLE countries(
3-
id serial primary key,
4-
name varchar(255)
3+
"id" serial primary key,
4+
"name" varchar(255)
55
);
66

77
DROP TABLE IF EXISTS regions;
88
CREATE TABLE regions(
9-
id serial primary key,
10-
name varchar(255),
11-
country_id int
9+
"id" serial primary key,
10+
"name" varchar(255),
11+
"country_id" int,
12+
"order" int
1213
);
1314

14-
CREATE INDEX regions_country_id ON regions (country_id);
15+
CREATE INDEX regions_country_id ON regions ("country_id");
1516

1617
DROP TABLE IF EXISTS prefectures;
1718
CREATE TABLE prefectures(
18-
id serial primary key,
19-
name varchar(255),
20-
region_id int
19+
"id" serial primary key,
20+
"name" varchar(255),
21+
"region_id" int
2122
);
2223

23-
CREATE INDEX prefectures_region_id ON prefectures (region_id);
24+
CREATE INDEX prefectures_region_id ON prefectures ("region_id");
2425

25-
INSERT INTO countries (name) values ('france');
26-
INSERT INTO regions (name, country_id) values ('Tokyo', 2);
26+
INSERT INTO countries ("name") values ('france');
27+
INSERT INTO regions ("name", "country_id", "order") values ('Tokyo', 2, 10);

db/tables.sqlite3.sql

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
DROP TABLE IF EXISTS countries;
22
CREATE TABLE countries(
3-
id integer primary key autoincrement,
4-
name varchar(255)
3+
"id" integer primary key autoincrement,
4+
"name" varchar(255)
55
);
66

77
DROP TABLE IF EXISTS regions;
88
CREATE TABLE regions(
9-
id integer primary key autoincrement,
10-
name varchar(255),
11-
country_id int
9+
"id" integer primary key autoincrement,
10+
"name" varchar(255),
11+
"country_id" int,
12+
"order" int
1213
);
1314

14-
CREATE INDEX regions_country_id ON regions (country_id);
15+
CREATE INDEX regions_country_id ON regions ("country_id");
1516

1617
DROP TABLE IF EXISTS prefectures;
1718
CREATE TABLE prefectures(
18-
id integer primary key autoincrement,
19-
name varchar(255),
20-
region_id int
19+
"id" integer primary key autoincrement,
20+
"name" varchar(255),
21+
"region_id" int
2122
);
2223

23-
CREATE INDEX prefectures_region_id ON prefectures (region_id);
24+
CREATE INDEX prefectures_region_id ON prefectures ("region_id");
2425

25-
INSERT INTO countries (name) values ('france');
26-
INSERT INTO regions (name, country_id) values ('Tokyo', 2);
26+
INSERT INTO countries ("name") values ('france');
27+
INSERT INTO regions ("name", "country_id", "order") values ('Tokyo', 2, 10);

fixtures/002_regions.json.gz

14 Bytes
Binary file not shown.

populator.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (p *Populator) generateCondition(keys []string, data map[string]interface{}
3939
var args []interface{}
4040
for _, key := range keys {
4141
if v, ok := data[key]; ok {
42-
conditions = append(conditions, fmt.Sprintf("%s=%s", key, p.getPlaceholder()))
42+
conditions = append(conditions, fmt.Sprintf("%s=%s", surroundKeyWithQuote(key, p.Driver), p.getPlaceholder()))
4343
args = append(args, v)
4444
} else {
4545
return "", nil, fmt.Errorf("key %s not found in record %v", key, data)
@@ -49,7 +49,8 @@ func (p *Populator) generateCondition(keys []string, data map[string]interface{}
4949
}
5050

5151
func (p *Populator) generateSelectQuery(fixture Fixture) (string, []interface{}, error) {
52-
query := fmt.Sprintf("SELECT %s FROM %s WHERE ", strings.Join(fixture.Keys, ", "), fixture.TableName)
52+
quotedKeys := surroundKeysWithQuotes(fixture.Keys, p.Driver)
53+
query := fmt.Sprintf("SELECT %s FROM %s WHERE ", strings.Join(quotedKeys, ", "), fixture.TableName)
5354
var conditions []string
5455
var args []interface{}
5556
for _, data := range fixture.Data {
@@ -87,7 +88,8 @@ func (p *Populator) generateInsertStmt(fixture Fixture, data []map[string]interf
8788
args = append(args, recordArgs...)
8889
}
8990
values := strings.Join(placeholders, ",")
90-
return fmt.Sprintf(query, fixture.TableName, strings.Join(keys, ","), values), args
91+
quotedKeys := surroundKeysWithQuotes(keys, p.Driver)
92+
return fmt.Sprintf(query, fixture.TableName, strings.Join(quotedKeys, ","), values), args
9193
}
9294

9395
func (p *Populator) getExistingData(fixture Fixture) ([]map[string]interface{}, error) {

util.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

3-
import (
4-
"reflect"
5-
)
3+
import "reflect"
64

75
func contains(key string, slice []string) bool {
86
for _, v := range slice {
@@ -24,6 +22,27 @@ func extractKeys(data []map[string]interface{}) (res []string) {
2422
return res
2523
}
2624

25+
func quoteForDriver(driver string) string {
26+
if driver == "mysql" {
27+
return "`"
28+
}
29+
return `"`
30+
}
31+
32+
func surroundKeysWithQuotes(keys []string, driver string) []string {
33+
res := make([]string, len(keys))
34+
quote := quoteForDriver(driver)
35+
for i, key := range keys {
36+
res[i] = quote + key + quote
37+
}
38+
return res
39+
}
40+
41+
func surroundKeyWithQuote(key, driver string) string {
42+
quote := quoteForDriver(driver)
43+
return quote + key + quote
44+
}
45+
2746
// from stretchr/testify/assert/assertions
2847
func ObjectsAreEqual(expected, actual interface{}) bool {
2948

0 commit comments

Comments
 (0)