"use strict";
exports.__esModule = true;
exports.dialect = exports.toProtoScalar = exports.toSchemaScalar = exports.joiBase = void 0;
var Joi = require("joi");
// defintions based on sql-server 1 datatypes
// https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15
exports.joiBase = function (type) {
    switch (type) {
        // Exact numerics
        case "bigint":
        case "numeric":
            return Joi.string();
        case "bit":
        case "smallint":
            return Joi.number().integer();
        case "decimal":
        case "smallmoney":
            return Joi.string();
        case "int":
        case "tinyint":
            return Joi.number().integer();
        case "money":
            return Joi.string();
        // Approximate numerics
        case "float":
            return Joi.string();
        case "real":
            return Joi.number();
        // Date and time
        case "date":
        case "datetimeoffset":
        case "datetime2":
        case "smalldatetime":
        case "datetime":
        case "time":
            return Joi.string();
        // Character strings
        case "char":
        case "varchar":
        case "text":
            return Joi.string();
        // Unicode character strings
        case "nchar":
        case "nvarchar":
        case "ntext":
        case "Binary strings":
        case "binary":
        case "varbinary":
        case "image":
            return Joi.string();
        // Other data types
        case "cursor":
        case "rowversion":
        case "hierarchyid":
        case "uniqueidentifier":
        case "sql_variant":
        case "xml":
        case "table":
            return Joi.string();
        default:
            return Joi.string();
    }
};
exports.toSchemaScalar = function (type) {
    switch (type) {
        // Exact numerics
        case "bigint":
        case "numeric":
            return "String";
        case "bit":
        case "smallint":
            return "Float";
        case "decimal":
        case "smallmoney":
            return "String";
        case "int":
        case "tinyint":
            return "Float";
        case "money":
            return "String";
        // Approximate numerics
        case "float":
            return "String";
        case "real":
            return "Float";
        // Date and time
        case "date":
        case "datetimeoffset":
        case "datetime2":
        case "smalldatetime":
        case "datetime":
        case "time":
            return "String";
        // Character strings
        case "char":
        case "varchar":
        case "text":
            return "String";
        // Unicode character strings
        case "nchar":
        case "nvarchar":
        case "ntext":
        case "Binary strings":
        case "binary":
        case "varbinary":
        case "image":
            return "String";
        // Other data types
        case "cursor":
        case "rowversion":
        case "hierarchyid":
        case "uniqueidentifier":
        case "sql_variant":
        case "xml":
        case "table":
            return "String";
        default:
            return "String";
    }
};
exports.toProtoScalar = function (type) {
    switch (type) {
        // Exact numerics
        case "bigint":
        case "numeric":
            return "string";
        case "bit":
        case "smallint":
            return "uint32";
        case "decimal":
        case "smallmoney":
            return "string";
        case "int":
        case "tinyint":
            return "uint32";
        case "money":
            return "string";
        // Approximate numerics
        case "float":
            return "string";
        case "real":
            return "string";
        // Date and time
        case "date":
        case "datetimeoffset":
        case "datetime2":
        case "smalldatetime":
        case "datetime":
        case "time":
            return "string";
        // Character strings
        case "char":
        case "varchar":
        case "text":
            return "string";
        // Unicode character strings
        case "nchar":
        case "nvarchar":
        case "ntext":
        case "Binary strings":
        case "binary":
        case "varbinary":
        case "image":
            return "string";
        // Other data types
        case "cursor":
        case "rowversion":
        case "hierarchyid":
        case "uniqueidentifier":
        case "sql_variant":
        case "xml":
        case "table":
            return "string";
        default:
            return "string";
    }
};
exports.dialect = function (_a) {
    var migrationTable = _a.migrationTable;
    var dbSurveyQuery = "\n        with keys as (\n            SELECT\n                main.TABLE_CATALOG\n                ,main.TABLE_SCHEMA\n                ,main.TABLE_NAME\n                ,main.COLUMN_NAME\n                ,detail.CONSTRAINT_TYPE\n            FROM\n                INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE main\n            JOIN\n                INFORMATION_SCHEMA.TABLE_CONSTRAINTS detail\n                    on\n                        main.TABLE_CATALOG = detail.TABLE_CATALOG\n                        and\n                        main.TABLE_SCHEMA = detail.TABLE_SCHEMA\n                        and\n                        main.TABLE_NAME = detail.TABLE_NAME\n                        and\n                        main.CONSTRAINT_NAME = detail.CONSTRAINT_NAME\n            where\n                detail.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')\n        ),\n        survey as (\n            SELECT\n                detail.TABLE_CATALOG,\n                detail.TABLE_SCHEMA resource_schema,\n                case\n                when main.TABLE_TYPE = 'VIEW' then 'view'\n                else 'table'\n                end resource_type,\n                detail.TABLE_NAME resource_name,\n                detail.ordinal_position resource_column_id,\n                detail.column_name resource_column_name,\n                case\n                when detail.is_nullable = 'NO' then 1\n                else 0\n                end notnull,\n                detail.data_type \"type\"\n            FROM\n                information_schema.tables main\n            JOIN (\n                select\n                col.TABLE_CATALOG,\n                col.TABLE_SCHEMA,\n                col.TABLE_NAME,\n                col.ordinal_position,\n                col.column_name,\n                col.data_type,\n                case\n                    when col.character_maximum_length is not null then col.character_maximum_length\n                    else col.numeric_precision\n                end as max_length,\n                col.is_nullable\n                from\n                information_schema.columns col\n                where\n                col.table_schema not in (\n                    'sys',\n                    'information_schema'\n                )\n                ) detail\n                on\n                main.TABLE_SCHEMA = detail.TABLE_SCHEMA\n                and\n                main.TABLE_NAME = detail.TABLE_NAME\n            WHERE\n                main.table_schema not in (\n                'sys',\n                'information_schema'\n                )\n        )\n        select\n            survey.resource_schema\n            ,survey.resource_type\n            ,survey.resource_name\n            ,survey.resource_column_id\n            ,survey.resource_column_name\n            ,survey.notnull\n            ,survey.type\n            ,case\n            when keys.CONSTRAINT_TYPE = 'PRIMARY KEY' then 1\n            else 0\n            end primarykey\n            ,case\n            when keys.CONSTRAINT_TYPE = 'UNIQUE' then 1\n            else 0\n            end uniquekey\n        from\n            survey\n        left join keys\n            on\n                keys.TABLE_CATALOG = survey.TABLE_CATALOG\n                and\n                keys.TABLE_SCHEMA = survey.resource_schema\n                and\n                keys.TABLE_NAME = survey.resource_name\n                and\n                keys.COLUMN_NAME = survey.resource_column_name\n        order by\n            survey.resource_schema\n            ,survey.resource_type\n            ,survey.resource_name\n            ,survey.resource_column_id\n        ;\n    ";
    var versionQuery = "select @@version as db_version;";
    return {
        dbSurveyQuery: dbSurveyQuery,
        versionQuery: versionQuery,
        joiBase: exports.joiBase,
        toSchemaScalar: exports.toSchemaScalar,
        toProtoScalar: exports.toProtoScalar
    };
};
//# sourceMappingURL=mssql.js.map