mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +00:00
Added PostgreSQL schema generator (BETA) (#20255)
* Initial version of PostgreSQL schema generator 'postgresql-schema' (based on 'mysql-schema' generator code) * PostgreSQL schema generator. Initial version. * Tested. Fixed errors. Documentation updated. * Samples for 'postgresql-schema' updated. * Removed current date/time from mustache templates for postgresql-schema generator. Re-created samples. * Re-created docs file for postgresql-schema generator. * Removed unecessary LocalTime computing code and imports for postgresql-schema generator. * Errors fixed for postgresql-schema generator. Samples recreated. * Docs updated.
This commit is contained in:
parent
e025a7ddfa
commit
dc175c5335
@ -1139,6 +1139,7 @@ Here is a list of template creators:
|
||||
* GraphQL: @wing328 [:heart:](https://www.patreon.com/wing328)
|
||||
* Ktorm: @Luiz-Monad
|
||||
* MySQL: [@ybelenko](https://github.com/ybelenko)
|
||||
* PostgreSQL: [@iri](https://github.com/iri)
|
||||
* Postman Collection: @gcatanese
|
||||
* Protocol Buffer: @wing328
|
||||
* WSDL: @adessoDpd
|
||||
|
5
bin/configs/postgresql-schema-petstore.yaml
Normal file
5
bin/configs/postgresql-schema-petstore.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
generatorName: postgresql-schema
|
||||
outputDir: samples/schema/petstore/postgresql
|
||||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
|
||||
templateDir: modules/openapi-generator/src/main/resources/postgresql-schema
|
||||
|
0
bin/utils/openapi-generator-cli.sh
Normal file → Executable file
0
bin/utils/openapi-generator-cli.sh
Normal file → Executable file
@ -168,6 +168,7 @@ The following generators are available:
|
||||
* [graphql-schema](generators/graphql-schema.md)
|
||||
* [ktorm-schema (beta)](generators/ktorm-schema.md)
|
||||
* [mysql-schema](generators/mysql-schema.md)
|
||||
* [postgresql-schema (beta)](generators/postgresql-schema.md)
|
||||
* [postman-collection (beta)](generators/postman-collection.md)
|
||||
* [protobuf-schema (beta)](generators/protobuf-schema.md)
|
||||
* [wsdl-schema (beta)](generators/wsdl-schema.md)
|
||||
|
1031
docs/generators/postgresql-schema.md
Normal file
1031
docs/generators/postgresql-schema.md
Normal file
File diff suppressed because it is too large
Load Diff
@ -30,7 +30,7 @@ public enum GeneratorLanguage {
|
||||
JAVASCRIPT("Javascript"), GRAPH_QL("GraphQL"), GROOVY("Groovy"),
|
||||
HASKELL("Haskell"), HTTP("Jetbrains HTTP Client (HTTP/REST)"), TYPESCRIPT("Typescript"), K_SIX("k6"), KOTLIN("Kotlin"),
|
||||
KTORM("Ktorm"), LUA("Lua"), MYSQL("Mysql"), NIM("Nim"),
|
||||
OBJECTIVE_C("Objective-C"), OCAML("OCaml"), PERL("Perl"), PHP("PHP"),
|
||||
OBJECTIVE_C("Objective-C"), OCAML("OCaml"), PERL("Perl"), PHP("PHP"), POSTGRESQL("Postgresql"),
|
||||
POWERSHELL("PowerShell"), PROTOBUF("Protocol Buffers (Protobuf)"), PYTHON("Python"),
|
||||
R("R"), RUBY("Ruby"), RUST("Rust"), SCALA("Scala"), SWIFT("Swift"),
|
||||
WSDL("Web Services Description Language (WSDL)"), JULIA("Julia"), XOJO("Xojo");
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -86,6 +86,7 @@ org.openapitools.codegen.languages.LuaClientCodegen
|
||||
org.openapitools.codegen.languages.MarkdownDocumentationCodegen
|
||||
org.openapitools.codegen.languages.JavaMicroprofileServerCodegen
|
||||
org.openapitools.codegen.languages.MysqlSchemaCodegen
|
||||
org.openapitools.codegen.languages.PostgresqlSchemaCodegen
|
||||
org.openapitools.codegen.languages.N4jsClientCodegen
|
||||
org.openapitools.codegen.languages.NimClientCodegen
|
||||
org.openapitools.codegen.languages.NodeJSExpressServerCodegen
|
||||
|
48
modules/openapi-generator/src/main/resources/postgresql-schema/README.mustache
vendored
Normal file
48
modules/openapi-generator/src/main/resources/postgresql-schema/README.mustache
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
# PostgreSQL Schema Codegen
|
||||
|
||||
Main goal of this generator is to provide PostgreSQL database DDL script that drops and then creates database objects for the given OpenAPI application
|
||||
|
||||
[PostgreSQL documentation](https://dev.postgresql.com/doc/)
|
||||
|
||||
## Requirements
|
||||
- PostgreSQL Server v9.4 or newer
|
||||
|
||||
## OpenAPI Data Type to PostgreSQL data type mapping
|
||||
|
||||
| OpenAPI data type | OpenAPI data format | Dependent properties | PostgreSQL data types | Default PostgreSQL data type |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| `integer` | `int32` | `minimum` / `maximum` / `minimumExclusive` / `maximumExclusive` | `SMALLINT` / `INT` / `BIGINT` | `INT` |
|
||||
| `integer` | `int64` | `minimum` / `maximum` / `minimumExclusive` / `maximumExclusive` | `SMALLINT` / `INT` / `BIGINT` | `BIGINT` |
|
||||
| `boolean` | | | `BOOLEAN` | `BOOLEAN` |
|
||||
| `number` | `float` | | `DECIMAL` | `DECIMAL` |
|
||||
| `number` | `double` | | `DECIMAL` | `DECIMAL` |
|
||||
| `string` | | `minLength` / `maxLength` | `VARCHAR` / `TEXT` | `TEXT` |
|
||||
| `string` | `byte` | | `BYTEA` | `BYTEA` |
|
||||
| `string` | `binary` | | `BYTEA` | `BYTEA` |
|
||||
| `file` | | | `BYTEA` | `BYTEA` |
|
||||
| `string` | `date` | | `DATE` | `DATE` |
|
||||
| `string` | `date-time` | | `TIMESTAMP` | `TIMESTAMP` |
|
||||
| `string` | `enum` | | `ENUM`<br>(via separate ENUM data type) | `ENUM`<br>(via separate ENUM data type) |
|
||||
| `array` | | | `JSON` / `JSONB` / `TEXT` | `JSON` |
|
||||
| `object` | | | `JSON` / `JSONB` / `TEXT` | `JSON` |
|
||||
| `\Model\User` (referenced definition) | | | `TEXT` | `TEXT` |
|
||||
|
||||
## How to use
|
||||
|
||||
Produced files:
|
||||
|
||||
1. `postgresql_schema.sql` that contains:
|
||||
|
||||
- `DROP ...` SQL statements for dropping every table and data type generated by this script;
|
||||
|
||||
- `CREATE ...` SQL statements for creating every table and data types for them (for `ENUM` types).
|
||||
|
||||
*Note: For safety reasons `DROP ...` SQL statements are commented out by default. Uncomment them before use.*
|
||||
|
||||
*Note: `ENUM` data types are implemented by creating separate data types first using command `CREATE TYPE` (one data type for each `ENUM` column). Then created data type is used as data type for table column.*
|
||||
|
||||
2. `postgresql_schema_oauth2.sql` that contains table creation commands for Oauth2-related tables.
|
||||
|
||||
3. [Model folder]({{modelSrcPath}}) contains files with sample SQL queries for each table. Copy-paste them, edit and use.
|
||||
|
||||
*Note: 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.*
|
66
modules/openapi-generator/src/main/resources/postgresql-schema/postgresql_schema.mustache
vendored
Normal file
66
modules/openapi-generator/src/main/resources/postgresql-schema/postgresql_schema.mustache
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
--
|
||||
-- Schema objects for PostgreSQL
|
||||
-- "{{appName}}"
|
||||
-- Created using 'openapi-generator' ('postgresql-schema' generator)
|
||||
-- (https://openapi-generator.tech/docs/generators/postgresql-schema)
|
||||
--
|
||||
|
||||
--
|
||||
-- DROP OBJECTS
|
||||
-- (remove comment prefix to start using DROP commands)
|
||||
--
|
||||
-- TABLES
|
||||
--
|
||||
{{#models}}{{#model}}{{#hasVars}}{{^isArray}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}-- DROP TABLE IF EXISTS {{#defaultDatabaseName}}{{{.}}}.{{/defaultDatabaseName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}};
|
||||
{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/isArray}}{{/hasVars}}{{/model}}{{/models}}
|
||||
--
|
||||
-- TYPES
|
||||
--
|
||||
{{#models}}{{#model}}{{#hasVars}}{{^isArray}}{{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#typeDefinition}}-- DROP TYPE IF EXISTS {{typeName}};
|
||||
{{/typeDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}}{{/isArray}}{{/hasVars}}{{/model}}{{/models}}
|
||||
|
||||
--
|
||||
-- CREATE OBJECTS
|
||||
--
|
||||
-- TYPES
|
||||
--
|
||||
{{#models}}{{#model}}{{#hasVars}}{{^isArray}}{{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#typeDefinition}}CREATE TYPE {{typeName}} AS ENUM{{#typeArguments}}{{#-first}}({{/-first}}{{#isString}}'{{/isString}}{{argumentValue}}{{#isString}}'{{/isString}}{{^-last}}, {{/-last}}{{#-last}});
|
||||
{{/-last}}{{/typeArguments}}{{/typeDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}}{{/isArray}}{{/hasVars}}{{/model}}{{/models}}
|
||||
--
|
||||
-- TABLES
|
||||
--
|
||||
{{#models}}{{#model}}{{#hasVars}}{{^isArray}}--
|
||||
-- Table {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}'{{tblName}}'{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}} generated from model '{{classVarName}}'
|
||||
{{#description}}
|
||||
-- {{.}}
|
||||
{{/description}}
|
||||
--
|
||||
{{#vendorExtensions}}
|
||||
{{#x-postgresql-schema}}
|
||||
{{#tableDefinition}}
|
||||
CREATE TABLE IF NOT EXISTS {{#defaultDatabaseName}}{{{.}}}.{{/defaultDatabaseName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}} (
|
||||
{{/tableDefinition}}
|
||||
{{/x-postgresql-schema}}
|
||||
{{/vendorExtensions}}
|
||||
{{#vars}}
|
||||
{{#vendorExtensions}}
|
||||
{{#x-postgresql-schema}}
|
||||
{{#columnDefinition}}
|
||||
{{#colNameQuoted}}"{{/colNameQuoted}}{{colName}}{{#colNameQuoted}}"{{/colNameQuoted}} {{colDataType}}{{#colDataTypeArguments}}{{#-first}}({{/-first}}{{#isString}}'{{/isString}}{{argumentValue}}{{#isString}}'{{/isString}}{{^-last}}, {{/-last}}{{#-last}}){{/-last}}{{/colDataTypeArguments}}{{#colNotNull}} NOT NULL{{/colNotNull}}{{#colDefault}} DEFAULT {{#isString}}'{{defaultValue}}'{{/isString}}{{^isString}}{{defaultValue}}{{/isString}}{{/colDefault}}{{^-last}},{{/-last}}
|
||||
{{/columnDefinition}}
|
||||
{{/x-postgresql-schema}}
|
||||
{{/vendorExtensions}}
|
||||
{{/vars}}
|
||||
{{#vendorExtensions}}
|
||||
{{#x-postgresql-schema}}
|
||||
{{#tableDefinition}}
|
||||
);
|
||||
{{/tableDefinition}}
|
||||
{{/x-postgresql-schema}}
|
||||
{{/vendorExtensions}}
|
||||
{{#vendorExtensions}}
|
||||
{{#x-postgresql-schema}}{{#tableDefinition}}{{#tblComment}}COMMENT ON TABLE {{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}} IS '{{.}}'{{/tblComment}};
|
||||
{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#columnDefinition}}{{#colComment}}COMMENT ON COLUMN {{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}.{{#colNameQuoted}}"{{/colNameQuoted}}{{colName}}{{#colNameQuoted}}"{{/colNameQuoted}} IS '{{.}}';
|
||||
{{/colComment}}{{/columnDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}}{{#vendorExtensions}}
|
||||
{{/vendorExtensions}}
|
||||
{{/isArray}}{{/hasVars}}{{/model}}{{/models}}
|
121
modules/openapi-generator/src/main/resources/postgresql-schema/postgresql_schema_oauth2.mustache
vendored
Normal file
121
modules/openapi-generator/src/main/resources/postgresql-schema/postgresql_schema_oauth2.mustache
vendored
Normal file
@ -0,0 +1,121 @@
|
||||
{{#hasOAuthMethods}}
|
||||
--
|
||||
-- OAuth2 framework tables for PostgreSQL
|
||||
-- Created using 'openapi-generator' ('postgresql-schema' generator)
|
||||
-- (https://openapi-generator.tech/docs/generators/postgresql-schema)
|
||||
--
|
||||
|
||||
--
|
||||
-- DROP TABLES
|
||||
-- (remove comment prefix to start using DROP commands)
|
||||
--
|
||||
-- DROP TABLE IF EXISTS oauth_clients;
|
||||
-- DROP TABLE IF EXISTS oauth_access_tokens;
|
||||
-- DROP TABLE IF EXISTS oauth_authorization_codes;
|
||||
-- DROP TABLE IF EXISTS oauth_refresh_tokens;
|
||||
-- DROP TABLE IF EXISTS oauth_users;
|
||||
-- DROP TABLE IF EXISTS oauth_scopes;
|
||||
-- DROP TABLE IF EXISTS oauth_jwt;
|
||||
-- DROP TABLE IF EXISTS oauth_jti;
|
||||
-- DROP TABLE IF EXISTS oauth_public_keys;
|
||||
|
||||
|
||||
--
|
||||
-- Table oauth_clients
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_clients (
|
||||
client_id VARCHAR(80) NOT NULL PRIMARY KEY,
|
||||
client_secret VARCHAR(80) DEFAULT NULL,
|
||||
redirect_uri VARCHAR(2000) DEFAULT NULL,
|
||||
grant_types VARCHAR(80) DEFAULT NULL,
|
||||
scope VARCHAR(4000) DEFAULT NULL,
|
||||
user_id VARCHAR(80) DEFAULT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_access_tokens
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_access_tokens (
|
||||
access_token VARCHAR(40) NOT NULL PRIMARY KEY,
|
||||
client_id VARCHAR(80) DEFAULT NULL,
|
||||
user_id VARCHAR(80) DEFAULT NULL,
|
||||
expires TIMESTAMP NOT NULL,
|
||||
scope VARCHAR(4000) DEFAULT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_authorization_codes
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_authorization_codes (
|
||||
authorization_code VARCHAR(40) NOT NULL PRIMARY KEY,
|
||||
client_id VARCHAR(80) DEFAULT NULL,
|
||||
user_id VARCHAR(80) DEFAULT NULL,
|
||||
redirect_uri VARCHAR(2000) NOT NULL,
|
||||
expires TIMESTAMP NOT NULL,
|
||||
scope VARCHAR(4000) DEFAULT NULL,
|
||||
id_token VARCHAR(1000) DEFAULT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_refresh_tokens
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_refresh_tokens (
|
||||
refresh_token VARCHAR(40) NOT NULL PRIMARY KEY,
|
||||
client_id VARCHAR(80),
|
||||
user_id VARCHAR(80),
|
||||
expires TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
scope VARCHAR(4000)
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_users
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_users (
|
||||
username VARCHAR(80) DEFAULT NULL,
|
||||
password VARCHAR(255) DEFAULT NULL,
|
||||
first_name VARCHAR(80) DEFAULT NULL,
|
||||
last_name VARCHAR(80) DEFAULT NULL,
|
||||
email VARCHAR(2000) DEFAULT NULL,
|
||||
email_verified SMALLINT DEFAULT NULL,
|
||||
scope VARCHAR(4000) DEFAULT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_scopes
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_scopes (
|
||||
scope VARCHAR(80) NOT NULL PRIMARY KEY,
|
||||
is_default SMALLINT DEFAULT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_jwt
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_jwt (
|
||||
client_id VARCHAR(80) NOT NULL,
|
||||
subject VARCHAR(80) DEFAULT NULL,
|
||||
public_key VARCHAR(2000) NOT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_jti
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_jti (
|
||||
issuer VARCHAR(80) NOT NULL,
|
||||
subject VARCHAR(80) DEFAULT NULL,
|
||||
audience VARCHAR(80) DEFAULT NULL,
|
||||
expires TIMESTAMP NOT NULL,
|
||||
jti VARCHAR(2000) NOT NULL
|
||||
);
|
||||
|
||||
--
|
||||
-- Table oauth_public_keys
|
||||
--
|
||||
CREATE TABLE IF NOT EXISTS oauth_public_keys (
|
||||
client_id VARCHAR(80) DEFAULT NULL,
|
||||
public_key VARCHAR(2000) DEFAULT NULL,
|
||||
private_key VARCHAR(2000) DEFAULT NULL,
|
||||
encryption_algorithm VARCHAR(100) DEFAULT 'RS256'
|
||||
);
|
||||
{{/hasOAuthMethods}}
|
||||
|
28
modules/openapi-generator/src/main/resources/postgresql-schema/query_examples.mustache
vendored
Normal file
28
modules/openapi-generator/src/main/resources/postgresql-schema/query_examples.mustache
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
--
|
||||
-- "{{appName}}"
|
||||
-- Prepared SQL queries for {{#models}}{{#model}}'{{{name}}}'{{/model}}{{/models}} definition.
|
||||
-- Created using 'openapi-generator' ('postgresql-schema' generator)
|
||||
-- (https://openapi-generator.tech/docs/generators/postgresql-schema)
|
||||
--
|
||||
|
||||
{{#models}}{{#model}}
|
||||
--
|
||||
-- SELECT template for table {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}'{{tblName}}'{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}
|
||||
--
|
||||
SELECT {{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#columnDefinition}}{{#colNameQuoted}}"{{/colNameQuoted}}{{colName}}{{#colNameQuoted}}"{{/colNameQuoted}}{{^-last}}, {{/-last}}{{/columnDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}} FROM {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}{{#defaultDatabaseName}}{{{.}}}.{{/defaultDatabaseName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}} WHERE 1=1;
|
||||
|
||||
--
|
||||
-- INSERT template for table {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}'{{tblName}}'{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}
|
||||
--
|
||||
INSERT INTO {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}{{#defaultDatabaseName}}{{{.}}}.{{/defaultDatabaseName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}} ({{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#columnDefinition}}{{#colNameQuoted}}"{{/colNameQuoted}}{{colName}}{{#colNameQuoted}}"{{/colNameQuoted}}{{^-last}}, {{/-last}}{{/columnDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}}) VALUES ({{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#columnDefinition}}{{#namedParametersEnabled}}:{{colName}}{{/namedParametersEnabled}}{{^namedParametersEnabled}}?{{/namedParametersEnabled}}{{^-last}}, {{/-last}}{{/columnDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}});
|
||||
|
||||
--
|
||||
-- UPDATE template for table {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}'{{tblName}}'{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}
|
||||
--
|
||||
UPDATE {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{.}}}`.{{/defaultDatabaseName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}} SET {{#vars}}{{#vendorExtensions}}{{#x-postgresql-schema}}{{#columnDefinition}}{{#colNameQuoted}}"{{/colNameQuoted}}{{colName}}{{#colNameQuoted}}"{{/colNameQuoted}} = {{#namedParametersEnabled}}:{{colName}}{{/namedParametersEnabled}}{{^namedParametersEnabled}}?{{/namedParametersEnabled}}{{^-last}}, {{/-last}}{{/columnDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}{{/vars}} WHERE 1=2;
|
||||
|
||||
--
|
||||
-- DELETE template for table {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}'{{tblName}}'{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}}
|
||||
--
|
||||
DELETE FROM {{#vendorExtensions}}{{#x-postgresql-schema}}{{#tableDefinition}}{{#defaultDatabaseName}}`{{{.}}}`.{{/defaultDatabaseName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{tblName}}{{#tblNameQuoted}}"{{/tblNameQuoted}}{{/tableDefinition}}{{/x-postgresql-schema}}{{/vendorExtensions}} WHERE 1=2;
|
||||
{{/model}}{{/models}}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.openapitools.codegen.options;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.openapitools.codegen.languages.PostgresqlSchemaCodegen;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class PostgresqlSchemaOptionsProvider implements OptionsProvider {
|
||||
public static final String DEFAULT_DATABASE_NAME_VALUE = "database_name";
|
||||
public static final String JSON_DATA_TYPE_VALUE = "json";
|
||||
public static final String IDENTIFIER_NAMING_CONVENTION_VALUE = "snake_case";
|
||||
public static final String NAMED_PARAMETERS_ENABLED_VALUE = "true";
|
||||
public static final String ID_AUTOINC_ENABLED_VALUE = "false";
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return "postgresql-schema";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> createOptions() {
|
||||
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
|
||||
return builder.put(PostgresqlSchemaCodegen.DEFAULT_DATABASE_NAME, DEFAULT_DATABASE_NAME_VALUE)
|
||||
.put(PostgresqlSchemaCodegen.JSON_DATA_TYPE, JSON_DATA_TYPE_VALUE)
|
||||
.put(PostgresqlSchemaCodegen.IDENTIFIER_NAMING_CONVENTION, IDENTIFIER_NAMING_CONVENTION_VALUE)
|
||||
.put(PostgresqlSchemaCodegen.NAMED_PARAMETERS_ENABLED, NAMED_PARAMETERS_ENABLED_VALUE)
|
||||
.put(PostgresqlSchemaCodegen.ID_AUTOINC_ENABLED, ID_AUTOINC_ENABLED_VALUE)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.openapitools.codegen.postgresql;
|
||||
|
||||
import org.openapitools.codegen.languages.PostgresqlSchemaCodegen;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
public class PostgresqlSchemaCodegenTest {
|
||||
|
||||
@Test
|
||||
public void testGetPostgresqlMatchedIntegerDataType() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(null, null, null), "INTEGER");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(-128L, 0L, false), "SMALLINT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(0L, 255L, false), "SMALLINT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(500L, 100L, null), "SMALLINT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(500L, 100L, false), "SMALLINT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(-32768L, 32767L, false), "SMALLINT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(0L, 65535L, false), "INTEGER");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(-8388608L, 0L, false), "INTEGER");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(0L, 16777215L, false), "INTEGER");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(Long.parseLong(String.valueOf(Integer.MIN_VALUE)),
|
||||
0L, false), "INTEGER");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(0L,
|
||||
Long.parseLong(String.valueOf(Integer.MAX_VALUE)), false), "INTEGER");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(0L, 4294967295L, false), "BIGINT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedIntegerDataType(-2147483649L, 0L, false), "BIGINT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPostgresqlMatchedStringDataType() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(6, 6), "VARCHAR");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(0, 0), "VARCHAR");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(255, 255), "VARCHAR");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(null, 100), "VARCHAR");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(null, 255), "VARCHAR");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(50, 255), "VARCHAR");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(100, 20), "VARCHAR");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(null, null), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(100, null), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(255, null), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(null, 256), "VARCHAR");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(16777215, null), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(16777215, 100), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(null, 16777215), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(100, 16777215), "TEXT");
|
||||
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(16777216, null), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(null, 16777216), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(16777216, 16777216), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(100, 16777216), "TEXT");
|
||||
Assert.assertSame(codegen.getPostgresqlMatchedStringDataType(100, Integer.MAX_VALUE), "TEXT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToCodegenPostgresqlDataTypeArgument() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
String strArgument = "HelloWorld";
|
||||
HashMap<String, Object> strProp = codegen.toCodegenPostgresqlDataTypeArgument(strArgument);
|
||||
Assert.assertTrue((Boolean) strProp.get("isString"));
|
||||
Assert.assertFalse((Boolean) strProp.get("isFloat"));
|
||||
Assert.assertFalse((Boolean) strProp.get("isInteger"));
|
||||
Assert.assertFalse((Boolean) strProp.get("isNumeric"));
|
||||
Assert.assertSame(strProp.get("argumentValue"), strArgument);
|
||||
|
||||
Integer intArgument = 10;
|
||||
HashMap<String, Object> intProp = codegen.toCodegenPostgresqlDataTypeArgument(intArgument);
|
||||
Assert.assertFalse((Boolean) intProp.get("isString"));
|
||||
Assert.assertFalse((Boolean) intProp.get("isFloat"));
|
||||
Assert.assertTrue((Boolean) intProp.get("isInteger"));
|
||||
Assert.assertTrue((Boolean) intProp.get("isNumeric"));
|
||||
Assert.assertSame(intProp.get("argumentValue"), intArgument);
|
||||
|
||||
Double floatArgument = 3.14;
|
||||
HashMap<String, Object> floatProp = codegen.toCodegenPostgresqlDataTypeArgument(floatArgument);
|
||||
Assert.assertFalse((Boolean) floatProp.get("isString"));
|
||||
Assert.assertTrue((Boolean) floatProp.get("isFloat"));
|
||||
Assert.assertFalse((Boolean) floatProp.get("isInteger"));
|
||||
Assert.assertTrue((Boolean) floatProp.get("isNumeric"));
|
||||
Assert.assertSame(floatProp.get("argumentValue"), floatArgument);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToCodegenPostgresqlDataTypeDefault() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
HashMap<String, Object> defaultMap = null;
|
||||
ArrayList<String> intFixture = new ArrayList<String>(Arrays.asList(
|
||||
"SMALLINT", "INTEGER", "BIGINT"));
|
||||
for (String intType : intFixture) {
|
||||
defaultMap = codegen.toCodegenPostgresqlDataTypeDefault("150", intType);
|
||||
Assert.assertTrue((Boolean) defaultMap.get("isNumeric"));
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isString"));
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isKeyword"));
|
||||
Assert.assertSame(defaultMap.get("defaultValue"), "150");
|
||||
}
|
||||
|
||||
ArrayList<String> dateFixture = new ArrayList<String>(Arrays.asList(
|
||||
"TIMESTAMP", "DATE"));
|
||||
for (String dateType : dateFixture) {
|
||||
defaultMap = codegen.toCodegenPostgresqlDataTypeDefault("2018-08-12", dateType);
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isNumeric"));
|
||||
Assert.assertTrue((Boolean) defaultMap.get("isString"));
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isKeyword"));
|
||||
Assert.assertSame(defaultMap.get("defaultValue"), "2018-08-12");
|
||||
}
|
||||
defaultMap = codegen.toCodegenPostgresqlDataTypeDefault("CURRENT_TIMESTAMP", "TIMESTAMP");
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isNumeric"));
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isString"));
|
||||
Assert.assertTrue((Boolean) defaultMap.get("isKeyword"));
|
||||
Assert.assertSame(defaultMap.get("defaultValue"), "CURRENT_TIMESTAMP");
|
||||
|
||||
defaultMap = codegen.toCodegenPostgresqlDataTypeDefault("CURRENT_DATE", "DATE");
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isNumeric"));
|
||||
Assert.assertFalse((Boolean) defaultMap.get("isString"));
|
||||
Assert.assertTrue((Boolean) defaultMap.get("isKeyword"));
|
||||
Assert.assertSame(defaultMap.get("defaultValue"), "CURRENT_DATE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPostgresqlDataType() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
ArrayList<String> trueFixture = new ArrayList<String>(Arrays.asList(
|
||||
"INTEGER", "Integer", "INT", "int", "Int", "TIMESTAMP", "timestamp", "TimeStamp", "VARCHAR", "varchar",
|
||||
"VarChar", "JSON", "json", "Json", "JSONB", "jsonb", "Jsonb"));
|
||||
ArrayList<String> falseFixture = new ArrayList<String>(Arrays.asList(
|
||||
"unknown", "HashMap", "HASHMAP", "hashmap"));
|
||||
for (String trueValue : trueFixture) {
|
||||
Assert.assertTrue(codegen.isPostgresqlDataType(trueValue),
|
||||
"'" + trueValue + "' isn't PostgreSQL data type");
|
||||
}
|
||||
for (String falseValue : falseFixture) {
|
||||
Assert.assertFalse(codegen.isPostgresqlDataType(falseValue),
|
||||
"'" + falseValue + "' is PostgreSQL data type");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToPostgresqlIdentifier() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
Assert.assertEquals(codegen.toPostgresqlIdentifier("table_name", "tbl_", ""), "table_name");
|
||||
Assert.assertEquals(codegen.toPostgresqlIdentifier("table_name ", "tbl_", ""), "table_name");
|
||||
Assert.assertEquals(codegen.toPostgresqlIdentifier("12345678", "tbl_", ""), "tbl_12345678");
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = RuntimeException.class)
|
||||
public void testToPostgresqlIdentifierWithEmptyString() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
codegen.toPostgresqlIdentifier(" ", "tbl_", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapePostgresqlUnquotedIdentifier() {
|
||||
final PostgresqlSchemaCodegen codegen = new PostgresqlSchemaCodegen();
|
||||
Assert.assertEquals(codegen.escapePostgresqlUnquotedIdentifier("table1Z$_"), "table1Z$_");
|
||||
Assert.assertEquals(codegen.escapePostgresqlUnquotedIdentifier("table1Z$_!#%~&?()*+-./"), "table1Z$_");
|
||||
Assert.assertEquals(codegen.escapePostgresqlUnquotedIdentifier("table1Z$_русскийтекст"),
|
||||
"table1Z$_русскийтекст");
|
||||
Assert.assertEquals(codegen.escapePostgresqlQuotedIdentifier("table𐀀"), "table");
|
||||
Assert.assertEquals(codegen.escapePostgresqlQuotedIdentifier("table_name!'()<29>"), "table_name!'()<29>");
|
||||
Assert.assertEquals(codegen.escapePostgresqlQuotedIdentifier("table_name𐌅𐌌 |