Source code: com/rohanclan/ashpool/core/CreateFilter.java
1 /*
2 * Ashpool - XML Database
3 * Copyright (C) 2003 Rob Rohan
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 *
19 * CreateFilter.java
20 *
21 * Created on February 7, 2003, 8:27 PM
22 */
23
24 package com.rohanclan.ashpool.core;
25
26 import java.util.*;
27 /**
28 *
29 * @author rob
30 */
31 public class CreateFilter {
32 private static final String XSDNS = "xs:";
33
34 private TableManager tableman;
35 private BasicXSLEngine bXSL;
36 private AResultSet rs;
37
38
39 /** Creates a new instance of CreateFilter */
40 public CreateFilter(TableManager tman) {
41 tableman = tman;
42 bXSL = new BasicXSLEngine();
43 rs = new AResultSet();
44 }
45
46 /** sets the table manager */
47 public void setTableManager(TableManager tman){
48 tableman = tman;
49 }
50
51 /** set the xslt engine */
52 public void setXSLEngine(BasicXSLEngine bxsl){
53 this.bXSL = bxsl;
54 }
55
56 /** will look like
57 * create table mytable ( id int, firstname string, date datetime )
58 */
59 public void executeQuery(String sql, AResultSet rs){
60
61 StringTokenizer stok = new StringTokenizer(sql," ");
62 //create
63 stok.nextElement();
64 //table
65 stok.nextElement();
66 //tablename
67 String newfilename = stok.nextElement().toString();
68
69 //the opening (
70 stok.nextElement();
71
72 StringBuffer columndefs = new StringBuffer("");
73 StringBuffer mtelements = new StringBuffer("");
74
75 while(stok.hasMoreElements()){
76 String name = stok.nextElement().toString();
77 if(name.equals(")")){
78 break;
79 }
80 String type = stok.nextElement().toString();
81 //this is for the future, pk's and auto numbers and such
82 //if the type doesn't have a , - for now though it's just the
83 //last column
84 if(type.indexOf(",") < 0){
85 columndefs.append(
86 S_ELE
87 + name
88 + S_TYP
89 + XSDNS + type
90 + S_NIL
91 + "true"
92 + E_ELE);
93 mtelements.append("<" + name + "/>");
94
95 //if the type does have a comma this is the last of this def
96 }else{
97 columndefs.append(
98 S_ELE
99 + name
100 + S_TYP
101 + XSDNS + type.substring(0,type.length() -1)
102 + S_NIL
103 + "true"
104 + E_ELE);
105 mtelements.append("<" + name + "/>");
106 }
107 }
108
109
110 tableman.createTable(
111 newfilename, S_XSD + columndefs.toString() + E_XSD, TableManager.TYPE_SCHEMA
112 );
113
114 tableman.createTable(
115 newfilename, S_XML + mtelements.toString() + E_XML, TableManager.TYPE_TABLE
116 );
117
118 rs.setQuickResultSet("Create Table", "Create.");
119 }
120
121 /** returns the currently supported data types */
122 public void getSupportedDataTypes(AResultSet rs){
123 java.util.Vector dTypes = new java.util.Vector();
124 java.util.Vector dDesc = new java.util.Vector();
125 //add some fields to the column vector
126 dTypes.add("integer");
127 dDesc.add("normal integer datatype (4)");
128 dTypes.add("string");
129 dDesc.add("normal text datatype (12)");
130 dTypes.add("boolean");
131 dDesc.add("true or false (16)");
132 dTypes.add("decimal");
133 dDesc.add("(3)");
134 dTypes.add("float");
135 dDesc.add("32 bit (6)");
136 dTypes.add("double");
137 dDesc.add("64 bit (8)");
138 dTypes.add("duration");
139 dDesc.add("");
140 dTypes.add("dateTime");
141 dDesc.add("(93) i.e. 2003-12-31T23:59:59");
142 dTypes.add("time");
143 dDesc.add(" (92) i.e. 23:59:59");
144 dTypes.add("date");
145 dDesc.add("(91) i.e. 2003-12-31");
146 dTypes.add("gYearMonth");
147 dDesc.add("(12) i.e. 2003-12");
148 dTypes.add("gYear");
149 dDesc.add("(12) i.e. 2003");
150 dTypes.add("gMonthDay");
151 dDesc.add("(12) i.e. 12-31");
152 dTypes.add("gDay");
153 dDesc.add("(12) i.e. 31");
154 dTypes.add("gMonth");
155 dDesc.add("(12) i.e. 12");
156 dTypes.add("hexBinary");
157 dDesc.add("hex numbers that stand for binary numbers (-1)");
158 dTypes.add("base64Binary");
159 dDesc.add("base 64 encoded data (-1)");
160 dTypes.add("anyURI");
161 dDesc.add("URIs (spaces are discouraged) (12)");
162 dTypes.add("QName");
163 dDesc.add("(12)");
164
165 //add the vector to the resultset
166 rs.addColumn("Data Types",dTypes,java.sql.Types.VARCHAR);
167 rs.addColumn("Description",dDesc, java.sql.Types.VARCHAR);
168 }
169
170 //////////////////////////////////////////////////////////////////////////
171 private static final String COPYRIGHT="(c) 2003 Rohan Clan. rob@rohanclan.com";
172
173 private static final String S_XSD="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
174 + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
175 + " elementFormDefault=\"qualified\""
176 + " attributeFormDefault=\"unqualified\">"
177
178 + "<xs:element name=\"t\">"
179 + "<xs:annotation>"
180 + "<xs:documentation>" + COPYRIGHT + "</xs:documentation>"
181 + "</xs:annotation>"
182 +"</xs:element>"
183
184 +"<xs:element name=\"r\" type=\"ashpoolRow\"/>"
185 +"<xs:complexType name=\"ashpoolRow\">"
186 +"<xs:sequence>";
187 /* column data
188 <xs:element name="id" type="xs:int" nillable="false"/>
189 <xs:element name="firstname" type="xs:string" nillable="true"/>
190 <xs:element name="date" type="xs:dateTime" nillable="true"/>
191 */
192 private static final String E_XSD="</xs:sequence>"
193 +"</xs:complexType>"
194 +"</xs:schema>";
195 /////////////////////////////////////////////////////////////////////////////
196 private static final String S_XML="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
197 +"<t>"
198 +"<r>";
199 private static final String E_XML="</r>"
200 +"</t>";
201
202 //////////////////////////////////////////////////////////////////////////////
203 private static final String S_ELE = "<xs:element name=\"";
204 private static final String S_TYP = "\" type=\"";
205 private static final String S_NIL = "\" nillable=\"";
206 private static final String S_REQ = "\" use=\"";
207 private static final String E_ELE = "\" maxOccurs=\"unbounded\" />";
208 }