@@ -146,74 +146,6 @@ public <T> T createMapping(Class<T> mappingInterface) {
146146 });
147147 }
148148
149- /**
150- * @see SQLDatabaseConnection#save(String, Object)
151- */
152- @ Override
153- public QueryResult save (String table , Object obj ) { // by default, it creates and upsert request.
154- Pair <String [], UnknownValueWrapper []> defsValsPair = buildDefsVals (obj );
155- if (defsValsPair == null ) {
156- return new QueryResultImpl (false );
157- }
158-
159- String [] defs = defsValsPair .getFirst ();
160- UnknownValueWrapper [] vals = defsValsPair .getSecond ();
161-
162- UpsertQuery upsert = upsert ().into (table , defs );
163- for (UnknownValueWrapper wrapper : vals ) {
164- upsert .appendVal (wrapper .getObject ());
165- }
166-
167- SetStatement <InsertQuery > setStmt = upsert .onDuplicateKey ();
168- for (int i = 0 ; i < defs .length ; i ++) {
169- setStmt .and (defs [i ], vals [i ].getObject ());
170- }
171-
172- return setStmt .execute ();
173- }
174-
175- @ SuppressWarnings ("unchecked" )
176- @ Nullable
177- protected Pair <String [], UnknownValueWrapper []> buildDefsVals (Object obj ) {
178- Objects .requireNonNull (obj );
179-
180- Class <?> aClass = obj .getClass ();
181-
182- Map <String , Object > fields = new HashMap <>();
183- for (Field field : aClass .getDeclaredFields ()) {
184-
185- if (Modifier .isTransient (field .getModifiers ())) {
186- // Transient fields are ignored.
187- continue ;
188- }
189-
190- try {
191- field .setAccessible (true );
192- Object o = field .get (obj );
193- if (field .isAnnotationPresent (JsonField .class )) {
194- o = options .getGson ().toJson (o );
195- } else if (Validator .validateAutoIncrement (field ) && field .get (obj ) == null ) {
196- // If field is PrimaryKey and autoIncrement true and is null,
197- // We will skip this to use auto increment strategy on SQL server.
198- continue ;
199- }
200- fields .put (options .getNamingStrategy ().fieldNameToColumn (field .getName ()), o );
201- } catch (IllegalAccessException e ) {
202- e .printStackTrace ();
203- return null ;
204- }
205- }
206- // I make entry array for indexing safety.
207- Map .Entry <String , Object >[] entryArray = fields .entrySet ().toArray (new Map .Entry [0 ]);
208- String [] defs = new String [entryArray .length ];
209- UnknownValueWrapper [] vals = new UnknownValueWrapper [entryArray .length ];
210- for (int i = 0 ; i < entryArray .length ; i ++) {
211- defs [i ] = entryArray [i ].getKey ();
212- vals [i ] = new UnknownValueWrapper (entryArray [i ].getValue ());
213- }
214- return new Pair <>(defs , vals );
215- }
216-
217149 /**
218150 * @see SQLDatabaseConnection#query(Query, Class)
219151 */
@@ -280,6 +212,76 @@ public QueryResult exec(Query query) {
280212 }
281213 }
282214
215+ /**
216+ * @see SQLDatabaseConnection#save(String, Object)
217+ */
218+ @ Override
219+ public QueryResult save (String table , Object obj ) { // by default, it creates and upsert request.
220+ Pair <String [], UnknownValueWrapper []> data = buildDefsVals (obj );
221+
222+ if (data == null ) {
223+ return new QueryResultImpl (false );
224+ }
225+
226+ return save (obj ).table (table ).execute ();
227+ }
228+
229+ public QueryResult insert (String table , Object obj ) {
230+ Pair <String [], UnknownValueWrapper []> data = buildDefsVals (obj );
231+
232+ if (data == null )
233+ return new QueryResultImpl (false );
234+
235+ InsertQuery query = insert ().into (table , data .getFirst ());
236+ for (UnknownValueWrapper valueWrapper : data .getSecond ()) {
237+ query .appendVal (valueWrapper .getObject ());
238+ }
239+
240+ return query .execute ();
241+ }
242+
243+ @ SuppressWarnings ("unchecked" )
244+ @ Nullable
245+ protected Pair <String [], UnknownValueWrapper []> buildDefsVals (Object obj ) {
246+ Objects .requireNonNull (obj );
247+
248+ Class <?> aClass = obj .getClass ();
249+
250+ Map <String , Object > fields = new HashMap <>();
251+ for (Field field : aClass .getDeclaredFields ()) {
252+
253+ if (Modifier .isTransient (field .getModifiers ())) {
254+ // Transient fields are ignored.
255+ continue ;
256+ }
257+
258+ try {
259+ field .setAccessible (true );
260+ Object o = field .get (obj );
261+ if (field .isAnnotationPresent (JsonField .class )) {
262+ o = options .getGson ().toJson (o );
263+ } else if (Validator .validateAutoIncrement (field ) && field .get (obj ) == null ) {
264+ // If field is PrimaryKey and autoIncrement true and is null,
265+ // We will skip this to use auto increment strategy on SQL server.
266+ continue ;
267+ }
268+ fields .put (options .getNamingStrategy ().fieldNameToColumn (field .getName ()), o );
269+ } catch (IllegalAccessException e ) {
270+ e .printStackTrace ();
271+ return null ;
272+ }
273+ }
274+ // I make entry array for indexing safety.
275+ Map .Entry <String , Object >[] entryArray = fields .entrySet ().toArray (new Map .Entry [0 ]);
276+ String [] defs = new String [entryArray .length ];
277+ UnknownValueWrapper [] vals = new UnknownValueWrapper [entryArray .length ];
278+ for (int i = 0 ; i < entryArray .length ; i ++) {
279+ defs [i ] = entryArray [i ].getKey ();
280+ vals [i ] = new UnknownValueWrapper (entryArray [i ].getValue ());
281+ }
282+ return new Pair <>(defs , vals );
283+ }
284+
283285 private boolean handleAutoReconnect () {
284286 if (options .isAutoReconnect () && !isConnected ()) {
285287 debug ("Trying to make a new connection with the database!" );
@@ -323,6 +325,29 @@ public DeleteQuery delete() {
323325 return new DeleteQuery (this );
324326 }
325327
328+ public UpsertQuery save (Object obj ) {
329+ Pair <String [], UnknownValueWrapper []> data = buildDefsVals (obj );
330+
331+ if (data == null ) {
332+ return null ;
333+ }
334+
335+ String [] defs = data .getFirst ();
336+ UnknownValueWrapper [] vals = data .getSecond ();
337+
338+ UpsertQuery upsert = upsert ().into (null , defs );
339+ for (UnknownValueWrapper wrapper : vals ) {
340+ upsert .appendVal (wrapper .getObject ());
341+ }
342+
343+ SetStatement <InsertQuery > setStmt = upsert .onDuplicateKey ();
344+ for (int i = 0 ; i < defs .length ; i ++) {
345+ setStmt .and (defs [i ], vals [i ].getObject ());
346+ }
347+
348+ return (UpsertQuery ) setStmt .getAncestor ();
349+ }
350+
326351 public void debug (String message ) {
327352 if (options .isDebug ()) {
328353 System .out .println (message );
0 commit comments