001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.database.xml;
020:
021: import org.lucane.common.Logging;
022:
023: import org.w3c.dom.*;
024: import java.sql.*;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027:
028: public class DefaultTableCreator extends TableCreator {
029: /**
030: * Create a table from a xml node
031: *
032: * @param connection an already opened connection
033: * @param node the node describing the table
034: */
035: public void createTableFromXmlNode(Connection connection, Node node)
036: throws Exception {
037: ArrayList indexes = new ArrayList();
038: StringBuffer query = new StringBuffer("CREATE TABLE ");
039: String tableName = node.getAttributes().getNamedItem("name")
040: .getNodeValue();
041: query.append(tableName);
042: query.append("(");
043:
044: //-- columns
045: node = node.getFirstChild();
046: while (node != null) {
047: if (node.getNodeName().equals("column")) {
048: String columnName = node.getAttributes().getNamedItem(
049: "name").getNodeValue();
050: String columnType = node.getAttributes().getNamedItem(
051: "type").getNodeValue();
052: String columnNotNull = "false";
053: if (node.getAttributes().getNamedItem("notnull") != null)
054: columnNotNull = node.getAttributes().getNamedItem(
055: "notnull").getNodeValue();
056:
057: query.append(columnName);
058: query.append(" ");
059: query.append(getDbLayer().resolveType(columnType));
060:
061: if (Boolean.valueOf(columnNotNull).booleanValue())
062: query.append(" NOT NULL");
063:
064: query.append(", ");
065: } else if (node.getNodeName().equals("primary-key")) {
066: String columns = node.getAttributes().getNamedItem(
067: "columns").getNodeValue();
068: query.append(" PRIMARY KEY(");
069: query.append(columns);
070: query.append("), ");
071: } else if (node.getNodeName().equals("index")) {
072: String name = node.getAttributes().getNamedItem("name")
073: .getNodeValue();
074: String columns = node.getAttributes().getNamedItem(
075: "columns").getNodeValue();
076: indexes.add(new TableIndex(name, tableName, columns));
077: }
078: node = node.getNextSibling();
079: }
080: query.delete(query.length() - 2, query.length());
081: query.append(")");
082:
083: //-- execution
084: Statement s = connection.createStatement();
085: s.execute(query.toString());
086: s.close();
087:
088: Logging.getLogger().finest(query.toString());
089:
090: createIndexes(connection, indexes.iterator());
091: }
092:
093: /**
094: * Create indexes
095: */
096: private void createIndexes(Connection connection, Iterator indexes)
097: throws SQLException {
098: while (indexes.hasNext()) {
099: TableIndex index = (TableIndex) indexes.next();
100: StringBuffer query = new StringBuffer();
101: query.append("CREATE INDEX ");
102: query.append("idx_");
103: query.append(index.getTable());
104: query.append('_');
105: query.append(index.getName());
106: query.append(" ON ");
107: query.append(index.getTable());
108: query.append(" (");
109: query.append(index.getColumns());
110: query.append(')');
111:
112: Statement s = connection.createStatement();
113: s.execute(query.toString());
114: s.close();
115:
116: Logging.getLogger().finest(query.toString());
117: }
118: }
119: }
|