-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuerySample.java
185 lines (180 loc) · 6.51 KB
/
QuerySample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package Samples.Query;
import java.sql.Connection;
import java.util.List;
import ORM.DbConnection.DAO.QueryDAO;
import ORM.Utils.Formats.ParamValue;
import ORM.Utils.Formats.UsableMethods;
import Samples.Models.Foreign.UsersODM;
import Samples.Models.Primary.TestODM;
public class QuerySample {
private Connection cursor;
public QuerySample(Connection cursor) {
this.cursor = cursor;
}
public void prepareCountSample() {
UsersODM m = new UsersODM();
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "users", m);
String[]
c = {"rol"},
v = {"admin"};
ParamValue condition = new ParamValue(c, v, "or");
List<String> data = dao.prepareCount(condition, "description, rol");
for(String d: data) {
System.out.println(d);
}
}
public void selectWithLimit() {
UsersODM m = new UsersODM();
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "users", m);
String[]
c = {""},
v = {""};
int
limit = 2,
offset = 0;
ParamValue condition = new ParamValue(c, v, "and", limit, offset);
List<UsersODM> data = dao.preparedSelect(condition);
for(UsersODM u: data) {
System.out.println(u);
}
}
public void selectInSample() {
UsersODM m = new UsersODM();
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "users", m);
String[]
c = {"rol"},
v = {"test", "user", "admin"};
ParamValue condition = new ParamValue(c, v, "or");
List<String> data = dao.selectIn(condition, "description, rol");
for(String d: data) {
System.out.println(d);
}
}
public void selectPatternSample() {
UsersODM m = new UsersODM();
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "users", m);
String[]
c = {"rol"},
v = {"test", "user", "admin"};
ParamValue condition = new ParamValue(c, v, "or");
List<String> data = dao.selectPattern(condition, "description, rol");
for(String d: data) {
System.out.println(d);
}
}
public void preparedSelectMinMaxSample() {
UsersODM m = new UsersODM();
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "test", m);
String[]
c = {"min", "max"},
v = {"rol", "description"};
ParamValue params = new ParamValue(c, v, "");
String[]
c1 = {"rol"},
v1 = {"admin"};
ParamValue condition = new ParamValue(c1, v1, "and");
List<String> data = dao.preparedSelectMinMax(params, condition);
for(String d: data) {
System.out.println(d);
}
}
public void preparedSelectSample() {
UsersODM m = new UsersODM();
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "users", m);
String[]
c = {"rol"},
v = {"testing"};
ParamValue condition = new ParamValue(c, v, "and");
List<UsersODM> models = dao.preparedSelect(condition);
if(models.size() > 0) {
for(UsersODM model: models) {
System.out.println(model.getInstanceData());
}
}
}
public void preparedFindSample() {
TestODM m = new TestODM();
QueryDAO<TestODM> dao = new QueryDAO<>(cursor, "test", m);
String columns = "";
ParamValue condition = null;
List<String> data = dao.preparedFind(condition, columns);
if(data.size() > 0) {
for(String d: data) {
System.out.println(d);
}
}
}
public void preparedInsertSample() {
UsersODM m = new UsersODM("my description of this user", "testing");
QueryDAO<UsersODM> dao = new QueryDAO<>(cursor, "users", m);
boolean isAdded = dao.preparedInsert(m);
if(isAdded) {
System.out.println("[ INFO ]: data has been inserted");
}
}
public void insertSample() {
TestODM m = new TestODM("model name", "model email", 2, "model rol");
QueryDAO<TestODM> dao = new QueryDAO<>(cursor, "test", m);
boolean isAdded = dao.insert(m);
if(isAdded) {
System.out.println("[ INFO ]: data has been inserted");
}
}
/**
* remember that the foreign model declare the pk of the relation and the model that declares the fk is the
* primary model.
* {@link TestODM} declares the fk.
* {@link UsersODM} declares the pk that is the fk of the previous class.
*/
public void innerJoinSample() {
// set model with the relation
TestODM m = new TestODM("model name", "model email", 1, "model rol");
// set foreign model
UsersODM foreign = new UsersODM("foreign model description", "foreign model");
UsableMethods[] foreignModels = new UsableMethods[1];
foreignModels[0] = foreign;
// set foreign tables
String[] foreignTables = {"users"};
// set conditions
ParamValue[] conditions = new ParamValue[1];
String[]
c = {"users_id_fk"},
v = {"id_pk"};
ParamValue condition = new ParamValue(c, v, "and");
conditions[0] = condition;
QueryDAO<TestODM> dao = new QueryDAO<>(cursor, "test", foreign);
List<String> data = dao.innerJoin(m, foreignModels, foreignTables, conditions);
if(data.size() > 0) {
for(String d: data) {
System.out.println(d);
}
}
}
public void preparedUpdateSample() {
TestODM m = new TestODM("model name", "model email", 1, "model rol");
String[]
c = {"name", "rol"},
v = {"model name", "model rol"};
ParamValue condition = new ParamValue(c, v, "and");
QueryDAO<TestODM> dao = new QueryDAO<>(cursor, "test", m);
boolean isUpdated = dao.preparedUpdate(m, condition);
if(isUpdated) {
System.out.println("[ INFO ]: record has been updated");
}
}
public void preparedDeleteSample() {
String[]
c = {"name", "email"},
v = {"model name", "model email"};
ParamValue condition = new ParamValue(c, v, "and");
/**
* just for building
*/
TestODM m = new TestODM();
QueryDAO<TestODM> dao = new QueryDAO<>(cursor, "test", m);
boolean isDeleted = dao.preparedDelete(condition);
if(isDeleted) {
System.out.println("[ INFO ]: record has been deleted");
}
}
}