1.1 --- a/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java Fri Jul 30 14:13:25 2010 +0200
1.2 +++ b/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java Mon Nov 11 13:17:34 2019 +0100
1.3 @@ -4,21 +4,31 @@
1.4 import java.io.Writer;
1.5 import java.sql.CallableStatement;
1.6 import java.sql.Connection;
1.7 +import java.sql.DatabaseMetaData;
1.8 import java.sql.Driver;
1.9 import java.sql.ResultSet;
1.10 import java.sql.ResultSetMetaData;
1.11 import java.sql.SQLException;
1.12 +import java.util.ArrayList;
1.13 +import java.util.Collections;
1.14 +import java.util.List;
1.15 +import java.util.Map.Entry;
1.16 import java.util.Properties;
1.17 import java.util.ServiceLoader;
1.18 import java.util.Set;
1.19 import javax.annotation.processing.AbstractProcessor;
1.20 +import javax.annotation.processing.Completion;
1.21 +import javax.annotation.processing.Completions;
1.22 import javax.annotation.processing.Filer;
1.23 import javax.annotation.processing.Processor;
1.24 import javax.annotation.processing.RoundEnvironment;
1.25 import javax.annotation.processing.SupportedAnnotationTypes;
1.26 import javax.annotation.processing.SupportedSourceVersion;
1.27 import javax.lang.model.SourceVersion;
1.28 +import javax.lang.model.element.AnnotationMirror;
1.29 +import javax.lang.model.element.AnnotationValue;
1.30 import javax.lang.model.element.Element;
1.31 +import javax.lang.model.element.ExecutableElement;
1.32 import javax.lang.model.element.PackageElement;
1.33 import javax.lang.model.element.TypeElement;
1.34 import javax.tools.JavaFileObject;
1.35 @@ -154,4 +164,66 @@
1.36 }
1.37 throw new SQLException("No driver found for " + url);
1.38 }
1.39 +
1.40 +
1.41 +
1.42 + // BEGIN: livedb.completions
1.43 + @Override
1.44 + public Iterable<? extends Completion> getCompletions(
1.45 + Element element, AnnotationMirror annotation,
1.46 + ExecutableElement member, String userText
1.47 + ) {
1.48 + if (!"query".equals(member.getSimpleName().toString())) {
1.49 + return Collections.emptyList();
1.50 + }
1.51 + if (userText == null || userText.length() <= 1) {
1.52 + return Collections.singleton(Completions.of("\"SELECT "));
1.53 + }
1.54 + if (userText.toUpperCase().matches(".*FROM *")) {
1.55 + String user = extractValue(annotation, "user");
1.56 + String password = extractValue(annotation, "password");
1.57 + String url = extractValue(annotation, "url");
1.58 + if (user == null || password == null || url == null) {
1.59 + return Collections.emptyList();
1.60 + }
1.61 + try {
1.62 + List<Completion> arr = new ArrayList<Completion>();
1.63 + Connection c = getConnection(url, user, password);
1.64 + DatabaseMetaData meta = c.getMetaData();
1.65 + ResultSet res = meta.getTables(null, null, "%", null);
1.66 + boolean ok = res.first();
1.67 + while (ok) {
1.68 + String txt = userText + res.getString("TABLE_NAME");
1.69 + arr.add(Completions.of(txt));
1.70 + ok = res.next();
1.71 + }
1.72 + return arr;
1.73 + } catch (SQLException ex) {
1.74 + throw new IllegalStateException(ex);
1.75 + }
1.76 + }
1.77 + return Collections.emptyList();
1.78 + }
1.79 + // END: livedb.completions
1.80 +
1.81 + private static String extractValue(AnnotationMirror am, String param) {
1.82 + AnnotationValue av = null;
1.83 + for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet()) {
1.84 + if (entry.getKey().toString().equals(param + "()")) {
1.85 + av = entry.getValue();
1.86 + break;
1.87 + }
1.88 + }
1.89 + if (av == null) {
1.90 + return null;
1.91 + }
1.92 + String s = av.toString();
1.93 + if (s.startsWith("\"")) {
1.94 + s = s.substring(1);
1.95 + }
1.96 + if (s.endsWith("\"")) {
1.97 + s = s.substring(0, s.length() - 1);
1.98 + }
1.99 + return s;
1.100 + }
1.101 }