diff --git a/app/server/appsmith-plugins/mysqlPlugin/src/main/java/com/external/plugins/MySqlPlugin.java b/app/server/appsmith-plugins/mysqlPlugin/src/main/java/com/external/plugins/MySqlPlugin.java index 4236a1eed2..3b7c8e6557 100644 --- a/app/server/appsmith-plugins/mysqlPlugin/src/main/java/com/external/plugins/MySqlPlugin.java +++ b/app/server/appsmith-plugins/mysqlPlugin/src/main/java/com/external/plugins/MySqlPlugin.java @@ -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 columnNames = new ArrayList<>(); + final List 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!") )); } diff --git a/app/server/appsmith-plugins/mysqlPlugin/src/test/java/com/external/plugins/MySqlPluginTest.java b/app/server/appsmith-plugins/mysqlPlugin/src/test/java/com/external/plugins/MySqlPluginTest.java index 46dd11bd77..f453182a4f 100644 --- a/app/server/appsmith-plugins/mysqlPlugin/src/test/java/com/external/plugins/MySqlPluginTest.java +++ b/app/server/appsmith-plugins/mysqlPlugin/src/test/java/com/external/plugins/MySqlPluginTest.java @@ -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!"), }, diff --git a/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java b/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java index 756429c50b..9b36f1cb17 100644 --- a/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java +++ b/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java @@ -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 columnNames = new ArrayList<>(); + final List 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!") )); diff --git a/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java b/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java index b5151787cf..c7eb590735 100644 --- a/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java +++ b/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java @@ -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!"), },