"use strict";
exports.__esModule = true;
exports.dialect = exports.toProtoScalar = exports.toSchemaScalar = exports.joiBase = void 0;
var Joi = require("joi");
var cnst = require(".././const");
// defintions based on mysql 8 datatypes
// https://dev.mysql.com/doc/refman/8.0/en/data-types.html
exports.joiBase = function (type) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    switch (type) {
        // 11.1 Numeric Data Types
        case cnst.BIT:
        case cnst.TINYINT:
        case cnst.SMALLINT:
        case cnst.MEDIUMINT:
        case cnst.INT:
        case cnst.INTEGER:
            return Joi.number().integer();
        // BIGINT
        // DECIMAL
        // DEC
        // NUMERIC
        // FIXED
        // FLOAT
        // DOUBLE
        // DOUBLE PRECISION
        // REAL
        // 11.2 Date and Time Data Types
        // DATE
        // TIME
        // DATETIME
        // TIMESTAMP
        case cnst.YEAR:
            return Joi.number().integer();
        // 11.3 String Data Types
        // CHAR
        // NATIONAL CHAR
        // NCHAR
        // VARCHAR
        // NATIONAL VARCHAR
        // NVARCHAR
        // BINARY
        // VARBINARY
        // TINYBLOB
        // TINYTEXT
        // BLOB
        // TEXT
        // MEDIUMBLOB
        // MEDIUMTEXT
        // LONGBLOB
        // LONGTEXT
        // ENUM
        // SET
        // 11.4 Spatial Data Types
        case cnst.GEOMETRY:
            return (_a = Joi.string()).invalid.apply(_a, cnst.SYMBOLS_GEO_POLYGON);
        case cnst.POINT:
            return (_b = Joi.string()).invalid.apply(_b, cnst.SYMBOLS_GEO_POINT);
        case cnst.LINESTRING:
            return (_c = Joi.string()).invalid.apply(_c, cnst.SYMBOLS_GEO_LINE);
        case cnst.POLYGON:
            return (_d = Joi.string()).invalid.apply(_d, cnst.SYMBOLS_GEO_POLYGON);
        case cnst.MULTIPOINT:
            return (_e = Joi.string()).invalid.apply(_e, cnst.SYMBOLS_GEO_POINT);
        case cnst.MULTILINESTRING:
            return (_f = Joi.string()).invalid.apply(_f, cnst.SYMBOLS_GEO_LINE);
        case cnst.MULTIPOLYGON:
            return (_g = Joi.string()).invalid.apply(_g, cnst.SYMBOLS_GEO_POLYGON);
        case cnst.GEOMETRYCOLLECTION:
            return (_h = Joi.string()).invalid.apply(_h, cnst.SYMBOLS_GEO_POLYGON);
        // 11.5 The JSON Data Type
        // JSON
        case cnst.JSON:
        case cnst.JSONB:
            return Joi.string().invalid(cnst.SYMBOL_JSON); // will want to use JSONB on output
        default:
            return Joi.string();
    }
};
exports.toSchemaScalar = function (type) {
    switch (type) {
        // 11.1 Numeric Data Types
        case cnst.BIT:
        case cnst.TINYINT:
        case cnst.SMALLINT:
        case cnst.MEDIUMINT:
        case cnst.INT:
        case cnst.INTEGER:
            return "Float";
        // BIGINT
        // DECIMAL
        // DEC
        // NUMERIC
        // FIXED
        // FLOAT
        // DOUBLE
        // DOUBLE PRECISION
        // REAL
        // 11.2 Date and Time Data Types
        // DATE
        // TIME
        // DATETIME
        // TIMESTAMP
        case cnst.YEAR:
            return "Float";
        // 11.3 String Data Types
        // CHAR
        // NATIONAL CHAR
        // NCHAR
        // VARCHAR
        // NATIONAL VARCHAR
        // NVARCHAR
        // BINARY
        // VARBINARY
        // TINYBLOB
        // TINYTEXT
        // BLOB
        // TEXT
        // MEDIUMBLOB
        // MEDIUMTEXT
        // LONGBLOB
        // LONGTEXT
        // ENUM
        // SET
        // 11.4 Spatial Data Types
        // GEOMETRY
        // POINT
        // LINESTRING
        // POLYGON
        // MULTIPOINT
        // MULTILINESTRING
        // MULTIPOLYGON
        // GEOMETRYCOLLECTION
        case cnst.GEOMETRY:
        case cnst.POINT:
        case cnst.LINESTRING:
        case cnst.POLYGON:
        case cnst.MULTIPOINT:
        case cnst.MULTILINESTRING:
        case cnst.MULTIPOLYGON:
        case cnst.GEOMETRYCOLLECTION:
            return "JSONB";
        // 11.5 The JSON Data Type
        // JSON
        case cnst.JSON:
        case cnst.JSONB:
            return "JSONB";
        default:
            return "String";
    }
};
exports.toProtoScalar = function (type) {
    switch (type) {
        // 11.1 Numeric Data Types
        case cnst.BIT:
        case cnst.TINYINT:
        case cnst.SMALLINT:
        case cnst.MEDIUMINT:
        case cnst.INT:
        case cnst.INTEGER:
            return "uint32";
        // BIGINT
        // DECIMAL
        // DEC
        // NUMERIC
        // FIXED
        // FLOAT
        // DOUBLE
        // DOUBLE PRECISION
        // REAL
        // 11.2 Date and Time Data Types
        // DATE
        // TIME
        // DATETIME
        // TIMESTAMP
        case cnst.YEAR:
            return "uint32";
        // 11.3 String Data Types
        // CHAR
        // NATIONAL CHAR
        // NCHAR
        // VARCHAR
        // NATIONAL VARCHAR
        // NVARCHAR
        // BINARY
        // VARBINARY
        // TINYBLOB
        // TINYTEXT
        // BLOB
        // TEXT
        // MEDIUMBLOB
        // MEDIUMTEXT
        // LONGBLOB
        // LONGTEXT
        // ENUM
        // SET
        // 11.4 Spatial Data Types
        // GEOMETRY
        // POINT
        // LINESTRING
        // POLYGON
        // MULTIPOINT
        // MULTILINESTRING
        // MULTIPOLYGON
        // GEOMETRYCOLLECTION
        case cnst.GEOMETRY:
        case cnst.POINT:
        case cnst.LINESTRING:
        case cnst.POLYGON:
        case cnst.MULTIPOINT:
        case cnst.MULTILINESTRING:
        case cnst.MULTIPOLYGON:
        case cnst.GEOMETRYCOLLECTION:
            return "google.protobuf.Struct";
        // 11.5 The JSON Data Type
        // JSON
        case cnst.JSON:
        case cnst.JSONB:
            return "google.protobuf.Struct";
        default:
            return "string";
    }
};
exports.dialect = function (_a) {
    var migrationTable = _a.migrationTable;
    var dbSurveyQuery = "\n  SELECT\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 True\n      else False\n    end notnull,\n    detail.data_type \"type\",\n    case\n      stats.index_name\n      when 'PRIMARY' then 1\n      else 0\n    end as primarykey,\n    case\n      stats.non_unique\n      when 1 then 0\n      else 1\n    end as uniquekey\n  FROM\n    information_schema.tables main\n  JOIN (\n    select\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 ('sys',\n      'information_schema',\n      'mysql',\n      'performance_schema')\n    order by\n      col.table_schema,\n      col.table_name,\n      col.ordinal_position\n    ) detail\n      on\n      main.TABLE_SCHEMA = detail.TABLE_SCHEMA\n      and\n      main.TABLE_NAME = detail.TABLE_NAME\n  LEFT JOIN information_schema.statistics stats\n      on\n      detail.TABLE_SCHEMA = stats.index_schema\n      and\n      detail.TABLE_NAME = stats.table_name\n      and\n      detail.column_name = stats.column_name\n  WHERE\n    main.table_schema not in (\n      'sys',\n      'performance_schema',\n      'information_schema',\n      'mysql'\n    )\n    and\n    detail.TABLE_NAME not in ('" + migrationTable + "', '" + migrationTable + "_lock')\n  ORDER BY\n    detail.TABLE_SCHEMA,\n    detail.TABLE_NAME,\n    detail.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=mysql.js.map