Skip to content

Commit

Permalink
Updates to types and casting
Browse files Browse the repository at this point in the history
  • Loading branch information
codedmart committed Jan 7, 2025
1 parent 8ebdb7b commit aea3288
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 85 deletions.
4 changes: 2 additions & 2 deletions ndc-cli/src/main/kotlin/io/hasura/cli/MySQLConfigGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
61 changes: 8 additions & 53 deletions ndc-ir/src/main/kotlin/io/hasura/ndc/common/NDCScalars.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -133,4 +88,4 @@ fun javaSqlTypeToNDCScalar(sqlType: Int, precision: Int? = null, scale: Int? = n
// Default Fallback
else -> NDCScalar.JSON
}
}
}

0 comments on commit aea3288

Please sign in to comment.