Add sql table to go struct tool

This commit is contained in:
Illya Marchenko 2024-04-15 15:11:54 +03:00
parent 3eea98c1c9
commit 4c80b42680
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
3 changed files with 163 additions and 0 deletions

@ -48,6 +48,7 @@ export default {
},
'GO': {
'go_json_to_struct': 'JSON to Go struct',
'sql_tables_to_struct': 'SQL tables Go struct',
},
'JSON': {
'json_formatter': 'JSON formatter',

@ -103,6 +103,11 @@ const router = createRouter({
name: 'go_json_to_struct',
component: () => import('../views/GoJSONToStruct.vue'),
},
{
path: '/sql_tables_to_struct',
name: 'sql_tables_to_struct',
component: () => import('../views/GoSQLTablesToStruct.vue'),
},
/**
* SQL manipulation

@ -0,0 +1,157 @@
<template>
<h2 class="tool-title">SQL tables Go struct</h2>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="result">MariaDB</label>
<MonacoEditor name="dialects_mariadb" language="sql" :value="toolData.dialects.mariadb" readonly="true" style="height: 500px"></MonacoEditor>
</div>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="result">PostgreSQL</label>
<MonacoEditor name="dialects_postgresql" language="sql" :value="toolData.dialects.postgresql" readonly="true" style="height: 500px"></MonacoEditor>
</div>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="result">OracleSQL</label>
<MonacoEditor name="dialects_oraclesql" language="sql" :value="toolData.dialects.oraclesql" readonly="true" style="height: 500px"></MonacoEditor>
</div>
</template>
<script>
import MonacoEditor from "@/components/MonacoEditor.vue";
export default {
components: {
MonacoEditor
},
data() {
return {
toolData: {
dialects: {
mariadb: `WITH models AS (
WITH data AS (
SELECT
REPLACE(CONCAT(UPPER(SUBSTRING(table_name, 1, 1)), LOWER(SUBSTRING(table_name, 2))), '_', '') AS table_name,
REPLACE(CONCAT(UPPER(SUBSTRING(column_name, 1, 1)), LOWER(SUBSTRING(column_name, 2))), '_', '') AS column_name,
CASE data_type
WHEN 'timestamp' THEN 'time.Time'
WHEN 'tinyint(1)' THEN 'bool'
WHEN 'bigint' THEN 'int64'
WHEN 'int' THEN 'int'
WHEN 'tinyint' THEN 'int8'
WHEN 'double' THEN 'float64'
WHEN 'decimal' THEN 'float32'
WHEN 'date' THEN 'time.Time'
WHEN 'char' THEN 'string'
WHEN 'varchar' THEN 'string'
WHEN 'text' THEN 'string'
WHEN 'mediumtext' THEN 'string'
WHEN 'longtext' THEN 'string'
WHEN 'enum' THEN 'string'
-- add your own type converters as needed or it will default to 'string'
ELSE CONCAT("UNKNOWN_", data_type)
END AS type_info,
CONCAT('\`json:"', column_name, '"\`') AS annotation
FROM information_schema.columns
WHERE table_schema IN ('work-euroline.local')
ORDER BY table_schema, table_name, ordinal_position
)
SELECT table_name, GROUP_CONCAT(CONCAT('\\t', column_name, '\\t', type_info, '\\t', annotation) SEPARATOR '\\n') AS fields
FROM data
GROUP BY table_name
)
SELECT CONCAT('type ', table_name, ' struct {\\n', fields, '\\n}') AS models
FROM models
ORDER BY table_name`,
postgresql: `WITH models AS (
WITH data AS (
SELECT
replace(initcap(table_name::text), '_', '') table_name,
replace(initcap(column_name::text), '_', '') column_name,
CASE data_type
WHEN 'timestamp without time zone' THEN 'time.Time'
WHEN 'timestamp with time zone' THEN 'time.Time'
WHEN 'boolean' THEN 'bool'
WHEN 'bigint' THEN 'int64'
WHEN 'integer' THEN 'int'
WHEN 'ARRAY' THEN 'pgtype.Array[string]'
WHEN 'date' THEN 'pgtype.Date'
WHEN 'character varying' THEN 'string'
-- add your own type converters as needed or it will default to 'string'
ELSE 'UNKNOWN'
END AS type_info,
'\`json:"' || column_name ||'"\`' AS annotation
FROM information_schema.columns
WHERE table_schema IN ('public')
ORDER BY table_schema, table_name, ordinal_position
)
SELECT table_name, STRING_AGG(E'\\t' || column_name || E'\\t' || type_info || E'\\t' || annotation, E'\\n') fields
FROM data
GROUP BY table_name
)
SELECT 'type ' || table_name || E' struct {\\n' || fields || E'\\n}' models
FROM models ORDER BY table_name`,
oraclesql: `SELECT
'type ' || table_name || ' struct {' || CHR(10) || fields || CHR(10) || '}' AS models
FROM
(
SELECT
table_name,
REPLACE(RTRIM(XMLAGG(XMLELEMENT(E, CHR(9) || column_name || CHR(9) || type_info || CHR(9) || annotation || CHR(10))).EXTRACT('//text()').GETCLOBVAL(), CHR(10)), '&quot;', '"') AS fields
FROM
(
SELECT
REPLACE(INITCAP(table_name), '_', '') AS table_name,
REPLACE(INITCAP(column_name), '_', '') AS column_name,
CASE data_type
WHEN 'TIMESTAMP' THEN 'time.Time'
WHEN 'TIMESTAMP(6)' THEN 'time.Time'
WHEN 'TIMESTAMP WITH TIME ZONE' THEN 'time.Time'
WHEN 'TIMESTAMP WITH LOCAL TIME ZONE' THEN 'time.Time'
WHEN 'LONG' THEN 'int64'
WHEN 'NUMBER' THEN
CASE
WHEN data_precision IS NULL AND data_scale = 0 THEN 'int'
WHEN data_precision IS NOT NULL AND data_scale = 0 THEN 'int64'
ELSE 'float64'
END
WHEN 'FLOAT' THEN 'float32'
WHEN 'BINARY_FLOAT' THEN 'float32'
WHEN 'BINARY_DOUBLE' THEN 'float64'
WHEN 'CHAR' THEN 'string'
WHEN 'VARCHAR2' THEN 'sql.NullString'
WHEN 'NCHAR' THEN 'string'
WHEN 'NVARCHAR2' THEN 'string'
WHEN 'CLOB' THEN 'go_ora.Clob'
WHEN 'NCLOB' THEN 'go_ora.Nlob'
WHEN 'BLOB' THEN 'go_ora.Blob'
WHEN 'DATE' THEN 'go_ora.NullTimeStamp'
WHEN 'RAW' THEN '[]byte'
WHEN 'UNDEFINED' THEN 'database.OraUNDEFINED'
-- add your own type converters as needed or it will default to 'string'
ELSE 'UNKNOWN'
END AS type_info,
'\`json:"' || column_name || '" db:"' || column_name || '"\`' AS annotation
FROM all_tab_columns
WHERE owner = 'DELOPRO'
)
--WHERE type_info = 'UNKNOWN'
GROUP BY table_name
)
--WHERE table_name = 'V8EventVw'
ORDER BY table_name`,
}
},
};
},
}
</script>
<style lang="scss">
</style>