Revision 1956 trunk/libraries/libGDBMS/src/com/hardcode/gdbms/control/GDBMSMain.java

View differences:

GDBMSMain.java
12 12
import javax.xml.transform.sax.TransformerHandler;
13 13
import javax.xml.transform.stream.StreamResult;
14 14

  
15
import org.apache.log4j.PropertyConfigurator;
15 16
import org.xml.sax.SAXException;
16 17

  
17 18
import com.hardcode.driverManager.Driver;
18 19
import com.hardcode.driverManager.DriverLoadException;
19 20
import com.hardcode.driverManager.DriverManager;
20 21
import com.hardcode.driverManager.DriverValidation;
21
import com.hardcode.gdbms.engine.data.DBDriver;
22
import com.hardcode.gdbms.engine.customQuery.QueryManager;
22 23
import com.hardcode.gdbms.engine.data.DataSource;
23 24
import com.hardcode.gdbms.engine.data.DataSourceFactory;
24
import com.hardcode.gdbms.engine.data.DriverException;
25
import com.hardcode.gdbms.engine.data.FileDriver;
25
import com.hardcode.gdbms.engine.data.driver.DBDriver;
26
import com.hardcode.gdbms.engine.data.driver.DriverException;
27
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
26 28
import com.hardcode.gdbms.engine.instruction.Adapter;
27
import com.hardcode.gdbms.engine.instruction.SelectAdapter;
28 29
import com.hardcode.gdbms.engine.instruction.SemanticException;
29
import com.hardcode.gdbms.engine.instruction.UnionAdapter;
30 30
import com.hardcode.gdbms.engine.instruction.Utilities;
31
import com.hardcode.gdbms.engine.test.SumQuery;
31 32
import com.hardcode.gdbms.gui.Frame;
32 33
import com.hardcode.gdbms.gui.GDBMSAdapterTreeModel;
33 34
import com.hardcode.gdbms.gui.GDBMSParseTreeModel;
......
37 38
import com.hardcode.gdbms.parser.ParseException;
38 39
import com.hardcode.gdbms.parser.SQLEngine;
39 40
import com.hardcode.gdbms.parser.SimpleNode;
41
import com.hardcode.gdbms.parser.TokenMgrError;
40 42

  
41 43

  
42 44
/**
......
45 47
 * @author Fernando Gonz?lez Cort?s
46 48
 */
47 49
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\\"));
50
	private DataSourceFactory ds;
71 51

  
72
        //Setup del factory de DataSources
73
        DataSourceFactory.setDriverManager(dm);
74
    }
52
	/**
53
	 * Inicializa los subsistemas
54
	 *
55
	 * @throws IOException Si se produce alg?n error al inicializar los drivers
56
	 * @throws InstantiationException Si se produce alg?n error al inicializar
57
	 * 		   los drivers
58
	 * @throws IllegalAccessException Si se produce alg?n error al inicializar
59
	 * 		   los drivers
60
	 * @throws ClassNotFoundException Si se produce alg?n error al inicializar
61
	 * 		   los drivers
62
	 * @throws DriverLoadException
63
	 */
64
	private void setup()
65
		throws IOException, InstantiationException, IllegalAccessException, 
66
			ClassNotFoundException, DriverLoadException {
67
		//Setup del log4j
68
		PropertyConfigurator.configure(this.getClass().getClassLoader()
69
				 .getResource("log4j.properties"));
70
		
71
		//Setup de los drivers
72
		DriverManager dm = new DriverManager();
73
		dm.setValidation(new DriverValidation() {
74
				public boolean validate(Driver d) {
75
					return ((d instanceof ObjectDriver) ||
76
					(d instanceof DBDriver));
77
				}
78
			});
79
		dm.loadDrivers(new File("drivers"));
75 80

  
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();
81
		dm.getLoadFailures();
94 82

  
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
        }
83
		//Setup del factory de DataSources
84
		ds = new DataSourceFactory();
85
		ds.setDriverManager(dm);
101 86

  
102
        System.out.println(aux);
87
		//Setup de las tablas
88
		ds.addFileDataSource("csv", "persona", "persona.csv", "persona.csv",
89
			null);
90
		ds.addFileDataSource("csv", "coche", "coche.csv", "coche.csv", null);
91
		ds.addFileDataSource("gdbms dbf driver", "vias",
92
			"C:\\Documents and Settings\\fernando\\Mis documentos\\province.dbf",
93
			"vias.dbf", null);
94
		ds.addFileDataSource("gdbms dbf driver", "vias",
95
			"/root/cartografia/win/vias.dbf", "vias.dbf", null);
103 96

  
104
        try {
105
            for (int row = 0; row < rc; row++) {
106
                aux.setLength(0);
97
		//        addDataSource(new FooReadDriver(), "foo");
98
		/*                addDBDataSource("mysql", "192.168.0.1", -1, "root", "root", "mysql", new String[]{"*"}, "user", null,
99
		   "mysql");
100
		 */
101
		ds.addDBDataSourceByTable("pb", "127.0.0.1", 5432, "root", "",
102
			"sigusal", "pbedifihist", "postgresql", null);
103
		ds.addDBDataSourceByTable("person", "www.freesql.org", 3306,
104
			"fergonco", "fergonco", "fergonco", "person", "mysql", null);
105
		ds.addDBDataSourceByTable("fernando", "www.freesql.org", 3306,
106
			"fergonco", "fergonco", "fergonco", "person", "mysql", null);
107
		ds.addDBDataSourceBySQL("ages", "www.freesql.org", 3306, "elfernan",
108
			"elfernan", "elfernan",
109
			"select 3*p1.age as tripleage from person p1, person p2, person p3, person p4, person p5",
110
			"mysql", null);
107 111

  
108
                for (int j = 0; j < fc; j++) {
109
                    aux.append(ds.getFieldValue(row, j)).append("\t");
110
                }
112
		//CustomQueries
113
		QueryManager.registerQuery(new SumQuery());
114
	}
111 115

  
112
                //System.out.println(aux);
113
            }
114
        } catch (Throwable t) {
115
            t.printStackTrace();
116
        } finally {
117
            ds.close();
118
        }
119
  */  }
116
	/**
117
	 * Muestra por la consola el contenido de un DataSource
118
	 *
119
	 * @param ds DataSource a mostrar
120
	 *
121
	 * @throws DriverException Si hay un error leyendo del DataSource
122
	 */
123
	private void mostrar(DataSource ds) throws DriverException {
124
		//Se muestra la tabla
125
		ds.start();
120 126

  
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();
127
		ds.getRowCount();
134 128

  
135
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
136
        String sql = "";
137
        System.out.print("> ");
138
        sql = in.readLine();
129
		Frame f = new Frame();
130
		f.setTableModel(new GDBMSTableModel(ds));
131
		f.show();
139 132

  
140
        while (!sql.equals("quit")) {
141
        	long t1 = System.currentTimeMillis();
142
            ByteArrayInputStream bytes = new ByteArrayInputStream(sql.getBytes());
143
            SQLEngine parser = new SQLEngine(bytes);
133
		/*
134
		   StringBuffer aux = new StringBuffer();
135
		   int fc = ds.getFieldCount();
136
		   int rc = ds.getRowCount();
137
		
138
		   for (int i = 0; i < fc; i++) {
139
		       aux.append(ds.getFieldName(i)).append("\t");
140
		   }
141
		   System.out.println(aux);
142
		   try {
143
		       for (int row = 0; row < rc; row++) {
144
		           aux.setLength(0);
145
		           for (int j = 0; j < fc; j++) {
146
		               aux.append(ds.getFieldValue(row, j)).append("\t");
147
		           }
148
		           //System.out.println(aux);
149
		       }
150
		   } catch (Throwable t) {
151
		       t.printStackTrace();
152
		   } finally {
153
		       ds.close();
154
		   }
155
		 */
156
	}
144 157

  
145
            try {
146
                parser.SQLStatement();
147
            } catch (ParseException e) {
148
                e.printStackTrace();
149
            }
158
	/**
159
	 * Bucle principal
160
	 *
161
	 * @throws DriverLoadException
162
	 * @throws IOException
163
	 * @throws InstantiationException
164
	 * @throws IllegalAccessException
165
	 * @throws ClassNotFoundException
166
	 */
167
	private void run()
168
		throws DriverLoadException, IOException, InstantiationException, 
169
			IllegalAccessException, ClassNotFoundException {
170
		setup();
150 171

  
151
            Node root = parser.getRootNode();
152
            Adapter rootAdapter = Utilities.buildTree(root.jjtGetChild(0));
172
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
173
		String sql = "";
174
		System.out.print("> ");
175
		sql = in.readLine();
153 176

  
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);
177
		while (!sql.equals("quit")) {
178
			long t1 = System.currentTimeMillis();
179
			ByteArrayInputStream bytes = new ByteArrayInputStream(sql.getBytes());
180
			SQLEngine parser = new SQLEngine(bytes);
166 181

  
167
                        InstructionHandler handler = new InstructionHandler();
168
                        SemanticParser sp = new SemanticParser();
169
                        sp.setContentHandler(handler);
170
                        sp.parse(root);
182
			try {
183
				parser.SQLStatement();
171 184

  
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
                }
185
				Node root = parser.getRootNode();
186
				Adapter rootAdapter = Utilities.buildTree(root.jjtGetChild(0),
187
						sql, ds);
183 188

  
184
                if (result != null) {
185
                    mostrar(result);
186
                }
187
            } catch (SemanticException e1) {
188
                e1.printStackTrace();
189
            }
189
				ParseTreeFrame parseTree = new ParseTreeFrame();
190
				parseTree.setTreeModel(new GDBMSParseTreeModel(
191
						(SimpleNode) root));
192
				parseTree.show();
190 193

  
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("> ");
194
				ParseTreeFrame parseTree2 = new ParseTreeFrame();
195
				parseTree2.setTreeModel(new GDBMSAdapterTreeModel(rootAdapter));
196
				parseTree2.show();
197
				Utilities.simplify(rootAdapter);
197 198

  
198
            sql = in.readLine();
199
        }
200
    }
199
				/*            ParseTreeFrame parseTree3 = new ParseTreeFrame();
200
				   parseTree3.setTreeModel(new GDBMSAdapterTreeModel(rootAdapter));
201
				   parseTree3.show();
202
				 */
203
				/*            obtenerXML(root);
204
				   InstructionHandler handler = new InstructionHandler();
205
				   SemanticParser sp = new SemanticParser();
206
				   sp.setContentHandler(handler);
207
				   sp.parse(root);
208
				   SelectInstruction s = handler.getInstr();
209
				 */
210
				DataSource result = null;
211
				long t2 = System.currentTimeMillis();
201 212

  
202
    /**
203
     * DOCUMENT ME!
204
     *
205
     * @param root
206
     */
207
    private void obtenerXML(Node root) {
208
        TransformerFactory transFact = TransformerFactory.newInstance();
213
				result = ds.executeSQL(sql, DataSourceFactory.AUTOMATIC_MODE);
214
				
215
				if (result != null) {
216
					mostrar(result);
217
				}
209 218

  
210
        if (transFact.getFeature(SAXTransformerFactory.FEATURE)) {
211
            SAXTransformerFactory saxTransFact = (SAXTransformerFactory) transFact;
212
            TransformerHandler transHand = null;
219
				long t3 = System.currentTimeMillis();
213 220

  
214
            try {
215
                transHand = saxTransFact.newTransformerHandler();
216
                transHand.setResult(new StreamResult(System.out));
221
				System.out.println("Tiempo de parseado: " +
222
					((t2 - t1) / 1000.0) + " segundos");
223
				System.out.println("Tiempo de ejecuci?n: " +
224
					((t3 - t2) / 1000.0) + " segundos");
225
				System.out.println("Tiempo de parseado y ejecuci?n: " +
226
					((t3 - t1) / 1000.0) + " segundos");
227
			} catch (ParseException e) {
228
				e.printStackTrace();
229
			} catch (TokenMgrError e) {
230
				e.printStackTrace();
231
			} catch (SemanticException e1) {
232
				e1.printStackTrace();
233
			} catch (DriverLoadException e) {
234
				e.printStackTrace();
235
			} catch (DriverException e) {
236
				e.printStackTrace();
237
			} catch (IOException e) {
238
				e.printStackTrace();
239
			}
217 240

  
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
    }
241
			System.out.print("> ");
242
			sql = in.readLine();
243
		}
244
	}
231 245

  
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);
246
	/**
247
	 * DOCUMENT ME!
248
	 *
249
	 * @param root
250
	 */
251
	private void obtenerXML(Node root) {
252
		TransformerFactory transFact = TransformerFactory.newInstance();
240 253

  
241
        for (int i = 0; i < root.jjtGetNumChildren(); i++) {
242
            parse(root.jjtGetChild(i), sp);
243
        }
244
    }
254
		if (transFact.getFeature(SAXTransformerFactory.FEATURE)) {
255
			SAXTransformerFactory saxTransFact = (SAXTransformerFactory) transFact;
256
			TransformerHandler transHand = null;
245 257

  
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
    }
258
			try {
259
				transHand = saxTransFact.newTransformerHandler();
260
				transHand.setResult(new StreamResult(System.out));
261

  
262
				SemanticParser sp = new SemanticParser();
263
				sp.setContentHandler(transHand);
264
				sp.parse(root);
265
			} catch (TransformerConfigurationException e) {
266
				e.printStackTrace();
267
			} catch (SAXException e) {
268
				e.printStackTrace();
269
			}
270
		} else {
271
			System.err.println("SAXTransformerFactory is not supported.");
272
			System.exit(1);
273
		}
274
	}
275

  
276
	/**
277
	 * Hace que el visitor sp visite el nodo actual y cada uno de sus hijos
278
	 *
279
	 * @param root nodo raiz que se va a visitar
280
	 * @param sp visitante
281
	 */
282
	private void parse(Node root, SemanticParser sp) {
283
		root.jjtAccept(sp, null);
284

  
285
		for (int i = 0; i < root.jjtGetNumChildren(); i++) {
286
			parse(root.jjtGetChild(i), sp);
287
		}
288
	}
289

  
290
	/**
291
	 * metodo de entrada
292
	 *
293
	 * @param args Si se produce alg?n error al inicializar los drivers
294
	 *
295
	 * @throws DriverException
296
	 * @throws SAXException
297
	 * @throws IOException
298
	 * @throws InstantiationException
299
	 * @throws IllegalAccessException
300
	 * @throws ClassNotFoundException
301
	 * @throws DriverLoadException
302
	 */
303
	public static void main(String[] args)
304
		throws DriverException, SAXException, IOException, 
305
			InstantiationException, IllegalAccessException, 
306
			ClassNotFoundException, DriverLoadException {
307
		GDBMSMain db = new GDBMSMain();
308
		db.run();
309
	}
263 310
}

Also available in: Unified diff