Skip to content

Commit 8f5cba3

Browse files
author
paulo
committed
Otimizando codigo
1 parent 9c88315 commit 8f5cba3

File tree

7 files changed

+167
-137
lines changed

7 files changed

+167
-137
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ protected void onCreate(Bundle savedInstanceState) {
1717
SimpleSQL simpleSQL = new SimpleSQL(new HelperBD(this));
1818
Pessoa pessoa = new Pessoa();
1919
pessoa.setName("paulo");
20-
pessoa.setEmail("pauloiury1@gmail.com");
21-
pessoa.setPhone("1231789201");
20+
pessoa.setEmail("pauloury1@gmail.com");
21+
pessoa.setPhone("12317820");
2222
boolean result = simpleSQL.insert(pessoa).execute();
2323

24-
List<Pessoa> list = (List<Pessoa>) simpleSQL.selectTable(new Pessoa())
25-
.fields(new String[]{"*"})
24+
List<Pessoa> list = simpleSQL.selectTable(new Pessoa())
25+
.fields("*")
2626
.execute();
27+
28+
simpleSQL.updateTable(new Pessoa())
29+
.set("name","email")
30+
.values("Novo Nome","Novo Email")
31+
.where()
32+
.column("id")
33+
.equals()
34+
.fieldInt(1)
35+
.execute();
36+
37+
list.clear();
2738
}
2839
}

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 = 1;
2020
private SimpleSQL simpleSQL;
2121

2222
public HelperBD(@Nullable Context context) {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ public class Pessoa {
2525
@Column(type = "TEXT",non_null = true)
2626
private String phone;
2727

28-
@Column(type = "TEXT")
29-
@ForeignKey(table = Teste.class, column ="teste" )
30-
private String teste;
31-
3228
public int getId() {
3329
return id;
3430
}

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,34 @@ public static Object getString(String string) {
7878
*/
7979
public void create(Object obj, SQLiteDatabase db) throws SQLException {
8080
if (obj.getClass().getAnnotation(Table.class) != null) {
81-
String columns = "";
81+
StringBuilder columns = new StringBuilder();
82+
StringBuilder foreignKeys = new StringBuilder();
8283
String tabela = obj.getClass().getSimpleName();
8384
Field[] fields = obj.getClass().getDeclaredFields();
84-
String foreignKeys = "";
85-
int count = 0;
8685
for (Field field : fields) {
8786
field.setAccessible(true); // As the attributes are private, we set it to visible
8887
Column column =
8988
field.getAnnotation(Column.class);
9089
if (column != null) {
91-
if (count > 0)
92-
columns += " , ";
93-
columns += field.getName() + " " + column.type().toUpperCase();
94-
columns += checkAnnotations(field, column.non_null());
90+
if (field != fields[0])
91+
columns.append(" , ");
92+
columns.append(field.getName())
93+
.append(' ')
94+
.append(column.type().toUpperCase())
95+
.append(checkAnnotations(field, column.non_null()));
9596
if (field.isAnnotationPresent(ForeignKey.class)) {
9697
ForeignKey foreignKey = field.getAnnotation(ForeignKey.class);
97-
foreignKeys += " , FOREIGN KEY (" + field.getName() + ")" +
98-
" REFERENCES " + foreignKey.table().getSimpleName() + "(" + foreignKey.column() + ")";
98+
foreignKeys.append(" , FOREIGN KEY (")
99+
.append(field.getName())
100+
.append(")")
101+
.append(" REFERENCES ")
102+
.append(foreignKey.table().getSimpleName())
103+
.append("(")
104+
.append(foreignKey.column())
105+
.append(")");
99106
}
100107
} else
101108
throw new SQLException("The " + field.getName() + "attribute did not have the column annotation");
102-
count++;
103109
}
104110
db.execSQL("CREATE TABLE " + tabela + " ( " + columns + foreignKeys + ");");
105111
} else
@@ -113,16 +119,16 @@ public void create(Object obj, SQLiteDatabase db) throws SQLException {
113119
*/
114120

115121
private String checkAnnotations(Field field, boolean not_null) {
116-
String annotations = "";
122+
StringBuilder annotations = new StringBuilder();
117123
if (field.isAnnotationPresent(Key.class))
118-
annotations += " PRIMARY KEY";
124+
annotations.append(" PRIMARY KEY");
119125
if (field.isAnnotationPresent(AutoIncrement.class))
120-
annotations += " AUTOINCREMENT";
126+
annotations.append(" AUTOINCREMENT");
121127
if (not_null && !field.isAnnotationPresent(Key.class))
122-
annotations += " NOT NULL";
128+
annotations.append(" NOT NULL");
123129
if (field.isAnnotationPresent(Unique.class))
124-
annotations += " UNIQUE";
125-
return annotations;
130+
annotations.append(" UNIQUE");
131+
return annotations.toString();
126132
}
127133

128134

simplesql/src/main/java/com/simplesql/crud/DeleteColumn.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,82 @@
99
* Method DELETE COLUMN
1010
*/
1111
public class DeleteColumn {
12-
private String table, SQLString;
12+
private StringBuilder SQLString = new StringBuilder();
1313

1414
public DeleteColumn(Object objectType) {
15-
this.table = objectType.getClass().getSimpleName();
16-
this.SQLString = "DELETE FROM " + table;
15+
this.SQLString.append("DELETE FROM ")
16+
.append(objectType.getClass().getSimpleName()); //Name table
1717
}
1818

1919
public DeleteColumn equals() {
20-
SQLString += " = ";
20+
SQLString.append(" = ");
2121
return this;
2222
}
2323

2424
public DeleteColumn where() {
25-
SQLString += " WHERE ";
25+
SQLString.append(" WHERE ");
2626
return this;
2727
}
2828

2929
public DeleteColumn and() {
30-
SQLString += " AND ";
30+
SQLString.append(" AND ");
3131
return this;
3232
}
3333

3434
public DeleteColumn or() {
35-
SQLString += " OR ";
35+
SQLString.append(" OR ");
3636
return this;
3737
}
3838

3939
public DeleteColumn like(String s) {
40-
SQLString += "LIKE " + "\"s\"";
40+
SQLString.append("LIKE ")
41+
.append("\"")
42+
.append(s)
43+
.append("\"");
4144
return this;
4245
}
4346

4447
public DeleteColumn fieldString(String value) {
45-
SQLString += "\"" + value + "\"";
48+
SQLString.append("\"")
49+
.append(value)
50+
.append("\"");
4651
return this;
4752
}
4853

4954
public DeleteColumn fieldInt(int value) {
50-
SQLString += value;
55+
SQLString.append(value);
5156
return this;
5257
}
5358

5459
public DeleteColumn fieldLong(long value) {
55-
SQLString += value;
60+
SQLString.append(value);
5661
return this;
5762
}
5863

5964
public DeleteColumn fieldFloat(float value) {
60-
SQLString += value;
65+
SQLString.append(value);
6166
return this;
6267
}
6368

6469
public DeleteColumn fieldBoolean(boolean value) {
65-
SQLString += value;
70+
SQLString.append(value);
6671
return this;
6772
}
6873

6974
public DeleteColumn column(String field) {
70-
SQLString += field;
75+
SQLString.append(field);
7176
return this;
7277
}
7378

7479
public DeleteColumn writeSQL(String sql) {
75-
SQLString += sql;
80+
SQLString.append(sql);
7681
return this;
7782
}
7883

7984
public boolean execute() {
8085
SQLiteDatabase write = helperBD.getWritableDatabase();
8186
try {
82-
write.execSQL(SQLString);
87+
write.execSQL(SQLString.toString());
8388
return true;
8489
} catch (Exception e) {
8590
return false;

0 commit comments

Comments
 (0)