Skip to content

Commit 37cbc00

Browse files
committed
update insert
1 parent 17a1996 commit 37cbc00

4 files changed

Lines changed: 87 additions & 54 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/br/com/sql/Example.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
1818
SimpleSQL simpleSQL = new SimpleSQL(new HelperBD(this));
1919
Pessoa pessoa = new Pessoa();
2020
pessoa.setName("paulo");
21-
simpleSQL.insert(pessoa);
21+
simpleSQL.insert(pessoa).execute(pessoa);
2222

2323
List<Pessoa> list = simpleSQL.selectTable(new Pessoa())
2424
.fields(new String[]{"name"})

app/src/main/java/br/com/sql/Pessoa.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ public String getName() {
3030
public void setName(String name) {
3131
this.name = name;
3232
}
33+
3334
}

simplesql/src/main/java/com/simplesql/simplesql/config/SimpleSQL.java

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public Update updateTable(Object typeObject) {
4242
return new Update(typeObject);
4343
}
4444

45+
public Insert insert(Object o){
46+
return new Insert(o);
47+
}
48+
4549
/**
4650
* Developed by Lucas Nascimento
4751
* Method SELECT
@@ -150,33 +154,33 @@ public Select or() {
150154
return this;
151155
}
152156

153-
public Select innerJoin(){
157+
public Select innerJoin() {
154158
this.innerJoin = true;
155-
SQLString = SQLString+" INNER JOIN ";
159+
SQLString = SQLString + " INNER JOIN ";
156160
return this;
157161
}
158162

159-
public Select leftJoin(){
163+
public Select leftJoin() {
160164
this.leftJoin = true;
161-
SQLString = SQLString+" LEFT JOIN ";
165+
SQLString = SQLString + " LEFT JOIN ";
162166
return this;
163167
}
164168

165-
public Select rigthJoin(){
169+
public Select rigthJoin() {
166170
this.rightJoin = true;
167-
SQLString = SQLString+" RIGHT JOIN ";
171+
SQLString = SQLString + " RIGHT JOIN ";
168172
return this;
169173
}
170174

171-
public Select fullJoin(){
175+
public Select fullJoin() {
172176
this.innerJoin = true;
173-
SQLString = SQLString+" FULL JOIN ";
177+
SQLString = SQLString + " FULL JOIN ";
174178
return this;
175179
}
176180

177-
public Select on(){
181+
public Select on() {
178182
this.on = true;
179-
SQLString = SQLString+" ON ";
183+
SQLString = SQLString + " ON ";
180184
return this;
181185
}
182186

@@ -259,7 +263,7 @@ else if (field.getType() == short.class)
259263
* Method UPDATE
260264
*/
261265

262-
public class Update{
266+
public class Update {
263267
private Object typeObject;
264268
private String tableName, field, writeSQL, column, table, stringSet;
265269
private String SQLString;
@@ -302,30 +306,30 @@ public Update where() {
302306
return this;
303307
}
304308

305-
public Update equals(){
306-
SQLString = SQLString +" = ";
309+
public Update equals() {
310+
SQLString = SQLString + " = ";
307311
return this;
308312
}
309313

310-
public Update or(){
311-
SQLString = SQLString+" OR ";
314+
public Update or() {
315+
SQLString = SQLString + " OR ";
312316
return this;
313317
}
314318

315-
public Update and(){
316-
SQLString = SQLString+" AND ";
319+
public Update and() {
320+
SQLString = SQLString + " AND ";
317321
return this;
318322
}
319323

320-
public Update column(String name){
324+
public Update column(String name) {
321325
this.column = name;
322-
SQLString = SQLString +" "+name+" ";
326+
SQLString = SQLString + " " + name + " ";
323327
return this;
324328
}
325329

326330
public Update fieldString(String value) {
327331
this.field = field;
328-
SQLString = SQLString + "\""+value+"\"";
332+
SQLString = SQLString + "\"" + value + "\"";
329333
return this;
330334
}
331335

@@ -461,45 +465,73 @@ public String create(Object obj, SQLiteDatabase db) {
461465
/**
462466
* Developed by Paulo Iury
463467
* Method INSERT
464-
*
465-
* @param obj
466468
*/
467-
public boolean insert(Object obj) {
469+
public class Insert {
470+
private Object object;
471+
private ContentValues values;
468472

469-
try {
470-
SQLiteDatabase write = helperBD.getReadableDatabase();
471-
ContentValues values = new ContentValues();
472-
Table persistable =
473-
obj.getClass().getAnnotation(Table.class);
474-
if (persistable != null) {
475-
Field[] fields = obj.getClass().getDeclaredFields();
476-
for (Field field : fields) {
477-
// como os atributos são private,
478-
// setamos ele como visible
479-
field.setAccessible(true);
480-
// Se o atributo tem a anotação
481-
if (field.isAnnotationPresent(Column.class)) {
482-
if (!field.isAnnotationPresent(AutoIncrement.class)) {
483-
String value = field.get(obj).toString();
484-
values.put(field.getName(), value);
473+
public Insert(Object obj) {
474+
this.object = obj;
475+
values = new ContentValues();
476+
}
477+
478+
public boolean execute(Object obj) {
479+
480+
try {
481+
SQLiteDatabase write = helperBD.getReadableDatabase();
482+
Table persistable =
483+
obj.getClass().getAnnotation(Table.class);
484+
if (persistable != null) {
485+
Field[] fields = obj.getClass().getDeclaredFields();
486+
for (Field field : fields) {
487+
// como os atributos são private,
488+
// setamos ele como visible
489+
field.setAccessible(true);
490+
// Se o atributo tem a anotação
491+
Column column = field.getAnnotation(Column.class);
492+
if (column != null) {
493+
if (!field.isAnnotationPresent(AutoIncrement.class) && field.get(obj) != null) {
494+
checkObject(field, obj);
495+
}
496+
} else {
497+
throw new SQLException("The " + field.getName() + "attribute did not have the column annotation");
485498
}
486-
} else {
487-
throw new SQLException("The " + field.getName() + "attribute did not have the column annotation");
488499
}
489-
}
490-
long result = write.insert(obj.getClass().getSimpleName(), null, values);
491-
return result != -1;
492-
} else
493-
throw new SQLException("This class does not have the table annotation");
494-
} catch (SQLException e) {
495-
e.printStackTrace();
496-
return false;
497-
} catch (Exception e) {
498-
e.printStackTrace();
499-
return false;
500+
long result = write.insert(obj.getClass().getSimpleName(), null, values);
501+
return result != -1;
502+
} else
503+
throw new SQLException("This class does not have the table annotation");
504+
} catch (SQLException e) {
505+
e.printStackTrace();
506+
return false;
507+
} catch (Exception e) {
508+
e.printStackTrace();
509+
return false;
510+
}
511+
512+
}
513+
514+
private void checkObject(Field field, Object obj) {
515+
try {
516+
if (field.get(obj).getClass() == String.class)
517+
values.put(field.getName(), (String) field.get(obj));
518+
else if (field.get(obj).getClass() == long.class)
519+
values.put(field.getName(), (long) field.get(obj));
520+
else if (field.get(obj).getClass() == float.class)
521+
values.put(field.getName(), (float) field.get(obj));
522+
else if (field.get(obj).getClass() == byte[].class)
523+
values.put(field.getName(), (byte[]) field.get(obj));
524+
else if (field.get(obj).getClass() == int.class)
525+
values.put(field.getName(), (int) field.get(obj));
526+
else if (field.get(obj).getClass() == short.class)
527+
values.put(field.getName(), (short) field.get(obj));
528+
} catch (IllegalAccessException e) {
529+
e.printStackTrace();
530+
}
500531
}
501532
}
502533

534+
503535
private String checkAnnotations(Field c, boolean not_null) {
504536
String annotations = "";
505537
if (c.isAnnotationPresent(Key.class))

0 commit comments

Comments
 (0)