Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/mysql-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sidebar_label: mysql-schema
|defaultDatabaseName|Default database name for all MySQL queries| ||
|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|
|jsonDataTypeEnabled|Use special JSON MySQL data type for complex model properties. Requires MySQL version 5.7.8. Generates TEXT data type when disabled| |true|
|namedParametersEnabled|Generates model prepared SQLs with named parameters, eg. :petName. Question mark placeholder used when option disabled.| |false|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
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;

Expand All @@ -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;

Expand All @@ -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() {
Expand All @@ -81,6 +85,8 @@ public MysqlSchemaCodegen() {
// 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();
Expand Down Expand Up @@ -179,6 +185,7 @@ public MysqlSchemaCodegen() {
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,
Expand Down Expand Up @@ -226,10 +233,19 @@ public void processOpts() {
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"));
}
Expand Down Expand Up @@ -1158,6 +1174,24 @@ public Boolean getJsonDataTypeEnabled() {
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.
Expand All @@ -1184,4 +1218,22 @@ public String getIdentifierNamingConvention() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ public void testGetJsonDataTypeEnabled() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ protected void verifyOptions() {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -37,6 +38,7 @@ public Map<String, String> createOptions() {
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();
}

Expand Down
26 changes: 26 additions & 0 deletions samples/schema/petstore/mysql/Model/$Special[modelName].sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for '$special[model.name]' definition.
--


--
-- SELECT template for table `$special[model.name]`
--
SELECT `$special[property.name]` FROM `$special[model.name]` WHERE 1;

--
-- INSERT template for table `$special[model.name]`
--
INSERT INTO `$special[model.name]`(`$special[property.name]`) VALUES (?);

--
-- UPDATE template for table `$special[model.name]`
--
UPDATE `$special[model.name]` SET `$special[property.name]` = ? WHERE 1;

--
-- DELETE template for table `$special[model.name]`
--
DELETE FROM `$special[model.name]` WHERE 0;

26 changes: 26 additions & 0 deletions samples/schema/petstore/mysql/Model/200Response.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for '200_response' definition.
--


--
-- SELECT template for table `200_response`
--
SELECT `name`, `class` FROM `200_response` WHERE 1;

--
-- INSERT template for table `200_response`
--
INSERT INTO `200_response`(`name`, `class`) VALUES (?, ?);

--
-- UPDATE template for table `200_response`
--
UPDATE `200_response` SET `name` = ?, `class` = ? WHERE 1;

--
-- DELETE template for table `200_response`
--
DELETE FROM `200_response` WHERE 0;

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for 'AdditionalPropertiesAnyType' definition.
--


--
-- SELECT template for table `AdditionalPropertiesAnyType`
--
SELECT `name` FROM `AdditionalPropertiesAnyType` WHERE 1;

--
-- INSERT template for table `AdditionalPropertiesAnyType`
--
INSERT INTO `AdditionalPropertiesAnyType`(`name`) VALUES (?);

--
-- UPDATE template for table `AdditionalPropertiesAnyType`
--
UPDATE `AdditionalPropertiesAnyType` SET `name` = ? WHERE 1;

--
-- DELETE template for table `AdditionalPropertiesAnyType`
--
DELETE FROM `AdditionalPropertiesAnyType` WHERE 0;

26 changes: 26 additions & 0 deletions samples/schema/petstore/mysql/Model/AdditionalPropertiesArray.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for 'AdditionalPropertiesArray' definition.
--


--
-- SELECT template for table `AdditionalPropertiesArray`
--
SELECT `name` FROM `AdditionalPropertiesArray` WHERE 1;

--
-- INSERT template for table `AdditionalPropertiesArray`
--
INSERT INTO `AdditionalPropertiesArray`(`name`) VALUES (?);

--
-- UPDATE template for table `AdditionalPropertiesArray`
--
UPDATE `AdditionalPropertiesArray` SET `name` = ? WHERE 1;

--
-- DELETE template for table `AdditionalPropertiesArray`
--
DELETE FROM `AdditionalPropertiesArray` WHERE 0;

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for 'AdditionalPropertiesBoolean' definition.
--


--
-- SELECT template for table `AdditionalPropertiesBoolean`
--
SELECT `name` FROM `AdditionalPropertiesBoolean` WHERE 1;

--
-- INSERT template for table `AdditionalPropertiesBoolean`
--
INSERT INTO `AdditionalPropertiesBoolean`(`name`) VALUES (?);

--
-- UPDATE template for table `AdditionalPropertiesBoolean`
--
UPDATE `AdditionalPropertiesBoolean` SET `name` = ? WHERE 1;

--
-- DELETE template for table `AdditionalPropertiesBoolean`
--
DELETE FROM `AdditionalPropertiesBoolean` WHERE 0;

26 changes: 26 additions & 0 deletions samples/schema/petstore/mysql/Model/AdditionalPropertiesClass.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for 'AdditionalPropertiesClass' definition.
--


--
-- SELECT template for table `AdditionalPropertiesClass`
--
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;

--
-- INSERT template for table `AdditionalPropertiesClass`
--
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

--
-- UPDATE template for table `AdditionalPropertiesClass`
--
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;

--
-- DELETE template for table `AdditionalPropertiesClass`
--
DELETE FROM `AdditionalPropertiesClass` WHERE 0;

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- OpenAPI Petstore.
-- Prepared SQL queries for 'AdditionalPropertiesInteger' definition.
--


--
-- SELECT template for table `AdditionalPropertiesInteger`
--
SELECT `name` FROM `AdditionalPropertiesInteger` WHERE 1;

--
-- INSERT template for table `AdditionalPropertiesInteger`
--
INSERT INTO `AdditionalPropertiesInteger`(`name`) VALUES (?);

--
-- UPDATE template for table `AdditionalPropertiesInteger`
--
UPDATE `AdditionalPropertiesInteger` SET `name` = ? WHERE 1;

--
-- DELETE template for table `AdditionalPropertiesInteger`
--
DELETE FROM `AdditionalPropertiesInteger` WHERE 0;

Loading