Skip to content

Commit ca148d4

Browse files
author
Guillaume Grussenmeyer
committed
Added support for schema creation and dropping according to prefixes defined at tables level
1 parent 53300c7 commit ca148d4

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/main/scala/org/squeryl/Schema.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ class Schema(implicit val fieldMapper: FieldMapper) {
175175
_dbAdapter.dropTable(t)
176176
_dbAdapter.postDropTable(t)
177177
}
178+
179+
_dropSchemas
178180
}
179181

180182
def create = {
183+
_createSchemas
181184
_createTables
182185
if(_dbAdapter.supportsForeignKeyConstraints)
183186
_declareForeignKeyConstraints
@@ -279,6 +282,24 @@ class Schema(implicit val fieldMapper: FieldMapper) {
279282
}
280283
}
281284

285+
private def schemaNames = _tables.map( _.prefix ).filter( _.isDefined ).map( _.get ).toSet[String]
286+
287+
private def _createSchemas = {
288+
for (schemaName <- this.schemaNames ) {
289+
val sw = new StatementWriter( _dbAdapter )
290+
_dbAdapter.writeCreateSchema( schemaName, sw )
291+
_executeDdl(sw.statement)
292+
}
293+
}
294+
295+
private def _dropSchemas = {
296+
for( schemaName <- this.schemaNames ) {
297+
val sw = new StatementWriter( _dbAdapter )
298+
_dbAdapter.writeDropSchema( schemaName, sw )
299+
_executeDdl(sw.statement)
300+
}
301+
}
302+
282303
private def _createConstraintsOfCompositePKs =
283304
for(cpk <- _allCompositePrimaryKeys) {
284305
val createConstraintStmt = _dbAdapter.writeCompositePrimaryKeyConstraint(cpk._1, cpk._2)

src/main/scala/org/squeryl/internals/DatabaseAdapter.scala

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,22 @@ trait DatabaseAdapter {
180180
def bigDecimalTypeDeclaration(precision:Int, scale:Int) = "decimal(" + precision + "," + scale + ")"
181181
def timestampTypeDeclaration = "timestamp"
182182
def binaryTypeDeclaration = "binary"
183-
def uuidTypeDeclaration = "char(36)"
183+
def uuidTypeDeclaration = "char(36)"
184184
def intArrayTypeDeclaration = intTypeDeclaration + "[]"
185185
def longArrayTypeDeclaration = longTypeDeclaration + "[]"
186186
def doubleArrayTypeDeclaration = doubleTypeDeclaration + "[]"
187-
188-
def jdbcIntArrayCreationType = intTypeDeclaration
189-
def jdbcLongArrayCreationType = longTypeDeclaration
190-
def jdbcDoubleArrayCreationType = doubleTypeDeclaration
191-
192-
final def arrayCreationType(ptype : Class[_]) : String = {
187+
188+
def jdbcIntArrayCreationType = intTypeDeclaration
189+
def jdbcLongArrayCreationType = longTypeDeclaration
190+
def jdbcDoubleArrayCreationType = doubleTypeDeclaration
191+
192+
final def arrayCreationType(ptype : Class[_]) : String = {
193193
val rv = ptype.getName() match {
194194
case "java.lang.Integer" => jdbcIntArrayCreationType
195195
case "java.lang.Double" => jdbcDoubleArrayCreationType
196-
case "java.lang.Long" => jdbcLongArrayCreationType
196+
case "java.lang.Long" => jdbcLongArrayCreationType
197197
case _ => ""
198-
}
198+
}
199199
rv
200200
}
201201

@@ -281,8 +281,18 @@ trait DatabaseAdapter {
281281
)
282282
}
283283
sw.write(")")
284-
}
285-
284+
}
285+
286+
def writeCreateSchema(name: String, sw: StatementWriter) = {
287+
sw.write("create schema ")
288+
sw.write( quoteName(name) )
289+
}
290+
291+
def writeDropSchema(name: String, sw: StatementWriter) = {
292+
sw.write("drop schema ")
293+
sw.write( quoteName(name ) )
294+
}
295+
286296
def convertParamsForJdbc(params: Iterable[AnyRef]) =
287297
for(p <- params) yield {
288298
p match {
@@ -821,7 +831,7 @@ trait DatabaseAdapter {
821831
else if(classOf[scala.Array[Double]].isAssignableFrom(c))
822832
doubleArrayTypeDeclaration
823833
else
824-
Utils.throwError("unsupported type " + ar.getClass.getCanonicalName)
834+
Utils.throwError("unsupported type " + ar.getClass.getCanonicalName)
825835

826836
decl
827837
}

0 commit comments

Comments
 (0)