Browse Source

Abstract database maintenance

master
Chris Smith 15 years ago
parent
commit
306d3c283b

+ 3
- 87
src/uk/co/md87/evetool/api/EveApi.java View File

@@ -22,14 +22,7 @@
22 22
 
23 23
 package uk.co.md87.evetool.api;
24 24
 
25
-import java.io.BufferedReader;
26
-import java.io.IOException;
27
-import java.io.InputStreamReader;
28 25
 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 26
 import java.util.logging.Level;
34 27
 import java.util.logging.Logger;
35 28
 
@@ -38,6 +31,7 @@ import uk.co.md87.evetool.api.io.DBCache;
38 31
 import uk.co.md87.evetool.api.parser.ApiElement;
39 32
 import uk.co.md87.evetool.api.parser.ApiParser;
40 33
 import uk.co.md87.evetool.api.parser.ApiResult;
34
+import uk.co.md87.evetool.api.util.TableCreator;
41 35
 import uk.co.md87.evetool.api.wrappers.CharacterList;
42 36
 import uk.co.md87.evetool.api.wrappers.CharacterSheet;
43 37
 import uk.co.md87.evetool.api.wrappers.SkillInTraining;
@@ -83,7 +77,7 @@ public class EveApi {
83 77
     public EveApi(final Connection sqlConnection) {
84 78
         this.conn = sqlConnection;
85 79
         
86
-        checkTables();
80
+        new TableCreator(conn, "../db/", TABLES).checkTables();
87 81
 
88 82
         this.downloader = new ApiDownloader(new DBCache(conn), new ApiParser());
89 83
     }
@@ -171,7 +165,7 @@ public class EveApi {
171 165
      */
172 166
     protected <T> ApiResponse<T> getResponse(final String method, final Class<T> type,
173 167
             final boolean needKey, final boolean needChar) {
174
-        // TODO: Require userid + apikey
168
+        // TODO: Require userid + apikey (remove from downloader)
175 169
         final ApiResult result = downloader.getPage(method, null);
176 170
 
177 171
         if (result.wasSuccessful()) {
@@ -189,82 +183,4 @@ public class EveApi {
189 183
         }
190 184
     }
191 185
 
192
-    // TODO: Abstract db maintenance
193
-    // TODO: Version tables somehow
194
-    /**
195
-     * Creates the table with the specified name. SQL is read from the
196
-     * <code>db</code> package.
197
-     *
198
-     * @param table The table to be created
199
-     */
200
-    protected void createTable(final String table) {
201
-        LOGGER.log(Level.FINE, "Creating table " + table);
202
-        final StringBuilder sql = new StringBuilder();
203
-        final BufferedReader stream = new BufferedReader(new InputStreamReader(
204
-                getClass().getResourceAsStream("db/" + table.toLowerCase() + ".sql")));
205
-        String line;
206
-
207
-        try {
208
-            do {
209
-                line = stream.readLine();
210
-
211
-                if (line != null) {
212
-                    sql.append(line);
213
-                }
214
-            } while (line != null);
215
-        } catch (IOException ex) {
216
-            LOGGER.log(Level.SEVERE, "Error when reading SQL for table: " + table, ex);
217
-            return;
218
-        }
219
-        
220
-        try {
221
-            conn.createStatement().execute(sql.toString());
222
-        } catch (SQLException ex) {
223
-            LOGGER.log(Level.SEVERE, "Error creating table: " + table, ex);
224
-        }
225
-    }
226
-
227
-    /**
228
-     * Checks to ensure that all required tables exist. If any table is missing,
229
-     * it will be created.
230
-     *
231
-     * @see #createTable(java.lang.String)
232
-     */
233
-    protected void checkTables() {
234
-        LOGGER.log(Level.FINEST, "Checking that tables exist");
235
-
236
-        final List<String> tables = getTables();
237
-
238
-        for (String table : TABLES) {
239
-            if (!tables.contains(table.toUpperCase())) {
240
-                createTable(table);
241
-            }
242
-        }
243
-
244
-        LOGGER.log(Level.FINEST, "Done checking tables");
245
-    }
246
-
247
-    /**
248
-     * Retrieves a list of tables that exist in the database.
249
-     *
250
-     * @return A list of table names that exist
251
-     */
252
-    protected List<String> getTables() {
253
-        final List<String> tables = new ArrayList<String>();
254
-
255
-        try {
256
-            final ResultSet rs = conn.getMetaData().getTables(null, null, null, null);
257
-
258
-            while (rs.next()) {
259
-                tables.add(rs.getString("TABLE_NAME"));
260
-            }
261
-
262
-            rs.close();
263
-        } catch (SQLException ex) {
264
-            LOGGER.log(Level.SEVERE, "SQL Error when checking for tables", ex);
265
-        }
266
-
267
-        return tables;
268
-    }
269
-
270 186
 }

+ 149
- 0
src/uk/co/md87/evetool/api/util/TableCreator.java View File

@@ -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
+}

Loading…
Cancel
Save