Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / control / GDBMSMain.java @ 466

History | View | Annotate | Download (8.89 KB)

1
package com.hardcode.gdbms.control;
2

    
3
import java.io.BufferedReader;
4
import java.io.ByteArrayInputStream;
5
import java.io.File;
6
import java.io.IOException;
7
import java.io.InputStreamReader;
8

    
9
import javax.xml.transform.TransformerConfigurationException;
10
import javax.xml.transform.TransformerFactory;
11
import javax.xml.transform.sax.SAXTransformerFactory;
12
import javax.xml.transform.sax.TransformerHandler;
13
import javax.xml.transform.stream.StreamResult;
14

    
15
import org.xml.sax.SAXException;
16

    
17
import com.hardcode.driverManager.Driver;
18
import com.hardcode.driverManager.DriverLoadException;
19
import com.hardcode.driverManager.DriverManager;
20
import com.hardcode.driverManager.DriverValidation;
21
import com.hardcode.gdbms.engine.data.DBDriver;
22
import com.hardcode.gdbms.engine.data.DataSource;
23
import com.hardcode.gdbms.engine.data.DataSourceFactory;
24
import com.hardcode.gdbms.engine.data.DriverException;
25
import com.hardcode.gdbms.engine.data.FileDriver;
26
import com.hardcode.gdbms.engine.instruction.Adapter;
27
import com.hardcode.gdbms.engine.instruction.SelectAdapter;
28
import com.hardcode.gdbms.engine.instruction.SemanticException;
29
import com.hardcode.gdbms.engine.instruction.UnionAdapter;
30
import com.hardcode.gdbms.engine.instruction.Utilities;
31
import com.hardcode.gdbms.gui.Frame;
32
import com.hardcode.gdbms.gui.GDBMSAdapterTreeModel;
33
import com.hardcode.gdbms.gui.GDBMSParseTreeModel;
34
import com.hardcode.gdbms.gui.GDBMSTableModel;
35
import com.hardcode.gdbms.gui.ParseTreeFrame;
36
import com.hardcode.gdbms.parser.Node;
37
import com.hardcode.gdbms.parser.ParseException;
38
import com.hardcode.gdbms.parser.SQLEngine;
39
import com.hardcode.gdbms.parser.SimpleNode;
40

    
41

    
42
/**
43
 * Aplicaci?n principal
44
 *
45
 * @author Fernando Gonz?lez Cort?s
46
 */
47
public class GDBMSMain {
48
    /**
49
     * Inicializa los subsistemas
50
     *
51
     * @throws IOException Si se produce alg?n error al inicializar los drivers
52
     * @throws InstantiationException Si se produce alg?n error al inicializar
53
     *         los drivers
54
     * @throws IllegalAccessException Si se produce alg?n error al inicializar
55
     *         los drivers
56
     * @throws ClassNotFoundException Si se produce alg?n error al inicializar
57
     *         los drivers
58
     * @throws DriverLoadException
59
     */
60
    private void setup()
61
        throws IOException, InstantiationException, IllegalAccessException, 
62
            ClassNotFoundException, DriverLoadException {
63
        //Setup de los drivers
64
        DriverManager dm = new DriverManager();
65
        dm.setValidation(new DriverValidation() {
66
                public boolean validate(Driver d) {
67
                    return (d instanceof FileDriver) || (d instanceof DBDriver);
68
                }
69
            });
70
        dm.loadDrivers(new File(".\\drivers\\"));
71

    
72
        //Setup del factory de DataSources
73
        DataSourceFactory.setDriverManager(dm);
74
    }
75

    
76
    /**
77
     * Muestra por la consola el contenido de un DataSource
78
     *
79
     * @param ds DataSource a mostrar
80
     *
81
     * @throws IOException Si hay un error leyendo del DataSource
82
     */
83
    private void mostrar(DataSource ds) throws DriverException {
84
        //Se muestra la tabla
85
        ds.start();
86
        
87
        ds.getRowCount();
88
        
89
        Frame f = new Frame();
90
        f.setTableModel(new GDBMSTableModel(ds));
91
        f.show();
92
/*                
93
        StringBuffer aux = new StringBuffer();
94

95
        int fc = ds.getFieldCount();
96
        int rc = ds.getRowCount();
97
        
98
        for (int i = 0; i < fc; i++) {
99
            aux.append(ds.getFieldName(i)).append("\t");
100
        }
101

102
        System.out.println(aux);
103

104
        try {
105
            for (int row = 0; row < rc; row++) {
106
                aux.setLength(0);
107

108
                for (int j = 0; j < fc; j++) {
109
                    aux.append(ds.getFieldValue(row, j)).append("\t");
110
                }
111

112
                //System.out.println(aux);
113
            }
114
        } catch (Throwable t) {
115
            t.printStackTrace();
116
        } finally {
117
            ds.close();
118
        }
119
  */  }
120

    
121
    /**
122
     * Bucle principal
123
     *
124
     * @throws SAXException
125
     * @throws IOException
126
     * @throws InstantiationException
127
     * @throws IllegalAccessException
128
     * @throws ClassNotFoundException
129
     * @throws DriverLoadException
130
     */
131
    private void run()
132
        throws DriverException, SAXException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, DriverLoadException {
133
        setup();
134

    
135
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
136
        String sql = "";
137
        System.out.print("> ");
138
        sql = in.readLine();
139

    
140
        while (!sql.equals("quit")) {
141
                long t1 = System.currentTimeMillis();
142
            ByteArrayInputStream bytes = new ByteArrayInputStream(sql.getBytes());
143
            SQLEngine parser = new SQLEngine(bytes);
144

    
145
            try {
146
                parser.SQLStatement();
147
            } catch (ParseException e) {
148
                e.printStackTrace();
149
            }
150

    
151
            Node root = parser.getRootNode();
152
            Adapter rootAdapter = Utilities.buildTree(root.jjtGetChild(0));
153

    
154
            ParseTreeFrame parseTree = new ParseTreeFrame();
155
            parseTree.setTreeModel(new GDBMSParseTreeModel((SimpleNode)root));
156
            parseTree.show();
157
            ParseTreeFrame parseTree2 = new ParseTreeFrame();
158
            parseTree2.setTreeModel(new GDBMSAdapterTreeModel(rootAdapter));
159
            parseTree2.show();
160
            Utilities.simplify(rootAdapter);
161
/*            ParseTreeFrame parseTree3 = new ParseTreeFrame();
162
            parseTree3.setTreeModel(new GDBMSAdapterTreeModel(rootAdapter));
163
            parseTree3.show();
164
*/
165
/*            obtenerXML(root);
166

167
                        InstructionHandler handler = new InstructionHandler();
168
                        SemanticParser sp = new SemanticParser();
169
                        sp.setContentHandler(handler);
170
                        sp.parse(root);
171

172
                        SelectInstruction s = handler.getInstr();
173
            */
174
            DataSource result = null;
175
            long t2 = System.currentTimeMillis();
176
            
177
            try {
178
                if (rootAdapter instanceof SelectAdapter) {
179
                    result = DataSourceFactory.createRandomDataSource((SelectAdapter) rootAdapter);
180
                } else if (rootAdapter instanceof UnionAdapter) {
181
                    result = DataSourceFactory.createRandomDataSource((UnionAdapter) rootAdapter);
182
                }
183

    
184
                if (result != null) {
185
                    mostrar(result);
186
                }
187
            } catch (SemanticException e1) {
188
                e1.printStackTrace();
189
            }
190

    
191
            long t3 = System.currentTimeMillis();
192
            
193
            System.out.println("Tiempo de parseado: " + ((t2 - t1)/1000.0)+" segundos");
194
            System.out.println("Tiempo de ejecuci?n: " + ((t3 - t2)/1000.0)+" segundos");
195
            System.out.println("Tiempo de parseado y ejecuci?n: " + ((t3 - t1)/1000.0)+" segundos");
196
            System.out.print("> ");
197

    
198
            sql = in.readLine();
199
        }
200
    }
201

    
202
    /**
203
     * DOCUMENT ME!
204
     *
205
     * @param root
206
     */
207
    private void obtenerXML(Node root) {
208
        TransformerFactory transFact = TransformerFactory.newInstance();
209

    
210
        if (transFact.getFeature(SAXTransformerFactory.FEATURE)) {
211
            SAXTransformerFactory saxTransFact = (SAXTransformerFactory) transFact;
212
            TransformerHandler transHand = null;
213

    
214
            try {
215
                transHand = saxTransFact.newTransformerHandler();
216
                transHand.setResult(new StreamResult(System.out));
217

    
218
                SemanticParser sp = new SemanticParser();
219
                sp.setContentHandler(transHand);
220
                sp.parse(root);
221
            } catch (TransformerConfigurationException e) {
222
                e.printStackTrace();
223
            } catch (SAXException e) {
224
                e.printStackTrace();
225
            }
226
        } else {
227
            System.err.println("SAXTransformerFactory is not supported.");
228
            System.exit(1);
229
        }
230
    }
231

    
232
    /**
233
     * Hace que el visitor sp visite el nodo actual y cada uno de sus hijos
234
     *
235
     * @param root nodo raiz que se va a visitar
236
     * @param sp visitante
237
     */
238
    private void parse(Node root, SemanticParser sp) {
239
        root.jjtAccept(sp, null);
240

    
241
        for (int i = 0; i < root.jjtGetNumChildren(); i++) {
242
            parse(root.jjtGetChild(i), sp);
243
        }
244
    }
245

    
246
    /**
247
     * metodo de entrada
248
     *
249
     * @param args Si se produce alg?n error al inicializar los drivers
250
     * @throws DriverException
251
     * @throws SAXException
252
     * @throws IOException
253
     * @throws InstantiationException
254
     * @throws IllegalAccessException
255
     * @throws ClassNotFoundException
256
     * @throws DriverLoadException
257
     */
258
    public static void main(String[] args)
259
        throws DriverException, SAXException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, DriverLoadException {
260
        GDBMSMain db = new GDBMSMain();
261
        db.run();
262
    }
263
}