Merging livedb branch, as it seems to work OK now
authorJaroslav Tulach <jtulach@netbeans.org>
Fri, 16 Jul 2010 23:36:14 +0200
changeset 3602b670a8a31ae
parent 356 23511f8a9718
parent 359 9d430d9bc4b1
child 361 6507a9474b6d
Merging livedb branch, as it seems to work OK now
     1.1 --- a/samples/libs/build.xml	Mon Jun 28 09:29:30 2010 +0200
     1.2 +++ b/samples/libs/build.xml	Fri Jul 16 23:36:14 2010 +0200
     1.3 @@ -5,6 +5,14 @@
     1.4              <param name="library" value="junit-4.4"/>
     1.5              <param name="url" value="http://switch.dl.sourceforge.net/sourceforge/junit/junit-4.4.jar"/>
     1.6          </antcall>
     1.7 +        <antcall target="-library">
     1.8 +            <param name="library" value="derby"/>
     1.9 +            <param name="url" value="http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/derby/derby/10.6.1.0/derby-10.6.1.0.jar"/>
    1.10 +        </antcall>
    1.11 +        <antcall target="-library">
    1.12 +            <param name="library" value="derbyclient"/>
    1.13 +            <param name="url" value="http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/derby/derbyclient/10.6.1.0/derbyclient-10.6.1.0.jar"/>
    1.14 +        </antcall>
    1.15          <antcall target="-library-from-nbm">
    1.16              <param name="library" value="org.openide.util.lookup"/>
    1.17              <param name="library.include" value="**/org-openide-util-lookup.jar"/>
     2.1 --- a/samples/livedb/build.xml	Mon Jun 28 09:29:30 2010 +0200
     2.2 +++ b/samples/livedb/build.xml	Fri Jul 16 23:36:14 2010 +0200
     2.3 @@ -71,4 +71,39 @@
     2.4      nbproject/build-impl.xml file. 
     2.5  
     2.6      -->
     2.7 +    
     2.8 +    <target name="-pre-compile-test">
     2.9 +        <antcall target="create-db"/>
    2.10 +    </target>
    2.11 +    
    2.12 +    <!-- BEGIN: livedb.derby.create -->
    2.13 +    <target name="-check-db">
    2.14 +        <property name="db" location="build/classes/db"/>
    2.15 +        <available property="db.exists" file="${db}"/>
    2.16 +    </target>
    2.17 +    <target name="create-db" unless="db.exists" depends="init,-check-db">
    2.18 +        <mkdir dir="${db}"/>
    2.19 +        <delete dir="${db}"/>
    2.20 +        <echo message="Creating DB in ${db}"/>
    2.21 +        <sql classpath="${file.reference.derby.jar}" userid="j1" password="j1" 
    2.22 +            url="jdbc:derby:${db};create=true" 
    2.23 +            driver="org.apache.derby.jdbc.EmbeddedDriver"
    2.24 +        >
    2.25 +        create table APP.AGE (
    2.26 +            NAME VARCHAR(30),
    2.27 +            AGE NUMERIC(3)
    2.28 +        );
    2.29 +        insert into APP.AGE values ('apidesign', 3);
    2.30 +        </sql>
    2.31 +        <!-- don't forget to shutdown the DB -->
    2.32 +        <sql classpath="${file.reference.derby.jar}" userid="j1" password="j1" 
    2.33 +            url="jdbc:derby:${db};shutdown=true" 
    2.34 +            driver="org.apache.derby.jdbc.EmbeddedDriver" 
    2.35 +            onerror="continue"
    2.36 +            errorproperty="ignore.error" 
    2.37 +            failonconnectionerror="false"
    2.38 +        >none</sql>
    2.39 +        <echo message="DB created OK."/>
    2.40 +    </target>
    2.41 +    <!-- END: livedb.derby.create -->
    2.42  </project>
     3.1 --- a/samples/livedb/nbproject/build-impl.xml	Mon Jun 28 09:29:30 2010 +0200
     3.2 +++ b/samples/livedb/nbproject/build-impl.xml	Fri Jul 16 23:36:14 2010 +0200
     3.3 @@ -55,7 +55,14 @@
     3.4      </target>
     3.5      <target depends="-pre-init,-init-private,-init-user,-init-project,-init-macrodef-property" name="-do-init">
     3.6          <available file="${manifest.file}" property="manifest.available"/>
     3.7 -        <available file="${application.splash}" property="splashscreen.available"/>
     3.8 +        <condition property="splashscreen.available">
     3.9 +            <and>
    3.10 +                <not>
    3.11 +                    <equals arg1="${application.splash}" arg2="" trim="true"/>
    3.12 +                </not>
    3.13 +                <available file="${application.splash}"/>
    3.14 +            </and>
    3.15 +        </condition>
    3.16          <condition property="main.class.available">
    3.17              <and>
    3.18                  <isset property="main.class"/>
    3.19 @@ -231,7 +238,7 @@
    3.20                      <compilerarg line="${endorsed.classpath.cmd.line.arg}"/>
    3.21                      <compilerarg line="${javac.compilerargs}"/>
    3.22                      <compilerarg value="-processorpath"/>
    3.23 -                    <compilerarg path="@{processorpath}"/>
    3.24 +                    <compilerarg path="@{processorpath}:${empty.dir}"/>
    3.25                      <compilerarg line="${ap.processors.internal}"/>
    3.26                      <compilerarg line="${annotation.processing.processor.options}"/>
    3.27                      <compilerarg value="-s"/>
    3.28 @@ -700,6 +707,14 @@
    3.29                  <include name="**/*.java"/>
    3.30              </fileset>
    3.31          </javadoc>
    3.32 +        <copy todir="${dist.javadoc.dir}">
    3.33 +            <fileset dir="${src.dir}" excludes="${excludes}" includes="${includes}">
    3.34 +                <filename name="**/doc-files/**"/>
    3.35 +            </fileset>
    3.36 +            <fileset dir="${build.generated.sources.dir}" erroronmissingdir="false">
    3.37 +                <include name="**/doc-files/**"/>
    3.38 +            </fileset>
    3.39 +        </copy>
    3.40      </target>
    3.41      <target depends="init,-javadoc-build" if="netbeans.home" name="-javadoc-browse" unless="no.javadoc.preview">
    3.42          <nbbrowse file="${dist.javadoc.dir}/index.html"/>
     4.1 --- a/samples/livedb/nbproject/genfiles.properties	Mon Jun 28 09:29:30 2010 +0200
     4.2 +++ b/samples/livedb/nbproject/genfiles.properties	Fri Jul 16 23:36:14 2010 +0200
     4.3 @@ -1,8 +1,8 @@
     4.4  build.xml.data.CRC32=acb473e7
     4.5  build.xml.script.CRC32=61ba5a6a
     4.6 -build.xml.stylesheet.CRC32=958a1d3e@1.38.0.45
     4.7 +build.xml.stylesheet.CRC32=28e38971@1.39.0.45
     4.8  # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
     4.9  # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
    4.10  nbproject/build-impl.xml.data.CRC32=acb473e7
    4.11 -nbproject/build-impl.xml.script.CRC32=c82572ef
    4.12 -nbproject/build-impl.xml.stylesheet.CRC32=c75ce636@1.38.0.45
    4.13 +nbproject/build-impl.xml.script.CRC32=e442701c
    4.14 +nbproject/build-impl.xml.stylesheet.CRC32=78d75bcc@1.39.0.45
     5.1 --- a/samples/livedb/nbproject/project.properties	Mon Jun 28 09:29:30 2010 +0200
     5.2 +++ b/samples/livedb/nbproject/project.properties	Fri Jul 16 23:36:14 2010 +0200
     5.3 @@ -26,6 +26,7 @@
     5.4  dist.javadoc.dir=${dist.dir}/javadoc
     5.5  endorsed.classpath=
     5.6  excludes=
     5.7 +file.reference.derby.jar=../libs/dist/derby.jar
     5.8  file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar
     5.9  file.reference.org-openide-util-lookup.jar=../libs/dist/org-openide-util-lookup.jar
    5.10  includes=**
    5.11 @@ -42,6 +43,7 @@
    5.12  javac.test.classpath=\
    5.13      ${javac.classpath}:\
    5.14      ${build.classes.dir}:\
    5.15 +    ${file.reference.derby.jar}:\
    5.16      ${file.reference.junit-4.4.jar}
    5.17  javac.test.processorpath=\
    5.18      ${javac.test.classpath}
     6.1 --- a/samples/livedb/src/org/apidesign/livedb/LiveDB.java	Mon Jun 28 09:29:30 2010 +0200
     6.2 +++ b/samples/livedb/src/org/apidesign/livedb/LiveDB.java	Fri Jul 16 23:36:14 2010 +0200
     6.3 @@ -12,6 +12,9 @@
     6.4  @Target(ElementType.PACKAGE)
     6.5  @Retention(RetentionPolicy.SOURCE)
     6.6  public @interface LiveDB {
     6.7 +    String url();
     6.8 +    String user();
     6.9 +    String password();
    6.10 +    String query();
    6.11      String classname();
    6.12 -    String field();
    6.13  }
     7.1 --- a/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java	Mon Jun 28 09:29:30 2010 +0200
     7.2 +++ b/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java	Fri Jul 16 23:36:14 2010 +0200
     7.3 @@ -7,7 +7,19 @@
     7.4  
     7.5  import java.io.IOException;
     7.6  import java.io.Writer;
     7.7 +import java.sql.CallableStatement;
     7.8 +import java.sql.Connection;
     7.9 +import java.sql.Driver;
    7.10 +import java.sql.DriverManager;
    7.11 +import java.sql.ResultSet;
    7.12 +import java.sql.ResultSetMetaData;
    7.13 +import java.sql.SQLException;
    7.14 +import java.util.Enumeration;
    7.15 +import java.util.Properties;
    7.16 +import java.util.ServiceLoader;
    7.17  import java.util.Set;
    7.18 +import java.util.logging.Level;
    7.19 +import java.util.logging.Logger;
    7.20  import javax.annotation.processing.AbstractProcessor;
    7.21  import javax.annotation.processing.Processor;
    7.22  import javax.annotation.processing.RoundEnvironment;
    7.23 @@ -38,16 +50,76 @@
    7.24              try {
    7.25                  JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe);
    7.26                  Writer w = src.openWriter();
    7.27 +                Connection c = getConnection(db.url(), db.user(), db.password());
    7.28 +                CallableStatement s = c.prepareCall(db.query());
    7.29 +                ResultSet rs = s.executeQuery();
    7.30 +                ResultSetMetaData md = rs.getMetaData();
    7.31                  w.append("package " + pe.getQualifiedName() + ";\n");
    7.32 +                w.append("import java.util.List;\n");
    7.33 +                w.append("import java.util.ArrayList;\n");
    7.34 +                w.append("import java.sql.*;\n");
    7.35                  w.append("class " + db.classname() + " {\n");
    7.36 -                w.append("  public String " + db.field() + ";\n");
    7.37 +                for (int i = 1; i <= md.getColumnCount(); i++) {
    7.38 +                    w.append("  public final " + md.getColumnClassName(i) + " " + md.getColumnName(i) + ";\n");
    7.39 +                }
    7.40 +                w.append("  private " + db.classname() + "(\n");
    7.41 +                for (int i = 1; i <= md.getColumnCount(); i++) {
    7.42 +                    w.append("    " + md.getColumnClassName(i) + " " + md.getColumnName(i));
    7.43 +                    if (i < md.getColumnCount()) {
    7.44 +                        w.append(",\n");
    7.45 +                    } else {
    7.46 +                        w.append("\n");
    7.47 +                    }
    7.48 +                }
    7.49 +                w.append("  ) {\n");
    7.50 +                for (int i = 1; i <= md.getColumnCount(); i++) {
    7.51 +                    w.append("    this." + md.getColumnName(i) + " = " + md.getColumnName(i) + ";\n");
    7.52 +                }
    7.53 +                w.append("  }\n");
    7.54 +                w.append("  public static List<" + db.classname() + "> query() throws SQLException {\n");
    7.55 +                w.append("    Connection c = DriverManager.getConnection(\"" + db.url() + "\", \"" + db.user() + "\", \"" + db.password() +"\");\n");
    7.56 +                w.append("    List<" + db.classname() + "> res = new ArrayList<" + db.classname() + ">();\n");
    7.57 +                w.append("    CallableStatement s = c.prepareCall(\"" + db.query() + "\");\n");
    7.58 +                w.append("    ResultSet rs = s.executeQuery();\n");
    7.59 +                w.append("    ResultSetMetaData md = rs.getMetaData();\n");
    7.60 +                w.append("    while (rs.next()) {\n");
    7.61 +                w.append("      res.add(new " + db.classname() + "(\n");
    7.62 +                for (int i = 1; i <= md.getColumnCount(); i++) {
    7.63 +                    w.append("        (" + md.getColumnClassName(i) + ")rs.getObject(" + i + ")");
    7.64 +                    if (i < md.getColumnCount()) {
    7.65 +                        w.append(",\n");
    7.66 +                    } else {
    7.67 +                        w.append("\n");
    7.68 +                    }
    7.69 +                }
    7.70 +                w.append("      ));\n");
    7.71 +                w.append("    };\n");
    7.72 +                w.append("    return res;\n");
    7.73 +                w.append("  }");
    7.74                  w.append("}");
    7.75                  w.close();
    7.76              } catch (IOException ex) {
    7.77                  throw new IllegalStateException(ex);
    7.78 +            } catch (SQLException ex) {
    7.79 +                throw new IllegalStateException(ex);
    7.80              }
    7.81          }
    7.82          return true;
    7.83      }
    7.84  
    7.85 +    private static Connection getConnection(String url, String user, String password) 
    7.86 +    throws SQLException {
    7.87 +        final ClassLoader cl = LiveDBProcessor.class.getClassLoader();
    7.88 +        for (Driver d : ServiceLoader.load(Driver.class, cl)) {
    7.89 +//            System.out.println("looked up: " + d);
    7.90 +            if (d.acceptsURL(url)) {
    7.91 +                //System.out.println("accepts: " + d);
    7.92 +                Properties p = new Properties();
    7.93 +                p.put("user", user);
    7.94 +                p.put("password", password);
    7.95 +                return d.connect(url, p);
    7.96 +            }
    7.97 +        }
    7.98 +        throw new SQLException("No driver found for " + url);
    7.99 +    }
   7.100  }
     8.1 --- a/samples/livedb/test/org/apidesign/livedb/example/LiveDBTest.java	Mon Jun 28 09:29:30 2010 +0200
     8.2 +++ b/samples/livedb/test/org/apidesign/livedb/example/LiveDBTest.java	Fri Jul 16 23:36:14 2010 +0200
     8.3 @@ -5,6 +5,8 @@
     8.4  
     8.5  package org.apidesign.livedb.example;
     8.6  
     8.7 +import java.sql.SQLException;
     8.8 +import java.util.List;
     8.9  import junit.framework.TestCase;
    8.10  
    8.11  /**
    8.12 @@ -17,10 +19,12 @@
    8.13          super(testName);
    8.14      }
    8.15  
    8.16 -    public void testSomeMethod() {
    8.17 -        DBAccess db = new DBAccess();
    8.18 -        db.jarda = "Ahoj";
    8.19 -        assertEquals("Ahoj", db.jarda);
    8.20 +    public void testSomeMethod() throws SQLException {
    8.21 +        List<Age> ages = Age.query();
    8.22 +        assertEquals("One record", 1, ages.size());
    8.23 +        Age age = ages.get(0);
    8.24 +        assertEquals("name is apidesign", "apidesign", age.NAME);
    8.25 +        assertEquals("it is three years old", 3, age.AGE.intValue());
    8.26      }
    8.27  
    8.28  }
     9.1 --- a/samples/livedb/test/org/apidesign/livedb/example/package-info.java	Mon Jun 28 09:29:30 2010 +0200
     9.2 +++ b/samples/livedb/test/org/apidesign/livedb/example/package-info.java	Fri Jul 16 23:36:14 2010 +0200
     9.3 @@ -1,6 +1,10 @@
     9.4  
     9.5  
     9.6 -@LiveDB(classname="DBAccess", field="jarda")
     9.7 +@LiveDB(
     9.8 +    classname="Age", password="j1", user="j1",
     9.9 +    query="select * from APP.AGE", 
    9.10 +    url="jdbc:derby:classpath:db"
    9.11 +)
    9.12  package org.apidesign.livedb.example;
    9.13  
    9.14  import org.apidesign.livedb.LiveDB;