"use strict";
exports.__esModule = true;
exports.dialect = exports.toProtoScalar = exports.toSchemaScalar = exports.joiBase = void 0;
var Joi = require("joi");
// defintions based on oracle 19 datatypes
// https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlqr/Data-Types.html#GUID-B6965FC9-3660-4849-BBD6-91725986EBD0
exports.joiBase = function (type) {
    switch (type) {
        case "NUMBER":
        case "FLOAT":
            return Joi.number();
        default:
            return Joi.string();
    }
};
exports.toSchemaScalar = function (type) {
    switch (type) {
        case "NUMBER":
        case "FLOAT":
            return "Float";
        default:
            return "String";
    }
};
exports.toProtoScalar = function (type) {
    switch (type) {
        case "NUMBER":
        case "FLOAT":
            return "sint32";
        default:
            return "string";
    }
};
exports.dialect = function (_a) {
    var migrationTable = _a.migrationTable;
    var dbSurveyQuery = ("\n\tWITH tables AS (\n\t\tSELECT\n\t\t\tcol.owner AS resource_schema\n\t\t\t,'table' resource_type\n\t\t\t,col.table_name resource_name\n\t\t\t,col.COLUMN_ID resource_column_id\n\t\t\t,col.column_name resource_column_name\n\t\t\t,CASE\n\t\t\t\tWHEN col.nullable <> 'Y' THEN 1\n\t\t\t\tELSE 0\n\t\t\tEND notnull\n\t\t\t,col.data_type type\n\t\t\t,col.data_precision\n\t\t\t,col.data_length\n\t\t\t,CASE\n\t\t\t\tWHEN pk.primary_key = 'PK' THEN 1\n\t\t\t\tELSE 0\n\t\t\tEND primarykey\n\t\t\t,CASE\n\t\t\t\tWHEN uk.unique_key = 'UK' THEN 1\n\t\t\t\tELSE 0\n\t\t\tEND uniquekey\n\t\tFROM\n\t\t\tall_tables tab\n\t\tINNER JOIN all_tab_columns col ON\n\t\t\tcol.owner = tab.owner\n\t\t\tAND col.table_name = tab.table_name\n\t\tLEFT JOIN all_col_comments comm ON\n\t\t\tcol.owner = comm.owner\n\t\t\tAND col.table_name = comm.table_name\n\t\t\tAND col.column_name = comm.column_name\n\t\tLEFT JOIN (\n\t\t\tSELECT\n\t\t\t\tconstr.owner,\n\t\t\t\tcol_const.table_name,\n\t\t\t\tcol_const.column_name,\n\t\t\t\t'PK' primary_key\n\t\t\tFROM\n\t\t\t\tall_constraints constr\n\t\t\tINNER JOIN all_cons_columns col_const ON\n\t\t\t\tconstr.constraint_name = col_const.constraint_name\n\t\t\t\tAND col_const.owner = constr.owner\n\t\t\tWHERE\n\t\t\t\tconstr.constraint_type = 'P') pk ON\n\t\t\tcol.table_name = pk.table_name\n\t\t\tAND col.column_name = pk.column_name\n\t\t\tAND col.owner = pk.owner\n\t\tLEFT JOIN (\n\t\t\tSELECT\n\t\t\t\tconstr.owner,\n\t\t\t\tcol_const.table_name,\n\t\t\t\tcol_const.column_name,\n\t\t\t\t'FK' foreign_key\n\t\t\tFROM\n\t\t\t\tall_constraints constr\n\t\t\tINNER JOIN all_cons_columns col_const ON\n\t\t\t\tconstr.constraint_name = col_const.constraint_name\n\t\t\t\tAND col_const.owner = constr.owner\n\t\t\tWHERE\n\t\t\t\tconstr.constraint_type = 'R'\n\t\t\tGROUP BY\n\t\t\t\tconstr.owner,\n\t\t\t\tcol_const.table_name,\n\t\t\t\tcol_const.column_name) fk ON\n\t\t\tcol.table_name = fk.table_name\n\t\t\tAND col.column_name = fk.column_name\n\t\t\tAND col.owner = fk.owner\n\t\tLEFT JOIN (\n\t\t\tSELECT\n\t\t\t\tconstr.owner,\n\t\t\t\tcol_const.table_name,\n\t\t\t\tcol_const.column_name,\n\t\t\t\t'UK' unique_key\n\t\t\tFROM\n\t\t\t\tall_constraints constr\n\t\t\tINNER JOIN all_cons_columns col_const ON\n\t\t\t\tconstr.constraint_name = col_const.constraint_name\n\t\t\t\tAND constr.owner = col_const.owner\n\t\t\tWHERE\n\t\t\t\tconstr.constraint_type = 'U'\n\t\tUNION\n\t\t\tSELECT\n\t\t\t\tind.owner,\n\t\t\t\tcol_ind.table_name,\n\t\t\t\tcol_ind.column_name,\n\t\t\t\t'UK' unique_key\n\t\t\tFROM\n\t\t\t\tall_indexes ind\n\t\t\tINNER JOIN all_ind_columns col_ind ON\n\t\t\t\tind.index_name = col_ind.index_name\n\t\t\tWHERE\n\t\t\t\tind.uniqueness = 'UNIQUE') uk ON\n\t\t\tcol.table_name = uk.table_name\n\t\t\tAND col.column_name = uk.column_name\n\t\t\tAND col.owner = uk.owner\n\t\tLEFT JOIN (\n\t\t\tSELECT\n\t\t\t\tconstr.owner,\n\t\t\t\tcol_const.table_name,\n\t\t\t\tcol_const.column_name,\n\t\t\t\t'Check' check_constraint\n\t\t\tFROM\n\t\t\t\tall_constraints constr\n\t\t\tINNER JOIN all_cons_columns col_const ON\n\t\t\t\tconstr.constraint_name = col_const.constraint_name\n\t\t\t\tAND col_const.owner = constr.owner\n\t\t\tWHERE\n\t\t\t\tconstr.constraint_type = 'C'\n\t\t\tGROUP BY\n\t\t\t\tconstr.owner,\n\t\t\t\tcol_const.table_name,\n\t\t\t\tcol_const.column_name) check_const ON\n\t\t\tcol.table_name = check_const.table_name\n\t\t\tAND col.column_name = check_const.column_name\n\t\t\tAND col.owner = check_const.owner\n\t),\n\tviews AS (\n\t\tSELECT\n\t\t\tcol.owner AS resource_schema,\n\t\t\t'view' resource_type,\n\t\t\tcol.table_name resource_name,\n\t\t\tcol.COLUMN_ID resource_column_id,\n\t\t\tcol.column_name resource_column_name,\n\t\tCASE\n\t\t\t\tWHEN col.nullable <> 'Y' THEN 1\n\t\t\t\tELSE 0\n\t\t\tEND notnull,\n\t\t\tcol.data_type type,\n\t\t\tcol.data_precision,\n\t\t\tcol.data_length,\n\t\t\t0 primarykey,\n\t\t\t0 uniquekey\n\t\tFROM\n\t\t\tall_views v\n\t\tINNER JOIN all_tab_columns col ON\n\t\t\tv.view_name = col.table_name\n\t\tINNER JOIN all_col_comments comm ON\n\t\t\tcol.table_name = comm.table_name\n\t\t\tAND col.owner = comm.owner\n\t\t\tAND col.column_name = comm.column_name\n\t),\n\tmat_views AS (\n\t\tSELECT\n\t\t\tcol.owner AS resource_schema\n\t\t\t,'materialized view' resource_type\n\t\t\t,col.table_name resource_name\n\t\t\t,col.COLUMN_ID resource_column_id\n\t\t\t,col.column_name resource_column_name\n\t\t\t,CASE\n\t\t\t\tWHEN col.nullable <> 'Y' THEN 1\n\t\t\t\tELSE 0\n\t\t\tEND notnull\n\t\t\t,col.data_type type\n\t\t\t,col.data_precision\n\t\t\t,col.data_length\n\t\t\t,0 primarykey\n\t\t\t,0 uniquekey\n\t\tFROM\n\t\t\tall_mviews v\n\t\tINNER JOIN all_tab_columns col ON\n\t\t\tv.mview_name = col.table_name\n\t\tINNER JOIN all_col_comments comm ON\n\t\t\tcol.table_name = comm.table_name\n\t\t\tAND col.owner = comm.owner\n\t\t\tAND col.column_name = comm.column_name\n\t),\n\tresources AS (\n\t\tSELECT * FROM tables\n\t\tUNION\n\t\tSELECT * FROM views\n\t\tUNION\n\t\tSELECT * FROM mat_views\n\t)\n\tSELECT\n\t\t resource_schema \"resource_schema\"\n\t\t,resource_type \"resource_type\"\n\t\t,resource_name \"resource_name\"\n\t\t,resource_column_id \"resource_column_id\"\n\t\t,resource_column_name \"resource_column_name\"\n\t\t,notnull \"notnull\"\n\t\t,type \"type\"\n\t\t,data_precision \"data_precision\"\n\t\t,data_length \"data_length\"\n\t\t,primarykey \"primarykey\"\n\t\t,uniquekey \"uniquekey\"\n\tFROM resources\n\tWHERE\n\t\tresource_schema NOT IN (\n\t\t\t 'ANONYMOUS'\n\t\t\t,'APEX_040000'\n\t\t\t,'APEX_PUBLIC_USER'\n\t\t\t,'APPQOSSYS'\n\t\t\t,'AUDSYS'\n\t\t\t,'CTXSYS'\n\t\t\t,'DBSFWUSER'\n\t\t\t,'DBSNMP'\n\t\t\t,'DIP'\n\t\t\t,'DVSYS'\n\t\t\t,'EXFSYS'\n\t\t\t,'FLOWS_30000'\n\t\t\t,'FLOWS_FILES'\n\t\t\t,'GSMADMIN_INTERNAL'\n\t\t\t,'LBACSYS'\n\t\t\t,'MDDATA'\n\t\t\t,'MDSYS'\n\t\t\t,'MGMT_VIEW'\n\t\t\t,'OJVMSYS'\n\t\t\t,'OLAPSYS'\n\t\t\t,'ORACLE_OCM'\n\t\t\t,'ORDDATA'\n\t\t\t,'ORDPLUGINS'\n\t\t\t,'ORDSYS'\n\t\t\t,'OUTLN'\n\t\t\t,'OWBSYS'\n\t\t\t,'PUBLIC'\n\t\t\t,'SI_INFORMTN_SCHEMA'\n\t\t\t,'SPATIAL_CSW_ADMIN_USR'\n\t\t\t,'SPATIAL_WFS_ADMIN_USR'\n\t\t\t,'SYS'\n\t\t\t,'SYSMAN'\n\t\t\t,'SYSTEM'\n\t\t\t,'TSMSYS'\n\t\t\t,'WKPROXY'\n\t\t\t,'WKSYS'\n\t\t\t,'WK_TEST'\n\t\t\t,'WMSYS'\n\t\t\t,'XDB'\n\t\t\t,'XS$NULL'\n\t\t\t,'" + migrationTable + "', '" + migrationTable + "_lock'\n\t\t)\n\tORDER BY\n\t\tresource_schema\n\t\t,resource_type\n\t\t,resource_name\n\t\t,resource_column_id\n  ").replace(/[\n\r\t]/g, " ");
    var versionQuery = "SELECT BANNER_FULL \"db_version\" FROM v$version";
    return {
        dbSurveyQuery: dbSurveyQuery,
        versionQuery: versionQuery,
        joiBase: exports.joiBase,
        toSchemaScalar: exports.toSchemaScalar,
        toProtoScalar: exports.toProtoScalar
    };
};
//# sourceMappingURL=oracle.js.map