mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-18 20:07:07 +00:00
[mysql] Add basic SQL queries (#5757)
* Add basic SQL queries template * Add namedParametersEnabled option * Move model related SQLs into Model folder * Update README template * Refresh samples
This commit is contained in:
@@ -20,10 +20,12 @@ import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.meta.features.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.io.File;
|
||||
|
||||
import static org.openapitools.codegen.utils.StringUtils.underscore;
|
||||
|
||||
@@ -35,6 +37,7 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
public static final String DEFAULT_DATABASE_NAME = "defaultDatabaseName";
|
||||
public static final String JSON_DATA_TYPE_ENABLED = "jsonDataTypeEnabled";
|
||||
public static final String IDENTIFIER_NAMING_CONVENTION = "identifierNamingConvention";
|
||||
public static final String NAMED_PARAMETERS_ENABLED = "namedParametersEnabled";
|
||||
public static final Integer ENUM_MAX_ELEMENTS = 65535;
|
||||
public static final Integer IDENTIFIER_MAX_LENGTH = 64;
|
||||
|
||||
@@ -58,6 +61,7 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
protected String tableNamePrefix = "tbl_", tableNameSuffix = "";
|
||||
protected String columnNamePrefix = "col_", columnNameSuffix = "";
|
||||
protected Boolean jsonDataTypeEnabled = true;
|
||||
protected Boolean namedParametersEnabled = false;
|
||||
protected String identifierNamingConvention = "original";
|
||||
|
||||
public MysqlSchemaCodegen() {
|
||||
@@ -81,6 +85,8 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
// clear import mapping (from default generator) as mysql does not use import directives
|
||||
importMapping.clear();
|
||||
|
||||
setModelPackage("Model");
|
||||
modelTemplateFiles.put("sql_query.mustache", ".sql");
|
||||
//modelTestTemplateFiles.put("model_test.mustache", ".php");
|
||||
// no doc files
|
||||
// modelDocTemplateFiles.clear();
|
||||
@@ -179,6 +185,7 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
cliOptions.clear();
|
||||
addOption(DEFAULT_DATABASE_NAME, "Default database name for all MySQL queries", defaultDatabaseName);
|
||||
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);
|
||||
addSwitch(NAMED_PARAMETERS_ENABLED, "Generates model prepared SQLs with named parameters, eg. :petName. Question mark placeholder used when option disabled.", namedParametersEnabled);
|
||||
|
||||
// we used to snake_case table/column names, let's add this option
|
||||
CliOption identifierNamingOpt = new CliOption(IDENTIFIER_NAMING_CONVENTION,
|
||||
@@ -226,10 +233,19 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
additionalProperties.put(JSON_DATA_TYPE_ENABLED, getJsonDataTypeEnabled());
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(NAMED_PARAMETERS_ENABLED)) {
|
||||
this.setNamedParametersEnabled(Boolean.valueOf(additionalProperties.get(NAMED_PARAMETERS_ENABLED).toString()));
|
||||
}
|
||||
|
||||
additionalProperties.put(NAMED_PARAMETERS_ENABLED, getNamedParametersEnabled());
|
||||
|
||||
if (additionalProperties.containsKey(IDENTIFIER_NAMING_CONVENTION)) {
|
||||
this.setIdentifierNamingConvention((String) additionalProperties.get(IDENTIFIER_NAMING_CONVENTION));
|
||||
}
|
||||
|
||||
// make model src path available in mustache template
|
||||
additionalProperties.put("modelSrcPath", "./" + toSrcPath(modelPackage));
|
||||
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
supportingFiles.add(new SupportingFile("mysql_schema.mustache", "", "mysql_schema.sql"));
|
||||
}
|
||||
@@ -1158,6 +1174,24 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
return this.jsonDataTypeEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables named parameters in prepared SQLs
|
||||
*
|
||||
* @param enabled true to enable, otherwise false
|
||||
*/
|
||||
public void setNamedParametersEnabled(Boolean enabled) {
|
||||
this.namedParametersEnabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether named parameters enabled or disabled in prepared SQLs
|
||||
*
|
||||
* @return true if enabled otherwise false
|
||||
*/
|
||||
public Boolean getNamedParametersEnabled() {
|
||||
return this.namedParametersEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets identifier naming convention for table names and column names.
|
||||
* This is not related to database name which is defined by defaultDatabaseName option.
|
||||
@@ -1184,4 +1218,22 @@ public class MysqlSchemaCodegen extends DefaultCodegen implements CodegenConfig
|
||||
return this.identifierNamingConvention;
|
||||
}
|
||||
|
||||
/**
|
||||
* Slightly modified version of AbstractPhpCodegen.toSrcPath method.
|
||||
*
|
||||
* @param packageName package name
|
||||
*
|
||||
* @return path
|
||||
*/
|
||||
public String toSrcPath(String packageName) {
|
||||
// Trim prefix file separators from package path
|
||||
String packagePath = StringUtils.removeStart(
|
||||
// Replace period, backslash, forward slash with file separator in package name
|
||||
packageName.replaceAll("[\\.\\\\/]", Matcher.quoteReplacement("/")),
|
||||
File.separator
|
||||
);
|
||||
|
||||
// Trim trailing file separators from the overall path
|
||||
return StringUtils.removeEnd(packagePath, File.separator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,3 +46,11 @@ Produced file(`mysql_schema.sql`) contains every table definition. Current imple
|
||||
1. Click **Import** link in left sidebar
|
||||
2. In **File upload** fieldset click to **Choose Files** and find generated `mysql_schema.sql`
|
||||
3. Push **Execute** button
|
||||
|
||||
### Prepared SQL queries
|
||||
|
||||
[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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
27
modules/openapi-generator/src/main/resources/mysql-schema/sql_query.mustache
vendored
Normal file
27
modules/openapi-generator/src/main/resources/mysql-schema/sql_query.mustache
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
--
|
||||
-- {{appName}}.{{#defaultDatabaseName}}
|
||||
-- Database: `{{{defaultDatabaseName}}}`{{/defaultDatabaseName}}
|
||||
-- Prepared SQL queries for {{#models}}{{#model}}'{{{name}}}'{{/model}}{{/models}} definition.
|
||||
--
|
||||
|
||||
{{#models}}{{#model}}
|
||||
--
|
||||
-- SELECT template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
|
||||
--
|
||||
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;
|
||||
|
||||
--
|
||||
-- INSERT template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
|
||||
--
|
||||
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}});
|
||||
|
||||
--
|
||||
-- UPDATE template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
|
||||
--
|
||||
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;
|
||||
|
||||
--
|
||||
-- DELETE template for table {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}}
|
||||
--
|
||||
DELETE FROM {{#vendorExtensions}}{{#x-mysqlSchema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{defaultDatabaseName}}}`.{{/defaultDatabaseName}}`{{tblName}}`{{/tableDefinition}}{{/x-mysqlSchema}}{{/vendorExtensions}} WHERE 0;
|
||||
{{/model}}{{/models}}
|
||||
@@ -274,6 +274,23 @@ public class MysqlSchemaCodegenTest {
|
||||
Assert.assertFalse(codegen.getJsonDataTypeEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNamedParametersEnabled() {
|
||||
final MysqlSchemaCodegen codegen = new MysqlSchemaCodegen();
|
||||
codegen.setNamedParametersEnabled(true);
|
||||
Assert.assertTrue(codegen.getNamedParametersEnabled());
|
||||
codegen.setNamedParametersEnabled(false);
|
||||
Assert.assertFalse(codegen.getNamedParametersEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamedParametersEnabled() {
|
||||
final MysqlSchemaCodegen codegen = new MysqlSchemaCodegen();
|
||||
Assert.assertFalse(codegen.getNamedParametersEnabled());
|
||||
codegen.setNamedParametersEnabled(true);
|
||||
Assert.assertTrue(codegen.getNamedParametersEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetIdentifierNamingConvention() {
|
||||
final MysqlSchemaCodegen codegen = new MysqlSchemaCodegen();
|
||||
|
||||
@@ -42,5 +42,6 @@ public class MysqlSchemaOptionsTest extends AbstractOptionsTest {
|
||||
verify(clientCodegen).setDefaultDatabaseName(MysqlSchemaOptionsProvider.DEFAULT_DATABASE_NAME_VALUE);
|
||||
verify(clientCodegen).setJsonDataTypeEnabled(Boolean.valueOf(MysqlSchemaOptionsProvider.JSON_DATA_TYPE_ENABLED_VALUE));
|
||||
verify(clientCodegen).setIdentifierNamingConvention(MysqlSchemaOptionsProvider.IDENTIFIER_NAMING_CONVENTION_VALUE);
|
||||
verify(clientCodegen).setNamedParametersEnabled(Boolean.valueOf(MysqlSchemaOptionsProvider.NAMED_PARAMETERS_ENABLED_VALUE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class MysqlSchemaOptionsProvider implements OptionsProvider {
|
||||
public static final String DEFAULT_DATABASE_NAME_VALUE = "database_name";
|
||||
public static final String JSON_DATA_TYPE_ENABLED_VALUE = "false";
|
||||
public static final String IDENTIFIER_NAMING_CONVENTION_VALUE = "snake_case";
|
||||
public static final String NAMED_PARAMETERS_ENABLED_VALUE = "true";
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
@@ -37,6 +38,7 @@ public class MysqlSchemaOptionsProvider implements OptionsProvider {
|
||||
return builder.put(MysqlSchemaCodegen.DEFAULT_DATABASE_NAME, DEFAULT_DATABASE_NAME_VALUE)
|
||||
.put(MysqlSchemaCodegen.JSON_DATA_TYPE_ENABLED, JSON_DATA_TYPE_ENABLED_VALUE)
|
||||
.put(MysqlSchemaCodegen.IDENTIFIER_NAMING_CONVENTION, IDENTIFIER_NAMING_CONVENTION_VALUE)
|
||||
.put(MysqlSchemaCodegen.NAMED_PARAMETERS_ENABLED, NAMED_PARAMETERS_ENABLED_VALUE)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user