import java.sql.SQLException; import java.util.Vector; import java.io.*; import Zql.*; /** *
* ZDemo is able to send SQL queries to simple CSV (comma-separated values) * files; the CSV syntax used here is very simple: * The 1st line contains the column names * Other lines contain column values (tuples) * Values are separated by commas, so they can't contain commas (it's just * for demo purposes). * Example: * Create a num.db text file that contains the following: * a,b,c,d,e * 1,1,1,1,1 * 2,2,2,2,2 * 1,2,3,4,5 * 5,4,3,2,1 * You can then run ZDemo, and query it; some legal queries follow: * select * from num; * select a, b from num; * select a+b, c from num; * select * from num where a = 1 or e = 1; * select * from num where a = 1 and b = 1 or e = 1; * select d, e from num where a + b + c <= 3; * select * from num where 3 = a + b + c; * select * from num where a = b or e = d - 1; * select * from num where b ** a <= 2; **/ public class ZDemo { public static void main(String args[]) { try { ZqlParser p = null; if(args.length < 1) { System.out.println("Reading SQL from stdin (quit; or exit; to quit)"); p = new ZqlParser(System.in); } else { p = new ZqlParser(new DataInputStream(new FileInputStream(args[0]))); } // Read all SQL statements from input ZStatement st; while((st = p.readStatement()) != null) { System.out.println(st.toString()); // Display the statement if(st instanceof ZQuery) { // An SQL query: query the DB queryDB((ZQuery)st); } else if(st instanceof ZInsert) { // An SQL insert insertDB((ZInsert)st); } } } catch(Exception e) { e.printStackTrace(); } } /** * Query the database */ static void queryDB(ZQuery q) throws Exception { Vector sel = q.getSelect(); // SELECT part of the query Vector from = q.getFrom(); // FROM part of the query ZExpression where = (ZExpression)q.getWhere(); // WHERE part of the query if(from.size() > 1) { throw new SQLException("Joins are not supported"); } // Retrieve the table name in the FROM clause ZFromItem table = (ZFromItem)from.elementAt(0); // We suppose the data is in a text file called