Skip to content

Commit ac213b0

Browse files
committed
Update insert
add annotation Unique
1 parent 9fe4e54 commit ac213b0

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

.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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import com.simplesql.simplesql.config.SimpleSQL;
88

9-
import java.sql.SQLException;
109
import java.util.List;
1110

1211
public class Example extends AppCompatActivity {
@@ -18,13 +17,14 @@ protected void onCreate(Bundle savedInstanceState) {
1817
SimpleSQL simpleSQL = new SimpleSQL(new HelperBD(this));
1918
Pessoa pessoa = new Pessoa();
2019
pessoa.setName("paulo");
21-
simpleSQL.insert(pessoa).execute();
20+
pessoa.setEmail("[email protected]");
21+
pessoa.setPhone("1231789201");
22+
boolean result = simpleSQL.insert(pessoa).execute();
2223

2324

2425

2526
List<Pessoa> list = simpleSQL.selectTable(new Pessoa())
26-
.fields(new String[]{"name"})
27+
.fields(new String[]{"*"})
2728
.execute();
28-
2929
}
3030
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
public class HelperBD extends SQLiteOpenHelper {
1818
private static final String NAME = "nome_banco.bd";
19-
private static final int VERSION = 14;
19+
private static final int VERSION = 11;
2020
private SimpleSQL simpleSQL;
2121

2222
public HelperBD(@Nullable Context context) {

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.simplesql.simplesql.annotations.Column;
55
import com.simplesql.simplesql.annotations.Key;
66
import com.simplesql.simplesql.annotations.Table;
7+
import com.simplesql.simplesql.annotations.Unique;
78

89
@Table
910
public class Pessoa {
@@ -15,7 +16,13 @@ public class Pessoa {
1516
@Column(type = "TEXT")
1617
private String name;
1718

19+
@Unique
20+
@Column(type = "TEXT",non_null = true)
21+
private String email;
1822

23+
@Unique
24+
@Column(type = "TEXT",non_null = true)
25+
private String phone;
1926

2027
public int getId() {
2128
return id;
@@ -33,4 +40,19 @@ public void setName(String name) {
3340
this.name = name;
3441
}
3542

43+
public String getEmail() {
44+
return email;
45+
}
46+
47+
public void setEmail(String email) {
48+
this.email = email;
49+
}
50+
51+
public String getPhone() {
52+
return phone;
53+
}
54+
55+
public void setPhone(String phone) {
56+
this.phone = phone;
57+
}
3658
}

simplesql/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
defaultConfig {
88
minSdkVersion 11
99
targetSdkVersion 29
10-
versionCode 23
11-
versionName "1.0.22"
10+
versionCode 28
11+
versionName "1.0.27"
1212

1313
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1414
consumerProguardFiles 'consumer-rules.pro'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.simplesql.simplesql.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.FIELD)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface Unique {
11+
}

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.database.sqlite.SQLiteDatabase;
66
import android.database.sqlite.SQLiteOpenHelper;
77
import android.os.Build;
8+
import android.util.Log;
89

910
import com.google.gson.Gson;
1011

@@ -21,6 +22,7 @@
2122
import com.simplesql.simplesql.annotations.ForeignKey;
2223
import com.simplesql.simplesql.annotations.Key;
2324
import com.simplesql.simplesql.annotations.Table;
25+
import com.simplesql.simplesql.annotations.Unique;
2426

2527
public class SimpleSQL {
2628
private SQLiteOpenHelper helperBD;
@@ -249,7 +251,7 @@ public List execute() {
249251
if (functionParameter) {
250252
if (fields[0] == null || fields[0].equals(""))
251253
columnFunction = "*";
252-
SQLString = SQLString.replace("*","").replace(KEY_FUNCTION_PARAMETER, fields[0]);
254+
SQLString = SQLString.replace("*", "").replace(KEY_FUNCTION_PARAMETER, fields[0]);
253255
}
254256
SQLString = SQLString + ";";
255257
List lstClasses = new ArrayList<>();
@@ -266,6 +268,7 @@ public List execute() {
266268
String hashJson = new Gson().toJson(hashMap);
267269
lstClasses.add(new Gson().fromJson(hashJson, (Type) typeObject.getClass()));
268270
}
271+
cursor.close();
269272
} catch (Exception e) {
270273
e.printStackTrace();
271274
return new ArrayList();
@@ -486,12 +489,10 @@ public String create(Object obj, SQLiteDatabase db) {
486489
Column column =
487490
field.getAnnotation(Column.class);
488491
if (column != null) {
489-
if (count == 0) {
492+
if (count == 0)
490493
columns += field.getName() + " " + column.type();
491-
} else {
494+
else
492495
columns += " , " + field.getName() + " " + column.type();
493-
494-
}
495496
columns += checkAnnotations(field, column.non_null());
496497
if (field.isAnnotationPresent(ForeignKey.class))
497498
foreignKeys.add(field);
@@ -508,6 +509,7 @@ public String create(Object obj, SQLiteDatabase db) {
508509
" REFERENCES " + foreignKey.table().getSimpleName() + "(" + foreignKey.column() + ")";
509510
}
510511
sql += ");";
512+
foreignKeys.clear();
511513
db.execSQL(sql);
512514
return "Table create success";
513515
} else
@@ -528,16 +530,20 @@ public String create(Object obj, SQLiteDatabase db) {
528530
public class Insert {
529531
private Object obj;
530532
private ContentValues values;
533+
private SQLiteDatabase write;
534+
private SQLiteDatabase read;
535+
private String table;
531536

532537
public Insert(Object obj) {
533538
this.obj = obj;
539+
table = obj.getClass().getSimpleName();
534540
values = new ContentValues();
541+
write = helperBD.getWritableDatabase();
542+
read = helperBD.getReadableDatabase();
535543
}
536544

537545
public boolean execute() {
538-
539546
try {
540-
SQLiteDatabase write = helperBD.getReadableDatabase();
541547
Table persistable =
542548
obj.getClass().getAnnotation(Table.class);
543549
if (persistable != null) {
@@ -551,21 +557,24 @@ public boolean execute() {
551557
if (column != null) {
552558
if (!field.isAnnotationPresent(AutoIncrement.class) && field.get(obj) != null)
553559
checkObject(field, obj);
554-
} else
560+
if (field.get(obj) == null && column.non_null())
561+
throw new SQLException("This " + field.getName() + " is not null but is empty");
562+
} else
555563
throw new SQLException("The " + field.getName() + "attribute did not have the column annotation");
556564
}
557-
long result = write.insert(obj.getClass().getSimpleName(), null, values);
565+
long result = write.insert(table, null, values);
558566
return result > -1;
559567
} else
560568
throw new SQLException("This class does not have the table annotation");
561569
} catch (SQLException e) {
570+
Log.e("Insert", e.getMessage());
562571
e.printStackTrace();
563572
return false;
564573
} catch (Exception e) {
574+
Log.e("Insert", e.getMessage());
565575
e.printStackTrace();
566576
return false;
567577
}
568-
569578
}
570579

571580
private void checkObject(Field field, Object obj) {
@@ -586,6 +595,16 @@ else if (field.get(obj).getClass() == Short.class)
586595
e.printStackTrace();
587596
}
588597
}
598+
599+
/* private void getCount(String f) {
600+
String sql = "SELECT IFNULL(MAX(" + f + "),0) + 1 AS id FROM " + table + ";";
601+
Cursor cursor = read.rawQuery(sql, null);
602+
if (cursor.moveToFirst())
603+
values.put(f, cursor.getInt(cursor.getColumnIndex("id")));
604+
else
605+
values.put(f, 1);
606+
}*/
607+
589608
}
590609

591610

@@ -597,7 +616,8 @@ private String checkAnnotations(Field c, boolean not_null) {
597616
annotations += " AUTOINCREMENT";
598617
if (not_null && !c.isAnnotationPresent(Key.class))
599618
annotations += " NOT NULL";
600-
619+
if (c.isAnnotationPresent(Unique.class))
620+
annotations += " UNIQUE";
601621
return annotations;
602622
}
603623

0 commit comments

Comments
 (0)