| author | Tomas Zeman <tzeman@volny.cz> |
| Fri, 10 Feb 2012 09:53:05 +0100 | |
| changeset 8 | 828565e7f571 |
| parent 7 | 8ef5e77ad79e |
| child 15 | 995184977e9b |
| permissions | -rw-r--r-- |
|
4
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
1 |
/* |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
2 |
* Copyright 2011 Tomas Zeman <tzeman@volny.cz> |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
3 |
* |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
5 |
* you may not use this file except in compliance with the License. |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
6 |
* You may obtain a copy of the License at |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
7 |
* |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
9 |
* |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
13 |
* See the License for the specific language governing permissions and |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
14 |
* limitations under the License. |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
15 |
*/ |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
16 |
package fis.base.model |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
17 |
|
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
18 |
import net.liftweb.squerylrecord.RecordTypeMode._ |
| 8 | 19 |
import net.liftweb.util.Helpers.{snakify, tryo}
|
20 |
import org.squeryl.{Schema, Session, Table}
|
|
21 |
import org.squeryl.internals.StatementWriter |
|
22 |
import scala.collection.mutable.ListBuffer |
|
|
4
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
23 |
|
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
24 |
trait BaseSchema extends Schema {
|
| 8 | 25 |
val entityTableName = "entity" |
26 |
val entities = new Table[EntityUnion](entityTableName) {
|
|
27 |
val tables = new ListBuffer[String] |
|
28 |
} |
|
29 |
||
30 |
val codeListItems = entityTable[CodeListItem]("code_list_item")
|
|
31 |
||
32 |
override def tableNameFromClass(c: Class[_]): String = |
|
33 |
snakify(c.getSimpleName) |
|
34 |
||
35 |
protected def entityTable[T <: Entity[_]](name: String)(implicit manifestT: |
|
36 |
Manifest[T]): Table[T] = {
|
|
37 |
||
38 |
val tbl = table(name)(manifestT) |
|
39 |
on(tbl) { t => declare(t.id.is(autoIncremented("entity_id_seq"))) }
|
|
40 |
entities.tables += name |
|
41 |
tbl |
|
42 |
} |
|
|
7
8ef5e77ad79e
base entity addons: entityTable()
Tomas Zeman <tzeman@volny.cz>
parents:
6
diff
changeset
|
43 |
|
| 8 | 44 |
override def create = {
|
45 |
table[EntityUnion](entityTableName) // register entity table as the last one |
|
46 |
super.create |
|
47 |
} |
|
|
7
8ef5e77ad79e
base entity addons: entityTable()
Tomas Zeman <tzeman@volny.cz>
parents:
6
diff
changeset
|
48 |
|
| 8 | 49 |
override protected def drop = {
|
50 |
val dba = Session.currentSession.databaseAdapter |
|
51 |
val sw = new StatementWriter(dba) |
|
52 |
sw.write("drop view " + entityTableName + " cascade")
|
|
53 |
ex(sw) |
|
54 |
super.drop |
|
55 |
} |
|
56 |
||
57 |
private def ex(sw: StatementWriter) = {
|
|
58 |
val sql = sw.statement |
|
59 |
val cs = Session.currentSession |
|
60 |
cs.log(sql) |
|
61 |
val s = cs.connection.createStatement |
|
62 |
tryo { s.execute(sql) }
|
|
63 |
tryo { s.close }
|
|
|
7
8ef5e77ad79e
base entity addons: entityTable()
Tomas Zeman <tzeman@volny.cz>
parents:
6
diff
changeset
|
64 |
} |
|
4
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
65 |
} |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
66 |
|
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
67 |
object BaseSchema extends BaseSchema |
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
68 |
|
| 8 | 69 |
class EntityViewH2Adapter extends SeqIdH2Adapter {
|
70 |
override def writeCreateTable[T](t: Table[T], sw: StatementWriter, |
|
71 |
schema: Schema) = schema match {
|
|
72 |
case sch: BaseSchema if t.name == sch.entityTableName => |
|
73 |
sw.write("create view " + t.name + " as ")
|
|
74 |
val it = sch.entities.tables.iterator |
|
75 |
while (it.hasNext) {
|
|
76 |
sw.write("select id, name, note from " + it.next)
|
|
77 |
if (it.hasNext) |
|
78 |
sw.write(" union ")
|
|
79 |
} |
|
80 |
case _ => super.writeCreateTable(t, sw, schema) |
|
81 |
} |
|
82 |
} |
|
|
4
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
83 |
|
|
bb4478e1cff7
9fb42f3c37f3e4fe Code list implementation
Tomas Zeman <tzeman@volny.cz>
parents:
diff
changeset
|
84 |
// vim: set ts=2 sw=2 et: |