Fixed template queries for PG, MySQL and Snowflake (#6698)

* Fixed template queries

* Fixed tests
This commit is contained in:
Nidhi 2021-08-19 16:35:32 +05:30 committed by GitHub
parent 4b5ca10e0d
commit 8be0580c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 40 deletions

View File

@ -763,7 +763,12 @@ public class MySqlPlugin extends BasePlugin {
columnNames.add(name); columnNames.add(name);
columnValues.add(value); columnValues.add(value);
setFragments.append("\n ").append(name).append(" = ").append(value); setFragments.append("\n ").append(name).append(" = ").append(value).append(",");
}
// Delete the last comma
if (setFragments.length() > 0) {
setFragments.deleteCharAt(setFragments.length() - 1);
} }
final String tableName = table.getName(); final String tableName = table.getName();
@ -779,8 +784,6 @@ public class MySqlPlugin extends BasePlugin {
+ "\n WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!", null) + "\n WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!", null)
)); ));
} }
return;
} }
@Override @Override

View File

@ -643,10 +643,10 @@ public class MySqlPluginTest {
new DatasourceStructure.Template("INSERT", "INSERT INTO possessions (id, title, user_id, username, email)\n" + new DatasourceStructure.Template("INSERT", "INSERT INTO possessions (id, title, user_id, username, email)\n" +
" VALUES (1, '', 1, '', '');", null), " VALUES (1, '', 1, '', '');", null),
new DatasourceStructure.Template("UPDATE", "UPDATE possessions SET\n" + new DatasourceStructure.Template("UPDATE", "UPDATE possessions SET\n" +
" id = 1\n" + " id = 1,\n" +
" title = ''\n" + " title = '',\n" +
" user_id = 1\n" + " user_id = 1,\n" +
" username = ''\n" + " username = '',\n" +
" email = ''\n" + " email = ''\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null), " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null),
new DatasourceStructure.Template("DELETE", "DELETE FROM possessions\n" + new DatasourceStructure.Template("DELETE", "DELETE FROM possessions\n" +
@ -686,15 +686,15 @@ public class MySqlPluginTest {
new DatasourceStructure.Template("INSERT", "INSERT INTO users (id, username, password, email, spouse_dob, dob, yob, time1, created_on, updated_on)\n" + new DatasourceStructure.Template("INSERT", "INSERT INTO users (id, username, password, email, spouse_dob, dob, yob, time1, created_on, updated_on)\n" +
" VALUES (1, '', '', '', '2019-07-01', '2019-07-01', '', '', '2019-07-01 10:00:00', '2019-07-01 10:00:00');", null), " VALUES (1, '', '', '', '2019-07-01', '2019-07-01', '', '', '2019-07-01 10:00:00', '2019-07-01 10:00:00');", null),
new DatasourceStructure.Template("UPDATE", "UPDATE users SET\n" + new DatasourceStructure.Template("UPDATE", "UPDATE users SET\n" +
" id = 1\n" + " id = 1,\n" +
" username = ''\n" + " username = '',\n" +
" password = ''\n" + " password = '',\n" +
" email = ''\n" + " email = '',\n" +
" spouse_dob = '2019-07-01'\n" + " spouse_dob = '2019-07-01',\n" +
" dob = '2019-07-01'\n" + " dob = '2019-07-01',\n" +
" yob = ''\n" + " yob = '',\n" +
" time1 = ''\n" + " time1 = '',\n" +
" created_on = '2019-07-01 10:00:00'\n" + " created_on = '2019-07-01 10:00:00',\n" +
" updated_on = '2019-07-01 10:00:00'\n" + " updated_on = '2019-07-01 10:00:00'\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null), " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null),
new DatasourceStructure.Template("DELETE", "DELETE FROM users\n" + new DatasourceStructure.Template("DELETE", "DELETE FROM users\n" +

View File

@ -726,6 +726,8 @@ public class PostgresPlugin extends BasePlugin {
value = "''"; value = "''";
} else if (type.startsWith("int")) { } else if (type.startsWith("int")) {
value = "1"; value = "1";
} else if (type.startsWith("float") || type.startsWith("double")) {
value = "1.0";
} else if ("date".equals(type)) { } else if ("date".equals(type)) {
value = "'2019-07-01'"; value = "'2019-07-01'";
} else if ("time".equals(type)) { } else if ("time".equals(type)) {
@ -746,7 +748,12 @@ public class PostgresPlugin extends BasePlugin {
columnNames.add("\"" + name + "\""); columnNames.add("\"" + name + "\"");
columnValues.add(value); columnValues.add(value);
setFragments.append("\n \"").append(name).append("\" = ").append(value); setFragments.append("\n \"").append(name).append("\" = ").append(value).append(",");
}
// Delete the last comma
if (setFragments.length() > 0) {
setFragments.deleteCharAt(setFragments.length() - 1);
} }
final String quotedTableName = table.getName().replaceFirst("\\.(\\w+)", ".\"$1\""); final String quotedTableName = table.getName().replaceFirst("\\.(\\w+)", ".\"$1\"");

View File

@ -118,7 +118,8 @@ public class PostgresPluginTest {
" created_on_tz TIMESTAMP WITH TIME ZONE ,\n" + " created_on_tz TIMESTAMP WITH TIME ZONE ,\n" +
" interval1 INTERVAL HOUR ,\n" + " interval1 INTERVAL HOUR ,\n" +
" numbers INTEGER[3] ,\n" + " numbers INTEGER[3] ,\n" +
" texts VARCHAR[2] \n" + " texts VARCHAR[2] ,\n" +
" rating FLOAT4 \n" +
")"); ")");
statement.execute("CREATE TABLE possessions (\n" + statement.execute("CREATE TABLE possessions (\n" +
@ -150,7 +151,7 @@ public class PostgresPluginTest {
" '18:32:45', '04:05:06 PST'," + " '18:32:45', '04:05:06 PST'," +
" TIMESTAMP '2018-11-30 20:45:15', TIMESTAMP WITH TIME ZONE '2018-11-30 20:45:15 CET'," + " TIMESTAMP '2018-11-30 20:45:15', TIMESTAMP WITH TIME ZONE '2018-11-30 20:45:15 CET'," +
" '1.2 years 3 months 2 hours'," + " '1.2 years 3 months 2 hours'," +
" '{1, 2, 3}', '{\"a\", \"b\"}'" + " '{1, 2, 3}', '{\"a\", \"b\"}', 1.0" +
")"); ")");
} }
@ -161,7 +162,7 @@ public class PostgresPluginTest {
" '15:45:30', '04:05:06 PST'," + " '15:45:30', '04:05:06 PST'," +
" TIMESTAMP '2019-11-30 23:59:59', TIMESTAMP WITH TIME ZONE '2019-11-30 23:59:59 CET'," + " TIMESTAMP '2019-11-30 23:59:59', TIMESTAMP WITH TIME ZONE '2019-11-30 23:59:59 CET'," +
" '2 years'," + " '2 years'," +
" '{1, 2, 3}', '{\"a\", \"b\"}'" + " '{1, 2, 3}', '{\"a\", \"b\"}', 2.0" +
")"); ")");
} }
@ -172,7 +173,7 @@ public class PostgresPluginTest {
" '15:45:30', '04:05:06 PST'," + " '15:45:30', '04:05:06 PST'," +
" TIMESTAMP '2021-01-31 23:59:59', TIMESTAMP WITH TIME ZONE '2021-01-31 23:59:59 CET'," + " TIMESTAMP '2021-01-31 23:59:59', TIMESTAMP WITH TIME ZONE '2021-01-31 23:59:59 CET'," +
" '0 years'," + " '0 years'," +
" '{1, 2, 3}', '{\"a\", \"b\"}'" + " '{1, 2, 3}', '{\"a\", \"b\"}', 3.0" +
")"); ")");
} }
@ -332,6 +333,7 @@ public class PostgresPluginTest {
"interval1", "interval1",
"numbers", "numbers",
"texts", "texts",
"rating"
}, },
new ObjectMapper() new ObjectMapper()
.convertValue(node, LinkedHashMap.class) .convertValue(node, LinkedHashMap.class)
@ -422,7 +424,7 @@ public class PostgresPluginTest {
new DatasourceStructure.Template("INSERT", "INSERT INTO public.\"possessions\" (\"title\", \"user_id\")\n" + new DatasourceStructure.Template("INSERT", "INSERT INTO public.\"possessions\" (\"title\", \"user_id\")\n" +
" VALUES ('', 1);", null), " VALUES ('', 1);", null),
new DatasourceStructure.Template("UPDATE", "UPDATE public.\"possessions\" SET\n" + new DatasourceStructure.Template("UPDATE", "UPDATE public.\"possessions\" SET\n" +
" \"title\" = ''\n" + " \"title\" = '',\n" +
" \"user_id\" = 1\n" + " \"user_id\" = 1\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null), " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null),
new DatasourceStructure.Template("DELETE", "DELETE FROM public.\"possessions\"\n" + new DatasourceStructure.Template("DELETE", "DELETE FROM public.\"possessions\"\n" +
@ -449,6 +451,7 @@ public class PostgresPluginTest {
new DatasourceStructure.Column("interval1", "interval", null, false), new DatasourceStructure.Column("interval1", "interval", null, false),
new DatasourceStructure.Column("numbers", "_int4", null, false), new DatasourceStructure.Column("numbers", "_int4", null, false),
new DatasourceStructure.Column("texts", "_varchar", null, false), new DatasourceStructure.Column("texts", "_varchar", null, false),
new DatasourceStructure.Column("rating", "float4", null, false),
}, },
usersTable.getColumns().toArray() usersTable.getColumns().toArray()
); );
@ -463,21 +466,22 @@ public class PostgresPluginTest {
assertArrayEquals( assertArrayEquals(
new DatasourceStructure.Template[]{ new DatasourceStructure.Template[]{
new DatasourceStructure.Template("SELECT", "SELECT * FROM public.\"users\" LIMIT 10;", null), new DatasourceStructure.Template("SELECT", "SELECT * FROM public.\"users\" LIMIT 10;", null),
new DatasourceStructure.Template("INSERT", "INSERT INTO public.\"users\" (\"username\", \"password\", \"email\", \"spouse_dob\", \"dob\", \"time1\", \"time_tz\", \"created_on\", \"created_on_tz\", \"interval1\", \"numbers\", \"texts\")\n" + new DatasourceStructure.Template("INSERT", "INSERT INTO public.\"users\" (\"username\", \"password\", \"email\", \"spouse_dob\", \"dob\", \"time1\", \"time_tz\", \"created_on\", \"created_on_tz\", \"interval1\", \"numbers\", \"texts\", \"rating\")\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, '{1, 2, 3}', '{\"first\", \"second\"}');", null), " 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, '{1, 2, 3}', '{\"first\", \"second\"}', 1.0);", null),
new DatasourceStructure.Template("UPDATE", "UPDATE public.\"users\" SET\n" + new DatasourceStructure.Template("UPDATE", "UPDATE public.\"users\" SET\n" +
" \"username\" = ''\n" + " \"username\" = '',\n" +
" \"password\" = ''\n" + " \"password\" = '',\n" +
" \"email\" = ''\n" + " \"email\" = '',\n" +
" \"spouse_dob\" = '2019-07-01'\n" + " \"spouse_dob\" = '2019-07-01',\n" +
" \"dob\" = '2019-07-01'\n" + " \"dob\" = '2019-07-01',\n" +
" \"time1\" = '18:32:45'\n" + " \"time1\" = '18:32:45',\n" +
" \"time_tz\" = '04:05:06 PST'\n" + " \"time_tz\" = '04:05:06 PST',\n" +
" \"created_on\" = TIMESTAMP '2019-07-01 10:00:00'\n" + " \"created_on\" = TIMESTAMP '2019-07-01 10:00:00',\n" +
" \"created_on_tz\" = TIMESTAMP WITH TIME ZONE '2019-07-01 06:30:00 CET'\n" + " \"created_on_tz\" = TIMESTAMP WITH TIME ZONE '2019-07-01 06:30:00 CET',\n" +
" \"interval1\" = 1\n" + " \"interval1\" = 1,\n" +
" \"numbers\" = '{1, 2, 3}'\n" + " \"numbers\" = '{1, 2, 3}',\n" +
" \"texts\" = '{\"first\", \"second\"}'\n" + " \"texts\" = '{\"first\", \"second\"}',\n" +
" \"rating\" = 1.0\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null), " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!", null),
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!", null), " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!", null),
@ -550,6 +554,7 @@ public class PostgresPluginTest {
assertEquals("2018-11-30T19:45:15Z", node.get("created_on_tz").asText()); assertEquals("2018-11-30T19:45:15Z", node.get("created_on_tz").asText());
assertEquals("1 years 5 mons 0 days 2 hours 0 mins 0.0 secs", node.get("interval1").asText()); assertEquals("1 years 5 mons 0 days 2 hours 0 mins 0.0 secs", node.get("interval1").asText());
assertTrue(node.get("spouse_dob").isNull()); assertTrue(node.get("spouse_dob").isNull());
assertEquals(1.0, node.get("rating").asDouble(), 0.0);
// Check the order of the columns. // Check the order of the columns.
assertArrayEquals( assertArrayEquals(
@ -567,6 +572,7 @@ public class PostgresPluginTest {
"interval1", "interval1",
"numbers", "numbers",
"texts", "texts",
"rating"
}, },
new ObjectMapper() new ObjectMapper()
.convertValue(node, LinkedHashMap.class) .convertValue(node, LinkedHashMap.class)
@ -639,6 +645,7 @@ public class PostgresPluginTest {
"interval1", "interval1",
"numbers", "numbers",
"texts", "texts",
"rating"
}, },
new ObjectMapper() new ObjectMapper()
.convertValue(node, LinkedHashMap.class) .convertValue(node, LinkedHashMap.class)
@ -721,6 +728,7 @@ public class PostgresPluginTest {
"interval1", "interval1",
"numbers", "numbers",
"texts", "texts",
"rating"
}, },
new ObjectMapper() new ObjectMapper()
.convertValue(node, LinkedHashMap.class) .convertValue(node, LinkedHashMap.class)

View File

@ -88,15 +88,15 @@ public class SqlUtils {
case "BOOLEAN": case "BOOLEAN":
return "true"; return "true";
case "DATE": case "DATE":
return "2021-01-01"; return "'2021-01-01'";
case "TIME": case "TIME":
return "00:00:01"; return "'00:00:01'";
case "DATETIME": case "DATETIME":
case "TIMESTAMP": case "TIMESTAMP":
case "TIMESTAMP_LTZ": case "TIMESTAMP_LTZ":
case "TIMESTAMP_NTZ": case "TIMESTAMP_NTZ":
case "TIMESTAMP_TZ": case "TIMESTAMP_TZ":
return "2021-01-01 00:00:01"; return "'2021-01-01 00:00:01'";
case "ARRAY": case "ARRAY":
return "array_construct(1, 2, 3)"; return "array_construct(1, 2, 3)";
case "VARIANT": case "VARIANT":
@ -142,7 +142,12 @@ public class SqlUtils {
columnNames.add(name); columnNames.add(name);
columnValues.add(value); columnValues.add(value);
setFragments.append("\n ").append(name).append(" = ").append(value); setFragments.append("\n ").append(name).append(" = ").append(value).append(",");
}
// Delete the last comma
if (setFragments.length() > 0) {
setFragments.deleteCharAt(setFragments.length() - 1);
} }
final String tableName = table.getSchema() + "." + table.getName(); final String tableName = table.getSchema() + "." + table.getName();