Fix unquoted identifiers in generated queries for Postgres (#639)

This commit is contained in:
Shrikant Sharat Kandula 2020-09-21 16:28:14 +05:30 committed by GitHub
parent 8b668484ad
commit 34edbe87e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View File

@ -442,6 +442,7 @@ public class PostgresPlugin extends BasePlugin {
final String columnNames = columnsWithoutDefault
.stream()
.map(DatasourceStructure.Column::getName)
.map(name -> "\"" + name + "\"")
.collect(Collectors.joining(", "));
final String columnValues = columnsWithoutDefault
@ -470,12 +471,13 @@ public class PostgresPlugin extends BasePlugin {
})
.collect(Collectors.joining(", "));
final String quotedTableName = table.getName().replaceFirst("\\.(\\w+)", ".\"$1\"");
table.getTemplates().addAll(List.of(
new DatasourceStructure.Template("SELECT", "SELECT * FROM " + table.getName() + " LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO " + table.getName()
new DatasourceStructure.Template("SELECT", "SELECT * FROM " + quotedTableName + " LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO " + quotedTableName
+ " (" + columnNames + ")\n"
+ " VALUES (" + columnValues + ");"),
new DatasourceStructure.Template("DELETE", "DELETE FROM " + table.getName()
new DatasourceStructure.Template("DELETE", "DELETE FROM " + quotedTableName
+ "\n WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!")
));
}

View File

@ -275,10 +275,10 @@ public class PostgresPluginTest {
assertArrayEquals(
new DatasourceStructure.Template[]{
new DatasourceStructure.Template("SELECT", "SELECT * FROM public.possessions LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO public.possessions (title, user_id)\n" +
new DatasourceStructure.Template("SELECT", "SELECT * FROM public.\"possessions\" LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO public.\"possessions\" (\"title\", \"user_id\")\n" +
" VALUES ('', 1);"),
new DatasourceStructure.Template("DELETE", "DELETE FROM public.possessions\n" +
new DatasourceStructure.Template("DELETE", "DELETE FROM public.\"possessions\"\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!"),
},
possessionsTable.getTemplates().toArray()
@ -313,10 +313,10 @@ public class PostgresPluginTest {
assertArrayEquals(
new DatasourceStructure.Template[]{
new DatasourceStructure.Template("SELECT", "SELECT * FROM public.users LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO public.users (username, password, email, spouse_dob, dob, time1, time_tz, created_on, created_on_tz, interval1)\n" +
new DatasourceStructure.Template("SELECT", "SELECT * FROM public.\"users\" LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO public.\"users\" (\"username\", \"password\", \"email\", \"spouse_dob\", \"dob\", \"time1\", \"time_tz\", \"created_on\", \"created_on_tz\", \"interval1\")\n" +
" VALUES ('', '', '', '2019-07-01', '2019-07-01', '18:32:45', '04:05:06 PST', TIMESTAMP '2019-07-01 10:00:00', TIMESTAMP WITH TIME ZONE '2019-07-01 06:30:00 CET', 1);"),
new DatasourceStructure.Template("DELETE", "DELETE FROM public.users\n" +
new DatasourceStructure.Template("DELETE", "DELETE FROM public.\"users\"\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!"),
},
usersTable.getTemplates().toArray()