diff --git a/ndc-cli/src/main/kotlin/io/hasura/cli/MySQLConfigGenerator.kt b/ndc-cli/src/main/kotlin/io/hasura/cli/MySQLConfigGenerator.kt index bced97e..8e5e862 100644 --- a/ndc-cli/src/main/kotlin/io/hasura/cli/MySQLConfigGenerator.kt +++ b/ndc-cli/src/main/kotlin/io/hasura/cli/MySQLConfigGenerator.kt @@ -25,7 +25,7 @@ object MySQLConfigGenerator : IConfigGenerator { //language=MySQL val sql = """ - SELECT + SELECT concat(tables.TABLE_SCHEMA, '.', tables.TABLE_NAME) AS TABLE_NAME, tables.TABLE_TYPE, tables.table_COMMENT as DESCRIPTION, @@ -45,7 +45,7 @@ object MySQLConfigGenerator : IConfigGenerator { 'name', columns.column_name, 'description', columns.column_comment, 'type', columns.data_type, - 'numeric_scale', columns.numeric_scale, + 'numeric_scale', coalesce(columns.numeric_scale, columns.numeric_precision), 'nullable', if (columns.is_nullable = 'yes', true, false), 'auto_increment', if(columns.extra = 'auto_increment',true,false), 'is_primarykey', if(columns.COLUMN_KEY = 'PRI', true, false) diff --git a/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/JSONGenerator.kt b/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/JSONGenerator.kt index f8ca5e8..503bc27 100644 --- a/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/JSONGenerator.kt +++ b/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/JSONGenerator.kt @@ -237,6 +237,7 @@ object JsonQueryGenerator : BaseQueryGenerator() { field.cast(SQLDataType.VARCHAR) NDCScalar.GEOMETRY, NDCScalar.GEOGRAPHY -> DSL.jsonObject(field.cast(SQLDataType.VARCHAR)) + NDCScalar.BOOLEAN -> field.cast(SQLDataType.BOOLEAN) else -> field } } @@ -292,6 +293,7 @@ object JsonQueryGenerator : BaseQueryGenerator() { ?: error("Column ${field.column} not found in table $collection") val scalarType = MySQLJDBCSchemaGenerator.mapScalarType(column.type, column.numeric_scale) + return Pair(ndcScalarTypeToSQLDataType(scalarType), scalarType) } diff --git a/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/MySQLJDBCSchemaGenerator.kt b/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/MySQLJDBCSchemaGenerator.kt index d351aa7..9f24766 100644 --- a/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/MySQLJDBCSchemaGenerator.kt +++ b/ndc-connector-mysql/src/main/kotlin/io/hasura/mysql/MySQLJDBCSchemaGenerator.kt @@ -251,45 +251,54 @@ object MySQLJDBCSchemaGenerator : JDBCSchemaGenerator() { ) } + // TODO: Proper types for MySQL rather then strings override fun mapScalarType(columnTypeStr: String, numericScale: Int?): NDCScalar { - return when (columnTypeStr.uppercase()) { - // Integer Types - "TINYINT" -> NDCScalar.INT8 - "SMALLINT" -> NDCScalar.INT16 - "MEDIUMINT" -> NDCScalar.INT32 - "INT", "INTEGER" -> NDCScalar.INT32 - "BIGINT" -> NDCScalar.INT64 + return when { + columnTypeStr.uppercase().startsWith("BIT") -> { + when (numericScale) { + 1 -> NDCScalar.BOOLEAN + else -> NDCScalar.BYTES + } + } + else -> when (columnTypeStr.uppercase()) { + // Integer Types + "TINYINT" -> NDCScalar.INT8 + "SMALLINT" -> NDCScalar.INT16 + "MEDIUMINT" -> NDCScalar.INT32 + "INT", "INTEGER" -> NDCScalar.INT32 + "BIGINT" -> NDCScalar.INT64 - // Floating-Point Types - "FLOAT" -> NDCScalar.FLOAT32 - "DOUBLE" -> NDCScalar.FLOAT64 + // Floating-Point Types + "FLOAT" -> NDCScalar.FLOAT32 + "DOUBLE" -> NDCScalar.FLOAT64 - // Numeric and Decimal Types - "DECIMAL", "NUMERIC" -> { - if (numericScale == 0) { - NDCScalar.BIGINTEGER // Integer-like numeric without scale - } else { - NDCScalar.BIGDECIMAL // Numeric with scale + // Numeric and Decimal Types + "DECIMAL", "NUMERIC" -> { + if (numericScale == 0) { + NDCScalar.BIGINTEGER // Integer-like numeric without scale + } else { + NDCScalar.BIGDECIMAL // Numeric with scale + } } - } - // Date and Time Types - "DATE" -> NDCScalar.DATE - "DATETIME" -> NDCScalar.TIMESTAMP - "TIME" -> NDCScalar.TIMESTAMP - "TIMESTAMP" -> NDCScalar.TIMESTAMPTZ + // Date and Time Types + "DATE" -> NDCScalar.DATE + "DATETIME" -> NDCScalar.TIMESTAMP + "TIME" -> NDCScalar.TIMESTAMP + "TIMESTAMP" -> NDCScalar.TIMESTAMPTZ - // String Types - "CHAR", "VARCHAR", "TEXT", "TINYTEXT", "MEDIUMTEXT", "LONGTEXT" -> NDCScalar.STRING + // String Types + "CHAR", "VARCHAR", "TEXT", "TINYTEXT", "MEDIUMTEXT", "LONGTEXT" -> NDCScalar.STRING - // Binary Types - "BLOB", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB" -> NDCScalar.BYTES + // Binary Types + "BLOB", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB" -> NDCScalar.BYTES - // JSON Type - "JSON" -> NDCScalar.JSON + // JSON Type + "JSON" -> NDCScalar.JSON - // Default Fallback - else -> NDCScalar.JSON + // Default Fallback + else -> NDCScalar.JSON + } } } } diff --git a/ndc-ir/src/main/kotlin/io/hasura/ndc/common/NDCScalars.kt b/ndc-ir/src/main/kotlin/io/hasura/ndc/common/NDCScalars.kt index dcdc7f6..f78ce62 100644 --- a/ndc-ir/src/main/kotlin/io/hasura/ndc/common/NDCScalars.kt +++ b/ndc-ir/src/main/kotlin/io/hasura/ndc/common/NDCScalars.kt @@ -26,61 +26,16 @@ enum class NDCScalar { fun toValue(): String = name.lowercase() } -fun javaSqlTypeToNDCScalar(sqlType: Int): NDCScalar { - return when (sqlType) { - // Boolean Types - java.sql.Types.BIT, - java.sql.Types.BOOLEAN -> NDCScalar.BOOLEAN - - // Integer Types - java.sql.Types.TINYINT -> NDCScalar.INT8 - java.sql.Types.SMALLINT -> NDCScalar.INT16 - java.sql.Types.INTEGER -> NDCScalar.INT32 - java.sql.Types.BIGINT -> NDCScalar.INT64 - - // Floating-Point Types - java.sql.Types.FLOAT, - java.sql.Types.REAL -> NDCScalar.FLOAT32 - java.sql.Types.DOUBLE -> NDCScalar.FLOAT64 - java.sql.Types.NUMERIC, - java.sql.Types.DECIMAL -> NDCScalar.BIGDECIMAL - - // String Types - java.sql.Types.CHAR, - java.sql.Types.VARCHAR, - java.sql.Types.LONGVARCHAR, - java.sql.Types.NCHAR, - java.sql.Types.NVARCHAR, - java.sql.Types.LONGNVARCHAR -> NDCScalar.STRING - - // Date and Time Types - java.sql.Types.DATE -> NDCScalar.DATE - java.sql.Types.TIME -> NDCScalar.TIMESTAMP - java.sql.Types.TIME_WITH_TIMEZONE -> NDCScalar.TIMESTAMPTZ - java.sql.Types.TIMESTAMP -> NDCScalar.TIMESTAMP - java.sql.Types.TIMESTAMP_WITH_TIMEZONE -> NDCScalar.TIMESTAMPTZ - - // Binary Types - java.sql.Types.BINARY, - java.sql.Types.VARBINARY, - java.sql.Types.LONGVARBINARY -> NDCScalar.BYTES - - // JSON Types - java.sql.Types.JAVA_OBJECT, - java.sql.Types.SQLXML, - java.sql.Types.OTHER, - java.sql.Types.STRUCT -> NDCScalar.JSON // Default to JSON, specific mappings need additional metadata - - // Default Fallback - else -> NDCScalar.JSON - } -} - fun javaSqlTypeToNDCScalar(sqlType: Int, precision: Int? = null, scale: Int? = null): NDCScalar { return when (sqlType) { - // Boolean Types - java.sql.Types.BIT, + // Boolean and Bit Types java.sql.Types.BOOLEAN -> NDCScalar.BOOLEAN + java.sql.Types.BIT -> { + when (precision) { + 1 -> NDCScalar.BOOLEAN + else -> NDCScalar.BYTES + } + } // Integer Types java.sql.Types.TINYINT -> NDCScalar.INT8 @@ -133,4 +88,4 @@ fun javaSqlTypeToNDCScalar(sqlType: Int, precision: Int? = null, scale: Int? = n // Default Fallback else -> NDCScalar.JSON } -} \ No newline at end of file +}