"use strict";
exports.__esModule = true;
exports.dialect = exports.toProtoScalar = exports.toSchemaScalar = exports.joiBase = void 0;
var Joi = require("joi");
// definitions based on AWS Docs
// https://docs.aws.amazon.com/redshift/latest/dg/c_Supported_data_types.html
exports.joiBase = function (type) {
    switch (type) {
        case "smallint":
        case "int2":
        case "integer":
        case "int":
        case "int4":
            return Joi.number().integer();
        case "bigint":
        case "int8":
        case "decimal":
        case "numeric":
            return Joi.string();
        case "real":
            return Joi.number();
        case "float4":
        case "double precision":
        case "float8":
        case "float":
            return Joi.string();
        case "boolean":
        case "bool":
            return Joi.boolean();
        case "char":
        case "character":
        case "nchar":
        case "bpchar":
        case "varchar":
        case "character varying":
        case "nvarchar":
        case "text":
        case "date":
        case "timestamp":
        case "timestamp without time zone":
            return Joi.string();
        default:
            return Joi.string();
    }
};
exports.toSchemaScalar = function (type) {
    switch (type) {
        case "smallint":
        case "int2":
        case "integer":
        case "int":
        case "int4":
            return "Float";
        case "bigint":
        case "int8":
        case "decimal":
        case "numeric":
            return "String";
        case "real":
            return "Float";
        case "float4":
        case "double precision":
        case "float8":
        case "float":
            return "String";
        case "boolean":
        case "bool":
            return "Boolean";
        case "char":
        case "character":
        case "nchar":
        case "bpchar":
        case "varchar":
        case "character varying":
        case "nvarchar":
        case "text":
        case "date":
        case "timestamp":
        case "timestamp without time zone":
            return "String";
        default:
            return "String";
    }
};
exports.toProtoScalar = function (type) {
    switch (type) {
        case "smallint":
        case "int2":
        case "integer":
        case "int":
        case "int4":
            return "uint32";
        case "bigint":
        case "int8":
        case "decimal":
        case "numeric":
            return "string";
        case "real":
            return "string";
        case "float4":
        case "double precision":
        case "float8":
        case "float":
            return "string";
        case "boolean":
        case "bool":
            return "bool";
        case "char":
        case "character":
        case "nchar":
        case "bpchar":
        case "varchar":
        case "character varying":
        case "nvarchar":
        case "text":
        case "date":
        case "timestamp":
        case "timestamp without time zone":
            return "string";
        default:
            return "string";
    }
};
exports.dialect = function (_a) {
    var migrationTable = _a.migrationTable;
    var dbSurveyQuery = "\n        select distinct\n        resource_schema,\n        resource_type,\n        resource_name,\n        resource_column_id,\n        resource_column_name,\n        \"notnull\",\n        \"type\",\n        case\n            when primarykey >0 then true\n            else false\n        end as primarykey,\n        case\n            when uniquekey >0 then true\n            else false\n        end as uniquekey\n        from\n        (\n        select\n            \"oid\",\n            resource_schema,\n            resource_type,\n            resource_name,\n            resource_column_id,\n            resource_column_name,\n            \"notnull\",\n            \"type\",\n            max(primarykey) primarykey,\n            max(uniquekey) uniquekey\n        from\n            (\n            select\n                c.oid oid,\n                s.nspname resource_schema,\n                case\n                when c.relkind = 'r' then 'table'\n                when c.relkind = 'p' then 'partition'\n                when c.relkind = 'v' then 'view'\n                when c.relkind = 'm' then 'materialized view'\n                else 'unknown'\n                end as resource_type,\n                c.relname as resource_name,\n                a.attnum as resource_column_id,\n                a.attname as resource_column_name,\n                a.attnotnull as notnull,\n                pg_catalog.format_type(a.atttypid, a.atttypmod) as type,\n                case\n                when p.contype = 'p' then 1\n                else 0\n                end as primarykey,\n                case\n                when p.contype = 'u' then 1\n                else 0\n                end as uniquekey\n            from\n                pg_attribute a\n            join pg_class c on\n                c.oid = a.attrelid\n            join pg_namespace s on\n                c.relnamespace = s.oid\n            left join pg_attrdef d on\n                d.adrelid = c.oid\n                and d.adnum = a.attnum\n            left join pg_namespace n on\n                n.oid = c.relnamespace\n            left join pg_constraint p on\n                p.conrelid = c.oid\n                and a.attnum = any (p.conkey)\n            where\n                c.relkind in ('r', 'p', 'v', 'm') -- tables, views, materialized views\n                and a.attnum > 0\n                and not a.attisdropped\n                and s.nspname not in ('information_schema', 'pg_catalog')\n                and c.relname not in ('knex_migrations', 'knex_migrations_lock')\n                and c.relname not in ('" + migrationTable + "', '" + migrationTable + "_lock')\n            ) main\n        group by\n            \"oid\",\n            resource_schema,\n            resource_type,\n            resource_name,\n            resource_column_id,\n            resource_column_name,\n            \"notnull\",\n            \"type\"\n            ) base\n        order by\n        resource_schema,\n        resource_name,\n        resource_column_name;\n  ";
    var versionQuery = "select version() as db_version;";
    return {
        dbSurveyQuery: dbSurveyQuery,
        versionQuery: versionQuery,
        joiBase: exports.joiBase,
        toSchemaScalar: exports.toSchemaScalar,
        toProtoScalar: exports.toProtoScalar
    };
};
//# sourceMappingURL=redshift.js.map