Android Open Source - DKO Join Generator






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kered.dko.ant;
//from  w w  w .j  a v  a  2  s  .c  o m
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

class JoinGenerator {

  /**
   * @param args
   * @throws IOException
   */
  public static void main(final String[] args) throws IOException {
    int n = 9;
    if (args.length == 1) {
      n = Integer.valueOf(args[0]);
    }

    final String r = ""; //genRandomString();

    final File file = new File("gen/joinsrc/org/kered/dko/JoinOld.java");
    System.err.println("writing (to J"+n+"): "+ file.getPath());
    genJoinsFile(file, n, r);
//    final File file2 = new File("gen/joinsrc/org/kered/dko/_Join.java");
//    gen_JoinsFile(file2, n, r);
  }

//  private static String genRandomString() {
//    final StringBuffer sb = new StringBuffer();
//    for (int i=0; i<3; ++i) {
//      sb.append((char)('A' + ((char)(Math.random()*26))));
//    }
//    return sb.toString();
//  }

  private static void genJoinsFile(final File file, final int n, final String r) throws IOException {
    final File dir = file.getParentFile();
    if(!dir.exists()) dir.mkdirs();
    final BufferedWriter w = new BufferedWriter(new FileWriter(file));
    genJoins(w, n, r);
    w.close();
  }

//  private static void gen_JoinsFile(final File file, final int n, final String r) throws IOException {
//    final File dir = file.getParentFile();
//    if(!dir.exists()) dir.mkdirs();
//    final BufferedWriter w = new BufferedWriter(new FileWriter(file));
//    gen_Joins(w, n, r);
//    w.close();
//  }

  private static String[] ops = {"insert", "update", "delete", "save", "exists"};

  private static void genJoins(final Writer w, final int n, final String r) throws IOException {
    w.write("package org.kered.dko;\n\n");
    w.write("import java.sql.SQLException;\n");
    w.write("import java.util.ArrayList;\n");
    w.write("import java.util.Collection;\n");
    w.write("import java.util.Collections;\n");
    w.write("import java.util.List;\n");
    w.write("import javax.sql.DataSource;\n");
    w.write("import org.kered.dko.Field.FK;\n");
    w.write("import org.kered.dko.Table.__Alias;\n");
    //w.write("import org.kered.dko._Join.*;\n");
    w.write("\n");

    w.write("public class JoinOld {\n");
    w.write("\n");

    w.write("\tstatic abstract class J extends Table {\n");
    w.write("\t\tList<Class<?>> types = null;\n");
    w.write("\t\tCollection<Field<?>> fields = null;\n");
    w.write("\t\tabstract void populateObjectArray(Object[] oa, int i);\n");
    w.write("\t}\n");

    for (int i=2; i<=n; ++i) {
      w.write("\n");

      final String tExtendsTable =genTExtendsTable(i);
      final String tTypes =genTTypes(i);

      writeJoinMethods(w, r, i, tExtendsTable, tTypes);
      w.write("\n");
      writeInnerQueryClass(w, r, i);
      w.write("\n");
      writeInnerJClass(w, i, tExtendsTable);

    }

    w.write("\n}\n");
  }

  private static void writeJoinMethods(final Writer w, final String r, final int i,
      final String tExtendsTable, final String tTypes) throws IOException {
    final String[] joinTypes = {"left", "right", "inner", "outer", "cross"};
    final String[] inputTypes = {"Class", "__Alias"};
    for (final String joinType : joinTypes) {
      for (final String inputType : inputTypes) {
        if (i == 2) {
          for (final String inputType2 : inputTypes) {
            writeJoinJavadoc(w, i, tTypes);
            w.write("\tpublic static <"+ tExtendsTable +"> Query<J"+ i +"<"+ tTypes);
            w.write(">> "+ joinType + i +"(final "+ inputType +"<T1> t1, "+ inputType2 +"<T2> t2");
            if (!"cross".equals(joinType)) w.write(", Condition on");
            w.write(") {\n");
            w.write("\t\tif (Util.sameDataSource(t1, t2)) {\n");
            w.write("\t\t\treturn new _"+ r +"_Query2<T1, T2>(new DBQuery<T1>(t1), t2, \""+ joinType +" join\", "+ ("cross".equals(joinType) ? "null" : "on") +");\n");
            w.write("\t\t} else {\n");
            w.write("\t\t\treturn new SoftJoin("+ joinTypeToConstant(joinType) +", J2.class, t1, t2, "+ ("cross".equals(joinType) ? "null" : "on") +");\n");
            w.write("\t\t}\n");
            w.write("\t}\n");
          }
          writeJoinJavadoc(w, i, tTypes);
          w.write("\tpublic static <"+ tExtendsTable +"> Query<J"+ i +"<"+ tTypes);
          w.write(">> "+ joinType + i +"(final Query<T"+ (i-1) +"> q, "+ inputType +"<T"+ i +"> t");
          if (!"cross".equals(joinType)) w.write(", Condition on");
          w.write(") {\n");
          w.write("\t\tif (Util.sameDataSource(q, t)) {\n");
          w.write("\t\t\treturn new _"+ r +"_Query2<T1, T2>(q, t, \""+ joinType +" join\", "+ ("cross".equals(joinType) ? "null" : "on") +");\n");
          w.write("\t\t} else {\n");
          w.write("\t\t\treturn new SoftJoin("+ joinTypeToConstant(joinType) +", J2.class, q, t, "+ ("cross".equals(joinType) ? "null" : "on") +");\n");
          w.write("\t\t}\n");
          w.write("\t}\n");
        } else {
          writeJoinJavadoc(w, i, tTypes);
          w.write("\tpublic static <"+ tExtendsTable +"> Query<J"+ i +"<"+ tTypes);
          w.write(">> "+ joinType + i +"(final Query<J"+ (i-1) +"<"+ genTTypes(i-1));
          w.write(">> q, "+ inputType +"<T"+ i +"> t");
          if (!"cross".equals(joinType)) w.write(", Condition on");
          w.write(") {\n");
          w.write("\t\tif (Util.sameDataSource(q, t)) {\n");
          w.write("\t\t\treturn new _"+ r +"_Query"+ i +"<"+ tTypes +">(q, t, \""+ joinType +" join\", "+ ("cross".equals(joinType) ? "null" : "on") +");\n");
          w.write("\t\t} else {\n");
          w.write("\t\t\treturn new SoftJoin("+ joinTypeToConstant(joinType) +", J"+ i +".class, q, t, "+ ("cross".equals(joinType) ? "null" : "on") +");\n");
          w.write("\t\t}\n");
          w.write("\t}\n");
        }
      }
    }
  }

  private static String joinTypeToConstant(final String joinType) {
      return "Constants.JOIN_TYPE."+joinType.toUpperCase();
  }

  private static void writeInnerJClass(final Writer w, final int i,
      final String tExtendsTable) throws IOException {
    w.write("\n");
    w.write("\t/**\n");
    w.write("\t * This class represents a join across "+ i +" tables.\n");
    w.write("\t * It contains "+ i +" typed references (t1 to t"+ i +") to the join row components.\n");
    w.write("\t * (each of them containing all the columns they contributed to the join)\n");
    w.write("\t */\n");
    w.write("\tpublic static class J"+ i +" <"+ tExtendsTable +"> extends J {\n");

    //w.write("\t\tprivate List<Field<?>> __NOSCO_PRIVATE_FIELDS = null;\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\tpublic final T"+ j +" t"+ j +";\n");
    }

    w.write("\t\tpublic J"+ i +"(");
    for (int j=1; j<=i; ++j) {
      w.write("final T"+ j +" t"+ j +"");
      if (j < i) w.write(", ");
    }
    w.write(") {\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\tthis.t"+ j +" = t"+ j +";\n");
    }
    w.write("\t\t}\n");

    w.write("\t\t@SuppressWarnings(\"unchecked\")\n");
    w.write("\t\tJ"+ i +"(final Object[] oa, final int offset) {\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\tt"+ j +" = (T"+ j +") oa[offset+"+ (j-1) +"];\n");
    }
    w.write("\t\t\tthis.fields = null;\n");
    w.write("\t\t}\n");

    w.write("\t\t@SuppressWarnings(\"unchecked\")\n");
    w.write("\t\tJ"+ i +"(final Object[] oa, final int offset, Collection<Field<?>> fields) {\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\tt"+ j +" = (T"+ j +") oa[offset+"+ (j-1) +"];\n");
    }
    w.write("\t\t\tthis.fields = fields;\n");
    w.write("\t\t}\n");

    w.write("\t\tvoid populateObjectArray(Object[] oa, int i){\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\toa[i+"+ (j-1) +"] = t"+ j +";\n");
    }
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tprotected String SCHEMA_NAME() {\n");
    w.write("\t\t\treturn ");
    for (int j=1; j<=i; ++j) {
      w.write("(t"+ j +"==null ? null : t"+ j +".SCHEMA_NAME())");
      if (j < i) w.write("+\" + \"+");
    }
    w.write(";\n");
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tprotected String TABLE_NAME() {\n");
    w.write("\t\t\treturn ");
    for (int j=1; j<=i; ++j) {
      w.write("(t"+ j +"==null ? null : t"+ j +".TABLE_NAME())");
      if (j < i) w.write("+\" + \"+");
    }
    w.write(";\n");
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tpublic List<Field<?>> fields() {\n");
    w.write("\t\t\tList<Field<?>> fields = new ArrayList<Field<?>>();\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\tif (t"+ j +"!=null) fields.addAll(t"+ j +".fields());\n");
    }
    w.write("\t\t\tfields = Collections.unmodifiableList(fields);\n");
    w.write("\t\t\treturn fields;\n");
    w.write("\t\t}\n");

    w.write("\t\t@SuppressWarnings(\"rawtypes\")\n");
    w.write("\t\t@Override\n");
    w.write("\t\tprotected FK[] FKS() {\n");
    w.write("\t\t\tfinal FK[] ret = {};\n");
    w.write("\t\t\treturn ret;\n");
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tpublic <S> S get(final Field<S> field) {\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\tif (t"+ j +"!=null) {\n");
      w.write("\t\t\t\ttry { return t"+ j +".get(field); }\n");
      w.write("\t\t\t\tcatch (final IllegalArgumentException e) { /* ignore */ }\n");
      w.write("\t\t\t}\n");
    }
    w.write("\t\t\tif (fields.contains(field)) return null;\n");
    w.write("\t\t\tthrow new IllegalArgumentException(\"unknown field \"+ field);\n");
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tpublic <S> void set(final Field<S> field, final S value) {\n");
    for (int j=1; j<=i; ++j) {
      w.write("\t\t\ttry { t"+ j +".set(field, value); return; }\n");
      w.write("\t\t\tcatch (final IllegalArgumentException e) { /* ignore */ }\n");
    }
    w.write("\t\t\tif (fields!=null && fields.contains(field)) throw new RuntimeException(\"you can't set this field because the joined object is null (some join types can return nulls from the database): \"+ field);;\n");
    w.write("\t\t\tthrow new IllegalArgumentException(\"unknown field \"+ field);\n");
    w.write("\t\t}\n");

    for (final String op : ops) {
      final String cmp = "exists".equals(op) ? " || " : " && ";
      w.write("\t\t@Override\n");
      w.write("\t\tpublic boolean "+ op +"() throws SQLException {\n");
      w.write("\t\t\treturn ");
      for (int j=1; j<=i; ++j) {
        w.write("(t"+ j +"!=null && t"+ j +"."+ op +"())");
        if (j < i) w.write(cmp);
      }
      w.write(";\n");
      w.write("\t\t}\n");
      w.write("\t\t@Override\n");
      w.write("\t\tpublic boolean "+ op +"(DataSource ds) throws SQLException {\n");
      w.write("\t\t\treturn ");
      for (int j=1; j<=i; ++j) {
        w.write("(t"+ j +"!=null && t"+ j +"."+ op +"(ds))");
        if (j < i) w.write(cmp);
      }
      w.write(";\n");
      w.write("\t\t}\n");
    }

    w.write("\t\t@Override\n");
    w.write("\t\tprotected Object __NOSCO_PRIVATE_mapType(final Object o) {\n");
    w.write("\t\t\treturn t1.__NOSCO_PRIVATE_mapType(o);\n");
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tpublic String toString() {\n");
    w.write("\t\t\treturn ");
    for (int j=1; j<=i; ++j) {
      w.write("t"+ j);
      if (j < i) w.write(" +\"+\"+ ");
    }
    w.write(";\n");
    w.write("\t\t}\n");

    w.write("\t\t@Override\n");
    w.write("\t\tpublic String toStringDetailed() {\n");
    w.write("\t\t\treturn ");
    for (int j=1; j<=i; ++j) {
      w.write("(t"+ j +"==null ? t"+ j +" : t"+ j +".toStringDetailed())");
      if (j < i) w.write(" +\"+\"+ ");
    }
    w.write(";\n");
    w.write("\t\t}\n");

    w.write("\t}\n");
  }

  private static void gen_Joins(final Writer w, final int n, final String r) throws IOException {
    w.write("package org.kered.dko;\n\n");
    w.write("import java.sql.SQLException;\n");
    w.write("import java.util.ArrayList;\n");
    w.write("import java.util.Collections;\n");
    w.write("import java.util.List;\n");
    w.write("import javax.sql.DataSource;\n");
    w.write("import org.kered.dko.Field.FK;\n");
    w.write("import org.kered.dko.Table.__Alias;\n");
    w.write("import org.kered.dko.Join.*;\n");
    w.write("\n");

    w.write("public class _Join {\n");
    w.write("\n");

//    w.write("\tstatic abstract class J extends Table {\n");
//    w.write("\t\tList<Class<?>> types = null;\n");
//    w.write("\t}\n");

    for (int i=2; i<=n; ++i) {
      w.write("\n");
      writeInnerQueryClass(w, r, i);
    }

    w.write("\n}\n");
  }

  private static void writeInnerQueryClass(final Writer w, final String r,
      final int i) throws IOException {
    final String tExtendsTable =genTExtendsTable(i);
    final String tTypes =genTTypes(i);


    final String[] joinTypes = {"left", "right", "inner", "outer", "cross"};
    final String[] inputTypes = {"Class", "__Alias"};

    w.write("\t/** \n");
    w.write("\t * Please don't reference this type directly.  Reference it as a {@code org.kered.dko.Query<Join.J"+ i +"<"+ tTypes +">>}\n");
    w.write("\t */\n");
    w.write("\tstatic class _"+ r +"_Query"+ i +"<"+ tExtendsTable +"> extends DBQuery<J"+ i +"<"+ tTypes +">> {\n");
    w.write("\t\tfinal int SIZE = "+ i +";\n");
    for (final String inputType : inputTypes) {
      if (i == 2) {
        w.write("\t\t_"+ r +"_Query2(final Query<T1> q, final "+ inputType +"<T2> t, String joinType, Condition on) {\n");
        w.write("\t\t\tsuper(J"+ i +".class, q, t, joinType, on);\n");
        w.write("\t\t}\n");
      } else {
        w.write("\t\t_"+ r +"_Query"+ i +"(final Query<J"+ (i-1) +"<"+ genTTypes(i-1) +">> q, final "+ inputType +"<T"+ i +"> t, String joinType, Condition on) {\n");
        w.write("\t\t\tsuper(J"+ i +".class, q, t, joinType, on);\n");
        w.write("\t\t}\n");
      }
    }
    w.write("\t}\n");
  }

  private static void writeJoinJavadoc(final Writer w, final int i,
      final String tTypes) throws IOException {
    w.write("\t/** \n");
    w.write("\t * Joins types "+ tTypes +" into a {@code org.kered.dko.Query<Join.J"+ i +"<"+ tTypes +">>}\n");
    w.write("\t */\n");
  }

  private static String genTTypes(final int i) {
    final StringBuffer sb = new StringBuffer();
    for (int j=1; j<=i; ++j) {
      sb.append("T"+ j);
      if (j < i) sb.append(", ");
    }
    return sb.toString();
  }

  public static String genTExtendsTable(final int i) {
    final StringBuffer sb = new StringBuffer();
    for (int j=1; j<=i; ++j) {
      sb.append("T"+ j +" extends Table");
      if (j < i) sb.append(", ");
    }
    return sb.toString();
  }

}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java