1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.riverock.dbrevision.system;
27
28 import java.io.InputStream;
29
30 import org.apache.log4j.Logger;
31
32 import org.riverock.dbrevision.annotation.schema.db.DbSchema;
33 import org.riverock.dbrevision.annotation.schema.db.DbSequence;
34 import org.riverock.dbrevision.annotation.schema.db.DbTable;
35 import org.riverock.dbrevision.annotation.schema.db.DbView;
36 import org.riverock.dbrevision.db.Database;
37 import org.riverock.dbrevision.db.DatabaseManager;
38 import org.riverock.dbrevision.db.DatabaseStructureManager;
39 import org.riverock.dbrevision.utils.Utils;
40 import org.riverock.dbrevision.exception.DbRevisionException;
41
42
43
44
45
46
47
48
49
50
51
52 @SuppressWarnings({"UnusedAssignment"})
53 public class DbStructureImport {
54 private static Logger log = Logger.getLogger(DbStructureImport.class);
55
56 public static void importStructure(Database database, InputStream stream, boolean isData ) {
57 log.debug("Unmarshal data from inputstream");
58 DbSchema millSchema;
59 try {
60 millSchema = Utils.getObjectFromXml(DbSchema.class, stream);
61 }
62 catch (Exception e) {
63 String es = "Error unmarshal DB structure from input stream";
64 log.error(es, e);
65 throw new DbRevisionException(es, e);
66 }
67 importStructure(database, millSchema, isData);
68 }
69
70 public static void importStructure(Database database, DbSchema millSchema, boolean isData) {
71 for (DbTable table : millSchema.getTables()) {
72
73 if (!DatabaseManager.isSkipTable(table.getName())) {
74 try {
75 log.debug("create table " + table.getName());
76 database.createTable(table);
77 }
78 catch (Exception e) {
79 String es = "Error create table ";
80 log.debug(es + table.getName(), e);
81 throw new DbRevisionException(es, e);
82 }
83 if (isData) {
84 DatabaseStructureManager.setDataTable(database, table);
85 }
86 }
87 else {
88 log.debug("skip table " + table.getName());
89 }
90
91 }
92
93 for (DbView view : millSchema.getViews()) {
94 DatabaseManager.createWithReplaceAllView(database, millSchema);
95 try {
96 log.debug("create view " + view.getName());
97 database.createView(view);
98 }
99 catch (Exception e) {
100 if (database.testExceptionViewExists(e)) {
101 log.debug("view " + view.getName() + " already exists");
102 log.debug("drop view " + view.getName());
103 DatabaseStructureManager.dropView(database, view);
104 log.debug("create view " + view.getName());
105 try {
106 database.createView(view);
107 }
108 catch (Exception e1) {
109 String es = "Error create view - ";
110 log.error(es, e1);
111 throw new DbRevisionException(es, e1);
112 }
113 }
114 else {
115 String es = "Error create view";
116 log.debug(es,e);
117 throw new DbRevisionException(es, e);
118 }
119 }
120 }
121 DatabaseManager.createWithReplaceAllView(database, millSchema);
122
123 for (DbSequence seq : millSchema.getSequences()) {
124 try {
125 log.debug("create sequence " + seq.getName());
126 database.createSequence(seq);
127 }
128 catch (Exception e) {
129 String es = "Error create sequence ";
130 log.debug(es + seq.getName(), e);
131 throw new DbRevisionException(es, e);
132 }
133 }
134 }
135 }