source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/util/GS3SQLConnection.java@ 6100

Last change on this file since 6100 was 6100, checked in by cs025, 20 years ago

Added generic GS2 textfile handler, classifier tables to database

  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1package org.greenstone.gsdl3.gs3build.util;
2
3import java.util.List;
4import java.util.ArrayList;
5
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.SQLException;
9import java.sql.Statement;
10import java.sql.ResultSet;
11
12
13import org.greenstone.gsdl3.gs3build.database.*;
14
15public class GS3SQLConnection
16{
17 private Connection connection;
18 private Statement statement;
19
20 class GS3SQLCreateTable
21 { String tableName;
22 List properties;
23 List primaryKey;
24 List indexed;
25
26 public GS3SQLCreateTable(String tableName)
27 { this.tableName = tableName;
28 this.properties = new ArrayList();
29 this.primaryKey = new ArrayList();
30 this.indexed = new ArrayList();
31 }
32
33 public void addAutoPrimaryKey(String fieldName)
34 {
35 GS3SQLField field = new GS3SQLField(fieldName, GS3SQLField.AUTOINTEGER_TYPE);
36 this.properties.add(field);
37 this.setPrimaryKey(fieldName);
38 }
39
40 public void addProperty(String fieldname)
41 { GS3SQLField field = new GS3SQLField(fieldname);
42 this.properties.add(field);
43 }
44
45 public void addProperty(String fieldname, String type)
46 { GS3SQLField field = new GS3SQLField(fieldname, type);
47 this.properties.add(field);
48 }
49
50 public void addProperty(String fieldname, int length)
51 { GS3SQLField field = new GS3SQLField(fieldname, length);
52 this.properties.add(field);
53 }
54
55 public void setPrimaryKey(String fieldname)
56 { this.primaryKey.clear();
57 this.primaryKey.add(fieldname);
58 }
59
60 public void extendPrimaryKey(String fieldname)
61 { this.primaryKey.add(fieldname);
62 }
63
64 public void addIndex(String fieldname)
65 { this.indexed.add(fieldname);
66 }
67
68 public String toString()
69 {
70 StringBuffer reply = new StringBuffer();
71
72 reply.append("CREATE TABLE ");
73 reply.append(this.tableName);
74 reply.append(" (");
75 for (int f = 0; f < this.properties.size(); f ++)
76 { GS3SQLField field = (GS3SQLField) this.properties.get(f);
77 if (f != 0)
78 { reply.append(",");
79 }
80 reply.append(field.toString());
81 }
82
83 for (int p = 0; p < this.primaryKey.size(); p ++)
84 { if (p == 0)
85 { reply.append(", PRIMARY KEY(");
86 }
87 else
88 { reply.append(",");
89 }
90 reply.append(this.primaryKey.get(p).toString());
91
92 if (p == this.primaryKey.size() - 1)
93 { reply.append(")");
94 }
95 }
96 // TODO: add primary key, index etc. notations
97 reply.append(");");
98
99 return reply.toString();
100 }
101 }
102
103 public GS3SQLConnection(java.sql.Connection connection)
104 { this.connection = connection;
105 }
106
107 public boolean execute(String sql)
108 {
109 try {
110 this.statement = this.connection.createStatement();
111 this.statement.execute(sql);
112 }
113 catch (SQLException ex) {
114 System.out.println(ex);
115 return false;
116 }
117 return true;
118 }
119
120 public Statement createStatement()
121 { try {
122 return this.connection.createStatement();
123 }
124 catch (SQLException ex)
125 {
126 return null;
127 }
128 }
129
130 public Statement getStatement()
131 { return this.statement;
132 }
133
134 public ResultSet getResultSet()
135 { try {
136 return this.statement.getResultSet();
137 }
138 catch (SQLException ex)
139 {
140 return null;
141 }
142 }
143
144 /**
145 * Initialise a collection for use.
146 *
147 * @return <code>boolean</code> <code>true</code> if the collection successfully initialised.
148 */
149 public boolean initCollection(String collectionName)
150 {
151 /*
152 // Check if the collection already exists
153 // if so, abort
154
155 // do a sample query to check if the collection exists
156 GS3SQLSelect select = new GS3SQLSelect("document");
157 select.addField("*");
158 if (this.execute(select.toString())) {
159 return true;
160 }
161
162 // if there is an error, assume that the database doesn't exist...
163 // and reconnect to a "test" database for now...
164 this.connection = GS3SQLConnectionFactory.reconnect("test");
165 */
166
167 // create database
168 String command = "CREATE DATABASE " + collectionName;
169
170 try {
171 this.statement = this.connection.createStatement();
172 this.statement.execute(command);
173
174 // reconnect with the new database
175 this.connection = GS3SQLConnectionFactory.reconnect(collectionName);
176
177 // create document table
178 GS3SQLCreateTable docTable = new GS3SQLCreateTable("document");
179 docTable.addProperty("DocID", 64); // The document identifer, unique in collection
180 docTable.setPrimaryKey("DocID"); // ...which is also the primary key
181 docTable.addProperty("DocType", 64); // The document type - used to regenerate the
182 // correct Document object type.
183 statement = this.connection.createStatement();
184 statement.execute(docTable.toString());
185
186 // create structure table
187 GS3SQLCreateTable structureMap = new GS3SQLCreateTable("structure");
188 structureMap.addAutoPrimaryKey("StructureRef");
189 structureMap.addProperty("DocID", 64); // a 'foreign key' in effect - but not treated
190 // as such for efficiency reasons
191 structureMap.addProperty("StructureID", 64); // this identifier
192 structureMap.addProperty("StructureType", 64); // the type of the structure
193 structureMap.addProperty("Label", 128); // the label of the structure
194 statement.execute(structureMap.toString());
195
196 // create section table
197 GS3SQLCreateTable sectionMap = new GS3SQLCreateTable("divisions");
198 sectionMap.addAutoPrimaryKey("DivisionRef");
199 sectionMap.addProperty("StructureRef", GS3SQLField.INTEGER_TYPE);
200 sectionMap.addProperty("DocID", 64); // a 'foreign key' in effect - but not treated
201 // as such for efficiency reasons
202 sectionMap.addProperty("ParentType", 64); // the type of the parent - document or section
203 sectionMap.addProperty("ParentRef", 64); // the parent sql reference identifier
204 sectionMap.addProperty("SectionID", 64); // this identifier
205 sectionMap.addProperty("DivisionType", 64); // the type of the section
206 sectionMap.addProperty("LabelOrder", 64); // the order of the section
207 sectionMap.addProperty("ShortLabel", 128); // the short label of the section
208 sectionMap.addProperty("UserLabel", 255); // the long label of the section
209 statement.execute(sectionMap.toString());
210
211 // a division->metadata reference mapping
212 GS3SQLCreateTable metaRefs = new GS3SQLCreateTable("divisionmetarefs");
213 metaRefs.addProperty("DocID", 64);
214 metaRefs.addProperty("DivisionRef", GS3SQLField.INTEGER_TYPE);
215 metaRefs.addProperty("DivisionType", 16); // whether a namespace or meta ref
216 metaRefs.addProperty("MetaID", 64); // the metadata reference itself, as a string
217 statement.execute(metaRefs.toString());
218
219 // a division->file reference mapping
220 GS3SQLCreateTable fileRefs = new GS3SQLCreateTable("divisionfilerefs");
221 fileRefs.addProperty("DocID", 64);
222 fileRefs.addProperty("DivisionRef", GS3SQLField.INTEGER_TYPE);
223 fileRefs.addProperty("DivisionType", 16); // whether section, group or file
224 fileRefs.addProperty("FileID", 32);
225 statement.execute(fileRefs.toString());
226
227 // create metadata table section table
228 GS3SQLCreateTable metadataSection = new GS3SQLCreateTable("metadata");
229 metadataSection.addAutoPrimaryKey("MetadataRef");
230 metadataSection.addProperty("DocID", 64); // this table isn't actually used for much
231 metadataSection.addProperty("MetaID", 64); // in and of itself...
232 metadataSection.addProperty("GroupID", 64); // the 'name' property
233 statement.execute(metadataSection.toString());
234
235 // create namespace table
236 GS3SQLCreateTable namespaces = new GS3SQLCreateTable("namespaces");
237 namespaces.addAutoPrimaryKey("NamespaceRef");
238 namespaces.addProperty("MetadataRef", GS3SQLField.INTEGER_TYPE);
239 namespaces.addProperty("DocID", 64); // these three fields are the primary key
240 namespaces.addProperty("MetaID", 64);
241 namespaces.addProperty("NamespaceID", 64);
242 namespaces.addProperty("NamespaceType", 64);
243 namespaces.addProperty("fileType", 64);
244 namespaces.addProperty("fileLoc");
245 namespaces.addProperty("creator", 128);
246 statement.execute(namespaces.toString());
247
248 // create metadata values table
249 GS3SQLCreateTable metadata = new GS3SQLCreateTable("mdvalues");
250 // todo: some unique id for the namespaces item
251 metadata.addProperty("NamespaceRef", GS3SQLField.INTEGER_TYPE);
252 metadata.addProperty("label", 256);
253 metadata.addProperty("value");
254 statement.execute(metadata.toString());
255
256 // create file section table
257 GS3SQLCreateTable filesec = new GS3SQLCreateTable("filesection");
258 filesec.addAutoPrimaryKey("FileSectionRef");
259 filesec.addProperty("DocID", 64);
260 filesec.addProperty("FileSecID", 64);
261 statement.execute(filesec.toString());
262
263 // create file groups table
264 GS3SQLCreateTable filegroups = new GS3SQLCreateTable("filegroups");
265 // TODO: some unique identifier
266 filegroups.addAutoPrimaryKey("FileGroupRef");
267 filegroups.addProperty("DocID", 64);
268 filegroups.addProperty("FileGroupID", 64);
269 filegroups.addProperty("ParentRef", GS3SQLField.INTEGER_TYPE);
270 filegroups.addProperty("ParentType", 16);
271 statement.execute(filegroups.toString());
272
273 // create file table
274 GS3SQLCreateTable files = new GS3SQLCreateTable("files");
275 // TODO: the unique identifier from file groups table
276 files.addProperty("FileGroupRef", GS3SQLField.INTEGER_TYPE);
277 files.addProperty("FileLocType", 64);
278 files.addProperty("FileLocation");
279 files.addProperty("MIMEType", 64);
280 files.addProperty("FileID", 32);
281 statement.execute(files.toString());
282
283 // create admin table...etc.
284
285 //
286 // END OF METS TABLES
287 //
288
289 //
290 // BEGINNING OF GSDL TABLES
291 //
292 GS3SQLCreateTable classifiers = new GS3SQLCreateTable("classifiers");
293 classifiers.addAutoPrimaryKey("ClassifyRef");
294 classifiers.addProperty("ClassifyID");
295 classifiers.addProperty("ParentID");
296 classifiers.addProperty("Name");
297 classifiers.addProperty("Description");
298 statement.execute(classifiers.toString());
299
300 GS3SQLCreateTable classDocs = new GS3SQLCreateTable("classdocuments");
301 classDocs.addProperty("ClassifyRef", GS3SQLField.INTEGER_TYPE);
302 classDocs.addProperty("DocID");
303 statement.execute(classDocs.toString());
304
305 GS3SQLCreateTable classData = new GS3SQLCreateTable("classdata");
306 classData.addProperty("ClassifyRef", GS3SQLField.INTEGER_TYPE);
307 classData.addProperty("Label");
308 classData.addProperty("Value");
309 statement.execute(classData.toString());
310
311 //
312 // END OF GSDL TABLES
313 //
314
315 }
316 catch (SQLException ex)
317 {
318 System.out.println(ex.toString());
319 return false;
320 }
321 return true;
322 }
323
324 public void deleteCollection(String collection)
325 {
326 try {
327 statement = this.connection.createStatement();
328 statement.execute("DROP DATABASE "+collection+";");
329 }
330 catch (SQLException ex)
331 {
332 }
333 }
334}
335
336
Note: See TracBrowser for help on using the repository browser.