01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.rdbms.postgresql;
07:
08: import java.sql.SQLException;
09:
10: import org.openrdf.sail.rdbms.managers.TransTableManager;
11: import org.openrdf.sail.rdbms.schema.IdSequence;
12: import org.openrdf.sail.rdbms.schema.RdbmsTable;
13: import org.openrdf.sail.rdbms.schema.TransactionTable;
14:
15: public class PgSqlTransTableManager extends TransTableManager {
16: private RdbmsTable table;
17: private IdSequence ids;
18:
19: @Override
20: public void setIdSequence(IdSequence ids) {
21: super .setIdSequence(ids);
22: this .ids = ids;
23: }
24:
25: @Override
26: public void close() throws SQLException {
27: if (table != null) {
28: try {
29: table.execute("DEALLOCATE " + table.getName()
30: + "_insert");
31: } catch (SQLException e) {
32: try {
33: table.rollback();
34: table.execute("DEALLOCATE " + table.getName()
35: + "_insert");
36: } catch (SQLException e1) {
37: // ignore
38: }
39: }
40: }
41: super .close();
42: }
43:
44: @Override
45: protected void createTemporaryTable(RdbmsTable table)
46: throws SQLException {
47: super .createTemporaryTable(table);
48: StringBuilder sb = new StringBuilder();
49: sb.append("PREPARE ").append(table.getName());
50: sb.append("_insert (");
51: sb.append(ids.getSqlType()).append(", ");
52: sb.append(ids.getSqlType()).append(", ");
53: sb.append(ids.getSqlType()).append(", ");
54: sb.append(ids.getSqlType()).append(") AS\n");
55: sb.append("INSERT INTO ").append(table.getName());
56: sb.append(" VALUES ($1, $2, $3, $4)");
57: table.execute(sb.toString());
58: this .table = table;
59: }
60:
61: @Override
62: public TransactionTable createTransactionTable() {
63: return new TransactionTable() {
64: @Override
65: protected String buildInsert(String tableName,
66: boolean predColumnPresent) throws SQLException {
67: if (table == null || !tableName.equals(table.getName()))
68: return super .buildInsert(tableName,
69: predColumnPresent);
70: StringBuilder sb = new StringBuilder();
71: sb.append("EXECUTE ").append(tableName);
72: sb.append("_insert(?, ?, ?, ?)");
73: return sb.toString();
74: }
75: };
76: }
77:
78: }
|