Skip to content

Commit

Permalink
make schema's columns to be instance fields, not static fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Sep 19, 2016
1 parent 34a1a75 commit 12badf7
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

public abstract class Deleter<Model, D extends Deleter<Model, ?>> extends OrmaConditionBase<Model, D> {

public Deleter(@NonNull OrmaConnection connection, @NonNull Schema<Model> schema) {
super(connection, schema);
public Deleter(@NonNull OrmaConnection connection) {
super(connection);
}

public Deleter(@NonNull Relation<Model, ?> relation) {
Expand All @@ -37,7 +37,7 @@ public Deleter(@NonNull Relation<Model, ?> relation) {
* @return Number of rows deleted.
*/
public int execute() {
return conn.delete(schema, getWhereClause(), getBindArgs());
return conn.delete(getSchema(), getWhereClause(), getBindArgs());
}

@CheckResult
Expand Down
16 changes: 8 additions & 8 deletions library/src/main/java/com/github/gfx/android/orma/Relation.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public abstract class Relation<Model, R extends Relation<Model, ?>> extends Orma

final protected ArrayList<OrderSpec<Model>> orderSpecs = new ArrayList<>();

public Relation(@NonNull OrmaConnection connection, @NonNull Schema<Model> schema) {
super(connection, schema);
public Relation(@NonNull OrmaConnection connection) {
super(connection);
}

public Relation(@NonNull Relation<Model, ?> relation) {
Expand Down Expand Up @@ -92,7 +92,7 @@ public Model get(@IntRange(from = 0) int position) {
public Model getOrCreate(@IntRange(from = 0) long position, @NonNull ModelFactory<Model> factory) {
Model model = selector().getOrNull(position);
if (model == null) {
return conn.createModel(schema, factory);
return conn.createModel(getSchema(), factory);
} else {
return model;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public void call(final Subscriber<? super Integer> subscriber) {
@Override
public void run() {
int position = indexOf(item);
ColumnDef<Model, ?> pk = schema.getPrimaryKey();
ColumnDef<Model, ?> pk = getSchema().getPrimaryKey();
int deletedRows = deleter()
.where(pk, "=", pk.getSerialized(item))
.execute();
Expand Down Expand Up @@ -176,11 +176,11 @@ public Single<Integer> truncateAsObservable(@IntRange(from = 0) final int size)
return Single.create(new Single.OnSubscribe<Integer>() {
@Override
public void call(SingleSubscriber<? super Integer> subscriber) {
String pk = schema.getPrimaryKey().getEscapedName();
String pk = getSchema().getPrimaryKey().getEscapedName();
Selector<Model, ?> subquery = selector();
subquery.limit(Integer.MAX_VALUE);
subquery.offset(size);
int deletedRows = conn.delete(schema, pk + " IN (" + subquery.buildQueryWithColumns(pk) + ")", getBindArgs());
int deletedRows = conn.delete(getSchema(), pk + " IN (" + subquery.buildQueryWithColumns(pk) + ")", getBindArgs());
subscriber.onSuccess(deletedRows);
}
});
Expand Down Expand Up @@ -225,7 +225,7 @@ public Inserter<Model> inserter() {

@NonNull
public Inserter<Model> inserter(@OnConflict int onConflictAlgorithm) {
return new Inserter<>(conn, schema, onConflictAlgorithm, true);
return new Inserter<>(conn, getSchema(), onConflictAlgorithm, true);
}

/**
Expand All @@ -236,7 +236,7 @@ public Inserter<Model> inserter(@OnConflict int onConflictAlgorithm) {
*/
@NonNull
public Inserter<Model> inserter(@OnConflict int onConflictAlgorithm, boolean withoutAutoId) {
return new Inserter<>(conn, schema, onConflictAlgorithm, withoutAutoId);
return new Inserter<>(conn, getSchema(), onConflictAlgorithm, withoutAutoId);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions library/src/main/java/com/github/gfx/android/orma/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public interface Schema<Model> extends MigrationSchema {
@NonNull
String getEscapedTableName();

@NonNull
String getTableAlias();

@NonNull
String getEscapedTableAlias();

/**
* @return An escaped table name, which may includes {@code JOIN} clauses, used in {@code SELECT}.
*/
Expand Down
18 changes: 9 additions & 9 deletions library/src/main/java/com/github/gfx/android/orma/Selector.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public abstract class Selector<Model, S extends Selector<Model, ?>>

protected long page = -1;

public Selector(@NonNull OrmaConnection conn, @NonNull Schema<Model> schema) {
super(conn, schema);
public Selector(@NonNull OrmaConnection conn) {
super(conn);
}

public Selector(@NonNull OrmaConditionBase<Model, ?> condition) {
Expand Down Expand Up @@ -156,7 +156,7 @@ private String getLimitClause() {
@IntRange(from = 0)
public int count() {
String sql = SQLiteQueryBuilder.buildQueryString(
false, schema.getSelectFromTableClause(), countSelections, getWhereClause(), groupBy, null, null, null);
false, getSchema().getSelectFromTableClause(), countSelections, getWhereClause(), groupBy, null, null, null);
return (int) conn.rawQueryForLong(sql, getBindArgs());
}

Expand Down Expand Up @@ -188,22 +188,22 @@ public Model valueOrNull() {
public Model value() throws NoValueException {
Model model = getOrNull(0);
if (model == null) {
throw new NoValueException("Expected single value but nothing for " + schema.getTableName());
throw new NoValueException("Expected single value but nothing for " + getSchema().getTableName());
}
return model;
}

@Nullable
public Model getOrNull(@IntRange(from = 0) long position) {
return conn.querySingle(schema, schema.getDefaultResultColumns(),
return conn.querySingle(getSchema(), getSchema().getDefaultResultColumns(),
getWhereClause(), getBindArgs(), groupBy, having, orderBy, position);
}

@NonNull
public Model get(@IntRange(from = 0) long position) {
Model model = getOrNull(position);
if (model == null) {
throw new NoValueException("Expected single value for " + position + " but nothing for " + schema.getTableName());
throw new NoValueException("Expected single value for " + position + " but nothing for " + getSchema().getTableName());
}
return model;
}
Expand All @@ -225,7 +225,7 @@ public Cursor executeWithColumns(@NonNull String... columns) {
*/
@NonNull
public String buildQuery() {
return buildQueryWithColumns(schema.getDefaultResultColumns());
return buildQueryWithColumns(getSchema().getDefaultResultColumns());
}

/**
Expand All @@ -234,7 +234,7 @@ public String buildQuery() {
@NonNull
public String buildQueryWithColumns(@NonNull String... columns) {
return SQLiteQueryBuilder.buildQueryString(
false, schema.getSelectFromTableClause(), columns,
false, getSchema().getSelectFromTableClause(), columns,
getWhereClause(), groupBy, having, orderBy, getLimitClause());
}

Expand Down Expand Up @@ -273,7 +273,7 @@ public void forEach(@NonNull Action1<Model> action) {

@NonNull
public Model newModelFromCursor(@NonNull Cursor cursor) {
return schema.newModelFromCursor(conn, cursor, 0);
return getSchema().newModelFromCursor(conn, cursor, 0);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public abstract class Updater<Model, U extends Updater<Model, ?>> extends OrmaCo

final protected ContentValues contents = new ContentValues();

public Updater(@NonNull OrmaConnection conn, @NonNull Schema<Model> schema) {
super(conn, schema);
public Updater(@NonNull OrmaConnection conn) {
super(conn);
}

public Updater(@NonNull Relation<Model, ?> relation) {
Expand All @@ -45,7 +45,7 @@ public ContentValues getContentValues() {
* @return The number of rows updated.
*/
public int execute() {
return conn.update(schema, contents, getWhereClause(), getBindArgs());
return conn.update(getSchema(), contents, getWhereClause(), getBindArgs());
}

@CheckResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public abstract class OrmaConditionBase<Model, C extends OrmaConditionBase<Model

protected final OrmaConnection conn;

protected final Schema<Model> schema;

protected String whereConjunction = " AND ";

@Nullable
Expand All @@ -42,23 +40,20 @@ public abstract class OrmaConditionBase<Model, C extends OrmaConditionBase<Model
@Nullable
protected ArrayList<String> bindArgs;

public OrmaConditionBase(@NonNull OrmaConnection conn, @NonNull Schema<Model> schema) {
public OrmaConditionBase(@NonNull OrmaConnection conn) {
this.conn = conn;
this.schema = schema;
}

public OrmaConditionBase(@NonNull OrmaConditionBase<Model, ?> condition) {
this(condition.conn, condition.schema);
this(condition.conn);
where(condition);
}

public OrmaConnection getConnection() {
return conn;
}

public Schema<Model> getSchema() {
return schema;
}
public abstract Schema<Model> getSchema();

protected void appendBindArgs(@NonNull Object... args) {
if (bindArgs == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
import com.github.gfx.android.orma.Schema;

import android.support.annotation.NonNull;
import android.support.v4.util.SimpleArrayMap;

import java.util.HashMap;
import java.util.Map;

/**
* The set of all the {@link Schema} instances.
*/
public class Schemas {

static final SimpleArrayMap<Class<?>, Schema<?>> SCHEMAS = new SimpleArrayMap<>();
static final Map<Class<?>, Schema<?>> SCHEMAS = new HashMap<>();

public static <M, T extends Schema<M>> T register(@NonNull T schema) {
SCHEMAS.put(schema.getModelClass(), schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.gfx.android.orma.test;

import com.github.gfx.android.orma.ColumnDef;
import com.github.gfx.android.orma.Schema;
import com.github.gfx.android.orma.test.model.Author_Schema;
import com.github.gfx.android.orma.test.model.Book_Schema;
import com.github.gfx.android.orma.test.model.ModelWithDirectAssociation_Schema;
Expand All @@ -25,6 +26,7 @@
import com.github.gfx.android.orma.test.model.ModelWithPrimaryKeyIsNotFirst_Schema;
import com.github.gfx.android.orma.test.model.ModelWithStorageTypes_Schema;
import com.github.gfx.android.orma.test.model.ModelWithTypeAdapters_Schema;
import com.github.gfx.android.orma.test.model.Publisher;
import com.github.gfx.android.orma.test.model.PublisherSchema;

import org.hamcrest.Matchers;
Expand All @@ -46,11 +48,15 @@ public void testPublisherSchema() throws Exception {
PublisherSchema schema = PublisherSchema.INSTANCE;

assertThat(schema.getTableName(), is("publishers"));
assertThat(schema.getPrimaryKey(), is((ColumnDef) PublisherSchema.id));
assertThat(schema.getPrimaryKey(), is((ColumnDef) schema.id));

assertThat(PublisherSchema.name.name, is("name"));
assertThat(PublisherSchema.startedYear.name, is("started_year"));
assertThat(PublisherSchema.startedMonth.name, is("started_month"));
assertThat(schema.name.name, is("name"));
assertThat(schema.name.schema, is((Schema<Publisher>) schema));
assertThat(schema.name.getEscapedName(), is(notNullValue()));
assertThat(schema.name.getFullyQualifiedName(), is(notNullValue()));

assertThat(schema.startedYear.name, is("started_year"));
assertThat(schema.startedMonth.name, is("started_month"));

assertThat("PRIMARY KEY is placed in the last",
schema.getCreateTableStatement(), is(
Expand All @@ -63,62 +69,64 @@ public void testBookSchema() throws Exception {
Book_Schema schema = Book_Schema.INSTANCE;

assertThat(schema.getTableName(), is("Book"));
assertThat(schema.getPrimaryKey(), is((ColumnDef) Book_Schema.bookId));

assertThat(Book_Schema.bookId.name, is("bookId"));
assertThat(Book_Schema.bookId.storageType, is("INTEGER"));
assertThat(Book_Schema.title.name, is("title"));
assertThat(Book_Schema.title.storageType, is("TEXT"));
assertThat(Book_Schema.content.name, is("content"));
assertThat(Book_Schema.content.storageType, is("TEXT"));
assertThat(Book_Schema.publisher.name, is("publisher"));
assertThat(Book_Schema.publisher.storageType, is("INTEGER"));
assertThat(schema.getPrimaryKey(), is((ColumnDef) schema.bookId));

assertThat(schema.bookId.name, is("bookId"));
assertThat(schema.bookId.storageType, is("INTEGER"));
assertThat(schema.title.name, is("title"));
assertThat(schema.title.storageType, is("TEXT"));
assertThat(schema.content.name, is("content"));
assertThat(schema.content.storageType, is("TEXT"));
assertThat(schema.publisher.name, is("publisher"));
assertThat(schema.publisher.storageType, is("INTEGER"));
}

@Test
public void testAuthorSchema() throws Exception {
Author_Schema schema = Author_Schema.INSTANCE;

assertThat(schema.getTableName(), is("Author"));
assertThat(schema.getPrimaryKey(), is((ColumnDef) Author_Schema.name));
assertThat(schema.getPrimaryKey(), is((ColumnDef) schema.name));
}

@Test
public void testPrimaryKeyAttributes() throws Exception {
assertThat(PublisherSchema.id.isPrimaryKey(), is(true));
assertThat(PublisherSchema.id.isAutoincremnt(), is(true));
assertThat(PublisherSchema.id.isAutoValue(), is(true));
assertThat(PublisherSchema.INSTANCE.id.isPrimaryKey(), is(true));
assertThat(PublisherSchema.INSTANCE.id.isAutoincremnt(), is(true));
assertThat(PublisherSchema.INSTANCE.id.isAutoValue(), is(true));

assertThat(Book_Schema.bookId.isPrimaryKey(), is(true));
assertThat(Book_Schema.bookId.isAutoincremnt(), is(false));
assertThat(Book_Schema.bookId.isAutoValue(), is(true));
assertThat(Book_Schema.INSTANCE.bookId.isPrimaryKey(), is(true));
assertThat(Book_Schema.INSTANCE.bookId.isAutoincremnt(), is(false));
assertThat(Book_Schema.INSTANCE.bookId.isAutoValue(), is(true));

assertThat(Author_Schema.name.isPrimaryKey(), is(true));
assertThat(Author_Schema.name.isAutoincremnt(), is(false));
assertThat(Author_Schema.name.isAutoValue(), is(false));
assertThat(Author_Schema.INSTANCE.name.isPrimaryKey(), is(true));
assertThat(Author_Schema.INSTANCE.name.isAutoincremnt(), is(false));
assertThat(Author_Schema.INSTANCE.name.isAutoValue(), is(false));
}

@Test
public void testColumnStorageTypes() throws Exception {
assertThat(ModelWithStorageTypes_Schema.date.storageType, is("INTEGER"));
assertThat(ModelWithStorageTypes_Schema.timestamp.storageType, is("DATETIME"));
assertThat(ModelWithStorageTypes_Schema.INSTANCE.getCreateTableStatement(), is(
ModelWithStorageTypes_Schema schema = ModelWithStorageTypes_Schema.INSTANCE;
assertThat(schema.date.storageType, is("INTEGER"));
assertThat(schema.timestamp.storageType, is("DATETIME"));
assertThat(schema.INSTANCE.getCreateTableStatement(), is(
"CREATE TABLE `ModelWithStorageTypes` (`date` INTEGER NOT NULL, `timestamp` DATETIME NOT NULL)"
));
}

@Test
public void testStaticTypeAdapterStorageTypes() throws Exception {
assertThat(ModelWithTypeAdapters_Schema.date.storageType, is("INTEGER"));
assertThat(ModelWithTypeAdapters_Schema.sqlDate.storageType, is("TEXT"));
assertThat(ModelWithTypeAdapters_Schema.sqlTime.storageType, is("TEXT"));
assertThat(ModelWithTypeAdapters_Schema.sqlTimestamp.storageType, is("TEXT"));
assertThat(ModelWithTypeAdapters_Schema.intTuple2.storageType, is("INTEGER"));
ModelWithTypeAdapters_Schema schema = ModelWithTypeAdapters_Schema.INSTANCE;
assertThat(schema.date.storageType, is("INTEGER"));
assertThat(schema.sqlDate.storageType, is("TEXT"));
assertThat(schema.sqlTime.storageType, is("TEXT"));
assertThat(schema.sqlTimestamp.storageType, is("TEXT"));
assertThat(schema.intTuple2.storageType, is("INTEGER"));
}

@Test
public void testStorageTypeForDirectAssociation() throws Exception {
assertThat(ModelWithDirectAssociation_Schema.author.storageType,
assertThat(ModelWithDirectAssociation_Schema.INSTANCE.author.storageType,
is(Author_Schema.INSTANCE.getPrimaryKey().storageType));
}

Expand All @@ -130,9 +138,9 @@ public void testInheritance() throws Exception {
assertThat("Base columns first, PrimaryKey las...t",
columns,
Matchers.<ColumnDef<ModelWithInheritance, ?>>contains(
ModelWithInheritance_Schema.baseColumn,
ModelWithInheritance_Schema.value,
ModelWithInheritance_Schema.id
ModelWithInheritance_Schema.INSTANCE.baseColumn,
ModelWithInheritance_Schema.INSTANCE.value,
ModelWithInheritance_Schema.INSTANCE.id
));
}

Expand All @@ -144,9 +152,9 @@ public void testPrimaryKeyPosition() throws Exception {
assertThat("Base columns first, PrimaryKey las...t",
columns,
Matchers.<ColumnDef<ModelWithPrimaryKeyIsNotFirst, ?>>contains(
ModelWithPrimaryKeyIsNotFirst_Schema.foo,
ModelWithPrimaryKeyIsNotFirst_Schema.bar,
ModelWithPrimaryKeyIsNotFirst_Schema.id
ModelWithPrimaryKeyIsNotFirst_Schema.INSTANCE.foo,
ModelWithPrimaryKeyIsNotFirst_Schema.INSTANCE.bar,
ModelWithPrimaryKeyIsNotFirst_Schema.INSTANCE.id
));
}
}
Loading

0 comments on commit 12badf7

Please sign in to comment.