Android Open Source - DKO Table






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;
/*from ww w. ja  va 2  s.  com*/
import java.lang.ref.WeakReference;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.kered.dko.Field.PK;
import org.kered.dko.TemporaryTableFactory.DummyTableWithName;


/**
 * This is the base class of all classes generated by this API.
 * Some of these methods are public only by necessity.  Please use only
 * {@code insert()}, {@code update()}, {@code save()} and {@code dirty()}.
 * I consider all others fair game for changing in later versions of the API.
 *
 * @author Derek Anderson
 */
public abstract class Table {

  /**
   * @return The database schema
   * @deprecated please use the static _SCHEMA_NAME class attribute (on the generated classes)
   */
  @Deprecated
  protected String SCHEMA_NAME() {
    return Util.getSchemaName(this.getClass());
  }

  /**
   * @return The database schema
   * @deprecated please use the static _TABLE_NAME class attribute (on the generated classes)
   */
  protected String TABLE_NAME() {
    throw new RuntimeException("fix me...");
    //return Util.getTABLE_NAME(this.getClass());
  }

  /**
   * @return A list of the fields defined for this class.
   * @deprecated please use the static _FIELDS class attribute (on the generated classes)
   */
  @Deprecated
  public List<Field<?>> FIELDS() {
    return Util.getFields(this.getClass());
  }

  /**
   * Please do not use.
   * @return
   */
  protected abstract Field.FK[] FKS();

  /**
   * Please do not use.
   */
  protected BitSet __NOSCO_FETCHED_VALUES = null;

  /**
   * Please do not use.
   */
  protected BitSet __NOSCO_UPDATED_VALUES = null;

  /**
   * Please do not use.
   */
  protected DataSource __NOSCO_ORIGINAL_DATA_SOURCE = null;

  @SuppressWarnings("rawtypes")
  UsageMonitor __NOSCO_USAGE_MONITOR = null;

  /**
   * Returns true if the object has been modified
   * @return true if the object has been modified
   */
  public boolean dirty() {
    return __NOSCO_UPDATED_VALUES != null && !__NOSCO_UPDATED_VALUES.isEmpty();
  }

  /**
   * Return the value of this instance that corresponds to the given field.
   * Throws an IllegalArgumentException if the field isn't part of this instance.
   * @param field
   * @return
   */
  public abstract <S> S get(Expression.Select<S> field);

  /**
   * @return A list of the fields populated in this instance.
   */
  public abstract List<Field<?>> fields();

  /**
   * Sets the value of this instance that corresponds to the given field.
   * @param field
   * @param value
   * @return
   */
  public abstract <S> Table set(Expression.Select<S> field, S value);

  /**
   * Creates and executes an insert statement for this object
   * (irregardless of if it's already in the database)
   * @return success
   * @throws SQLException
   */
  public abstract boolean insert() throws SQLException;

  /**
   * Creates and executes an update statement for this object
   * (irregardless of if it's already in the database)
   * @return success
   * @throws SQLException
   */
  public abstract boolean update() throws SQLException;

  /**
   * Creates and executes a delete statement for this object
   * (irregardless of if it's already in the database)
   * @return success
   * @throws SQLException
   */
  public abstract boolean delete() throws SQLException;

  /**
   * Creates and executes an insert or update statement for this object
   * based on if the object came from the database or not.
   * @return success
   * @throws SQLException
   */
  public abstract boolean save() throws SQLException;

  /**
   * Returns if this object exists in the database. &nbsp;
   * Executes SQL looking for the PK values, or all columns if no PK.
   * @return success
   * @throws SQLException
   */
  public abstract boolean exists() throws SQLException;

  /**
   * Creates and executes an insert statement for this object
   * (irregardless of if it's already in the database)
   * @return success
   * @throws SQLException
   */
  public abstract boolean insert(DataSource ds) throws SQLException;

  /**
   * Creates and executes an update statement for this object
   * (irregardless of if it's already in the database)
   * @return success
   * @throws SQLException
   */
  public abstract boolean update(DataSource ds) throws SQLException;

  /**
   * Creates and executes a delete statement for this object
   * (irregardless of if it's already in the database)
   * @return success
   * @throws SQLException
   */
  public abstract boolean delete(DataSource ds) throws SQLException;

  /**
   * Creates and executes an insert or update statement for this object
   * based on if the object came from the database or not.
   * @return success
   * @throws SQLException
   */
  public abstract boolean save(DataSource ds) throws SQLException;

  /**
   * Returns if this object exists in the database. &nbsp;
   * Executes SQL looking for the PK values, or all columns if no PK.
   * @return success
   * @throws SQLException
   */
  public abstract boolean exists(DataSource ds) throws SQLException;

  static Map<Table,java.lang.reflect.Field> _pkCache = new HashMap<Table, java.lang.reflect.Field>();
  static Field.PK GET_TABLE_PK(final Table table) {
    if (!_pkCache.containsKey(table)) {
      java.lang.reflect.Field field = null;
      try {
        field = table.getClass().getDeclaredField("PK");
      } catch (final SecurityException e) {
        e.printStackTrace();
      } catch (final NoSuchFieldException e) {
        e.printStackTrace();
      }
      _pkCache.put(table, field);
    }
    try {
      return (PK) _pkCache.get(table).get(table);
    } catch (final IllegalArgumentException e) {
      e.printStackTrace();
    } catch (final IllegalAccessException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * This is used for conditional statements where the fields are ambiguous. (ie: a self-join) &nbsp;
   * All generated subclasses of {@code Table} will contain a static {@code MyTable.as(String s)} method. &nbsp;
   * It will return an instance of this class. &nbsp; You will likely never have to create one manually. &nbsp;
   * (the constructor is public only because the generated classes are in a different package scope)
   * <p>
   * Example:
   * <pre>  {@code MyTable.ALL.cross(MyTable.as("t2")).where(MyTable.ID.from("t2").eq(MyTable.PARENT_ID))}</pre>
   * @author Derek Anderson
   * @param <S>
   */
  public static class __Alias<S extends Table> {

    final Class<S> table;
    final String schema_name;
    final String table_name;
    final String alias;
    public final Query<S> ALL;
    final DummyTableWithName dummyTable;

    public __Alias(final Class<S> table, final String alias) {
      this.table = table;
      this.schema_name = Util.getSchemaName(table);
      this.table_name = Util.getTableName(table);
      this.alias = alias;
      Query<S> all = null;
      try {
        final S instance = table.newInstance();
        all = new DBQuery<S>(this);
        final java.lang.reflect.Field f = table.getDeclaredField("ALL");
        f.setAccessible(true);
        @SuppressWarnings("unchecked")
        final
        DBQuery<S> q = (DBQuery<S>) f.get(null);
        all = all.use(q.ds);
      } catch (final Exception e) {
        throw new RuntimeException(e);
      }
      this.ALL = all;
      this.dummyTable = null;
    }

    public __Alias(final DummyTableWithName tmp, final String aliasName) {
      this.table = tmp.cls;
      this.schema_name = null;
      this.table_name = null;
      this.alias = aliasName;
      this.ALL = null;
      this.dummyTable = tmp;
    }

  }

  /**
   * Represents a primary key of a row. &nbsp; Supports compound primary keys. &nbsp;
   * Each attribute is accessible by calling {@code get(field)}. &nbsp; Note that this
   * different than {@code Field.PK} which represents the primary key of a table (ie: the
   * set of columns that make up the key).
   * <p>
   * Use with enums. &nbsp; Nosco generated enums of table values are always
   * {@code __PrimaryKey}s. &nbsp; They can be used in comparisons to foreign keys. &nbsp;
   * For instance, assume tables A and B, with a FK from A to B (and A has been enumed):
   *
   * <pre>  {@code B.ALL.where(A.FK_B.eq(A.PKS.INSTANCE_ONE))}</pre>
   *
   * {@code A.PKS.INSTANCE_ONE} is an instance of this class. &nbsp; {@code A.FK_B} is an
   * instance of {@code Field.FK}. &nbsp; {@code B.ALL} is an instance of {@code Query<B>}.
   *
   * @author Derek Anderson
   *
   * @param <S>
   */
  public static interface __PrimaryKey<S extends Table> {
    <R> R get(Field<R> field);
    List<Field<?>> FIELDS();
  }

  /**
   * Represents a simple (ie: non-compound) primary key of a row. &nbsp;
   * Attribute is accessible by calling {@code get(field)} or by calling {@code value()}.
   *
   * @author Derek Anderson
   *
   * @param <S>
   * @param <V>
   */
  public static interface __SimplePrimaryKey<S extends Table,V> extends __PrimaryKey<S> {
    public V value();
  }

  @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      final Field.PK<?> pk = Util.getPK(this);
      final List<Field<?>> fields = pk == null ? Util.getFields(this.getClass()) : pk.GET_FIELDS();
      for (final Field<?> f : fields) {
        final Object o = this.get(f);
        result = prime * result + ((o == null) ? 0 : o.hashCode());
      }
      return result;
  }

  @Override
  public boolean equals(final Object other) {
    if (other == this) return true;
    if (other == null) return false;
    if (!(other instanceof Table)) return false;
      final Field.PK<?> pk = Util.getPK(this);
      final List<Field<?>> fields = pk == null ? Util.getFields(this.getClass()) : pk.GET_FIELDS();
      for (final Field<?> f : fields) {
        final Object o1 = this.get(f);
        final Object o2 = ((Table)other).get(f);
        if (!((o1 == null) ? (o2 == null) : o1.equals(o2))) return false;
      }
      return true;
  }

  /**
   * Internal function - please don't use. &nbsp; Subject to change.
   * @param o
   * @return
   */
  protected abstract java.lang.Object __NOSCO_PRIVATE_mapType(java.lang.Object o);

  /**
   * {@code toStringDetailed()} will print out all the values of all the fields fetched.
   * @return
   */
  public String toStringDetailed() {
    StringBuffer sb = new StringBuffer();
    sb.append("[").append(this.getClass().getSimpleName());
    for (Field<?> field : fields()) {
      sb.append(" ").append(field.NAME).append(":").append(this.get(field));
    }
    sb.append("]");
    return sb.toString();
  }

  /**
   * The {@code toStringSimple()} methods generated by nosco print out PK values plus any "name" column it finds. &nbsp;
   * By default the genned {@code toString()} calls this.
   * @return
   */
  public String toStringSimple() {
    return toString();
  }

  /**
   * Internal function - please don't use. &nbsp; Subject to change.
   * @param context
   * @param conn
   */
  protected void __NOSCO_PRIVATE_preExecute(final SqlContext context, final Connection conn) throws SQLException {}

  /**
   * Internal function - please don't use. &nbsp; Subject to change.
   * @param conn
   */
  protected void __NOSCO_PRIVATE_postExecute(final SqlContext context, final Connection conn) throws SQLException {}

  /**
   * Internal function - please don't use. &nbsp; Subject to change.
   * @param conn
   */
  protected void __NOSCO_PRIVATE_accessedFkCallback(final Table table, final Field.FK<? extends Table> fk) {
    if (__NOSCO_USAGE_MONITOR!=null) __NOSCO_USAGE_MONITOR.accessedFkCallback(table, fk);
  }

  /**
   * Internal function - please don't use. &nbsp; Subject to change.
   * @param conn
   */
  protected void __NOSCO_PRIVATE_accessedColumnCallback(final Table table, final Field<?> field) {
    if (__NOSCO_USAGE_MONITOR!=null) __NOSCO_USAGE_MONITOR.__NOSCO_PRIVATE_accessedColumnCallback(table, field);
  }

  String TABLE_NAME(final SqlContext sqlContext) {
    return Util.getTableName(this.getClass());
  }

}




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