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@358
|
18 |
import java.util.Properties;
|
jtulach@358
|
19 |
import java.util.ServiceLoader;
|
jtulach@355
|
20 |
import java.util.Set;
|
jtulach@358
|
21 |
import java.util.logging.Level;
|
jtulach@358
|
22 |
import java.util.logging.Logger;
|
jtulach@355
|
23 |
import javax.annotation.processing.AbstractProcessor;
|
jtulach@355
|
24 |
import javax.annotation.processing.Processor;
|
jtulach@355
|
25 |
import javax.annotation.processing.RoundEnvironment;
|
jtulach@355
|
26 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
jtulach@355
|
27 |
import javax.annotation.processing.SupportedSourceVersion;
|
jtulach@355
|
28 |
import javax.lang.model.SourceVersion;
|
jtulach@355
|
29 |
import javax.lang.model.element.Element;
|
jtulach@355
|
30 |
import javax.lang.model.element.PackageElement;
|
jtulach@355
|
31 |
import javax.lang.model.element.TypeElement;
|
jtulach@355
|
32 |
import javax.tools.JavaFileObject;
|
jtulach@355
|
33 |
import org.apidesign.livedb.LiveDB;
|
jtulach@355
|
34 |
import org.openide.util.lookup.ServiceProvider;
|
jtulach@355
|
35 |
|
jtulach@355
|
36 |
/**
|
jtulach@355
|
37 |
*
|
jtulach@355
|
38 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@355
|
39 |
*/
|
jtulach@355
|
40 |
@SupportedAnnotationTypes("org.apidesign.livedb.LiveDB")
|
jtulach@355
|
41 |
@SupportedSourceVersion(SourceVersion.RELEASE_6)
|
jtulach@355
|
42 |
@ServiceProvider(service=Processor.class)
|
jtulach@355
|
43 |
public class LiveDBProcessor extends AbstractProcessor {
|
jtulach@355
|
44 |
@Override
|
jtulach@355
|
45 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jtulach@355
|
46 |
for (Element e : roundEnv.getElementsAnnotatedWith(LiveDB.class)) {
|
jtulach@355
|
47 |
LiveDB db = e.getAnnotation(LiveDB.class);
|
jtulach@355
|
48 |
PackageElement pe = (PackageElement)e;
|
jtulach@355
|
49 |
String clsName = pe.getQualifiedName().toString() + "." + db.classname();
|
jtulach@355
|
50 |
try {
|
jtulach@355
|
51 |
JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe);
|
jtulach@355
|
52 |
Writer w = src.openWriter();
|
jtulach@358
|
53 |
Connection c = getConnection(db.url(), db.user(), db.password());
|
jtulach@357
|
54 |
CallableStatement s = c.prepareCall(db.query());
|
jtulach@357
|
55 |
ResultSet rs = s.executeQuery();
|
jtulach@357
|
56 |
ResultSetMetaData md = rs.getMetaData();
|
jtulach@355
|
57 |
w.append("package " + pe.getQualifiedName() + ";\n");
|
jtulach@357
|
58 |
w.append("import java.util.List;\n");
|
jtulach@357
|
59 |
w.append("import java.util.ArrayList;\n");
|
jtulach@357
|
60 |
w.append("import java.sql.*;\n");
|
jtulach@355
|
61 |
w.append("class " + db.classname() + " {\n");
|
jtulach@357
|
62 |
for (int i = 1; i <= md.getColumnCount(); i++) {
|
jtulach@357
|
63 |
w.append(" public final " + md.getColumnClassName(i) + " " + md.getColumnName(i) + ";\n");
|
jtulach@357
|
64 |
}
|
jtulach@357
|
65 |
w.append(" private " + db.classname() + "(\n");
|
jtulach@357
|
66 |
for (int i = 1; i <= md.getColumnCount(); i++) {
|
jtulach@357
|
67 |
w.append(" " + md.getColumnClassName(i) + " " + md.getColumnName(i));
|
jtulach@357
|
68 |
if (i < md.getColumnCount()) {
|
jtulach@357
|
69 |
w.append(",\n");
|
jtulach@357
|
70 |
} else {
|
jtulach@357
|
71 |
w.append("\n");
|
jtulach@357
|
72 |
}
|
jtulach@357
|
73 |
}
|
jtulach@357
|
74 |
w.append(" ) {\n");
|
jtulach@357
|
75 |
for (int i = 1; i <= md.getColumnCount(); i++) {
|
jtulach@357
|
76 |
w.append(" this." + md.getColumnName(i) + " = " + md.getColumnName(i) + ";\n");
|
jtulach@357
|
77 |
}
|
jtulach@357
|
78 |
w.append(" }\n");
|
jtulach@357
|
79 |
w.append(" public static List<" + db.classname() + "> query() throws SQLException {\n");
|
jtulach@357
|
80 |
w.append(" Connection c = DriverManager.getConnection(\"" + db.url() + "\", \"" + db.user() + "\", \"" + db.password() +"\");\n");
|
jtulach@357
|
81 |
w.append(" List<" + db.classname() + "> res = new ArrayList<" + db.classname() + ">();\n");
|
jtulach@357
|
82 |
w.append(" CallableStatement s = c.prepareCall(\"" + db.query() + "\");\n");
|
jtulach@357
|
83 |
w.append(" ResultSet rs = s.executeQuery();\n");
|
jtulach@357
|
84 |
w.append(" ResultSetMetaData md = rs.getMetaData();\n");
|
jtulach@357
|
85 |
w.append(" while (rs.next()) {\n");
|
jtulach@357
|
86 |
w.append(" res.add(new " + db.classname() + "(\n");
|
jtulach@357
|
87 |
for (int i = 1; i <= md.getColumnCount(); i++) {
|
jtulach@357
|
88 |
w.append(" (" + md.getColumnClassName(i) + ")rs.getObject(" + i + ")");
|
jtulach@357
|
89 |
if (i < md.getColumnCount()) {
|
jtulach@357
|
90 |
w.append(",\n");
|
jtulach@357
|
91 |
} else {
|
jtulach@357
|
92 |
w.append("\n");
|
jtulach@357
|
93 |
}
|
jtulach@357
|
94 |
}
|
jtulach@357
|
95 |
w.append(" ));\n");
|
jtulach@357
|
96 |
w.append(" };\n");
|
jtulach@357
|
97 |
w.append(" return res;\n");
|
jtulach@357
|
98 |
w.append(" }");
|
jtulach@355
|
99 |
w.append("}");
|
jtulach@355
|
100 |
w.close();
|
jtulach@355
|
101 |
} catch (IOException ex) {
|
jtulach@355
|
102 |
throw new IllegalStateException(ex);
|
jtulach@357
|
103 |
} catch (SQLException ex) {
|
jtulach@357
|
104 |
throw new IllegalStateException(ex);
|
jtulach@355
|
105 |
}
|
jtulach@355
|
106 |
}
|
jtulach@355
|
107 |
return true;
|
jtulach@355
|
108 |
}
|
jtulach@355
|
109 |
|
jtulach@358
|
110 |
private static Connection getConnection(String url, String user, String password)
|
jtulach@358
|
111 |
throws SQLException {
|
jtulach@358
|
112 |
final ClassLoader cl = LiveDBProcessor.class.getClassLoader();
|
jtulach@358
|
113 |
for (Driver d : ServiceLoader.load(Driver.class, cl)) {
|
jtulach@358
|
114 |
// System.out.println("looked up: " + d);
|
jtulach@358
|
115 |
if (d.acceptsURL(url)) {
|
jtulach@358
|
116 |
//System.out.println("accepts: " + d);
|
jtulach@358
|
117 |
Properties p = new Properties();
|
jtulach@358
|
118 |
p.put("user", user);
|
jtulach@358
|
119 |
p.put("password", password);
|
jtulach@358
|
120 |
return d.connect(url, p);
|
jtulach@358
|
121 |
}
|
jtulach@357
|
122 |
}
|
jtulach@358
|
123 |
throw new SQLException("No driver found for " + url);
|
jtulach@357
|
124 |
}
|
jtulach@355
|
125 |
}
|