88import me .zort .sqllib .api .ISQLDatabaseOptions ;
99import me .zort .sqllib .api .ObjectMapper ;
1010import me .zort .sqllib .api .data .Row ;
11+ import me .zort .sqllib .api .options .NamingStrategy ;
1112import me .zort .sqllib .internal .annotation .JsonField ;
1213import me .zort .sqllib .util .Validator ;
1314import org .jetbrains .annotations .NotNull ;
@@ -29,12 +30,21 @@ public class DefaultObjectMapper implements ObjectMapper {
2930 @ Getter (AccessLevel .PROTECTED )
3031 private final List <ObjectMapper .FieldValueResolver > backupValueResolvers ;
3132 private final Map <Class <?>, ObjectMapper .TypeAdapter <?>> typeAdapters ;
32- private final SQLDatabaseConnectionImpl connectionWrapper ;
33+
34+ private final Gson gson ;
35+ private final NamingStrategy ns ;
36+ private SQLDatabaseConnectionImpl connectionWrapper = null ;
3337
3438 public DefaultObjectMapper (SQLDatabaseConnectionImpl connectionWrapper ) {
39+ this (connectionWrapper .getOptions ().getGson (), connectionWrapper .getOptions ().getNamingStrategy ());
40+ this .connectionWrapper = connectionWrapper ;
41+ }
42+
43+ public DefaultObjectMapper (Gson gson , NamingStrategy namingStrategy ) {
3544 this .backupValueResolvers = new CopyOnWriteArrayList <>();
3645 this .typeAdapters = new ConcurrentHashMap <>();
37- this .connectionWrapper = connectionWrapper ;
46+ this .gson = gson ;
47+ this .ns = namingStrategy ;
3848 }
3949
4050 @ Override
@@ -115,13 +125,14 @@ private Object buildElementValue(AnnotatedElement element, Row row) {
115125 Object obj = row .get (name );
116126 if (obj == null ) {
117127 String converted ;
118- if ((obj = row .get (converted = connectionWrapper .getOptions ().getNamingStrategy ().fieldNameToColumn (name ))) == null ) {
119-
120- // Now backup resolvers come.
121- for (ObjectMapper .FieldValueResolver resolver : backupValueResolvers ) {
122- Object backupValue = resolver .obtainValue (connectionWrapper , element , row , name , converted , type );
123- if (backupValue != null ) {
124- return backupValue ;
128+ if ((obj = row .get (converted = ns .fieldNameToColumn (name ))) == null ) {
129+ if (connectionWrapper != null ) { // Only if this class is used with connection, use resolvers
130+ // Now backup resolvers come.
131+ for (ObjectMapper .FieldValueResolver resolver : backupValueResolvers ) {
132+ Object backupValue = resolver .obtainValue (connectionWrapper , element , row , name , converted , type );
133+ if (backupValue != null ) {
134+ return backupValue ;
135+ }
125136 }
126137 }
127138
@@ -136,7 +147,6 @@ private Object buildElementValue(AnnotatedElement element, Row row) {
136147 }
137148 if (element .isAnnotationPresent (JsonField .class ) && obj instanceof String ) {
138149 String jsonString = (String ) obj ;
139- Gson gson = connectionWrapper .getOptions ().getGson ();
140150 return gson .fromJson (jsonString , type );
141151 } else {
142152 return obj ;
@@ -157,21 +167,19 @@ public DefsVals serializeValues(Object obj) {
157167 continue ;
158168 }
159169
160- ISQLDatabaseOptions options = connectionWrapper .getOptions ();
161-
162170 try {
163171 field .setAccessible (true );
164172 Object o = field .get (obj );
165173 if (typeAdapters .containsKey (field .getType ())) {
166174 o = typeAdapters .get (field .getType ()).serialize (field , o );
167175 } else if (field .isAnnotationPresent (JsonField .class )) {
168- o = options . getGson () .toJson (o );
176+ o = gson .toJson (o );
169177 } else if (Validator .validateAutoIncrement (field ) && field .get (obj ) == null ) {
170178 // If field is PrimaryKey and autoIncrement true and is null,
171179 // We will skip this to use auto increment strategy on SQL server.
172180 continue ;
173181 }
174- fields .put (options . getNamingStrategy () .fieldNameToColumn (field .getName ()), o );
182+ fields .put (ns .fieldNameToColumn (field .getName ()), o );
175183 } catch (IllegalAccessException e ) {
176184 e .printStackTrace ();
177185 return null ;
@@ -189,7 +197,9 @@ public DefsVals serializeValues(Object obj) {
189197 }
190198
191199 private void debug (String message ) {
192- connectionWrapper .debug (message );
200+ if (connectionWrapper != null ) {
201+ connectionWrapper .debug (message );
202+ }
193203 }
194204
195205}
0 commit comments