Skip to content

Commit

Permalink
functions is lower case
Browse files Browse the repository at this point in the history
  • Loading branch information
Toru-Takagi committed Jul 4, 2023
1 parent 1dc276b commit 088f047
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
32 changes: 16 additions & 16 deletions formatter/format_test.go
Expand Up @@ -186,15 +186,15 @@ ORDER BY u.user_uuid DESC,
SELECT
user_uuid
FROM users u
ORDER BY MIN(u.registered_at)
ORDER BY min(u.registered_at)
`,
},
{
name: "group by",
sql: `select count(*) from users u group by u.name, u.age`,
want: `
SELECT
COUNT(*)
count(*)
FROM users u
GROUP BY u.name, u.age
`,
Expand All @@ -214,23 +214,23 @@ FOR UPDATE SKIP LOCKED
sql: `select current_setting('search_path') as search_path`,
want: `
SELECT
CURRENT_SETTING('search_path') AS search_path
current_setting('search_path') AS search_path
`,
},
{
name: "set_config",
sql: `select set_config('test', $1, false)`,
want: `
SELECT
SET_CONFIG('test', $1, false)
set_config('test', $1, false)
`,
},
{
name: "array_agg",
sql: `select array_agg(t.tablename ORDER BY t.tablename) from pg_catalog.pg_tables AS t`,
want: `
SELECT
ARRAY_AGG(t.tablename ORDER BY t.tablename)
array_agg(t.tablename ORDER BY t.tablename)
FROM pg_catalog.pg_tables t
`,
},
Expand All @@ -252,7 +252,7 @@ SELECT
(
SELECT
ull.last_login_at,
CURRENT_SETTING('test')
current_setting('test')
FROM user_last_login ull
WHERE ull.user_uuid = u.user_uuid
) AS last_login_at
Expand Down Expand Up @@ -328,7 +328,7 @@ INNER JOIN user_last_login ull ON u.user_uuid = ull.user_uuid
sql: `select COUNT(*) OVER () AS total, user_uuid from users`,
want: `
SELECT
COUNT(*) OVER() AS total,
count(*) OVER() AS total,
user_uuid
FROM users
`,
Expand All @@ -348,7 +348,7 @@ INSERT INTO users(
$1,
$2,
$3,
NOW()
now()
)
`,
},
Expand All @@ -363,7 +363,7 @@ INSERT INTO users(
) VALUES (
:user_name,
:user_age,
NOW()
now()
)
`,
},
Expand All @@ -378,7 +378,7 @@ INSERT INTO users(
) VALUES (
'taro',
20,
NOW()
now()
)
`,
},
Expand All @@ -397,7 +397,7 @@ INSERT INTO deleted_users(
user_uuid,
user_name,
user_age,
NOW()
now()
FROM users
WHERE user_uuid = $1
`,
Expand All @@ -410,7 +410,7 @@ INSERT INTO users(
user_uuid,
user_name
) VALUES (
GEN_RANDOM_UUID(),
gen_random_uuid(),
$1
)
`,
Expand All @@ -422,7 +422,7 @@ INSERT INTO users(
INSERT INTO users(
tenant_name_id
) VALUES (
CURRENT_SETTING('tenant_name_id')
current_setting('tenant_name_id')
)
`,
},
Expand All @@ -446,7 +446,7 @@ ON CONFLICT(user_uuid)
DO UPDATE SET
user_name = EXCLUDED.user_name,
user_age = EXCLUDED.user_age,
updated_at = NOW()
updated_at = now()
`,
},
{
Expand Down Expand Up @@ -477,7 +477,7 @@ UPDATE users
SET
user_name = $1,
user_age = $2,
updated_at = NOW()
updated_at = now()
WHERE user_uuid = $3
`,
},
Expand All @@ -494,7 +494,7 @@ WHERE user_uuid = $1
sql: `delete from users where locale = current_setting('locale')`,
want: `
DELETE FROM users
WHERE locale = CURRENT_SETTING('locale')
WHERE locale = current_setting('locale')
`,
},
}
Expand Down
21 changes: 14 additions & 7 deletions formatter/node_formatter/format_funccall.go
Expand Up @@ -15,26 +15,33 @@ func FormatFuncname(ctx context.Context, funcCall *pg_query.Node_FuncCall) (stri
if s, ok := name.Node.(*pg_query.Node_String_); ok {
switch s.String_.Sval {
case "now":
bu.WriteString("NOW")
// https://www.postgresql.org/docs/15/functions-datetime.html
bu.WriteString("now")
bu.WriteString("(")
case "count":
bu.WriteString("COUNT")
// https://www.postgresql.org/docs/15/functions-aggregate.html
bu.WriteString("count")
bu.WriteString("(")
bu.WriteString("*")
case "min":
bu.WriteString("MIN")
// https://www.postgresql.org/docs/15/functions-aggregate.html
bu.WriteString("min")
bu.WriteString("(")
case "gen_random_uuid":
bu.WriteString("GEN_RANDOM_UUID")
// https://www.postgresql.org/docs/15/functions-uuid.html
bu.WriteString("gen_random_uuid")
bu.WriteString("(")
case "current_setting":
bu.WriteString("CURRENT_SETTING")
// https://www.postgresql.org/docs/15/functions-admin.html#FUNCTIONS-ADMIN-SET
bu.WriteString("current_setting")
bu.WriteString("(")
case "set_config":
bu.WriteString("SET_CONFIG")
// https://www.postgresql.org/docs/15/functions-admin.html#FUNCTIONS-ADMIN-SET
bu.WriteString("set_config")
bu.WriteString("(")
case "array_agg":
bu.WriteString("ARRAY_AGG")
// https://www.postgresql.org/docs/15/functions-aggregate.html
bu.WriteString("array_agg")
bu.WriteString("(")
}
}
Expand Down

0 comments on commit 088f047

Please sign in to comment.