Skip to content

Instantly share code, notes, and snippets.

@salesHgabriel
Created July 2, 2024 20:36
Show Gist options
  • Save salesHgabriel/9cb42662a6b010abd5e7855b5c0f3116 to your computer and use it in GitHub Desktop.
Save salesHgabriel/9cb42662a6b010abd5e7855b5c0f3116 to your computer and use it in GitHub Desktop.
Postgresql sql to Class C#
DO $$
DECLARE
tableName text := 'car_atp';
result text := 'public class ' || tableName || ' {';
columnName text;
columnType text;
nullableSign text;
BEGIN
FOR columnName, columnType, nullableSign IN
SELECT
column_name,
CASE
WHEN data_type = 'bigint' THEN 'long'
WHEN data_type = 'bytea' THEN 'byte[]'
WHEN data_type = 'boolean' THEN 'bool'
WHEN data_type = 'character' THEN 'string'
WHEN data_type = 'date' THEN 'DateTime'
WHEN data_type = 'timestamp with time zone' THEN 'DateTimeOffset'
WHEN data_type = 'timestamp without time zone' THEN 'DateTime'
WHEN data_type = 'numeric' THEN 'decimal'
WHEN data_type = 'double precision' THEN 'double'
WHEN data_type = 'integer' THEN 'int'
WHEN data_type = 'money' THEN 'decimal'
WHEN data_type = 'character varying' THEN 'string'
WHEN data_type = 'real' THEN 'float'
WHEN data_type = 'smallint' THEN 'short'
WHEN data_type = 'text' THEN 'string'
WHEN data_type = 'time without time zone' THEN 'TimeSpan'
WHEN data_type = 'uuid' THEN 'Guid'
ELSE 'UNKNOWN_' || data_type
END,
CASE
WHEN is_nullable = 'YES' AND data_type IN ('bigint', 'boolean', 'date', 'timestamp without time zone', 'timestamp with time zone', 'numeric', 'double precision', 'integer', 'money', 'real', 'smallint', 'time without time zone', 'uuid')
THEN '?'
ELSE ''
END
FROM information_schema.columns
WHERE table_name = tableName
ORDER BY ordinal_position
LOOP
result := result || E'\n public ' || columnType || nullableSign || ' ' || columnName || ' { get; set; }';
END LOOP;
result := result || E'\n}';
RAISE NOTICE '%', result;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment