-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dispatch generator DS on JDBC url. Cleanup
- Loading branch information
Showing
8 changed files
with
342 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
generator/src/ba/sake/squery/generator/DbDefExtractor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package ba.sake.squery.generator | ||
|
||
import java.sql.{Array => _, _} | ||
import javax.sql.DataSource | ||
import scala.util._ | ||
import scala.util.chaining._ | ||
import scala.collection.mutable.ArrayBuffer | ||
import org.apache.commons.lang3.StringUtils | ||
import org.apache.commons.text.CaseUtils | ||
|
||
object DbDefExtractor { | ||
def apply(ds: DataSource): DbDefExtractor = | ||
Using.resource(ds.getConnection()) { connection => | ||
val databaseMetaData = connection.getMetaData() | ||
val dbName = databaseMetaData.getDatabaseProductName().toLowerCase | ||
dbName match { | ||
case "postgres" => new PostgresDefExtractor(ds) | ||
case _ => new JdbcDefExtractor(ds) | ||
} | ||
} | ||
} | ||
|
||
abstract class DbDefExtractor(ds: DataSource) { | ||
|
||
def extract(): DbDef = Using.resource(ds.getConnection()) { connection => | ||
val databaseMetaData = connection.getMetaData() | ||
val dbName = databaseMetaData.getDatabaseProductName().toLowerCase | ||
val schemaNames = Using.resource(databaseMetaData.getSchemas()) { rs => | ||
val buff = ArrayBuffer.empty[String] | ||
while (rs.next()) { | ||
buff += rs.getString("TABLE_SCHEM") | ||
} | ||
buff.toSeq | ||
} | ||
val schemaDefs = schemaNames.map { schemaName => | ||
val tables = extractTables(connection, schemaName, databaseMetaData) | ||
SchemaDef(name = schemaName, tables = tables) | ||
} | ||
DbDef( | ||
name = dbName, | ||
schemas = schemaDefs | ||
) | ||
} | ||
|
||
// (table, column) -> ColumnType | ||
protected def getColumnTypes( | ||
connection: Connection, | ||
schemaName: String, | ||
columnsMetadatas: Seq[ColumnMetadata] | ||
): Map[(String, String), ColumnType] | ||
|
||
private def extractTables( | ||
connection: Connection, | ||
schemaName: String, | ||
databaseMetaData: DatabaseMetaData | ||
): Seq[TableDef] = { | ||
|
||
val allColumnsMetadatas = extractColumnMetadatas(databaseMetaData, schemaName) | ||
val allColumnTypes = getColumnTypes(connection, schemaName, allColumnsMetadatas) | ||
val allColumnDefs = allColumnsMetadatas.map { cMeta => | ||
val resolvedType = allColumnTypes((cMeta.table, cMeta.name)) | ||
ColumnDef(cMeta, resolvedType) | ||
} | ||
|
||
Using.resource(databaseMetaData.getTables(null, schemaName, null, Array("TABLE"))) { tablesRS => | ||
val tableDefsRes = ArrayBuffer.empty[TableDef] | ||
while (tablesRS.next()) { | ||
val tableName = tablesRS.getString("TABLE_NAME") | ||
val tableColumnDefs = allColumnDefs.filter(_.metadata.table == tableName) | ||
val pkColumns = Using.resource(databaseMetaData.getPrimaryKeys(null, schemaName, tableName)) { pksRS => | ||
val tableDefsRes = ArrayBuffer.empty[ColumnDef] | ||
while (pksRS.next()) { | ||
val pkColName = pksRS.getString("COLUMN_NAME") | ||
tableDefsRes += tableColumnDefs | ||
.find(_.metadata.name == pkColName) | ||
.getOrElse(throw new RuntimeException(s"PK column not found: ${pkColName}")) | ||
} | ||
tableDefsRes.toSeq | ||
} | ||
tableDefsRes += TableDef(schemaName, tableName, tableColumnDefs, pkColumns) | ||
} | ||
tableDefsRes.toSeq | ||
} | ||
} | ||
|
||
private def extractColumnMetadatas( | ||
databaseMetaData: DatabaseMetaData, | ||
schemaName: String | ||
): Seq[ColumnMetadata] = { | ||
val res = ArrayBuffer.empty[ColumnMetadata] | ||
Using.resource(databaseMetaData.getColumns(null, schemaName, null, null)) { resultSet => | ||
while (resultSet.next()) { | ||
val tableName = resultSet.getString("TABLE_NAME") | ||
val columnName = resultSet.getString("COLUMN_NAME") | ||
val typeName = resultSet.getString("TYPE_NAME") | ||
val jdbcType = resultSet.getInt("DATA_TYPE") // java.sql.Types | ||
val isNullable = resultSet.getString("IS_NULLABLE") == "YES" | ||
val isAutoInc = resultSet.getString("IS_AUTOINCREMENT") == "YES" | ||
val isGenerated = resultSet.getString("IS_GENERATEDCOLUMN") == "YES" | ||
val defaultValue = Option(resultSet.getString("COLUMN_DEF")) | ||
res += ColumnMetadata( | ||
schemaName, | ||
tableName, | ||
columnName, | ||
jdbcType, | ||
isNullable, | ||
isAutoInc, | ||
isGenerated, | ||
defaultValue | ||
) | ||
} | ||
} | ||
res.toSeq | ||
} | ||
|
||
// test utils | ||
protected def printAll(resultSet: ResultSet) = { | ||
val metadata = resultSet.getMetaData() | ||
val totalCols = metadata.getColumnCount() | ||
var columnNames = Seq.empty[String] | ||
for (i <- 1 to totalCols) { | ||
columnNames = columnNames.appended(metadata.getColumnName(i)) | ||
} | ||
|
||
while (resultSet.next()) { | ||
println("+" * 30) | ||
for (i <- 1 to totalCols) { | ||
val value = resultSet.getString(i) | ||
print(s"${columnNames(i - 1)} = ${value}; ") | ||
} | ||
println() | ||
} | ||
} | ||
} | ||
|
||
case class DbDef( | ||
name: String, | ||
schemas: Seq[SchemaDef] | ||
) | ||
|
||
case class SchemaDef( | ||
name: String, | ||
tables: Seq[TableDef] | ||
) | ||
|
||
case class TableDef(schema: String, name: String, columnDefs: Seq[ColumnDef], pkColumns: Seq[ColumnDef]) | ||
|
||
case class ColumnDef( | ||
metadata: ColumnMetadata, | ||
scalaType: ColumnType | ||
) | ||
|
||
sealed abstract class ColumnType | ||
object ColumnType { | ||
case class Predefined(name: String) extends ColumnType | ||
case class Enumeration(name: String, values: Seq[String]) extends ColumnType | ||
case class Unknown(originalName: String) extends ColumnType | ||
} | ||
|
||
// raw db data | ||
case class ColumnMetadata( | ||
schema: String, | ||
table: String, | ||
name: String, | ||
jdbcType: Int, | ||
isNullable: Boolean, | ||
isAutoInc: Boolean, | ||
isGenerated: Boolean, | ||
defaultValue: Option[String] | ||
) |
48 changes: 48 additions & 0 deletions
48
generator/src/ba/sake/squery/generator/JdbcDefExtractor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ba.sake.squery.generator | ||
|
||
import java.sql.{Array => _, _} | ||
import javax.sql.DataSource | ||
import scala.util._ | ||
import scala.util.chaining._ | ||
import scala.collection.mutable.ArrayBuffer | ||
import org.apache.commons.lang3.StringUtils | ||
import org.apache.commons.text.CaseUtils | ||
|
||
/** General data types extractor, based on JDBC metadata | ||
* | ||
* @param ds | ||
*/ | ||
class JdbcDefExtractor(ds: DataSource) extends DbDefExtractor(ds) { | ||
|
||
// (table, column) -> ColumnType | ||
override protected def getColumnTypes( | ||
connection: Connection, | ||
schemaName: String, | ||
columnsMetadatas: Seq[ColumnMetadata] | ||
): Map[(String, String), ColumnType] = { | ||
val databaseMetaData = connection.getMetaData() | ||
columnsMetadatas.map { cMeta => | ||
val tpe = cMeta.jdbcType match { | ||
case Types.BIT => ColumnType.Predefined("Boolean") | ||
case Types.BOOLEAN => ColumnType.Predefined("Boolean") | ||
case Types.TINYINT => ColumnType.Predefined("Byte") | ||
case Types.SMALLINT => ColumnType.Predefined("Short") | ||
case Types.INTEGER => ColumnType.Predefined("Int") | ||
case Types.BIGINT => ColumnType.Predefined("Long") | ||
case Types.DECIMAL => ColumnType.Predefined("Double") | ||
case Types.DOUBLE => ColumnType.Predefined("Double") | ||
case Types.NUMERIC => ColumnType.Predefined("Double") | ||
case Types.NVARCHAR => ColumnType.Predefined("String") | ||
case Types.VARCHAR => ColumnType.Predefined("String") | ||
case Types.DATE => ColumnType.Predefined("LocalDate") | ||
case Types.TIMESTAMP => ColumnType.Predefined("LocalDateTime") | ||
case Types.TIMESTAMP_WITH_TIMEZONE => ColumnType.Predefined("Instant") | ||
case Types.VARBINARY => ColumnType.Predefined("Array[Byte]") | ||
case Types.BINARY => ColumnType.Predefined("Array[Byte]") | ||
case _ => ColumnType.Unknown(cMeta.jdbcType.toString) | ||
} | ||
(cMeta.table, cMeta.name) -> tpe | ||
}.toMap | ||
} | ||
|
||
} |
Oops, something went wrong.