author | Jaroslav Tulach <jtulach@netbeans.org> |
Wed, 14 Jul 2010 20:56:33 +0200 | |
branch | livedb |
changeset 357 | 837370f791ba |
parent 355 | d1e7424dc988 |
child 358 | afdd66815ee3 |
permissions | -rw-r--r-- |
jtulach@355 | 1 |
/* |
jtulach@355 | 2 |
* To change this template, choose Tools | Templates |
jtulach@355 | 3 |
* and open the template in the editor. |
jtulach@355 | 4 |
*/ |
jtulach@355 | 5 |
|
jtulach@355 | 6 |
package org.apidesign.livedb.impl; |
jtulach@355 | 7 |
|
jtulach@355 | 8 |
import java.io.IOException; |
jtulach@355 | 9 |
import java.io.Writer; |
jtulach@357 | 10 |
import java.sql.CallableStatement; |
jtulach@357 | 11 |
import java.sql.Connection; |
jtulach@357 | 12 |
import java.sql.Driver; |
jtulach@357 | 13 |
import java.sql.DriverManager; |
jtulach@357 | 14 |
import java.sql.ResultSet; |
jtulach@357 | 15 |
import java.sql.ResultSetMetaData; |
jtulach@357 | 16 |
import java.sql.SQLException; |
jtulach@357 | 17 |
import java.util.Enumeration; |
jtulach@355 | 18 |
import java.util.Set; |
jtulach@355 | 19 |
import javax.annotation.processing.AbstractProcessor; |
jtulach@355 | 20 |
import javax.annotation.processing.Processor; |
jtulach@355 | 21 |
import javax.annotation.processing.RoundEnvironment; |
jtulach@355 | 22 |
import javax.annotation.processing.SupportedAnnotationTypes; |
jtulach@355 | 23 |
import javax.annotation.processing.SupportedSourceVersion; |
jtulach@355 | 24 |
import javax.lang.model.SourceVersion; |
jtulach@355 | 25 |
import javax.lang.model.element.Element; |
jtulach@355 | 26 |
import javax.lang.model.element.PackageElement; |
jtulach@355 | 27 |
import javax.lang.model.element.TypeElement; |
jtulach@355 | 28 |
import javax.tools.JavaFileObject; |
jtulach@355 | 29 |
import org.apidesign.livedb.LiveDB; |
jtulach@355 | 30 |
import org.openide.util.lookup.ServiceProvider; |
jtulach@355 | 31 |
|
jtulach@355 | 32 |
/** |
jtulach@355 | 33 |
* |
jtulach@355 | 34 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jtulach@355 | 35 |
*/ |
jtulach@355 | 36 |
@SupportedAnnotationTypes("org.apidesign.livedb.LiveDB") |
jtulach@355 | 37 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
jtulach@355 | 38 |
@ServiceProvider(service=Processor.class) |
jtulach@355 | 39 |
public class LiveDBProcessor extends AbstractProcessor { |
jtulach@355 | 40 |
@Override |
jtulach@355 | 41 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jtulach@355 | 42 |
for (Element e : roundEnv.getElementsAnnotatedWith(LiveDB.class)) { |
jtulach@355 | 43 |
LiveDB db = e.getAnnotation(LiveDB.class); |
jtulach@355 | 44 |
PackageElement pe = (PackageElement)e; |
jtulach@355 | 45 |
String clsName = pe.getQualifiedName().toString() + "." + db.classname(); |
jtulach@355 | 46 |
try { |
jtulach@355 | 47 |
JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe); |
jtulach@355 | 48 |
Writer w = src.openWriter(); |
jtulach@357 | 49 |
Connection c = DriverManager.getConnection(db.url(), db.user(), db.password()); |
jtulach@357 | 50 |
CallableStatement s = c.prepareCall(db.query()); |
jtulach@357 | 51 |
ResultSet rs = s.executeQuery(); |
jtulach@357 | 52 |
ResultSetMetaData md = rs.getMetaData(); |
jtulach@355 | 53 |
w.append("package " + pe.getQualifiedName() + ";\n"); |
jtulach@357 | 54 |
w.append("import java.util.List;\n"); |
jtulach@357 | 55 |
w.append("import java.util.ArrayList;\n"); |
jtulach@357 | 56 |
w.append("import java.sql.*;\n"); |
jtulach@355 | 57 |
w.append("class " + db.classname() + " {\n"); |
jtulach@357 | 58 |
for (int i = 1; i <= md.getColumnCount(); i++) { |
jtulach@357 | 59 |
w.append(" public final " + md.getColumnClassName(i) + " " + md.getColumnName(i) + ";\n"); |
jtulach@357 | 60 |
} |
jtulach@357 | 61 |
w.append(" private " + db.classname() + "(\n"); |
jtulach@357 | 62 |
for (int i = 1; i <= md.getColumnCount(); i++) { |
jtulach@357 | 63 |
w.append(" " + md.getColumnClassName(i) + " " + md.getColumnName(i)); |
jtulach@357 | 64 |
if (i < md.getColumnCount()) { |
jtulach@357 | 65 |
w.append(",\n"); |
jtulach@357 | 66 |
} else { |
jtulach@357 | 67 |
w.append("\n"); |
jtulach@357 | 68 |
} |
jtulach@357 | 69 |
} |
jtulach@357 | 70 |
w.append(" ) {\n"); |
jtulach@357 | 71 |
for (int i = 1; i <= md.getColumnCount(); i++) { |
jtulach@357 | 72 |
w.append(" this." + md.getColumnName(i) + " = " + md.getColumnName(i) + ";\n"); |
jtulach@357 | 73 |
} |
jtulach@357 | 74 |
w.append(" }\n"); |
jtulach@357 | 75 |
w.append(" public static List<" + db.classname() + "> query() throws SQLException {\n"); |
jtulach@357 | 76 |
w.append(" Connection c = DriverManager.getConnection(\"" + db.url() + "\", \"" + db.user() + "\", \"" + db.password() +"\");\n"); |
jtulach@357 | 77 |
w.append(" List<" + db.classname() + "> res = new ArrayList<" + db.classname() + ">();\n"); |
jtulach@357 | 78 |
w.append(" CallableStatement s = c.prepareCall(\"" + db.query() + "\");\n"); |
jtulach@357 | 79 |
w.append(" ResultSet rs = s.executeQuery();\n"); |
jtulach@357 | 80 |
w.append(" ResultSetMetaData md = rs.getMetaData();\n"); |
jtulach@357 | 81 |
w.append(" while (rs.next()) {\n"); |
jtulach@357 | 82 |
w.append(" res.add(new " + db.classname() + "(\n"); |
jtulach@357 | 83 |
for (int i = 1; i <= md.getColumnCount(); i++) { |
jtulach@357 | 84 |
w.append(" (" + md.getColumnClassName(i) + ")rs.getObject(" + i + ")"); |
jtulach@357 | 85 |
if (i < md.getColumnCount()) { |
jtulach@357 | 86 |
w.append(",\n"); |
jtulach@357 | 87 |
} else { |
jtulach@357 | 88 |
w.append("\n"); |
jtulach@357 | 89 |
} |
jtulach@357 | 90 |
} |
jtulach@357 | 91 |
w.append(" ));\n"); |
jtulach@357 | 92 |
w.append(" };\n"); |
jtulach@357 | 93 |
w.append(" return res;\n"); |
jtulach@357 | 94 |
w.append(" }"); |
jtulach@355 | 95 |
w.append("}"); |
jtulach@355 | 96 |
w.close(); |
jtulach@355 | 97 |
} catch (IOException ex) { |
jtulach@355 | 98 |
throw new IllegalStateException(ex); |
jtulach@357 | 99 |
} catch (SQLException ex) { |
jtulach@357 | 100 |
throw new IllegalStateException(ex); |
jtulach@355 | 101 |
} |
jtulach@355 | 102 |
} |
jtulach@355 | 103 |
return true; |
jtulach@355 | 104 |
} |
jtulach@355 | 105 |
|
jtulach@357 | 106 |
static { |
jtulach@357 | 107 |
// init drivers |
jtulach@357 | 108 |
Enumeration<Driver> en; |
jtulach@357 | 109 |
en = DriverManager.getDrivers(); |
jtulach@357 | 110 |
while (en.hasMoreElements()) { |
jtulach@357 | 111 |
Driver driver = en.nextElement(); |
jtulach@357 | 112 |
} |
jtulach@357 | 113 |
} |
jtulach@355 | 114 |
} |