Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/rohanclan/ashpool/core/CommandManager.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   * CommandManager.java
20   *
21   * Created on February 1, 2003, 10:15 AM
22   */
23  
24  package com.rohanclan.ashpool.core;
25  
26  import java.util.*;
27  import java.sql.*;
28  
29  /**
30   * Tries to figure out what the requested action really wants, and most often
31   * returns a AResultSet object. 
32   * @author  rob
33   */
34  public class CommandManager {
35      private static final byte SQL_TYPE = 0;
36      private static final byte XSL_TYPE = 1;
37      
38      private SelectFilter sf;
39      private CreateFilter cf;
40      private InsertFilter inf;
41      private AResultSet queryresults;
42      private BasicXSLEngine bXSL;
43      
44      /** handle to the tablemanager (for physical file changes) */
45      private TableManager tableman;
46      
47      /** Creates a new instance of CommandManager */
48      public CommandManager() {
49          queryresults = new AResultSet();
50          bXSL = new BasicXSLEngine();
51      }
52   
53      /** get access to the table manager */
54      public void setTableManager(TableManager tman) throws Exception {
55          tableman = tman;
56          //we will, more than likely need a some filters.
57          sf = new SelectFilter(tableman);
58          sf.setXSLEngine(bXSL);
59          cf = new CreateFilter(tableman);
60          cf.setXSLEngine(bXSL);
61          inf = new InsertFilter(tableman, this);
62      }
63      
64      /** gets the select filter */
65      public SelectFilter getSelectFilter(){
66          return sf;
67      }
68      
69      /** gets the create filter */
70      public CreateFilter getCreateFilter(){
71          return cf;
72      }
73      
74      /** gets the Insert filter */
75      public InsertFilter getInsertFilter(){
76          return inf;
77      }
78      
79      /** gets the table manager */
80      public TableManager getTableManager(){
81          return tableman;
82      }
83      
84      public AResultSet executeXPathStatement(String query) throws Exception{
85          return null;
86      }
87      
88      /** execute an sql style query. Try to figure out the needed filter, and
89       * send the command to that filter. This may be a bit short sighted
90       * depending on how crazy I want to let the sql statments get
91       */
92      public AResultSet executeSQLStatement(String query) throws Exception{
93          //clean out pretty white space (should be regex?)
94          query = query.replace('\n', ' ');
95          query = query.replace('\r', ' ');
96          query = query.replace('\t', ' ');
97        
98          StringTokenizer stok = new StringTokenizer(query," ");
99          //String Xpath;
100         
101         queryresults.reset();
102         
103         //parse the sting and figure out what needs to be done
104         //while(stok.hasMoreElements()){
105             String firstkeyword = stok.nextElement().toString().toLowerCase();
106 
107             //execute the command or list of commands
108             if(firstkeyword.equals("drop")){
109                 if(stok.nextElement().toString().toLowerCase().equals("table")){
110                     _dropTable(stok.nextElement().toString());
111                 }else{
112                     throw new SQLException("I don't understand the command: " + query);
113                 }
114             
115             //if it looks like a select statement
116             }else if(firstkeyword.equals("select")){
117                 String tname;
118                 //command to get a list of available tables?
119                 if(query.toLowerCase().trim().equals("select tables")){
120                     tableman.getTables(queryresults);
121                 //currently supported database types?
122                 }else if(query.toLowerCase().trim().equals("select types")){
123                     cf.getSupportedDataTypes(queryresults);
124                 
125                 //command to get a simple test ResultSet
126                 }else if(query.toLowerCase().trim().equals("select test")){
127                     queryresults.setQuickResultSet("Test_Query", "This is the result");
128                 
129                 //selecting the columns from a table. WARNING this seems a bit
130                 //risky as the command is "select columns <tablename>" very close
131                 //to a normal query!
132                 }else if(stok.nextElement().toString().toLowerCase().equals("columns")
133                     && !(tname = stok.nextElement().toString()).toLowerCase().equals("from")){
134                     
135                     sf.getTableColumns(tname, queryresults);
136                     
137                 //try to send it to a select filter
138                 }else{
139                     //fill the pass recordset with the passed sql query
140                     sf.executeQuery(query, queryresults);
141                 }
142             
143             }else if(firstkeyword.equals("update")){
144 
145             
146             }else if(firstkeyword.equals("insert")){
147                 inf.executeQuery(query, queryresults);
148             
149             }else if(firstkeyword.equals("create")){
150                 if(stok.nextElement().toString().toLowerCase().equals("table")){
151                     cf.executeQuery(query, queryresults);
152                 }
153             }else{
154                 throw new SQLException(firstkeyword + " is an unknown keyword");
155             }
156         //}
157 
158         return queryresults;   
159     }
160     
161     /** sql type query */
162     public AResultSet executeStatement(String query) throws Exception{
163        return executeSQLStatement(query);
164     }
165     
166     /** drops a table from the datastore */
167     private void _dropTable(String tablename){    
168         //let them know something happened
169         if(tableman.doDropTable(tablename)){
170             queryresults.setQuickResultSet("Result","Drop.");
171         }else{
172             queryresults.setQuickResultSet("Result","Drop Error.");
173         }
174     }
175     
176 }