Android Open Source - DKO Join






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;
//w w w. j  a va2  s .c om
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.kered.dko.Field.FK;

/**
 * This class represents a join across 2 tables. It contains 2 typed references
 * ('l' and 'r') to the join row components. (each of them containing all the
 * columns they contributed to the join)
 */
public class Join<L extends Table, R extends Table> extends Table {

  List<Class<?>> types = null;
  Collection<Field<?>> fields = null;

  public final L l;
  public final R r;

  public Join(final L l, final R r) {
    this.l = l;
    this.r = r;
  }

  Join(final Object[] oa) {
    this(oa, oa.length);
  }

  @SuppressWarnings("unchecked")
  Join(final Object[] oa, final int end) {
    if (end==2) l = (L) oa[0];
    else l = (L) new Join(oa, end-1);
    r = (R) oa[end-1];
  }

  @SuppressWarnings("unchecked")
  Join(final Object[] oa, final int offset, Collection<Field<?>> fields) {
    l = (L) oa[offset + 0];
    r = (R) oa[offset + 1];
    this.fields = fields;
  }

  @Override
  protected String SCHEMA_NAME() {
    return (l == null ? null : l.SCHEMA_NAME()) + " + " + (r == null ? null : r.SCHEMA_NAME());
  }

  @Override
  protected String TABLE_NAME() {
    return (l == null ? null : l.TABLE_NAME()) + " + " + (r == null ? null : r.TABLE_NAME());
  }

  @Override
  public List<Field<?>> fields() {
    List<Field<?>> fields = new ArrayList<Field<?>>();
    if (l != null)
      fields.addAll(l.fields());
    if (r != null)
      fields.addAll(r.fields());
    fields = Collections.unmodifiableList(fields);
    return fields;
  }

  private static final FK[] fks = {};
  @SuppressWarnings("rawtypes")
  @Override
  protected FK[] FKS() {
    return fks;
  }

  @Override
  public <S> S get(final Expression.Select<S> field) {
    if (l != null) {
      S o = l.get(field);
      if (o!=null) return o;
    }
    if (r != null) {
      S o = r.get(field);
      if (o!=null) return o;
    }
    if (fields!=null && fields.contains(field)) return null;
    //throw new IllegalArgumentException("unknown field " + field);
    return null;
  }

  @Override
  public <S> Table set(final Expression.Select<S> field, final S value) {
    try {
      l.set(field, value);
      return this;
    } catch (final IllegalArgumentException e) { /* ignore */ }
    try {
      r.set(field, value);
      return this;
    } catch (final IllegalArgumentException e) { /* ignore */ }
    if (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);
    ;
    throw new IllegalArgumentException("unknown field " + field);
  }

  @Override
  public boolean insert() throws SQLException {
    return (l != null && l.insert()) && (r != null && r.insert());
  }

  @Override
  public boolean insert(DataSource ds) throws SQLException {
    return (l != null && l.insert(ds)) && (r != null && r.insert(ds));
  }

  @Override
  public boolean update() throws SQLException {
    return (l != null && l.update()) && (r != null && r.update());
  }

  @Override
  public boolean update(DataSource ds) throws SQLException {
    return (l != null && l.update(ds)) && (r != null && r.update(ds));
  }

  @Override
  public boolean delete() throws SQLException {
    return (l != null && l.delete()) && (r != null && r.delete());
  }

  @Override
  public boolean delete(DataSource ds) throws SQLException {
    return (l != null && l.delete(ds)) && (r != null && r.delete(ds));
  }

  @Override
  public boolean save() throws SQLException {
    return (l != null && l.save()) && (r != null && r.save());
  }

  @Override
  public boolean save(DataSource ds) throws SQLException {
    return (l != null && l.save(ds)) && (r != null && r.save(ds));
  }

  @Override
  public boolean exists() throws SQLException {
    return (l != null && l.exists()) || (r != null && r.exists());
  }

  @Override
  public boolean exists(DataSource ds) throws SQLException {
    return (l != null && l.exists(ds)) || (r != null && r.exists(ds));
  }

  @Override
  protected Object __NOSCO_PRIVATE_mapType(final Object o) {
    return l.__NOSCO_PRIVATE_mapType(o);
  }

  @Override
  public String toString() {
    return l + "+" + r;
  }

  @Override
  public String toStringDetailed() {
    return (l == null ? l : l.toStringDetailed()) + "+"
        + (r == null ? r : r.toStringDetailed());
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = super.hashCode();
    result = prime * result + ((fields == null) ? 0 : fields.hashCode());
    result = prime * result + ((l == null) ? 0 : l.hashCode());
    result = prime * result + ((r == null) ? 0 : r.hashCode());
    result = prime * result + ((types == null) ? 0 : types.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (getClass() != obj.getClass())
      return false;
    Join other = (Join) obj;
    if (fields == null) {
      if (other.fields != null)
        return false;
    } else if (!fields.equals(other.fields))
      return false;
    if (l == null) {
      if (other.l != null)
        return false;
    } else if (!l.equals(other.l))
      return false;
    if (r == null) {
      if (other.r != null)
        return false;
    } else if (!r.equals(other.r))
      return false;
    if (types == null) {
      if (other.types != null)
        return false;
    } else if (!types.equals(other.types))
      return false;
    return true;
  }

}




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