Skip to content

Commit

Permalink
Merge pull request agrosner#307 from Raizlabs/develop
Browse files Browse the repository at this point in the history
Version 2.2.0
  • Loading branch information
agrosner committed Jul 3, 2015
2 parents 4268fd9 + bdea244 commit 30bb55f
Show file tree
Hide file tree
Showing 56 changed files with 1,094 additions and 356 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,5 @@ public class Classes {

public static final String DELETE_MODEL_LIST_TRANSACTION = TRANSACTION + "process.DeleteModelListTransaction";

public static final String SAVE_MODEL_LIST_TRANSACTION = TRANSACTION + "process.SaveModelTransaction";
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ public boolean isDelete() {
return isAll() || methods.contains(OneToMany.Method.DELETE);
}

public boolean isSave() {
return isAll() || methods.contains(OneToMany.Method.SAVE);
}

/**
* Writes the method to the specified java writer for loading from DB.
*
* @param javaWriter
* @throws IOException
*/
public void writeLoad(JavaWriter javaWriter) throws IOException {
if(isLoad()) {
if (isLoad()) {
javaWriter.emitStatement(getMethodName());
}
}
Expand All @@ -70,14 +74,23 @@ public void writeLoad(JavaWriter javaWriter) throws IOException {
* @throws IOException
*/
public void writeDelete(JavaWriter javaWriter) throws IOException {
if(isDelete()) {
if (isDelete()) {
javaWriter.emitStatement("new %1s<>(%1s.withModels(%1s)).onExecute()",
Classes.DELETE_MODEL_LIST_TRANSACTION,
Classes.PROCESS_MODEL_INFO, getMethodName());
Classes.DELETE_MODEL_LIST_TRANSACTION,
Classes.PROCESS_MODEL_INFO, getMethodName());

javaWriter.emitStatement("%1s = null", getVariableName());
}
}

public void writeSave(JavaWriter javaWriter) throws IOException {
if (isSave()) {
javaWriter.emitStatement("new %1s<>(%1s.withModels(%1s)).onExecute()",
Classes.SAVE_MODEL_LIST_TRANSACTION,
Classes.PROCESS_MODEL_INFO, getMethodName());
}
}


private String getMethodName() {
return String.format("%1s.%1s()", ModelUtils.getVariable(false), methodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.raizlabs.android.dbflow.processor.writer.ExistenceWriter;
import com.raizlabs.android.dbflow.processor.writer.FlowWriter;
import com.raizlabs.android.dbflow.processor.writer.LoadCursorWriter;
import com.raizlabs.android.dbflow.processor.writer.OneToManySaveWriter;
import com.raizlabs.android.dbflow.processor.writer.SQLiteStatementWriter;
import com.raizlabs.android.dbflow.processor.writer.WhereQueryWriter;
import com.raizlabs.android.dbflow.sql.QueryBuilder;
Expand Down Expand Up @@ -162,7 +163,8 @@ public TableDefinition(ProcessorManager manager, Element element) {
new LoadCursorWriter(this, false, implementsLoadFromCursorListener),
new WhereQueryWriter(this, false),
new CreationQueryWriter(manager, this),
new DeleteWriter(this, false)
new DeleteWriter(this, false),
new OneToManySaveWriter(this, false)
};

// single primary key checking for a long or int valued column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void handle(ProcessorManager processorManager, RoundEnvironment roundEnvi
databaseWriter.write(javaWriter);
javaWriter.close();
} catch (IOException e) {
e.printStackTrace();
processorManager.getMessager().printMessage(Diagnostic.Kind.WARNING, e.getMessage());
}
}

Expand All @@ -308,7 +308,7 @@ public void handle(ProcessorManager processorManager, RoundEnvironment roundEnvi

staticFlowManager.close();
} catch (IOException e) {
e.printStackTrace();
processorManager.getMessager().printMessage(Diagnostic.Kind.WARNING, e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public void write(JavaWriter javaWriter) throws IOException {
final TableDefinition tableDefinition = ((TableDefinition) baseTableDefinition);

if (tableDefinition.hasCachingId) {

WriterUtils.emitOverriddenMethod(javaWriter, new FlowWriter() {
@Override
public void write(JavaWriter javaWriter) throws IOException {
javaWriter.emitStatement("return true");
}
}, "boolean", "hasCachingId", Sets.newHashSet(Modifier.PUBLIC));

String[] params2 = new String[2];
params2[0] = ModelUtils.getParameter(isModelContainerDefinition, tableDefinition.getModelClassName());
params2[1] = ModelUtils.getVariable(isModelContainerDefinition);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.raizlabs.android.dbflow.processor.writer;

import com.google.common.collect.Sets;
import com.raizlabs.android.dbflow.processor.definition.OneToManyDefinition;
import com.raizlabs.android.dbflow.processor.definition.TableDefinition;
import com.raizlabs.android.dbflow.processor.utils.ModelUtils;
import com.raizlabs.android.dbflow.processor.utils.WriterUtils;
import com.squareup.javawriter.JavaWriter;

import java.io.IOException;

import javax.lang.model.element.Modifier;

/**
* Description: Overrides the save, update, and insert methods if the {@link com.raizlabs.android.dbflow.annotation.OneToMany.Method#SAVE} is used.
*/
public class OneToManySaveWriter implements FlowWriter {

private final TableDefinition tableDefinition;
private final boolean isModelContainerAdapter;

public OneToManySaveWriter(TableDefinition tableDefinition, boolean isModelContainerAdapter) {
this.tableDefinition = tableDefinition;
this.isModelContainerAdapter = isModelContainerAdapter;
}

@Override
public void write(JavaWriter javaWriter) throws IOException {

boolean shouldWrite = false;
for (OneToManyDefinition oneToManyDefinition : tableDefinition.oneToManyDefinitions) {
if(oneToManyDefinition.isSave()) {
shouldWrite = true;
break;
}
}

if(shouldWrite) {
writeMethod("save", javaWriter);
writeMethod("insert", javaWriter);
writeMethod("update", javaWriter);
}
}

private void writeMethod(final String methodName, JavaWriter javaWriter) {
WriterUtils.emitOverriddenMethod(javaWriter, new FlowWriter() {
@Override
public void write(JavaWriter javaWriter) throws IOException {
for (OneToManyDefinition oneToManyDefinition : tableDefinition.oneToManyDefinitions) {
oneToManyDefinition.writeSave(javaWriter);
}

javaWriter.emitStatement("super.%1s(%1s)", methodName, ModelUtils.getVariable(isModelContainerAdapter));
}
}, "void", methodName, Sets.newHashSet(Modifier.PUBLIC, Modifier.FINAL), tableDefinition.getModelClassName(),
ModelUtils.getVariable(isModelContainerAdapter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public QueryClass appendSpace() {
/**
* Appends the string with spaces on the front and end of the string
*
* @param string The string to append
* @param object The object to append
* @return This instance
*/
@SuppressWarnings("unchecked")
public QueryClass appendSpaceSeparated(String string) {
return (QueryClass) appendSpace().append(string).appendSpace();
public QueryClass appendSpaceSeparated(Object object) {
return (QueryClass) appendSpace().append(object).appendSpace();
}

/**
Expand Down Expand Up @@ -146,7 +146,10 @@ public QueryClass appendList(List<?> objects) {
*/
public QueryClass appendQualifier(String name, String value) {
if (value != null && value.length() > 0) {
append(name).appendSpaceSeparated(value);
if(name != null) {
append(name);
}
appendSpaceSeparated(value);
}
return castThis();
}
Expand Down Expand Up @@ -212,6 +215,15 @@ public String getQuery() {
return query.toString();
}

/**
* @param columnName The column name to use.
* @return A name in quotes. E.G. index =&gt; `index` so we can use keywords as column names without fear
* of clashing.
*/
public static String quote(String columnName) {
return "`" + columnName.replace(".", "`.`") + "`";
}

/**
* Returns a string containing the tokens joined by delimiters.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.raizlabs.android.dbflow.test.container;

import com.raizlabs.android.dbflow.sql.builder.Condition;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.Select;
import com.raizlabs.android.dbflow.structure.container.ForeignKeyContainer;
import com.raizlabs.android.dbflow.test.FlowTestCase;

import static com.raizlabs.android.dbflow.sql.builder.Condition.column;

/**
* Description: Asserts values are handled same for container and adapter
*/
Expand All @@ -22,7 +22,7 @@ public void testContainer() {
autoIncrementContainer.save();

autoIncrementContainer = new Select().from(AIContainerForeign.class).where(
Condition.column(AIContainerForeign$Table.ID).is(autoIncrementContainer.id)).querySingle();
column(AIContainerForeign$Table.ID).is(autoIncrementContainer.id)).querySingle();
assertNull(autoIncrementContainer.foreignModel);
assertNull(autoIncrementContainer.container);

Expand All @@ -34,7 +34,7 @@ public void testContainer() {
autoIncrementContainer.foreignModel = foreignModel;

AutoIncrementContainer foreignKeyContainer = new AutoIncrementContainer();
foreignKeyContainer.name = "container";
foreignKeyContainer.name = "container";
foreignKeyContainer.a_id = 54;
foreignKeyContainer.save();
assertTrue(foreignKeyContainer.exists());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.raizlabs.android.dbflow.test.container;

import com.raizlabs.android.dbflow.sql.builder.Condition;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.Select;
import com.raizlabs.android.dbflow.test.FlowTestCase;
import com.raizlabs.android.dbflow.test.structure.TestModel1;

import static com.raizlabs.android.dbflow.sql.builder.Condition.column;

/**
* Description:
*/
Expand Down Expand Up @@ -33,7 +34,7 @@ public void testForeignKeyModel() {


foreignInteractionModel = new Select().from(ForeignInteractionModel.class)
.where(Condition.column(ForeignInteractionModel$Table.NAME).is("Test2")).querySingle();
.where(column(ForeignInteractionModel$Table.NAME).is("Test2")).querySingle();
assertNotNull(foreignInteractionModel);
assertNotNull(foreignInteractionModel.testModel1);
TestModel1 testModel11 = foreignInteractionModel.getTestModel1();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MapModelTest extends FlowTestCase {
public void testMapModel() {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("name", "test");
MapModel<TestModel1> model1MapModel = new MapModel<TestModel1>(dataMap,TestModel1.class);
MapModel<TestModel1> model1MapModel = new MapModel<>(dataMap, TestModel1.class);
model1MapModel.save();

assertTrue(model1MapModel.exists());
Expand All @@ -28,7 +28,7 @@ public void testMapModel() {

otherDataMap.put("testModel", dataMap);

MapModel<TestModelContainerClass> testModelContainerClassMapModel = new MapModel<TestModelContainerClass>(otherDataMap, TestModelContainerClass.class);
MapModel<TestModelContainerClass> testModelContainerClassMapModel = new MapModel<>(otherDataMap, TestModelContainerClass.class);
testModelContainerClassMapModel.save();

assertTrue(testModelContainerClassMapModel.exists());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package com.raizlabs.android.dbflow.test.contentobserver;

import android.net.Uri;

import com.raizlabs.android.dbflow.runtime.FlowContentObserver;
import com.raizlabs.android.dbflow.sql.SqlUtils;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.structure.BaseModel;
import com.raizlabs.android.dbflow.structure.Model;
import com.raizlabs.android.dbflow.test.FlowTestCase;
import com.raizlabs.android.dbflow.test.structure.TestModel1;
import com.raizlabs.android.dbflow.test.structure.TestModel1$Table;

/**
* Description:
*/
public class ContentObserverTest extends FlowTestCase {

public void testNotificationUri() {

Uri notificationUri = SqlUtils.getNotificationUri(TestModel1.class, BaseModel.Action.SAVE, TestModel1$Table.NAME, "this is a %test");
assertEquals(notificationUri.getAuthority(), TestModel1$Table.TABLE_NAME);
assertEquals(notificationUri.getFragment(), BaseModel.Action.SAVE.name());
assertEquals(Uri.decode(notificationUri.getQueryParameter(TestModel1$Table.NAME)), "this is a %test");
}

public void testContentObserver() {
Delete.table(TestModel1.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.raizlabs.android.dbflow.test.example;

import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.ForeignKey;
import com.raizlabs.android.dbflow.annotation.ForeignKeyReference;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;
import com.raizlabs.android.dbflow.structure.container.ForeignKeyContainer;

/**
* Description:
*/
@Table(databaseName = ColonyDatabase.NAME)
public class Ant extends BaseModel {

@Column
@PrimaryKey(autoincrement = true)
long id;

@Column
String type;

@Column
boolean isMale;

@Column
@ForeignKey(references = {@ForeignKeyReference(columnName = "queen_id",
columnType = Long.class,
foreignColumnName = "id")},
saveForeignKeyModel = false)
ForeignKeyContainer<Queen> queenForeignKeyContainer;

public void associateQueen(Queen queen) {
queenForeignKeyContainer = new ForeignKeyContainer<>(Queen.class);
queenForeignKeyContainer.setModel(queen);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.raizlabs.android.dbflow.test.example;

import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;

/**
* Description:
*/
@Table(databaseName = ColonyDatabase.NAME)
public class Colony extends BaseModel {

@Column
@PrimaryKey(autoincrement = true)
long id;

@Column
String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.raizlabs.android.dbflow.test.example;

import com.raizlabs.android.dbflow.annotation.Database;

/**
* Description:
*/
@Database(name = ColonyDatabase.NAME, version = ColonyDatabase.VERSION)
public class ColonyDatabase {

public static final String NAME = "Colonies";

public static final int VERSION = 1;
}
Loading

0 comments on commit 30bb55f

Please sign in to comment.