Created
October 10, 2011 13:43
-
-
Save undetected1/1275366 to your computer and use it in GitHub Desktop.
Database types
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
case class DBRecord(url: String, driver: String, adapter: () => DatabaseAdapter) {} | |
class DBType(record: DBRecord) { | |
lazy val driver = Class.forName(record.driver).newInstance().asInstanceOf[Driver] | |
lazy val adapter = record.adapter() | |
} | |
object DBType { | |
val knownDB = List( | |
DBRecord("mysql", "com.mysql.jdbc.Driver", () => new MySQLAdapter), | |
DBRecord("postgresql", "org.postgresql.Driver", () => new PostgreSqlAdapter), | |
DBRecord("h2", "org.h2.Driver", () => new H2Adapter) | |
) | |
val urlLookup = knownDB.map(rec => rec.url -> rec).toMap | |
def fromURL(url: String) = { | |
val parts = url.split(':') | |
if (parts.length < 3 || parts(0) != "jdbc") None | |
else urlLookup.get(parts(1)).map(rec => new DBType(rec)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment