|
@@ -0,0 +1,149 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2009 Chris Smith
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package uk.co.md87.evetool.api.util;
|
|
24
|
+
|
|
25
|
+import java.io.BufferedReader;
|
|
26
|
+import java.io.IOException;
|
|
27
|
+import java.io.InputStreamReader;
|
|
28
|
+import java.sql.Connection;
|
|
29
|
+import java.sql.ResultSet;
|
|
30
|
+import java.sql.SQLException;
|
|
31
|
+import java.util.ArrayList;
|
|
32
|
+import java.util.List;
|
|
33
|
+import java.util.logging.Level;
|
|
34
|
+import java.util.logging.Logger;
|
|
35
|
+
|
|
36
|
+/**
|
|
37
|
+ * Checks for the existance of, and if neccessary creates, tables in a SQL
|
|
38
|
+ * database.
|
|
39
|
+ *
|
|
40
|
+ * @author chris
|
|
41
|
+ */
|
|
42
|
+public class TableCreator {
|
|
43
|
+
|
|
44
|
+ /** A logger for this class. */
|
|
45
|
+ private static final Logger LOGGER = Logger.getLogger(TableCreator.class.getName());
|
|
46
|
+
|
|
47
|
+ /** The SQL connection to use. */
|
|
48
|
+ private final Connection conn;
|
|
49
|
+
|
|
50
|
+ /** The names of the tables to check for. */
|
|
51
|
+ private final String[] tables;
|
|
52
|
+
|
|
53
|
+ /** The location to look for SQL files to create tables. */
|
|
54
|
+ private final String base;
|
|
55
|
+
|
|
56
|
+ /**
|
|
57
|
+ * Creates a new TableCreator which will check for the existance of the
|
|
58
|
+ * specified tables using the specified connection. If the tables do not
|
|
59
|
+ * exist, the TableCreator will attempt to load a .sql file from the
|
|
60
|
+ * specified base directory with the same name as the table.
|
|
61
|
+ *
|
|
62
|
+ * @param conn The SQL connection to use
|
|
63
|
+ * @param base The base directory for SQL files
|
|
64
|
+ * @param tables The tables to create
|
|
65
|
+ */
|
|
66
|
+ public TableCreator(final Connection conn, final String base, final String[] tables) {
|
|
67
|
+ this.conn = conn;
|
|
68
|
+ this.tables = tables;
|
|
69
|
+ this.base = base;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ // TODO: Version tables somehow
|
|
73
|
+ /**
|
|
74
|
+ * Creates the table with the specified name. SQL is read from the
|
|
75
|
+ * <code>db</code> package.
|
|
76
|
+ *
|
|
77
|
+ * @param table The table to be created
|
|
78
|
+ */
|
|
79
|
+ protected void createTable(final String table) {
|
|
80
|
+ LOGGER.log(Level.FINE, "Creating table " + table);
|
|
81
|
+ final StringBuilder sql = new StringBuilder();
|
|
82
|
+ final BufferedReader stream = new BufferedReader(new InputStreamReader(
|
|
83
|
+ getClass().getResourceAsStream(base + table.toLowerCase() + ".sql")));
|
|
84
|
+ String line;
|
|
85
|
+
|
|
86
|
+ try {
|
|
87
|
+ do {
|
|
88
|
+ line = stream.readLine();
|
|
89
|
+
|
|
90
|
+ if (line != null) {
|
|
91
|
+ sql.append(line);
|
|
92
|
+ }
|
|
93
|
+ } while (line != null);
|
|
94
|
+ } catch (IOException ex) {
|
|
95
|
+ LOGGER.log(Level.SEVERE, "Error when reading SQL for table: " + table, ex);
|
|
96
|
+ return;
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ try {
|
|
100
|
+ conn.createStatement().execute(sql.toString());
|
|
101
|
+ } catch (SQLException ex) {
|
|
102
|
+ LOGGER.log(Level.SEVERE, "Error creating table: " + table, ex);
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ /**
|
|
107
|
+ * Checks to ensure that all required tables exist. If any table is missing,
|
|
108
|
+ * it will be created.
|
|
109
|
+ *
|
|
110
|
+ * @see #createTable(java.lang.String)
|
|
111
|
+ */
|
|
112
|
+ public void checkTables() {
|
|
113
|
+ LOGGER.log(Level.FINEST, "Checking that tables exist");
|
|
114
|
+
|
|
115
|
+ final List<String> availTables = getTables();
|
|
116
|
+
|
|
117
|
+ for (String table : tables) {
|
|
118
|
+ if (!availTables.contains(table.toUpperCase())) {
|
|
119
|
+ createTable(table);
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ LOGGER.log(Level.FINEST, "Done checking tables");
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * Retrieves a list of tables that exist in the database.
|
|
128
|
+ *
|
|
129
|
+ * @return A list of table names that exist
|
|
130
|
+ */
|
|
131
|
+ protected List<String> getTables() {
|
|
132
|
+ final List<String> availTables = new ArrayList<String>();
|
|
133
|
+
|
|
134
|
+ try {
|
|
135
|
+ final ResultSet rs = conn.getMetaData().getTables(null, null, null, null);
|
|
136
|
+
|
|
137
|
+ while (rs.next()) {
|
|
138
|
+ availTables.add(rs.getString("TABLE_NAME"));
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ rs.close();
|
|
142
|
+ } catch (SQLException ex) {
|
|
143
|
+ LOGGER.log(Level.SEVERE, "SQL Error when checking for tables", ex);
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ return availTables;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+}
|