Revision 1217
Added by Jing Tao almost 22 years ago
DBSAXNode.java | ||
---|---|---|
42 | 42 |
*/ |
43 | 43 |
public class DBSAXNode extends BasicNode { |
44 | 44 |
|
45 |
private Connection conn;
|
|
45 |
private DBConnection connection;
|
|
46 | 46 |
private DBSAXNode parentNode; |
47 | 47 |
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter; |
48 | 48 |
|
... | ... | |
51 | 51 |
* |
52 | 52 |
* @param conn the JDBC Connection to which all information is written |
53 | 53 |
*/ |
54 |
public DBSAXNode (Connection conn, String docid) throws SAXException { |
|
54 |
public DBSAXNode (DBConnection conn, String docid) throws SAXException {
|
|
55 | 55 |
|
56 | 56 |
super(); |
57 |
this.conn = conn; |
|
57 |
this.connection = conn;
|
|
58 | 58 |
this.parentNode = null; |
59 | 59 |
writeChildNodeToDB("DOCUMENT", null, null, docid); |
60 | 60 |
updateRootNodeID(getNodeID()); |
... | ... | |
67 | 67 |
* @param tagname the name of the node |
68 | 68 |
* @param parentNode the parent node for this node being created |
69 | 69 |
*/ |
70 |
public DBSAXNode (Connection conn, String qName, String lName, |
|
70 |
public DBSAXNode (DBConnection conn, String qName, String lName,
|
|
71 | 71 |
DBSAXNode parentNode, long rootnodeid, |
72 | 72 |
String docid, String doctype) |
73 | 73 |
throws SAXException { |
74 | 74 |
|
75 | 75 |
super(lName); |
76 |
this.conn = conn; |
|
76 |
this.connection = conn;
|
|
77 | 77 |
this.parentNode = parentNode; |
78 | 78 |
setParentID(parentNode.getNodeID()); |
79 | 79 |
setRootNodeID(rootnodeid); |
... | ... | |
89 | 89 |
String data, String docid) |
90 | 90 |
throws SAXException { |
91 | 91 |
try { |
92 |
if(conn == null) |
|
92 |
/*if(conn == null)
|
|
93 | 93 |
{ |
94 | 94 |
try |
95 | 95 |
{ |
... | ... | |
101 | 101 |
MetaCatUtil.debug("Error while getting db connection in " + |
102 | 102 |
"DBSAXNode: " + e.getMessage()); |
103 | 103 |
} |
104 |
} |
|
104 |
}*/
|
|
105 | 105 |
|
106 | 106 |
PreparedStatement pstmt; |
107 | 107 |
if (nodetype == "DOCUMENT") { |
108 |
pstmt = conn.prepareStatement( |
|
108 |
pstmt = connection.prepareStatement(
|
|
109 | 109 |
"INSERT INTO xml_nodes " + |
110 | 110 |
"(nodetype, nodename, nodeprefix, docid) " + |
111 | 111 |
"VALUES (?, ?, ?, ?)"); |
112 |
|
|
113 |
// Increase DBConnection usage count |
|
114 |
connection.increaseUsageCount(1); |
|
112 | 115 |
MetaCatUtil.debugMessage("INSERTING DOCNAME: " + nodename); |
113 | 116 |
} else { |
114 |
pstmt = conn.prepareStatement( |
|
117 |
pstmt = connection.prepareStatement(
|
|
115 | 118 |
"INSERT INTO xml_nodes " + |
116 | 119 |
"(nodetype, nodename, nodeprefix, docid, " + |
117 | 120 |
"rootnodeid, parentnodeid, nodedata, nodeindex) " + |
118 | 121 |
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); |
122 |
// Increase DBConnection usage count |
|
123 |
connection.increaseUsageCount(1); |
|
119 | 124 |
} |
120 | 125 |
|
121 | 126 |
// Bind the values to the query |
... | ... | |
150 | 155 |
pstmt.close(); |
151 | 156 |
|
152 | 157 |
// get the generated unique id afterward |
153 |
long nid = dbAdapter.getUniqueID(conn, "xml_nodes"); |
|
158 |
long nid = dbAdapter. |
|
159 |
getUniqueID(connection.getConnections(), "xml_nodes"); |
|
160 |
//should incease connection usage!!!!!! |
|
161 |
|
|
154 | 162 |
|
155 | 163 |
if (nodetype.equals("DOCUMENT")) { |
156 | 164 |
// Record the root node id that was generated from the database |
... | ... | |
184 | 192 |
public void updateRootNodeID(long nodeid) throws SAXException { |
185 | 193 |
try { |
186 | 194 |
PreparedStatement pstmt; |
187 |
pstmt = conn.prepareStatement( |
|
195 |
pstmt = connection.prepareStatement(
|
|
188 | 196 |
"UPDATE xml_nodes set rootnodeid = ? " + |
189 | 197 |
"WHERE nodeid = ?"); |
198 |
// Increase DBConnection usage count |
|
199 |
connection.increaseUsageCount(1); |
|
190 | 200 |
|
191 | 201 |
// Bind the values to the query |
192 | 202 |
pstmt.setLong(1, nodeid); |
... | ... | |
208 | 218 |
public void writeNodename(String nodename) throws SAXException { |
209 | 219 |
try { |
210 | 220 |
PreparedStatement pstmt; |
211 |
pstmt = conn.prepareStatement( |
|
221 |
pstmt = connection.prepareStatement(
|
|
212 | 222 |
"UPDATE xml_nodes set nodename = ? " + |
213 | 223 |
"WHERE nodeid = ?"); |
224 |
// Increase DBConnection usage count |
|
225 |
connection.increaseUsageCount(1); |
|
214 | 226 |
|
215 | 227 |
// Bind the values to the query |
216 | 228 |
pstmt.setString(1, nodename); |
... | ... | |
229 | 241 |
private long generateNodeID() throws SAXException { |
230 | 242 |
long nid=0; |
231 | 243 |
Statement stmt; |
244 |
DBConnection dbConn = null; |
|
245 |
int serialNumber = -1; |
|
232 | 246 |
try { |
233 |
stmt = conn.createStatement(); |
|
247 |
// Get DBConnection |
|
248 |
dbConn=DBConnectionPool.getDBConnection("DBSAXNode.generateNodeID"); |
|
249 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
250 |
stmt = dbConn.createStatement(); |
|
234 | 251 |
stmt.execute("SELECT xml_nodes_id_seq.nextval FROM dual"); |
235 | 252 |
ResultSet rs = stmt.getResultSet(); |
236 | 253 |
boolean tableHasRows = rs.next(); |
... | ... | |
243 | 260 |
e.getMessage()); |
244 | 261 |
throw new SAXException(e.getMessage()); |
245 | 262 |
} |
263 |
finally |
|
264 |
{ |
|
265 |
// Return DBconnection |
|
266 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
267 |
}//finally |
|
246 | 268 |
|
247 | 269 |
return nid; |
248 | 270 |
} |
... | ... | |
276 | 298 |
} |
277 | 299 |
} |
278 | 300 |
|
279 |
/** |
|
280 |
* NOT USED |
|
281 |
* Update the node index (xml_index) for this node by generating |
|
282 |
* test strings that represent all of the relative and absolute |
|
283 |
* paths through the XML tree from document root to this node |
|
284 |
*/ |
|
285 |
private void updateNodeIndex(String docid, String doctype) |
|
286 |
throws SAXException |
|
287 |
{ |
|
288 |
Hashtable pathlist = new Hashtable(); |
|
289 |
boolean atStartingNode = true; |
|
290 |
boolean atRootDocumentNode = false; |
|
291 |
DBSAXNode nodePointer = this; |
|
292 |
StringBuffer currentPath = new StringBuffer(); |
|
293 |
int counter = 0; |
|
301 |
|
|
294 | 302 |
|
295 |
// Create a Hashtable of all of the paths to reach this node |
|
296 |
// including absolute paths and relative paths |
|
297 |
while (!atRootDocumentNode) { |
|
298 |
if (atStartingNode) { |
|
299 |
currentPath.insert(0, nodePointer.getTagName()); |
|
300 |
pathlist.put(currentPath.toString(), new Long(getNodeID())); |
|
301 |
counter++; |
|
302 |
atStartingNode = false; |
|
303 |
} else { |
|
304 |
currentPath.insert(0, "/"); |
|
305 |
currentPath.insert(0, nodePointer.getTagName()); |
|
306 |
pathlist.put(currentPath.toString(), new Long(getNodeID())); |
|
307 |
counter++; |
|
308 |
} |
|
309 |
|
|
310 |
// advance to the next parent node |
|
311 |
nodePointer = nodePointer.getParentNode(); |
|
312 |
|
|
313 |
// If we're at the DOCUMENT node (root of DOM tree), add |
|
314 |
// the root "/" to make the absolute path |
|
315 |
if (nodePointer.getNodeType().equals("DOCUMENT")) { |
|
316 |
currentPath.insert(0, "/"); |
|
317 |
pathlist.put(currentPath.toString(), new Long(getNodeID())); |
|
318 |
counter++; |
|
319 |
atRootDocumentNode = true; |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
try { |
|
324 |
// Create an insert statement to reuse for all of the path insertions |
|
325 |
PreparedStatement pstmt = conn.prepareStatement( |
|
326 |
"INSERT INTO xml_index (nodeid, path, docid, doctype, " + |
|
327 |
"parentnodeid) " + |
|
328 |
"VALUES (?, ?, ?, ?, ?)"); |
|
329 |
//((OraclePreparedStatement)pstmt).setExecuteBatch(counter); |
|
330 |
|
|
331 |
pstmt.setString(3, docid); |
|
332 |
pstmt.setString(4, doctype); |
|
333 |
pstmt.setLong(5, getParentID()); |
|
334 |
|
|
335 |
// Step through the hashtable and insert each of the path values |
|
336 |
Enumeration en = pathlist.keys(); |
|
337 |
while (en.hasMoreElements()) { |
|
338 |
String path = (String)en.nextElement(); |
|
339 |
Long nodeid = (Long)pathlist.get(path); |
|
340 |
pstmt.setLong(1, nodeid.longValue()); |
|
341 |
pstmt.setString(2, path); |
|
342 |
pstmt.executeUpdate(); |
|
343 |
|
|
344 |
//System.out.println(nodeid + " ==> " + path); |
|
345 |
} |
|
346 |
|
|
347 |
// Close the database statement |
|
348 |
pstmt.close(); |
|
349 |
} catch (SQLException sqe) { |
|
350 |
System.err.println("SQL Exception while inserting path to index in " + |
|
351 |
"DBSAXNode.updateNodeIndex"); |
|
352 |
System.err.println(sqe.getMessage()); |
|
353 |
throw new SAXException(sqe.getMessage()); |
|
354 |
} |
|
355 |
} |
|
356 |
|
|
357 | 303 |
/** |
358 | 304 |
* USED FROM SEPARATE THREAD RUNNED from DBSAXHandler on endDocument() |
359 | 305 |
* Update the node index (xml_index) for this node by generating |
360 | 306 |
* test strings that represent all of the relative and absolute |
361 | 307 |
* paths through the XML tree from document root to this node |
362 | 308 |
*/ |
363 |
public void updateNodeIndex(Connection conn, String docid, String doctype) |
|
309 |
public void updateNodeIndex(DBConnection conn, String docid, String doctype)
|
|
364 | 310 |
throws SAXException |
365 | 311 |
{ |
366 | 312 |
Hashtable pathlist = new Hashtable(); |
... | ... | |
404 | 350 |
"INSERT INTO xml_index (nodeid, path, docid, doctype, " + |
405 | 351 |
"parentnodeid) " + |
406 | 352 |
"VALUES (?, ?, ?, ?, ?)"); |
353 |
// Increase usage count |
|
354 |
conn.increaseUsageCount(1); |
|
407 | 355 |
|
408 | 356 |
pstmt.setString(3, docid); |
409 | 357 |
pstmt.setString(4, doctype); |
Also available in: Unified diff
Merge DBConnection branch to head.