Skip to content

Commit be3e13e

Browse files
ybelenkoMikailBag
authored andcommitted
[mysql] Add basic SQL queries (OpenAPITools#5757)
* Add basic SQL queries template * Add namedParametersEnabled option * Move model related SQLs into Model folder * Update README template * Refresh samples
1 parent 94ac635 commit be3e13e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1364
-0
lines changed

docs/generators/mysql-schema.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ sidebar_label: mysql-schema
88
|defaultDatabaseName|Default database name for all MySQL queries| ||
99
|identifierNamingConvention|Naming convention of MySQL identifiers(table names and column names). This is not related to database name which is defined by defaultDatabaseName option|<dl><dt>**original**</dt><dd>Do not transform original names</dd><dt>**snake_case**</dt><dd>Use snake_case names</dd></dl>|original|
1010
|jsonDataTypeEnabled|Use special JSON MySQL data type for complex model properties. Requires MySQL version 5.7.8. Generates TEXT data type when disabled| |true|
11+
|namedParametersEnabled|Generates model prepared SQLs with named parameters, eg. :petName. Question mark placeholder used when option disabled.| |false|
1112

1213
## IMPORT MAPPING
1314

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/MysqlSchemaCodegen.java

+52
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import org.openapitools.codegen.meta.features.*;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
23+
import org.apache.commons.lang3.StringUtils;
2324

2425
import java.util.*;
2526
import java.util.regex.Matcher;
2627
import java.util.regex.Pattern;
28+
import java.io.File;
2729

2830
import static org.openapitools.codegen.utils.StringUtils.underscore;
2931

@@ -35,6 +37,7 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
3537
public static final String DEFAULT_DATABASE_NAME = "defaultDatabaseName";
3638
public static final String JSON_DATA_TYPE_ENABLED = "jsonDataTypeEnabled";
3739
public static final String IDENTIFIER_NAMING_CONVENTION = "identifierNamingConvention";
40+
public static final String NAMED_PARAMETERS_ENABLED = "namedParametersEnabled";
3841
public static final Integer ENUM_MAX_ELEMENTS = 65535;
3942
public static final Integer IDENTIFIER_MAX_LENGTH = 64;
4043

@@ -58,6 +61,7 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
5861
protected String tableNamePrefix = "tbl_", tableNameSuffix = "";
5962
protected String columnNamePrefix = "col_", columnNameSuffix = "";
6063
protected Boolean jsonDataTypeEnabled = true;
64+
protected Boolean namedParametersEnabled = false;
6165
protected String identifierNamingConvention = "original";
6266

6367
public MysqlSchemaCodegen() {
@@ -81,6 +85,8 @@ public MysqlSchemaCodegen() {
8185
// clear import mapping (from default generator) as mysql does not use import directives
8286
importMapping.clear();
8387

88+
setModelPackage("Model");
89+
modelTemplateFiles.put("sql_query.mustache", ".sql");
8490
//modelTestTemplateFiles.put("model_test.mustache", ".php");
8591
// no doc files
8692
// modelDocTemplateFiles.clear();
@@ -179,6 +185,7 @@ public MysqlSchemaCodegen() {
179185
cliOptions.clear();
180186
addOption(DEFAULT_DATABASE_NAME, "Default database name for all MySQL queries", defaultDatabaseName);
181187
addSwitch(JSON_DATA_TYPE_ENABLED, "Use special JSON MySQL data type for complex model properties. Requires MySQL version 5.7.8. Generates TEXT data type when disabled", jsonDataTypeEnabled);
188+
addSwitch(NAMED_PARAMETERS_ENABLED, "Generates model prepared SQLs with named parameters, eg. :petName. Question mark placeholder used when option disabled.", namedParametersEnabled);
182189

183190
// we used to snake_case table/column names, let's add this option
184191
CliOption identifierNamingOpt = new CliOption(IDENTIFIER_NAMING_CONVENTION,
@@ -226,10 +233,19 @@ public void processOpts() {
226233
additionalProperties.put(JSON_DATA_TYPE_ENABLED, getJsonDataTypeEnabled());
227234
}
228235

236+
if (additionalProperties.containsKey(NAMED_PARAMETERS_ENABLED)) {
237+
this.setNamedParametersEnabled(Boolean.valueOf(additionalProperties.get(NAMED_PARAMETERS_ENABLED).toString()));
238+
}
239+
240+
additionalProperties.put(NAMED_PARAMETERS_ENABLED, getNamedParametersEnabled());
241+
229242
if (additionalProperties.containsKey(IDENTIFIER_NAMING_CONVENTION)) {
230243
this.setIdentifierNamingConvention((String) additionalProperties.get(IDENTIFIER_NAMING_CONVENTION));
231244
}
232245

246+
// make model src path available in mustache template
247+
additionalProperties.put("modelSrcPath", "./" + toSrcPath(modelPackage));
248+
233249
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
234250
supportingFiles.add(new SupportingFile("mysql_schema.mustache", "", "mysql_schema.sql"));
235251
}
@@ -1158,6 +1174,24 @@ public Boolean getJsonDataTypeEnabled() {
11581174
return this.jsonDataTypeEnabled;
11591175
}
11601176

1177+
/**
1178+
* Enables named parameters in prepared SQLs
1179+
*
1180+
* @param enabled true to enable, otherwise false
1181+
*/
1182+
public void setNamedParametersEnabled(Boolean enabled) {
1183+
this.namedParametersEnabled = enabled;
1184+
}
1185+
1186+
/**
1187+
* Whether named parameters enabled or disabled in prepared SQLs
1188+
*
1189+
* @return true if enabled otherwise false
1190+
*/
1191+
public Boolean getNamedParametersEnabled() {
1192+
return this.namedParametersEnabled;
1193+
}
1194+
11611195
/**
11621196
* Sets identifier naming convention for table names and column names.
11631197
* This is not related to database name which is defined by defaultDatabaseName option.
@@ -1184,4 +1218,22 @@ public String getIdentifierNamingConvention() {
11841218
return this.identifierNamingConvention;
11851219
}
11861220

1221+
/**
1222+
* Slightly modified version of AbstractPhpCodegen.toSrcPath method.
1223+
*
1224+
* @param packageName package name
1225+
*
1226+
* @return path
1227+
*/
1228+
public String toSrcPath(String packageName) {
1229+
// Trim prefix file separators from package path
1230+
String packagePath = StringUtils.removeStart(
1231+
// Replace period, backslash, forward slash with file separator in package name
1232+
packageName.replaceAll("[\\.\\\\/]", Matcher.quoteReplacement("/")),
1233+
File.separator
1234+
);
1235+
1236+
// Trim trailing file separators from the overall path
1237+
return StringUtils.removeEnd(packagePath, File.separator);
1238+
}
11871239
}

modules/openapi-generator/src/main/resources/mysql-schema/README.mustache

+8
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ Produced file(`mysql_schema.sql`) contains every table definition. Current imple
4646
1. Click **Import** link in left sidebar
4747
2. In **File upload** fieldset click to **Choose Files** and find generated `mysql_schema.sql`
4848
3. Push **Execute** button
49+
50+
### Prepared SQL queries
51+
52+
[Model folder]({{modelSrcPath}}) contains SQL queries(`SELECT *`, `SELECT`, `INSERT`, `UPDATE`, `DELETE`) usually suggested by `PHPMyAdmin` when you hit `SQL` tab. They are absolutely useless without adaptation to your needs. Copypaste them then edit.
53+
54+
Important! Some of SQLs(`INSERT`/`UPDATE`) contains {{#namedParametersEnabled}}named parameters eg. :namedParam{{/namedParametersEnabled}}{{^namedParametersEnabled}}question marks(`?`) which are parameter placeholders{{/namedParametersEnabled}}. You need to bind values to these params to execute query.
55+
56+
If your MySQL driver doesn't support named parameters(`PHP PDO` supports while `PHP mysqli` doesn't) you need to make sure that `namedParametersEnabled` generator option is disabled.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--
2+
-- {{appName}}.{{#defaultDatabaseName}}
3+
-- Database: `{{{defaultDatabaseName}}}`{{/defaultDatabaseName}}
4+
-- Prepared SQL queries for {{#models}}{{#model}}'{{{name}}}'{{/model}}{{/models}} definition.
5+
--
6+
7+
{{#models}}{{#model}}
8+
--
9+
-- SELECT template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
10+
--
11+
SELECT {{#vars}}{{#vendorExtensions}}{{#x-mysqlSchema}}{{#columnDefinition}}`{{colName}}`{{#hasMore}}, {{/hasMore}}{{/columnDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}{{/vars}} FROM {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{defaultDatabaseName}}}`.{{/defaultDatabaseName}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}} WHERE 1;
12+
13+
--
14+
-- INSERT template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
15+
--
16+
INSERT INTO {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{defaultDatabaseName}}}`.{{/defaultDatabaseName}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}({{#vars}}{{#vendorExtensions}}{{#x-mysqlSchema}}{{#columnDefinition}}`{{colName}}`{{#hasMore}}, {{/hasMore}}{{/columnDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}{{/vars}}) VALUES ({{#vars}}{{#vendorExtensions}}{{#x-mysqlSchema}}{{#columnDefinition}}{{#namedParametersEnabled}}:{{colName}}{{/namedParametersEnabled}}{{^namedParametersEnabled}}?{{/namedParametersEnabled}}{{#hasMore}}, {{/hasMore}}{{/columnDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}{{/vars}});
17+
18+
--
19+
-- UPDATE template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
20+
--
21+
UPDATE {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{defaultDatabaseName}}}`.{{/defaultDatabaseName}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}} SET {{#vars}}{{#vendorExtensions}}{{#x-mysqlSchema}}{{#columnDefinition}}`{{colName}}` = {{#namedParametersEnabled}}:{{colName}}{{/namedParametersEnabled}}{{^namedParametersEnabled}}?{{/namedParametersEnabled}}{{#hasMore}}, {{/hasMore}}{{/columnDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}{{/vars}} WHERE 1;
22+
23+
--
24+
-- DELETE template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
25+
--
26+
DELETE FROM {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{defaultDatabaseName}}}`.{{/defaultDatabaseName}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}} WHERE 0;
27+
{{/model}}{{/models}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/mysql/MysqlSchemaCodegenTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ public void testGetJsonDataTypeEnabled() {
274274
Assert.assertFalse(codegen.getJsonDataTypeEnabled());
275275
}
276276

277+
@Test
278+
public void testSetNamedParametersEnabled() {
279+
final MysqlSchemaCodegen codegen = new MysqlSchemaCodegen();
280+
codegen.setNamedParametersEnabled(true);
281+
Assert.assertTrue(codegen.getNamedParametersEnabled());
282+
codegen.setNamedParametersEnabled(false);
283+
Assert.assertFalse(codegen.getNamedParametersEnabled());
284+
}
285+
286+
@Test
287+
public void testGetNamedParametersEnabled() {
288+
final MysqlSchemaCodegen codegen = new MysqlSchemaCodegen();
289+
Assert.assertFalse(codegen.getNamedParametersEnabled());
290+
codegen.setNamedParametersEnabled(true);
291+
Assert.assertTrue(codegen.getNamedParametersEnabled());
292+
}
293+
277294
@Test
278295
public void testSetIdentifierNamingConvention() {
279296
final MysqlSchemaCodegen codegen = new MysqlSchemaCodegen();

modules/openapi-generator/src/test/java/org/openapitools/codegen/mysql/MysqlSchemaOptionsTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ protected void verifyOptions() {
4242
verify(clientCodegen).setDefaultDatabaseName(MysqlSchemaOptionsProvider.DEFAULT_DATABASE_NAME_VALUE);
4343
verify(clientCodegen).setJsonDataTypeEnabled(Boolean.valueOf(MysqlSchemaOptionsProvider.JSON_DATA_TYPE_ENABLED_VALUE));
4444
verify(clientCodegen).setIdentifierNamingConvention(MysqlSchemaOptionsProvider.IDENTIFIER_NAMING_CONVENTION_VALUE);
45+
verify(clientCodegen).setNamedParametersEnabled(Boolean.valueOf(MysqlSchemaOptionsProvider.NAMED_PARAMETERS_ENABLED_VALUE));
4546
}
4647
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/MysqlSchemaOptionsProvider.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class MysqlSchemaOptionsProvider implements OptionsProvider {
2525
public static final String DEFAULT_DATABASE_NAME_VALUE = "database_name";
2626
public static final String JSON_DATA_TYPE_ENABLED_VALUE = "false";
2727
public static final String IDENTIFIER_NAMING_CONVENTION_VALUE = "snake_case";
28+
public static final String NAMED_PARAMETERS_ENABLED_VALUE = "true";
2829

2930
@Override
3031
public String getLanguage() {
@@ -37,6 +38,7 @@ public Map<String, String> createOptions() {
3738
return builder.put(MysqlSchemaCodegen.DEFAULT_DATABASE_NAME, DEFAULT_DATABASE_NAME_VALUE)
3839
.put(MysqlSchemaCodegen.JSON_DATA_TYPE_ENABLED, JSON_DATA_TYPE_ENABLED_VALUE)
3940
.put(MysqlSchemaCodegen.IDENTIFIER_NAMING_CONVENTION, IDENTIFIER_NAMING_CONVENTION_VALUE)
41+
.put(MysqlSchemaCodegen.NAMED_PARAMETERS_ENABLED, NAMED_PARAMETERS_ENABLED_VALUE)
4042
.build();
4143
}
4244

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for '$special[model.name]' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `$special[model.name]`
9+
--
10+
SELECT `$special[property.name]` FROM `$special[model.name]` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `$special[model.name]`
14+
--
15+
INSERT INTO `$special[model.name]`(`$special[property.name]`) VALUES (?);
16+
17+
--
18+
-- UPDATE template for table `$special[model.name]`
19+
--
20+
UPDATE `$special[model.name]` SET `$special[property.name]` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `$special[model.name]`
24+
--
25+
DELETE FROM `$special[model.name]` WHERE 0;
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for '200_response' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `200_response`
9+
--
10+
SELECT `name`, `class` FROM `200_response` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `200_response`
14+
--
15+
INSERT INTO `200_response`(`name`, `class`) VALUES (?, ?);
16+
17+
--
18+
-- UPDATE template for table `200_response`
19+
--
20+
UPDATE `200_response` SET `name` = ?, `class` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `200_response`
24+
--
25+
DELETE FROM `200_response` WHERE 0;
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for 'AdditionalPropertiesAnyType' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `AdditionalPropertiesAnyType`
9+
--
10+
SELECT `name` FROM `AdditionalPropertiesAnyType` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `AdditionalPropertiesAnyType`
14+
--
15+
INSERT INTO `AdditionalPropertiesAnyType`(`name`) VALUES (?);
16+
17+
--
18+
-- UPDATE template for table `AdditionalPropertiesAnyType`
19+
--
20+
UPDATE `AdditionalPropertiesAnyType` SET `name` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `AdditionalPropertiesAnyType`
24+
--
25+
DELETE FROM `AdditionalPropertiesAnyType` WHERE 0;
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for 'AdditionalPropertiesArray' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `AdditionalPropertiesArray`
9+
--
10+
SELECT `name` FROM `AdditionalPropertiesArray` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `AdditionalPropertiesArray`
14+
--
15+
INSERT INTO `AdditionalPropertiesArray`(`name`) VALUES (?);
16+
17+
--
18+
-- UPDATE template for table `AdditionalPropertiesArray`
19+
--
20+
UPDATE `AdditionalPropertiesArray` SET `name` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `AdditionalPropertiesArray`
24+
--
25+
DELETE FROM `AdditionalPropertiesArray` WHERE 0;
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for 'AdditionalPropertiesBoolean' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `AdditionalPropertiesBoolean`
9+
--
10+
SELECT `name` FROM `AdditionalPropertiesBoolean` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `AdditionalPropertiesBoolean`
14+
--
15+
INSERT INTO `AdditionalPropertiesBoolean`(`name`) VALUES (?);
16+
17+
--
18+
-- UPDATE template for table `AdditionalPropertiesBoolean`
19+
--
20+
UPDATE `AdditionalPropertiesBoolean` SET `name` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `AdditionalPropertiesBoolean`
24+
--
25+
DELETE FROM `AdditionalPropertiesBoolean` WHERE 0;
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for 'AdditionalPropertiesClass' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `AdditionalPropertiesClass`
9+
--
10+
SELECT `map_string`, `map_number`, `map_integer`, `map_boolean`, `map_array_integer`, `map_array_anytype`, `map_map_string`, `map_map_anytype`, `anytype_1`, `anytype_2`, `anytype_3` FROM `AdditionalPropertiesClass` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `AdditionalPropertiesClass`
14+
--
15+
INSERT INTO `AdditionalPropertiesClass`(`map_string`, `map_number`, `map_integer`, `map_boolean`, `map_array_integer`, `map_array_anytype`, `map_map_string`, `map_map_anytype`, `anytype_1`, `anytype_2`, `anytype_3`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
16+
17+
--
18+
-- UPDATE template for table `AdditionalPropertiesClass`
19+
--
20+
UPDATE `AdditionalPropertiesClass` SET `map_string` = ?, `map_number` = ?, `map_integer` = ?, `map_boolean` = ?, `map_array_integer` = ?, `map_array_anytype` = ?, `map_map_string` = ?, `map_map_anytype` = ?, `anytype_1` = ?, `anytype_2` = ?, `anytype_3` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `AdditionalPropertiesClass`
24+
--
25+
DELETE FROM `AdditionalPropertiesClass` WHERE 0;
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- OpenAPI Petstore.
3+
-- Prepared SQL queries for 'AdditionalPropertiesInteger' definition.
4+
--
5+
6+
7+
--
8+
-- SELECT template for table `AdditionalPropertiesInteger`
9+
--
10+
SELECT `name` FROM `AdditionalPropertiesInteger` WHERE 1;
11+
12+
--
13+
-- INSERT template for table `AdditionalPropertiesInteger`
14+
--
15+
INSERT INTO `AdditionalPropertiesInteger`(`name`) VALUES (?);
16+
17+
--
18+
-- UPDATE template for table `AdditionalPropertiesInteger`
19+
--
20+
UPDATE `AdditionalPropertiesInteger` SET `name` = ? WHERE 1;
21+
22+
--
23+
-- DELETE template for table `AdditionalPropertiesInteger`
24+
--
25+
DELETE FROM `AdditionalPropertiesInteger` WHERE 0;
26+

0 commit comments

Comments
 (0)