Deleting entities
You have two ways to delete entities in KittyORM:
Delete associated with model row by calling
KittyMapper.delete(M model)
orKittyMapper.delete(List<M> models)
methods:1// Initializing database instance 2BasicDatabase db = new BasicDatabase(getContext()); 3// Getting mapper instance 4RandomMapper mapper = (RandomMapper) db.getMapper(RandomModel.class); 5// Getting existing model from database (assuming that 0l model exists) 6RandomModel toDelete = mapper.findByIPK(0l); 7// Deleting model 8long rowsAffected = mapper.delete(toDelete);
Delete all rows in a table that suit provided
WHERE
clause generated withSQLiteConditionBuilder.class
:1// Deleting by random_int range 2BasicDatabase db = new BasicDatabase(getContext()); 3// Getting mapper instance 4RandomMapper mapper = (RandomMapper) db.getMapper(RandomModel.class); 5// Creating clause for deletion 6SQLiteCondition condition = new SQLiteConditionBuilder() 7 .addColumn("random_int") 8 .addSQLOperator(GREATER_OR_EQUAL) 9 .addValue(0) 10 .addSQLOperator(AND) 11 .addColumn("random_int") 12// Just as tip, you can pass SQLite operator as string, not only as 13// SQLiteOperator enum element 14 .addSQLOperator("<=") 15 .addValue(10000) 16 .build(); 17// Deleting with generated clause 18mapper.deleteWhere(condition); 19 20// Also, you may use pass condition as SQLite string 21mapper.deleteWhere("random_int >= ? AND random_int <= ?", 0, 10000) 22 23// And, finally, in those string you may use POJO field names in #?fieldName 24mapper.deleteWhere("#?randomInt >= ? AND #?randomInt <= ?", 0, 10000)
Closer look on SQLiteConditionBuilder.class
SQLiteConditionBuilder.class
is a builder for generating WHERE
clauses within KittyORM. It is really simple to use and it is designed to generate WHERE
clauses in most natural for SQLite way: just add column names, SQLite operators and values for your clause in an order they should appear in SQLite clause. For example, clause WHERE a_column = 'a' AND (b_column > 0 OR BETWEEN 10 AND 20)
would be generated by following code:
1SQLiteCondition condition = new SQLiteConditionBuilder()
2 .addColumn("a_column")
3 .addSQLOperator(EQUAL)
4 .addValue("a")
5 .addSQLOperator(AND)
6 .addSQLOperator(OPEN_SUBC)
7 .addColumn("b_column")
8 .addSQLOperator(GREATER_THAN)
9 .addValue(0)
10 .addSQLOperator(OR)
11 .addSQLOperator(BETWEEN)
12 .addValue(10)
13 .addSQLOperator(AND)
14 .addValue(20)
15 .addSQLOperator(CLOSE_SUBC)
16 .build();
17
18// Note that following condition is equal to previous one
19SQLiteCondition condition2 = new SQLiteConditionBuilder()
20 .addColumn("a_column")
21 .addSQLOperator("=")
22 .addValue("a")
23 .addSQLOperator("AND")
24 .addSQLOperator("(")
25 .addColumn("b_column")
26 .addSQLOperator(">=")
27 .addValue(0)
28 .addSQLOperator("OR")
29 .addSQLOperator("BETWEEN")
30 .addValue(10)
31 .addSQLOperator("AND")
32 .addValue(20)
33 .addSQLOperator(")")
34 .build();
35
36// And, finally, you can pass to condition builder not only column names but POJO field names as well
37SQLiteCondition condition3 = new SQLiteConditionBuilder()
38 .addField("aColumn", SomeModel.class)
39 .addSQLOperator("=")
40 .addValue("a")
41 .addSQLOperator("AND")
42 .addSQLOperator("(")
43 .addField("bColumn", SomeModel.class)
44 .addSQLOperator(">=")
45 .addValue(0)
46 .addSQLOperator("OR")
47 .addSQLOperator("BETWEEN")
48 .addValue(10)
49 .addSQLOperator("AND")
50 .addValue(20)
51 .addSQLOperator(")")
52 .build();
53
54// Also, if you would like use SQLite string to build condition, you may use following method:
55SQLiteCondition condition4 = SQLiteConditionBuilder.fromSQL("WHERE a_column = ? AND (b_column > ? OR BETWEEN ? AND ?)", SomeModel.class, "a", 0, 10, 20);
56
57// And, for SQLite string, you may pass POJO field names as well using #?fieldName syntax
58SQLiteCondition condition5 = SQLiteConditionBuilder.fromSQL("WHERE #?aColumn = ? AND (#?bColumn > ? OR BETWEEN ? AND ?)", SomeModel.class, "a", 0, 10, 20);
net.akaish.kitty.orm.query.conditions.SQLiteOperator
enumeration.