11package me .zort .sqllib ;
22
3+ import lombok .AllArgsConstructor ;
34import lombok .Getter ;
45import me .zort .sqllib .api .Query ;
56import me .zort .sqllib .api .SQLConnection ;
89import me .zort .sqllib .api .data .Row ;
910import me .zort .sqllib .api .mapping .StatementMappingOptions ;
1011import me .zort .sqllib .internal .factory .SQLConnectionFactory ;
12+ import me .zort .sqllib .internal .impl .QueryResultImpl ;
1113import me .zort .sqllib .internal .query .*;
14+ import me .zort .sqllib .internal .query .part .SetStatement ;
1215import org .jetbrains .annotations .NotNull ;
1316import org .jetbrains .annotations .Nullable ;
1417
@@ -73,18 +76,6 @@ public SQLDatabaseConnection(final @NotNull SQLConnectionFactory connectionFacto
7376 public abstract <T > T createGate (Class <T > mappingInterface , @ NotNull StatementMappingOptions options );
7477 public abstract boolean buildEntitySchema (String tableName , Class <?> entityClass );
7578
76- /**
77- * Saves this mapping object into database using upsert query.
78- * <p>
79- * All mapping strategies are described in:
80- * {@link SQLDatabaseConnection#query(Query, Class)}.
81- *
82- * @param table Table to save into.
83- * @param obj The object to save.
84- * @return Result of the query.
85- */
86- public abstract QueryResult save (String table , Object obj );
87-
8879 /**
8980 * Performs new query and returns the result. This result is never null.
9081 * See: {@link QueryRowsResult#isSuccessful()}
@@ -121,9 +112,59 @@ public SQLDatabaseConnection(final @NotNull SQLConnectionFactory connectionFacto
121112 */
122113 public abstract QueryResult exec (Query query );
123114 public abstract QueryResult exec (String query );
115+ protected abstract DefsVals buildDefsVals (Object obj );
124116 public abstract boolean isLogSqlErrors ();
125117 public abstract boolean isDebug ();
126118
119+ /**
120+ * Saves this mapping object into database using upsert query.
121+ * <p>
122+ * All mapping strategies are described in:
123+ * {@link SQLDatabaseConnection#query(Query, Class)}.
124+ *
125+ * @param table Table to save into.
126+ * @param obj The object to save.
127+ * @return Result of the query.
128+ */
129+ // by default, it creates and upsert request.
130+ public QueryResult save (final @ NotNull String table , final @ NotNull Object obj ) {
131+ DefsVals defsVals = buildDefsVals (obj );
132+
133+ if (defsVals == null ) return new QueryResultImpl (false );
134+
135+ return save (obj ).table (table ).execute ();
136+ }
137+
138+ public UpsertQuery save (final @ NotNull Object obj ) {
139+ DefsVals defsVals = buildDefsVals (obj );
140+ if (defsVals == null ) return null ;
141+
142+ String [] defs = defsVals .getDefs ();
143+ SQLDatabaseConnectionImpl .UnknownValueWrapper [] vals = defsVals .getVals ();
144+ UpsertQuery upsert = upsert ().into (null , defs );
145+ for (SQLDatabaseConnectionImpl .UnknownValueWrapper wrapper : vals ) {
146+ upsert .appendVal (wrapper .getObject ());
147+ }
148+ SetStatement <InsertQuery > setStmt = upsert .onDuplicateKey ();
149+ for (int i = 0 ; i < defs .length ; i ++) {
150+ setStmt .and (defs [i ], vals [i ].getObject ());
151+ }
152+
153+ return (UpsertQuery ) setStmt .getAncestor ();
154+ }
155+
156+ public QueryResult insert (final @ NotNull String table , final @ NotNull Object obj ) {
157+ DefsVals defsVals = buildDefsVals (obj );
158+ if (defsVals == null ) return new QueryResultImpl (false );
159+
160+ InsertQuery query = insert ().into (table , defsVals .getDefs ());
161+ for (SQLDatabaseConnectionImpl .UnknownValueWrapper valueWrapper : defsVals .getVals ()) {
162+ query .appendVal (valueWrapper .getObject ());
163+ }
164+
165+ return query .execute ();
166+ }
167+
127168 // --***-- Query builders --***--
128169
129170 public SelectQuery select (String ... cols ) {
@@ -161,7 +202,6 @@ public DeleteQuery delete() {
161202 @ Override
162203 public final boolean connect () {
163204 if (isConnected ()) disconnect ();
164-
165205 try {
166206 connection = connectionFactory .connect ();
167207 lastError = null ;
@@ -176,7 +216,6 @@ public final boolean connect() {
176216 @ Override
177217 public final void disconnect () {
178218 if (!isConnected ()) return ;
179-
180219 try {
181220 connection .close ();
182221 lastError = null ;
@@ -195,4 +234,10 @@ protected void logSqlError(Exception e) {
195234 if (isLogSqlErrors ()) e .printStackTrace ();
196235 }
197236
237+ @ AllArgsConstructor
238+ @ Getter
239+ protected static class DefsVals {
240+ private final String [] defs ;
241+ private final SQLDatabaseConnectionImpl .UnknownValueWrapper [] vals ;
242+ }
198243}
0 commit comments