Add UPDATE query templates for Postgres and MySQL plugins (#646)

This commit is contained in:
Shrikant Sharat Kandula 2020-09-21 17:56:24 +05:30 committed by GitHub
parent fd1108af12
commit 6ef5838cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 63 deletions

View File

@ -415,40 +415,47 @@ public class MySqlPlugin extends BasePlugin {
.filter(column -> column.getDefaultValue() == null)
.collect(Collectors.toList());
final String columnNames = columnsWithoutDefault
.stream()
.map(DatasourceStructure.Column::getName)
.collect(Collectors.joining(", "));
final List<String> columnNames = new ArrayList<>();
final List<String> columnValues = new ArrayList<>();
final StringBuilder setFragments = new StringBuilder();
final String columnValues = columnsWithoutDefault
.stream()
.map(DatasourceStructure.Column::getType)
.map(type -> {
if (type == null) {
return "null";
} else if ("text".equals(type) || "varchar".equals(type)) {
return "''";
} else if (type.startsWith("int")) {
return "1";
} else if (type.startsWith("double")) {
return "1.0";
} else if (DATE_COLUMN_TYPE_NAME.equals(type)) {
return "'2019-07-01'";
} else if (DATETIME_COLUMN_TYPE_NAME.equals(type)
|| TIMESTAMP_COLUMN_TYPE_NAME.equals(type)) {
return "'2019-07-01 10:00:00'";
} else {
return "''";
}
})
.collect(Collectors.joining(", "));
for (DatasourceStructure.Column column : columnsWithoutDefault) {
final String name = column.getName();
final String type = column.getType();
String value;
if (type == null) {
value = "null";
} else if ("text".equals(type) || "varchar".equals(type)) {
value = "''";
} else if (type.startsWith("int")) {
value = "1";
} else if (type.startsWith("double")) {
value = "1.0";
} else if (DATE_COLUMN_TYPE_NAME.equals(type)) {
value = "'2019-07-01'";
} else if (DATETIME_COLUMN_TYPE_NAME.equals(type)
|| TIMESTAMP_COLUMN_TYPE_NAME.equals(type)) {
value = "'2019-07-01 10:00:00'";
} else {
value = "''";
}
columnNames.add(name);
columnValues.add(value);
setFragments.append("\n ").append(name).append(" = ").append(value);
}
final String tableName = table.getName();
table.getTemplates().addAll(List.of(
new DatasourceStructure.Template("SELECT", "SELECT * FROM " + table.getName() + " LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO " + table.getName()
+ " (" + columnNames + ")\n"
+ " VALUES (" + columnValues + ");"),
new DatasourceStructure.Template("DELETE", "DELETE FROM " + table.getName()
new DatasourceStructure.Template("SELECT", "SELECT * FROM " + tableName + " LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO " + tableName
+ " (" + String.join(", ", columnNames) + ")\n"
+ " VALUES (" + String.join(", ", columnValues) + ");"),
new DatasourceStructure.Template("UPDATE", "UPDATE " + tableName + " SET"
+ setFragments.toString() + "\n"
+ " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!"),
new DatasourceStructure.Template("DELETE", "DELETE FROM " + tableName
+ "\n WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!")
));
}

View File

@ -342,6 +342,13 @@ public class MySqlPluginTest {
new DatasourceStructure.Template("SELECT", "SELECT * FROM possessions LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO possessions (id, title, user_id, username, email)\n" +
" VALUES (1, '', 1, '', '');"),
new DatasourceStructure.Template("UPDATE", "UPDATE possessions SET\n" +
" id = 1\n" +
" title = ''\n" +
" user_id = 1\n" +
" username = ''\n" +
" email = ''\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!"),
new DatasourceStructure.Template("DELETE", "DELETE FROM possessions\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!"),
},
@ -378,6 +385,18 @@ public class MySqlPluginTest {
new DatasourceStructure.Template("SELECT", "SELECT * FROM users LIMIT 10;"),
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');"),
new DatasourceStructure.Template("UPDATE", "UPDATE users SET\n" +
" id = 1\n" +
" username = ''\n" +
" password = ''\n" +
" email = ''\n" +
" spouse_dob = '2019-07-01'\n" +
" dob = '2019-07-01'\n" +
" yob = ''\n" +
" time1 = ''\n" +
" created_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!"),
new DatasourceStructure.Template("DELETE", "DELETE FROM users\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may delete everything in the table!"),
},

View File

@ -439,44 +439,49 @@ public class PostgresPlugin extends BasePlugin {
.filter(column -> column.getDefaultValue() == null)
.collect(Collectors.toList());
final String columnNames = columnsWithoutDefault
.stream()
.map(DatasourceStructure.Column::getName)
.map(name -> "\"" + name + "\"")
.collect(Collectors.joining(", "));
final List<String> columnNames = new ArrayList<>();
final List<String> columnValues = new ArrayList<>();
final StringBuilder setFragments = new StringBuilder();
final String columnValues = columnsWithoutDefault
.stream()
.map(DatasourceStructure.Column::getType)
.map(type -> {
if (type == null) {
return "null";
} else if ("text".equals(type) || "varchar".equals(type)) {
return "''";
} else if (type.startsWith("int")) {
return "1";
} else if ("date".equals(type)) {
return "'2019-07-01'";
} else if ("time".equals(type)) {
return "'18:32:45'";
} else if ("timetz".equals(type)) {
return "'04:05:06 PST'";
} else if ("timestamp".equals(type)) {
return "TIMESTAMP '2019-07-01 10:00:00'";
} else if ("timestamptz".equals(type)) {
return "TIMESTAMP WITH TIME ZONE '2019-07-01 06:30:00 CET'";
} else {
return "''";
}
})
.collect(Collectors.joining(", "));
for (DatasourceStructure.Column column : columnsWithoutDefault) {
final String name = column.getName();
final String type = column.getType();
String value;
if (type == null) {
value = "null";
} else if ("text".equals(type) || "varchar".equals(type)) {
value = "''";
} else if (type.startsWith("int")) {
value = "1";
} else if ("date".equals(type)) {
value = "'2019-07-01'";
} else if ("time".equals(type)) {
value = "'18:32:45'";
} else if ("timetz".equals(type)) {
value = "'04:05:06 PST'";
} else if ("timestamp".equals(type)) {
value = "TIMESTAMP '2019-07-01 10:00:00'";
} else if ("timestamptz".equals(type)) {
value = "TIMESTAMP WITH TIME ZONE '2019-07-01 06:30:00 CET'";
} else {
value = "''";
}
columnNames.add("\"" + name + "\"");
columnValues.add(value);
setFragments.append("\n \"").append(name).append("\" = ").append(value);
}
final String quotedTableName = table.getName().replaceFirst("\\.(\\w+)", ".\"$1\"");
table.getTemplates().addAll(List.of(
new DatasourceStructure.Template("SELECT", "SELECT * FROM " + quotedTableName + " LIMIT 10;"),
new DatasourceStructure.Template("INSERT", "INSERT INTO " + quotedTableName
+ " (" + columnNames + ")\n"
+ " VALUES (" + columnValues + ");"),
+ " (" + String.join(", ", columnNames) + ")\n"
+ " VALUES (" + String.join(", ", columnValues) + ");"),
new DatasourceStructure.Template("UPDATE", "UPDATE " + quotedTableName + " SET"
+ setFragments.toString() + "\n"
+ " WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!"),
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

@ -278,6 +278,10 @@ public class PostgresPluginTest {
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("UPDATE", "UPDATE public.\"possessions\" SET\n" +
" \"title\" = ''\n" +
" \"user_id\" = 1\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!"),
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!"),
},
@ -316,6 +320,18 @@ public class PostgresPluginTest {
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("UPDATE", "UPDATE public.\"users\" SET\n" +
" \"username\" = ''\n" +
" \"password\" = ''\n" +
" \"email\" = ''\n" +
" \"spouse_dob\" = '2019-07-01'\n" +
" \"dob\" = '2019-07-01'\n" +
" \"time1\" = '18:32:45'\n" +
" \"time_tz\" = '04:05:06 PST'\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" +
" \"interval1\" = 1\n" +
" WHERE 1 = 0; -- Specify a valid condition here. Removing the condition may update every row in the table!"),
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!"),
},