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.schema;
07:
08: import java.sql.SQLException;
09: import java.sql.Statement;
10: import java.util.List;
11:
12: import org.openrdf.sail.rdbms.exceptions.RdbmsRuntimeException;
13:
14: /**
15: * Manages the namespace prefix in the database.
16: *
17: * @author James Leigh
18: *
19: */
20: public class NamespacesTable {
21: private RdbmsTable table;
22: private String INSERT;
23: private String UPDATE;
24: private String UPDATE_ALL_NULL;
25:
26: public NamespacesTable(RdbmsTable table) {
27: this .table = table;
28: StringBuilder sb = new StringBuilder();
29: sb.append("INSERT INTO ").append(table.getName());
30: sb.append(" (prefix, namespace) VALUES (?, ?)");
31: INSERT = sb.toString();
32: sb.delete(0, sb.length());
33: sb.append("UPDATE ").append(table.getName()).append("\n");
34: sb.append("SET prefix = ?\n");
35: sb.append("WHERE namespace = ?");
36: UPDATE = sb.toString();
37: sb.delete(0, sb.length());
38: sb.append("UPDATE ").append(table.getName()).append("\n");
39: sb.append("SET prefix = NULL\n");
40: sb.append("WHERE prefix IS NOT NULL");
41: UPDATE_ALL_NULL = sb.toString();
42: }
43:
44: public void initialize() throws SQLException {
45: if (!table.isCreated()) {
46: createTable();
47: }
48: }
49:
50: public void close() throws SQLException {
51: table.close();
52: }
53:
54: protected void createTable() throws SQLException {
55: StringBuilder sb = new StringBuilder();
56: sb.append(" prefix VARCHAR(127),\n");
57: sb.append(" namespace TEXT NOT NULL\n");
58: createTable(sb);
59: }
60:
61: protected void createTable(CharSequence sb) throws SQLException {
62: table.createTable(sb);
63: }
64:
65: public void insert(String prefix, String namespace)
66: throws SQLException {
67: int result = table.executeUpdate(INSERT, prefix, namespace);
68: if (result != 1 && result != Statement.SUCCESS_NO_INFO)
69: throw new RdbmsRuntimeException(
70: "Namespace could not be created");
71: table.modified(1, 0);
72: table.optimize();
73: }
74:
75: public void updatePrefix(String prefix, String namespace)
76: throws SQLException {
77: int result = table.executeUpdate(UPDATE, prefix, namespace);
78: if (result != 1 && result != Statement.SUCCESS_NO_INFO)
79: throw new RdbmsRuntimeException(
80: "Namespace prefix could not be changed");
81: }
82:
83: public void clearPrefixes() throws SQLException {
84: table.execute(UPDATE_ALL_NULL);
85: }
86:
87: public List<Object[]> selectAll() throws SQLException {
88: return table.select("prefix", "namespace");
89: }
90:
91: @Override
92: public String toString() {
93: return table.getName();
94: }
95:
96: }
|