Rewritting the processor to really connect to a database and read metadata from a real select statement
1.1 --- a/samples/livedb/src/org/apidesign/livedb/LiveDB.java Mon Jun 28 09:29:30 2010 +0200
1.2 +++ b/samples/livedb/src/org/apidesign/livedb/LiveDB.java Wed Jul 14 20:56:33 2010 +0200
1.3 @@ -12,6 +12,9 @@
1.4 @Target(ElementType.PACKAGE)
1.5 @Retention(RetentionPolicy.SOURCE)
1.6 public @interface LiveDB {
1.7 + String url();
1.8 + String user();
1.9 + String password();
1.10 + String query();
1.11 String classname();
1.12 - String field();
1.13 }
2.1 --- a/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java Mon Jun 28 09:29:30 2010 +0200
2.2 +++ b/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java Wed Jul 14 20:56:33 2010 +0200
2.3 @@ -7,6 +7,14 @@
2.4
2.5 import java.io.IOException;
2.6 import java.io.Writer;
2.7 +import java.sql.CallableStatement;
2.8 +import java.sql.Connection;
2.9 +import java.sql.Driver;
2.10 +import java.sql.DriverManager;
2.11 +import java.sql.ResultSet;
2.12 +import java.sql.ResultSetMetaData;
2.13 +import java.sql.SQLException;
2.14 +import java.util.Enumeration;
2.15 import java.util.Set;
2.16 import javax.annotation.processing.AbstractProcessor;
2.17 import javax.annotation.processing.Processor;
2.18 @@ -38,16 +46,69 @@
2.19 try {
2.20 JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe);
2.21 Writer w = src.openWriter();
2.22 + Connection c = DriverManager.getConnection(db.url(), db.user(), db.password());
2.23 + CallableStatement s = c.prepareCall(db.query());
2.24 + ResultSet rs = s.executeQuery();
2.25 + ResultSetMetaData md = rs.getMetaData();
2.26 w.append("package " + pe.getQualifiedName() + ";\n");
2.27 + w.append("import java.util.List;\n");
2.28 + w.append("import java.util.ArrayList;\n");
2.29 + w.append("import java.sql.*;\n");
2.30 w.append("class " + db.classname() + " {\n");
2.31 - w.append(" public String " + db.field() + ";\n");
2.32 + for (int i = 1; i <= md.getColumnCount(); i++) {
2.33 + w.append(" public final " + md.getColumnClassName(i) + " " + md.getColumnName(i) + ";\n");
2.34 + }
2.35 + w.append(" private " + db.classname() + "(\n");
2.36 + for (int i = 1; i <= md.getColumnCount(); i++) {
2.37 + w.append(" " + md.getColumnClassName(i) + " " + md.getColumnName(i));
2.38 + if (i < md.getColumnCount()) {
2.39 + w.append(",\n");
2.40 + } else {
2.41 + w.append("\n");
2.42 + }
2.43 + }
2.44 + w.append(" ) {\n");
2.45 + for (int i = 1; i <= md.getColumnCount(); i++) {
2.46 + w.append(" this." + md.getColumnName(i) + " = " + md.getColumnName(i) + ";\n");
2.47 + }
2.48 + w.append(" }\n");
2.49 + w.append(" public static List<" + db.classname() + "> query() throws SQLException {\n");
2.50 + w.append(" Connection c = DriverManager.getConnection(\"" + db.url() + "\", \"" + db.user() + "\", \"" + db.password() +"\");\n");
2.51 + w.append(" List<" + db.classname() + "> res = new ArrayList<" + db.classname() + ">();\n");
2.52 + w.append(" CallableStatement s = c.prepareCall(\"" + db.query() + "\");\n");
2.53 + w.append(" ResultSet rs = s.executeQuery();\n");
2.54 + w.append(" ResultSetMetaData md = rs.getMetaData();\n");
2.55 + w.append(" while (rs.next()) {\n");
2.56 + w.append(" res.add(new " + db.classname() + "(\n");
2.57 + for (int i = 1; i <= md.getColumnCount(); i++) {
2.58 + w.append(" (" + md.getColumnClassName(i) + ")rs.getObject(" + i + ")");
2.59 + if (i < md.getColumnCount()) {
2.60 + w.append(",\n");
2.61 + } else {
2.62 + w.append("\n");
2.63 + }
2.64 + }
2.65 + w.append(" ));\n");
2.66 + w.append(" };\n");
2.67 + w.append(" return res;\n");
2.68 + w.append(" }");
2.69 w.append("}");
2.70 w.close();
2.71 } catch (IOException ex) {
2.72 throw new IllegalStateException(ex);
2.73 + } catch (SQLException ex) {
2.74 + throw new IllegalStateException(ex);
2.75 }
2.76 }
2.77 return true;
2.78 }
2.79
2.80 + static {
2.81 + // init drivers
2.82 + Enumeration<Driver> en;
2.83 + en = DriverManager.getDrivers();
2.84 + while (en.hasMoreElements()) {
2.85 + Driver driver = en.nextElement();
2.86 + }
2.87 + }
2.88 }
3.1 --- a/samples/livedb/test/org/apidesign/livedb/example/LiveDBTest.java Mon Jun 28 09:29:30 2010 +0200
3.2 +++ b/samples/livedb/test/org/apidesign/livedb/example/LiveDBTest.java Wed Jul 14 20:56:33 2010 +0200
3.3 @@ -5,6 +5,8 @@
3.4
3.5 package org.apidesign.livedb.example;
3.6
3.7 +import java.sql.SQLException;
3.8 +import java.util.List;
3.9 import junit.framework.TestCase;
3.10
3.11 /**
3.12 @@ -17,10 +19,11 @@
3.13 super(testName);
3.14 }
3.15
3.16 - public void testSomeMethod() {
3.17 - DBAccess db = new DBAccess();
3.18 - db.jarda = "Ahoj";
3.19 - assertEquals("Ahoj", db.jarda);
3.20 + public void testSomeMethod() throws SQLException {
3.21 + List<Age> ages = Age.query();
3.22 + for (Age age : ages) {
3.23 + System.out.printf("%s is %s years old\n", age.NAME, age.AGE);
3.24 + }
3.25 }
3.26
3.27 }
4.1 --- a/samples/livedb/test/org/apidesign/livedb/example/package-info.java Mon Jun 28 09:29:30 2010 +0200
4.2 +++ b/samples/livedb/test/org/apidesign/livedb/example/package-info.java Wed Jul 14 20:56:33 2010 +0200
4.3 @@ -1,6 +1,10 @@
4.4
4.5
4.6 -@LiveDB(classname="DBAccess", field="jarda")
4.7 +@LiveDB(
4.8 + classname="Age", password="j1", user="j1",
4.9 + query="select * from APP.AGE",
4.10 + url="jdbc:derby://localhost:1527/livedb"
4.11 +)
4.12 package org.apidesign.livedb.example;
4.13
4.14 import org.apidesign.livedb.LiveDB;