Skip to content

Commit

Permalink
Merge pull request #379 from gfx/upsert2
Browse files Browse the repository at this point in the history
implement Relation#upsert()
  • Loading branch information
gfx authored Feb 7, 2017
2 parents 90143ef + b8e8a9d commit b456f59
Show file tree
Hide file tree
Showing 26 changed files with 624 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@Retention(RetentionPolicy.CLASS)
public @interface OnConflict {

int NONE = 0;
int NONE = 0; // SQLiteDatabase.CONFLICT_NONE

/**
* <blockquote cite="https://www.sqlite.org/lang_conflict.html">When an applicable constraint violation occurs, the
Expand All @@ -48,7 +48,7 @@
* (other than the implied transaction that is created on every command) then the ROLLBACK resolution algorithm works the
* same as the ABORT algorithm.</blockquote>
*/
int ROLLBACK = 1;
int ROLLBACK = 1; // SQLiteDatabase.CONFLICT_ROLLBACK

/**
* <blockquote cite="https://www.sqlite.org/lang_conflict.html">When an applicable constraint violation occurs, the ABORT
Expand All @@ -58,7 +58,7 @@
* is
* the default behavior and the behavior specified by the SQL standard.</blockquote>
*/
int ABORT = 2;
int ABORT = 2; // SQLiteDatabase.CONFLICT_ABORT

/**
* <blockquote cite="https://www.sqlite.org/lang_conflict.html">When an applicable constraint violation occurs, the FAIL
Expand All @@ -68,7 +68,7 @@
* violation on the 100th row that it attempts to update, then the first 99 row changes are preserved but changes to
* rows 100 and beyond never occur.</blockquote>
*/
int FAIL = 3;
int FAIL = 3; // SQLiteDatabase.CONFLICT_FAIL

/**
* <blockquote cite="https://www.sqlite.org/lang_conflict.html">When an applicable constraint violation occurs, the IGNORE
Expand All @@ -78,7 +78,7 @@
* normally.
* No error is returned when the IGNORE conflict resolution algorithm is used.</blockquote>
*/
int IGNORE = 4;
int IGNORE = 4; // SQLiteDatabase.CONFLICT_IGNORE

/**
* <blockquote cite="https://www.sqlite.org/lang_conflict.html">When a UNIQUE or PRIMARY KEY constraint violation occurs,
Expand All @@ -96,5 +96,5 @@
* REPLACE increment the change counter. The exceptional behaviors defined in this paragraph might change in a future
* release.</blockquote>
*/
int REPLACE = 5;
int REPLACE = 5; // SQLiteDatabase.CONFLICT_REPLACE
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.github.gfx.android.orma.example.orma;

import android.content.ContentValues;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Relation;
import com.github.gfx.android.orma.annotation.OnConflict;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -37,6 +39,21 @@ public Category reload(@NonNull Category model) {
return selector().idEq(model.id).value();
}

@NonNull
@Override
public Category upsertWithoutTransaction(@NonNull Category model) {
ContentValues contentValues = new ContentValues();
contentValues.put("`name`", model.name);
if (model.id != 0) {
int updatedRows = updater().idEq(model.id).putAll(contentValues).execute();
if (updatedRows != 0) {
return selector().idEq(model.id).value();
}
}
long rowId = conn.insert(schema, contentValues, OnConflict.NONE);
return conn.findByRowId(schema, rowId);
}

@NonNull
@Override
public Category_Selector selector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.github.gfx.android.orma.ColumnDef;
import com.github.gfx.android.orma.DatabaseHandle;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Schema;
import com.github.gfx.android.orma.annotation.OnConflict;
Expand Down Expand Up @@ -109,7 +110,7 @@ public String getEscapedTableAlias() {
@NonNull
@Override
public String getSelectFromTableClause() {
return "`Category`";
return "`Category`"+ ($alias != null ? " AS " + '`' + $alias + '`' : "");
}

@NonNull
Expand All @@ -127,6 +128,18 @@ public ColumnDef<Category, Long> getPrimaryKey() {
);
}

@NonNull
@Override
public Category_Relation createRelation(@NonNull DatabaseHandle db) {
return new Category_Relation(db.getConnection(), this);
}

@NonNull
@Override
public Category_Relation createRelation(@NonNull OrmaConnection conn) {
return new Category_Relation(conn, this);
}

@NonNull
@Override
public String[] getDefaultResultColumns() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.github.gfx.android.orma.example.orma;

import android.content.ContentValues;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Relation;
import com.github.gfx.android.orma.annotation.OnConflict;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -37,6 +39,22 @@ public Entry reload(@NonNull Entry model) {
return selector().idEq(model.id).value();
}

@NonNull
@Override
public Entry upsertWithoutTransaction(@NonNull Entry model) {
ContentValues contentValues = new ContentValues();
contentValues.put("`resourceType`", model.resourceType);
contentValues.put("`resourceId`", model.resourceId);
if (model.id != 0) {
int updatedRows = updater().idEq(model.id).putAll(contentValues).execute();
if (updatedRows != 0) {
return selector().idEq(model.id).value();
}
}
long rowId = conn.insert(schema, contentValues, OnConflict.NONE);
return conn.findByRowId(schema, rowId);
}

@NonNull
@Override
public Entry_Selector selector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.github.gfx.android.orma.ColumnDef;
import com.github.gfx.android.orma.DatabaseHandle;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Schema;
import com.github.gfx.android.orma.annotation.OnConflict;
Expand Down Expand Up @@ -130,7 +131,7 @@ public String getEscapedTableAlias() {
@NonNull
@Override
public String getSelectFromTableClause() {
return "`Entry`";
return "`Entry`"+ ($alias != null ? " AS " + '`' + $alias + '`' : "");
}

@NonNull
Expand All @@ -149,6 +150,18 @@ public ColumnDef<Entry, Long> getPrimaryKey() {
);
}

@NonNull
@Override
public Entry_Relation createRelation(@NonNull DatabaseHandle db) {
return new Entry_Relation(db.getConnection(), this);
}

@NonNull
@Override
public Entry_Relation createRelation(@NonNull OrmaConnection conn) {
return new Entry_Relation(conn, this);
}

@NonNull
@Override
public String[] getDefaultResultColumns() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.gfx.android.orma.example.orma;

import android.content.ContentValues;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Relation;
import com.github.gfx.android.orma.annotation.OnConflict;
import com.github.gfx.android.orma.example.tool.TypeAdapters;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -37,6 +40,23 @@ public Item2 reload(@NonNull Item2 model) {
return selector().nameEq(model.name).value();
}

@NonNull
@Override
public Item2 upsertWithoutTransaction(@NonNull Item2 model) {
ContentValues contentValues = new ContentValues();
contentValues.put("`category1`", Category_Schema.INSTANCE.createRelation(conn).upsertWithoutTransaction(model.category1).id);
contentValues.put("`category2`", Category_Schema.INSTANCE.createRelation(conn).upsertWithoutTransaction(model.category2).id);
contentValues.put("`zonedTimestamp`", TypeAdapters.serializeZonedDateTime(model.zonedTimestamp));
contentValues.put("`localDateTime`", TypeAdapters.serializeLocalDateTime(model.localDateTime));
contentValues.put("`name`", model.name);
int updatedRows = updater().nameEq(model.name).putAll(contentValues).execute();
if (updatedRows != 0) {
return selector().nameEq(model.name).value();
}
long rowId = conn.insert(schema, contentValues, OnConflict.NONE);
return conn.findByRowId(schema, rowId);
}

@NonNull
@Override
public Item2_Selector selector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.Nullable;
import com.github.gfx.android.orma.AssociationDef;
import com.github.gfx.android.orma.ColumnDef;
import com.github.gfx.android.orma.DatabaseHandle;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Schema;
import com.github.gfx.android.orma.annotation.OnConflict;
Expand Down Expand Up @@ -214,6 +215,18 @@ public ColumnDef<Item2, String> getPrimaryKey() {
);
}

@NonNull
@Override
public Item2_Relation createRelation(@NonNull DatabaseHandle db) {
return new Item2_Relation(db.getConnection(), this);
}

@NonNull
@Override
public Item2_Relation createRelation(@NonNull OrmaConnection conn) {
return new Item2_Relation(conn, this);
}

@NonNull
@Override
public String[] getDefaultResultColumns() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.github.gfx.android.orma.example.orma;

import android.content.ContentValues;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Relation;
import com.github.gfx.android.orma.annotation.OnConflict;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -37,6 +39,20 @@ public Item reload(@NonNull Item model) {
return selector().nameEq(model.name).value();
}

@NonNull
@Override
public Item upsertWithoutTransaction(@NonNull Item model) {
ContentValues contentValues = new ContentValues();
contentValues.put("`category`", Category_Schema.INSTANCE.createRelation(conn).upsertWithoutTransaction(model.category).id);
contentValues.put("`name`", model.name);
int updatedRows = updater().nameEq(model.name).putAll(contentValues).execute();
if (updatedRows != 0) {
return selector().nameEq(model.name).value();
}
long rowId = conn.insert(schema, contentValues, OnConflict.NONE);
return conn.findByRowId(schema, rowId);
}

@NonNull
@Override
public Item_Selector selector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.Nullable;
import com.github.gfx.android.orma.AssociationDef;
import com.github.gfx.android.orma.ColumnDef;
import com.github.gfx.android.orma.DatabaseHandle;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Schema;
import com.github.gfx.android.orma.annotation.OnConflict;
Expand Down Expand Up @@ -134,6 +135,18 @@ public ColumnDef<Item, String> getPrimaryKey() {
);
}

@NonNull
@Override
public Item_Relation createRelation(@NonNull DatabaseHandle db) {
return new Item_Relation(db.getConnection(), this);
}

@NonNull
@Override
public Item_Relation createRelation(@NonNull OrmaConnection conn) {
return new Item_Relation(conn, this);
}

@NonNull
@Override
public String[] getDefaultResultColumns() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.gfx.android.orma.example.orma;

import android.content.ContentValues;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.github.gfx.android.orma.BuiltInSerializers;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Relation;
import com.github.gfx.android.orma.annotation.OnConflict;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -37,6 +40,24 @@ public Todo reload(@NonNull Todo model) {
return selector().idEq(model.id).value();
}

@NonNull
@Override
public Todo upsertWithoutTransaction(@NonNull Todo model) {
ContentValues contentValues = new ContentValues();
contentValues.put("`title`", model.title);
contentValues.put("`content`", model.content);
contentValues.put("`done`", model.done);
contentValues.put("`createdTime`", BuiltInSerializers.serializeDate(model.createdTime));
if (model.id != 0) {
int updatedRows = updater().idEq(model.id).putAll(contentValues).execute();
if (updatedRows != 0) {
return selector().idEq(model.id).value();
}
}
long rowId = conn.insert(schema, contentValues, OnConflict.NONE);
return conn.findByRowId(schema, rowId);
}

@NonNull
@Override
public Todo_Selector selector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.Nullable;
import com.github.gfx.android.orma.BuiltInSerializers;
import com.github.gfx.android.orma.ColumnDef;
import com.github.gfx.android.orma.DatabaseHandle;
import com.github.gfx.android.orma.OrmaConnection;
import com.github.gfx.android.orma.Schema;
import com.github.gfx.android.orma.annotation.OnConflict;
Expand Down Expand Up @@ -177,7 +178,7 @@ public String getEscapedTableAlias() {
@NonNull
@Override
public String getSelectFromTableClause() {
return "`Todo`";
return "`Todo`"+ ($alias != null ? " AS " + '`' + $alias + '`' : "");
}

@NonNull
Expand All @@ -198,6 +199,18 @@ public ColumnDef<Todo, Long> getPrimaryKey() {
);
}

@NonNull
@Override
public Todo_Relation createRelation(@NonNull DatabaseHandle db) {
return new Todo_Relation(db.getConnection(), this);
}

@NonNull
@Override
public Todo_Relation createRelation(@NonNull OrmaConnection conn) {
return new Todo_Relation(conn, this);
}

@NonNull
@Override
public String[] getDefaultResultColumns() {
Expand Down
Loading

0 comments on commit b456f59

Please sign in to comment.