Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
5 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
66 changes: 66 additions & 0 deletions
66
wvlet-core/src/main/scala/wvlet/core/tablet/jdbc/SQLObjectMapper.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,66 @@ | ||
package wvlet.core.tablet.jdbc | ||
|
||
import java.sql.{Connection, ResultSet} | ||
|
||
import wvlet.obj._ | ||
import wvlet.log.io.IOUtil._ | ||
|
||
import scala.reflect.ClassTag | ||
|
||
/** | ||
* | ||
*/ | ||
object SQLObjectMapper { | ||
|
||
import scala.reflect.runtime.{universe => ru} | ||
|
||
def sqlTypeOf(tpe: ObjectType): String = { | ||
tpe match { | ||
case Primitive.Int => "integer" | ||
case Primitive.Long => "integer" | ||
case Primitive.Float => "float" | ||
case Primitive.Double => "float" | ||
case Primitive.Boolean => "boolean" | ||
case TextType.String => "string" | ||
case TextType.File => "string" | ||
case TextType.Date => "string" | ||
case _ => | ||
throw new IllegalArgumentException(s"Unknown type ${tpe}") | ||
} | ||
} | ||
|
||
def createTableSQLFor[A: ru.TypeTag](tableName: String): String = { | ||
val schema = ObjectSchema.of[A] | ||
val params = for (p <- schema.parameters) yield { | ||
s"${p.name} ${sqlTypeOf(p.valueType)}" | ||
} | ||
s"create table if not exists ${tableName} (${params.mkString(", ")})" | ||
} | ||
|
||
def quote(s: String) = s"'${s}'" | ||
|
||
def insertRecord[A: ru.TypeTag](obj: A, tableName: String, conn: Connection) { | ||
val schema = ObjectSchema.of[A] | ||
val colSize = schema.parameters.size | ||
val tuple = ("?" * colSize).mkString(", ") | ||
withResource(conn.prepareStatement(s"insert into ${tableName} values(${tuple})")) {prep => | ||
for ((p, i) <- schema.parameters.zipWithIndex) yield { | ||
val v = p.get(obj) | ||
prep.setObject(i + 1, v) | ||
} | ||
prep.execute() | ||
} | ||
} | ||
|
||
def readAs[A: ClassTag](rs: ResultSet): A = { | ||
val cl = implicitly[ClassTag[A]].runtimeClass | ||
val metadata = rs.getMetaData | ||
val cols = metadata.getColumnCount | ||
val b = ObjectBuilder(cl) | ||
for (i <- 1 to cols) { | ||
val colName = metadata.getColumnName(i) | ||
b.set(colName, rs.getObject(i)) | ||
} | ||
b.build.asInstanceOf[A] | ||
} | ||
} |