1.1 --- a/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java Mon Nov 11 13:17:34 2019 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,229 +0,0 @@
1.4 -package org.apidesign.livedb.impl;
1.5 -
1.6 -import java.io.IOException;
1.7 -import java.io.Writer;
1.8 -import java.sql.CallableStatement;
1.9 -import java.sql.Connection;
1.10 -import java.sql.DatabaseMetaData;
1.11 -import java.sql.Driver;
1.12 -import java.sql.ResultSet;
1.13 -import java.sql.ResultSetMetaData;
1.14 -import java.sql.SQLException;
1.15 -import java.util.ArrayList;
1.16 -import java.util.Collections;
1.17 -import java.util.List;
1.18 -import java.util.Map.Entry;
1.19 -import java.util.Properties;
1.20 -import java.util.ServiceLoader;
1.21 -import java.util.Set;
1.22 -import javax.annotation.processing.AbstractProcessor;
1.23 -import javax.annotation.processing.Completion;
1.24 -import javax.annotation.processing.Completions;
1.25 -import javax.annotation.processing.Filer;
1.26 -import javax.annotation.processing.Processor;
1.27 -import javax.annotation.processing.RoundEnvironment;
1.28 -import javax.annotation.processing.SupportedAnnotationTypes;
1.29 -import javax.annotation.processing.SupportedSourceVersion;
1.30 -import javax.lang.model.SourceVersion;
1.31 -import javax.lang.model.element.AnnotationMirror;
1.32 -import javax.lang.model.element.AnnotationValue;
1.33 -import javax.lang.model.element.Element;
1.34 -import javax.lang.model.element.ExecutableElement;
1.35 -import javax.lang.model.element.PackageElement;
1.36 -import javax.lang.model.element.TypeElement;
1.37 -import javax.tools.JavaFileObject;
1.38 -import org.apidesign.livedb.LiveDB;
1.39 -import org.openide.util.lookup.ServiceProvider;
1.40 -
1.41 -/**
1.42 - *
1.43 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.44 - */
1.45 -// BEGIN: livedb.processor
1.46 -@SupportedAnnotationTypes("org.apidesign.livedb.LiveDB")
1.47 -@SupportedSourceVersion(SourceVersion.RELEASE_6)
1.48 -@ServiceProvider(service=Processor.class)
1.49 -public final class LiveDBProcessor extends AbstractProcessor {
1.50 - @Override
1.51 - public boolean process(
1.52 - Set<? extends TypeElement> annotations, RoundEnvironment roundEnv
1.53 - ) {
1.54 - final Filer filer = processingEnv.getFiler();
1.55 - for (Element e : roundEnv.getElementsAnnotatedWith(LiveDB.class)) {
1.56 - LiveDB db = e.getAnnotation(LiveDB.class);
1.57 - PackageElement pe = (PackageElement)e;
1.58 - String clsName = pe.getQualifiedName() + "." + db.classname();
1.59 - try {
1.60 - JavaFileObject src = filer.createSourceFile(clsName, pe);
1.61 - Writer w = src.openWriter();
1.62 - Connection c = getConnection(
1.63 - db.url(), db.user(), db.password()
1.64 - );
1.65 - CallableStatement s = c.prepareCall(db.query());
1.66 - ResultSet rs = s.executeQuery();
1.67 - ResultSetMetaData md = rs.getMetaData();
1.68 - generateClassHeader(w, pe, db);
1.69 - w.append("class " + db.classname() + " {\n");
1.70 - for (int i = 1; i <= md.getColumnCount(); i++) {
1.71 - generateField(w, md, i);
1.72 - }
1.73 - generateConstructor(w, db, md);
1.74 - generateQueryMethod(w, db, md);
1.75 - w.append("}");
1.76 - w.close();
1.77 - } catch (IOException ex) {
1.78 - throw new IllegalStateException(ex);
1.79 - } catch (SQLException ex) {
1.80 - throw new IllegalStateException(ex);
1.81 - }
1.82 - }
1.83 - return true;
1.84 - }
1.85 -// FINISH: livedb.processor
1.86 -
1.87 - private void generateQueryMethod(Writer w, LiveDB db, ResultSetMetaData md) throws SQLException, IOException {
1.88 - w.append(" public static List<" + db.classname() + "> ")
1.89 - .append("query() throws SQLException {\n");
1.90 - w.append(" Connection c = DriverManager.getConnection(\"")
1.91 - .append(db.url()).append("\", \"")
1.92 - .append(db.user()).append("\", \"")
1.93 - .append(db.password()).append("\");\n");
1.94 - w.append(" List<").append(db.classname())
1.95 - .append("> res = new ArrayList<")
1.96 - .append(db.classname()).append(">();\n");
1.97 - w.append(" CallableStatement s = c.prepareCall(\"")
1.98 - .append(db.query()).append("\");\n");
1.99 - w.append(" ResultSet rs = s.executeQuery();\n");
1.100 - w.append(" ResultSetMetaData md = rs.getMetaData();\n");
1.101 - w.append(" while (rs.next()) {\n");
1.102 - w.append(" res.add(new " + db.classname() + "(\n");
1.103 - for (int i = 1; i <= md.getColumnCount(); i++) {
1.104 - w.append(" (")
1.105 - .append(md.getColumnClassName(i))
1.106 - .append(")rs.getObject(" + i).append(")");
1.107 - if (i < md.getColumnCount()) {
1.108 - w.append(",\n");
1.109 - } else {
1.110 - w.append("\n");
1.111 - }
1.112 - }
1.113 - w.append(" ));\n");
1.114 - w.append(" };\n");
1.115 - w.append(" return res;\n");
1.116 - w.append(" }");
1.117 - }
1.118 -
1.119 - private void generateConstructor(Writer w, LiveDB db, ResultSetMetaData md) throws SQLException, IOException {
1.120 - w.append(" private " + db.classname() + "(\n");
1.121 - for (int i = 1; i <= md.getColumnCount(); i++) {
1.122 - w.append(" ").append(md.getColumnClassName(i))
1.123 - .append(" ").append(md.getColumnName(i));
1.124 - if (i < md.getColumnCount()) {
1.125 - w.append(",\n");
1.126 - } else {
1.127 - w.append("\n");
1.128 - }
1.129 - }
1.130 - w.append(" ) {\n");
1.131 - for (int i = 1; i <= md.getColumnCount(); i++) {
1.132 - w.append(" this.")
1.133 - .append(md.getColumnName(i))
1.134 - .append(" = ")
1.135 - .append(md.getColumnName(i))
1.136 - .append(";\n");
1.137 - }
1.138 - w.append(" }\n");
1.139 - }
1.140 -
1.141 - private void generateField(Writer w, ResultSetMetaData md, int i) throws IOException, SQLException {
1.142 - w.append(" public final ")
1.143 - .append(md.getColumnClassName(i))
1.144 - .append(" ")
1.145 - .append(md.getColumnName(i))
1.146 - .append(";\n");
1.147 - }
1.148 -
1.149 - private void generateClassHeader(Writer w, PackageElement pe, LiveDB db) throws IOException {
1.150 - w.append("package " + pe.getQualifiedName() + ";\n");
1.151 - w.append("import java.util.List;\n");
1.152 - w.append("import java.util.ArrayList;\n");
1.153 - w.append("import java.sql.*;\n");
1.154 - }
1.155 - private static Connection getConnection(String url, String user, String password)
1.156 - throws SQLException {
1.157 - final ClassLoader cl = LiveDBProcessor.class.getClassLoader();
1.158 - for (Driver d : ServiceLoader.load(Driver.class, cl)) {
1.159 -// System.out.println("looked up: " + d);
1.160 - if (d.acceptsURL(url)) {
1.161 - //System.out.println("accepts: " + d);
1.162 - Properties p = new Properties();
1.163 - p.put("user", user);
1.164 - p.put("password", password);
1.165 - return d.connect(url, p);
1.166 - }
1.167 - }
1.168 - throw new SQLException("No driver found for " + url);
1.169 - }
1.170 -
1.171 -
1.172 -
1.173 - // BEGIN: livedb.completions
1.174 - @Override
1.175 - public Iterable<? extends Completion> getCompletions(
1.176 - Element element, AnnotationMirror annotation,
1.177 - ExecutableElement member, String userText
1.178 - ) {
1.179 - if (!"query".equals(member.getSimpleName().toString())) {
1.180 - return Collections.emptyList();
1.181 - }
1.182 - if (userText == null || userText.length() <= 1) {
1.183 - return Collections.singleton(Completions.of("\"SELECT "));
1.184 - }
1.185 - if (userText.toUpperCase().matches(".*FROM *")) {
1.186 - String user = extractValue(annotation, "user");
1.187 - String password = extractValue(annotation, "password");
1.188 - String url = extractValue(annotation, "url");
1.189 - if (user == null || password == null || url == null) {
1.190 - return Collections.emptyList();
1.191 - }
1.192 - try {
1.193 - List<Completion> arr = new ArrayList<Completion>();
1.194 - Connection c = getConnection(url, user, password);
1.195 - DatabaseMetaData meta = c.getMetaData();
1.196 - ResultSet res = meta.getTables(null, null, "%", null);
1.197 - boolean ok = res.first();
1.198 - while (ok) {
1.199 - String txt = userText + res.getString("TABLE_NAME");
1.200 - arr.add(Completions.of(txt));
1.201 - ok = res.next();
1.202 - }
1.203 - return arr;
1.204 - } catch (SQLException ex) {
1.205 - throw new IllegalStateException(ex);
1.206 - }
1.207 - }
1.208 - return Collections.emptyList();
1.209 - }
1.210 - // END: livedb.completions
1.211 -
1.212 - private static String extractValue(AnnotationMirror am, String param) {
1.213 - AnnotationValue av = null;
1.214 - for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) {
1.215 - if (entry.getKey().toString().equals(param + "()")) {
1.216 - av = entry.getValue();
1.217 - break;
1.218 - }
1.219 - }
1.220 - if (av == null) {
1.221 - return null;
1.222 - }
1.223 - String s = av.toString();
1.224 - if (s.startsWith("\"")) {
1.225 - s = s.substring(1);
1.226 - }
1.227 - if (s.endsWith("\"")) {
1.228 - s = s.substring(0, s.length() - 1);
1.229 - }
1.230 - return s;
1.231 - }
1.232 -}