11package org .javawebstack .orm ;
22
3+ import org .javawebstack .orm .exception .ORMQueryException ;
34import org .javawebstack .orm .mapper .TypeMapper ;
45
56import java .lang .reflect .InvocationTargetException ;
@@ -112,10 +113,14 @@ public List<T> all(){
112113 sb .append (limit );
113114 }
114115 sb .append (";" );
115- ResultSet rs = repository .getConnection ().read (sb .toString (), params .toArray ());
116- List <T > results = parseResults (rs );
117- repository .getConnection ().close (rs );
118- return results ;
116+ try {
117+ ResultSet rs = repository .getConnection ().read (sb .toString (), params .toArray ());
118+ List <T > results = parseResults (rs );
119+ repository .getConnection ().close (rs );
120+ return results ;
121+ } catch (SQLException throwables ) {
122+ throw new ORMQueryException (throwables );
123+ }
119124 }
120125
121126 public int count (){
@@ -127,16 +132,20 @@ public int count(){
127132 sb .append (where .query );
128133 params .addAll (where .params );
129134 sb .append (";" );
130- ResultSet rs = repository .getConnection ().read (sb .toString (), params .toArray ());
131- int count = -1 ;
132135 try {
133- rs .next ();
134- count = rs .getInt (1 );
136+ ResultSet rs = repository .getConnection ().read (sb .toString (), params .toArray ());
137+ int count = -1 ;
138+ try {
139+ rs .next ();
140+ count = rs .getInt (1 );
141+ } catch (SQLException throwables ) {
142+ throwables .printStackTrace ();
143+ }
144+ repository .getConnection ().close (rs );
145+ return count ;
135146 } catch (SQLException throwables ) {
136- throwables . printStackTrace ( );
147+ throw new ORMQueryException ( throwables );
137148 }
138- repository .getConnection ().close (rs );
139- return count ;
140149 }
141150
142151 public T refresh (T entry ){
@@ -149,10 +158,15 @@ public T refresh(T entry){
149158 sb .append (where .query );
150159 params .addAll (where .params );
151160 sb .append (";" );
152- ResultSet rs = repository .getConnection ().read (sb .toString (), params .toArray ());
153- parseResult (rs , entry );
154- repository .getConnection ().close (rs );
155- return entry ;
161+ try {
162+ ResultSet rs = repository .getConnection ().read (sb .toString (), params .toArray ());
163+ parseResult (rs , entry );
164+ repository .getConnection ().close (rs );
165+ return entry ;
166+ } catch (SQLException throwables ) {
167+ throw new ORMQueryException (throwables );
168+ }
169+
156170 }
157171
158172 public void finalDelete (){
@@ -165,7 +179,11 @@ public void finalDelete(){
165179 sb .append (where .query );
166180 params .addAll (where .params );
167181 sb .append (";" );
168- repository .getConnection ().write (sb .toString (), params .toArray ());
182+ try {
183+ repository .getConnection ().write (sb .toString (), params .toArray ());
184+ } catch (SQLException throwables ) {
185+ throw new ORMQueryException (throwables );
186+ }
169187 }
170188
171189 public Timestamp delete (){
@@ -179,7 +197,11 @@ public Timestamp delete(){
179197 params .add (deletedAt );
180198 QueryPart part = makeWhere ();
181199 params .addAll (part .params );
182- repository .getConnection ().write ("UPDATE `" +info .getTableName ()+"` SET `" +info .getColumnName (info .getSoftDeleteField ())+"`=?" +part .query +";" , params .toArray ());
200+ try {
201+ repository .getConnection ().write ("UPDATE `" +info .getTableName ()+"` SET `" +info .getColumnName (info .getSoftDeleteField ())+"`=?" +part .query +";" , params .toArray ());
202+ } catch (SQLException throwables ) {
203+ throw new ORMQueryException (throwables );
204+ }
183205 return deletedAt ;
184206 }
185207
@@ -191,7 +213,11 @@ public void restore(){
191213 params .add (null );
192214 QueryPart part = makeWhere ();
193215 params .addAll (part .params );
194- repository .getConnection ().write ("UPDATE `" +info .getTableName ()+"` SET `" +info .getColumnName (info .getSoftDeleteField ())+"`=?" +part .query +";" , params .toArray ());
216+ try {
217+ repository .getConnection ().write ("UPDATE `" +info .getTableName ()+"` SET `" +info .getColumnName (info .getSoftDeleteField ())+"`=?" +part .query +";" , params .toArray ());
218+ } catch (SQLException throwables ) {
219+ throw new ORMQueryException (throwables );
220+ }
195221 }
196222
197223 public void update (T entry ){
@@ -219,7 +245,11 @@ public void update(T entry){
219245 sb .append (where .query );
220246 params .addAll (where .params );
221247 sb .append (";" );
222- repository .getConnection ().write (sb .toString (), params .toArray ());
248+ try {
249+ repository .getConnection ().write (sb .toString (), params .toArray ());
250+ } catch (SQLException throwables ) {
251+ throw new ORMQueryException (throwables );
252+ }
223253 }
224254
225255 public void create (T entry ){
@@ -246,10 +276,14 @@ public void create(T entry){
246276 sb .append (") VALUES (" );
247277 sb .append (String .join ("," , values ));
248278 sb .append (");" );
249- int id = repository .getConnection ().write (sb .toString (), params .toArray ());
250- if (info .isAutoIncrement ())
251- setValue (info .getIdField (), entry , id );
252- entry .setEntryExists (true );
279+ try {
280+ int id = repository .getConnection ().write (sb .toString (), params .toArray ());
281+ if (info .isAutoIncrement ())
282+ setValue (info .getIdField (), entry , id );
283+ entry .setEntryExists (true );
284+ } catch (SQLException throwables ) {
285+ throw new ORMQueryException (throwables );
286+ }
253287 }
254288
255289 private QueryPart makeWhere (){
@@ -343,7 +377,7 @@ private void setValue(String fieldName, T entry, Object value) {
343377 }
344378 }
345379
346- private class Condition {
380+ private static class Condition {
347381 String fieldName ;
348382 String operator ;
349383 Object value ;
@@ -354,7 +388,7 @@ public Condition(String fieldName, String operator, Object value){
354388 }
355389 }
356390
357- private class QueryPart {
391+ private static class QueryPart {
358392 String query ;
359393 List <Object > params ;
360394 public QueryPart (String query , List <Object > params ) {
0 commit comments