Android Open Source - DKO Soft Join Util






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 v  a2 s. c o m
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.kered.dko.Condition.Binary;
import org.kered.dko.Condition.Binary2;
import org.kered.dko.Field.PK;

class SoftJoinUtil {

  static Map<Field<?>,Field<?>> getFieldsOpposingPK(Class<? extends Table> type, Condition condition) {
    PK<? extends Table> pk = Util.getPK(type);
    if (pk==null) return null;
    Set<Field<?>> pkFields = new HashSet<Field<?>>(pk.GET_FIELDS());
    Map<Field<?>,Field<?>> otherFields = new LinkedHashMap<Field<?>,Field<?>>();
    List<Condition> conditions;
    if (condition instanceof Condition.And) {
      conditions = ((Condition.And)condition).conditions;
    } else {
      conditions = new ArrayList<Condition>();
      conditions.add(condition);
    }
    for (Condition c : conditions) {
      if (c instanceof Binary) {
        Binary bc = (Binary)c;
        if (bc.cmp!=null && "=".equals(bc.cmp.trim())) {
          if (pkFields.contains(bc.field) && bc.field2!=null) otherFields.put(bc.field, bc.field2);
          if (pkFields.contains(bc.field2) && bc.field!=null) otherFields.put(bc.field2, bc.field);
          pkFields.remove(bc.field);
          pkFields.remove(bc.field2);
        }
      }
      if (c instanceof Binary2) {
        Binary2 bc = (Binary2)c;
        if (bc.cmp!=null && "=".equals(bc.cmp.trim())) {
          if (pkFields.contains(bc.o1) && bc.o2 instanceof Field) otherFields.put((Field<?>) bc.o1, (Field<?>) bc.o2);
          if (pkFields.contains(bc.o2) && bc.o1 instanceof Field) otherFields.put((Field<?>) bc.o2, (Field<?>) bc.o1);
          pkFields.remove(bc.o1);
          pkFields.remove(bc.o2);
        }
      }
    }
    return otherFields;
  }

  static boolean doesConditionCoverPK(Class<? extends Table> type, Condition condition) {
    PK<? extends Table> pk = Util.getPK(type);
    if (pk==null) return false;
    Set<Field<?>> fields = new HashSet<Field<?>>(pk.GET_FIELDS());
    List<Condition> conditions;
    if (condition instanceof Condition.And) {
      conditions = ((Condition.And)condition).conditions;
    } else {
      conditions = new ArrayList<Condition>();
      conditions.add(condition);
    }
    for (Condition c : conditions) {
      if (c instanceof Binary) {
        Binary bc = (Binary)c;
        if (bc.cmp!=null && "=".equals(bc.cmp.trim())) {
          fields.remove(bc.field);
          fields.remove(bc.field2);
        }
      }
      if (c instanceof Binary2) {
        Binary2 bc = (Binary2)c;
        if (bc.cmp!=null && "=".equals(bc.cmp.trim())) {
          fields.remove(bc.o1);
          fields.remove(bc.o2);
        }
      }
    }
    return fields.isEmpty();
  }

  static int getObjectSizeOfQuery(final Query<? extends Table> q) {
    try {
      final java.lang.reflect.Field f = q.getClass().getDeclaredField("SIZE");
      return f.getInt(q);
    } catch (final NoSuchFieldException e) {
      // not a Join._Query
      return 1;
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static class AddNullAtEnd<E extends Table> implements Iterable<E> {
    private Query<E> q;
    public AddNullAtEnd(Query<E> q) {
      this.q = q;
    }
    @Override
    public Iterator<E> iterator() {
      final Iterator<E> i = q.iterator();
      return new Iterator<E>() {
        boolean sentLastNull = false;
        @Override
        public boolean hasNext() {
          return i.hasNext() || !sentLastNull;
        }
        @Override
        public E next() {
          if (i.hasNext()) return i.next();
          sentLastNull = true;
          return null;
        }
        @Override
        public void remove() {
          i.remove();
        }
      };
    }

  }

  static boolean conditionIsAllReferencingQuery(final Condition condition,
      final Query<? extends Table> q) {
    if (!(q instanceof DBQuery))
      return false;
    try {
      final DBQuery<? extends Table> q2 = (DBQuery<? extends Table>) q
          .where(condition);
      final SqlContext context = new SqlContext(q2);
      q2.getWhereClauseAndBindings(context);
      return true;
    } catch (final Util.FieldNotPartOfSelectableTableSet e) {
      return false;
    }
  }


}




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