KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > database > xml > DefaultTableCreator


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.database.xml;
20
21 import org.lucane.common.Logging;
22
23 import org.w3c.dom.*;
24 import java.sql.*;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 public class DefaultTableCreator extends TableCreator
29 {
30     /**
31      * Create a table from a xml node
32      *
33      * @param connection an already opened connection
34      * @param node the node describing the table
35      */

36     public void createTableFromXmlNode(Connection connection, Node node)
37     throws Exception JavaDoc
38     {
39       ArrayList JavaDoc indexes = new ArrayList JavaDoc();
40       StringBuffer JavaDoc query =new StringBuffer JavaDoc("CREATE TABLE ");
41       String JavaDoc tableName = node.getAttributes().getNamedItem("name").getNodeValue();
42       query.append(tableName);
43       query.append("(");
44     
45       //-- columns
46
node = node.getFirstChild();
47       while(node != null)
48       {
49           if(node.getNodeName().equals("column"))
50           {
51               String JavaDoc columnName = node.getAttributes().getNamedItem("name").getNodeValue();
52               String JavaDoc columnType = node.getAttributes().getNamedItem("type").getNodeValue();
53               String JavaDoc columnNotNull = "false";
54               if(node.getAttributes().getNamedItem("notnull") != null)
55                 columnNotNull = node.getAttributes().getNamedItem("notnull").getNodeValue();
56               
57               query.append(columnName);
58               query.append(" ");
59               query.append(getDbLayer().resolveType(columnType));
60               
61               if(Boolean.valueOf(columnNotNull).booleanValue())
62                 query.append(" NOT NULL");
63               
64               query.append(", ");
65           }
66           else if(node.getNodeName().equals("primary-key"))
67           {
68               String JavaDoc columns = node.getAttributes().getNamedItem("columns").getNodeValue();
69               query.append(" PRIMARY KEY(");
70               query.append(columns);
71               query.append("), ");
72           }
73           else if(node.getNodeName().equals("index"))
74           {
75               String JavaDoc name = node.getAttributes().getNamedItem("name").getNodeValue();
76               String JavaDoc columns = node.getAttributes().getNamedItem("columns").getNodeValue();
77               indexes.add(new TableIndex(name, tableName, columns));
78           }
79           node = node.getNextSibling();
80       }
81       query.delete(query.length()-2, query.length());
82       query.append(")");
83     
84       //-- execution
85
Statement s = connection.createStatement();
86       s.execute(query.toString());
87       s.close();
88       
89       Logging.getLogger().finest(query.toString());
90       
91       createIndexes(connection, indexes.iterator());
92     }
93     
94     /**
95      * Create indexes
96      */

97     private void createIndexes(Connection connection, Iterator JavaDoc indexes)
98     throws SQLException
99     {
100         while(indexes.hasNext())
101         {
102             TableIndex index = (TableIndex)indexes.next();
103             StringBuffer JavaDoc query = new StringBuffer JavaDoc();
104             query.append("CREATE INDEX ");
105             query.append("idx_");
106             query.append(index.getTable());
107             query.append('_');
108             query.append(index.getName());
109             query.append(" ON ");
110             query.append(index.getTable());
111             query.append(" (");
112             query.append(index.getColumns());
113             query.append(')');
114             
115             Statement s = connection.createStatement();
116             s.execute(query.toString());
117             s.close();
118               
119             Logging.getLogger().finest(query.toString());
120         }
121     }
122 }
123
Popular Tags