1
+ package javaxt .orm ;
2
+
3
+ //******************************************************************************
4
+ //** Schema Class
5
+ //******************************************************************************
6
+ /**
7
+ * Used to generate an SQL schema for a given set of Models
8
+ *
9
+ ******************************************************************************/
10
+
11
+ public class Schema {
12
+
13
+ private Model [] models ;
14
+
15
+
16
+ //**************************************************************************
17
+ //** Constructor
18
+ //**************************************************************************
19
+ public Schema (Model [] models ){
20
+ this .models = models ;
21
+ }
22
+
23
+
24
+ //**************************************************************************
25
+ //** getSQL
26
+ //**************************************************************************
27
+ /** Returns a SQL script used to generate tables and indexes in a relational
28
+ * database.
29
+ */
30
+ public String getSQLScript (){
31
+ boolean hasGeometry = false ;
32
+ boolean hasLastModifiedField = false ;
33
+ java .util .HashSet <String > schemas = new java .util .HashSet <String >();
34
+ for (Model model : models ){
35
+
36
+
37
+ //Check whether any of the fields are geospatial/geometry types
38
+ if (!hasGeometry ){
39
+ for (Field field : model .getFields ()){
40
+ if (field .getColumnType ().startsWith ("geometry" )){
41
+ hasGeometry = true ;
42
+ break ;
43
+ }
44
+ }
45
+ }
46
+
47
+ if (model .hasLastModifiedField ()){
48
+ hasLastModifiedField = true ;
49
+ }
50
+
51
+
52
+ String schemaName = model .getSchemaName ();
53
+ if (schemaName !=null ){
54
+ schemas .add (model .escapeTableName (schemaName ));
55
+ }
56
+ }
57
+
58
+
59
+ //Generate SQL script
60
+ StringBuilder sql = new StringBuilder ();
61
+
62
+ for (String schemaName : schemas ){
63
+ sql .append ("CREATE SCHEMA IF NOT EXISTS " );
64
+ sql .append (schemaName );
65
+ sql .append (";\r \n " );
66
+ }
67
+
68
+ if (hasGeometry ){
69
+ sql .append ("CREATE EXTENSION IF NOT EXISTS postgis;\r \n " );
70
+ }
71
+
72
+
73
+ for (Model model : models ){
74
+ sql .append ("\r \n " );
75
+ sql .append (model .getTableSQL ());
76
+ }
77
+ for (Model model : models ){
78
+ String str = model .getDiamondTableSQL ();
79
+ if (!str .isEmpty ()){
80
+ sql .append ("\r \n " );
81
+ sql .append (str );
82
+ }
83
+ }
84
+
85
+ sql .append ("\r \n \r \n " );
86
+ for (Model model : models ){
87
+ sql .append (model .getForeignKeySQL ());
88
+ }
89
+
90
+ sql .append ("\r \n \r \n " );
91
+ for (Model model : models ){
92
+ sql .append (model .getIndexSQL ());
93
+ }
94
+
95
+ if (hasLastModifiedField ){
96
+ sql .append ("\r \n \r \n \r \n " );
97
+ sql .append (
98
+ "CREATE OR REPLACE FUNCTION last_modified() RETURNS trigger AS $last_modified$\r \n " +
99
+ " BEGIN\r \n " +
100
+ " NEW.LAST_MODIFIED := current_timestamp;\r \n " +
101
+ " RETURN NEW;\r \n " +
102
+ " END;\r \n " +
103
+ "$last_modified$ LANGUAGE plpgsql;\r \n \r \n "
104
+ );
105
+ sql .append ("\r \n \r \n " );
106
+ }
107
+
108
+ for (Model model : models ){
109
+ sql .append (model .getTriggerSQL ());
110
+ }
111
+
112
+ return sql .toString ();
113
+ }
114
+ }
0 commit comments