Implemented the localTypes mode for date/time/datetime fields.
authorMarco Zanon <info@marcozanon.com>
Tue, 22 Aug 2017 22:09:19 +0000 (22:09 +0000)
committerMarco Zanon <info@marcozanon.com>
Tue, 22 Aug 2017 22:09:19 +0000 (22:09 +0000)
6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java
6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionGenerator.java
6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnectionPool.java
6.x/src/java/com/marcozanon/macaco/database/MSqlStatementResults.java

index 027ae7424f831bdb4ce71f4bfe690ffe79e3ae4c..01e2489c21e2eb3a43b5367cbdff07aa128f0a05 100644 (file)
@@ -28,6 +28,7 @@ public class MDatabaseConnection extends MObject {
     protected String url = null;
     protected String username = null;
     protected String password = null;
+    protected boolean localTypesMode = false;
     protected MLogListener logListener = null;
 
     protected Connection connection = null;
@@ -36,7 +37,7 @@ public class MDatabaseConnection extends MObject {
 
     /* */
 
-    public MDatabaseConnection(String driver, String url, String username, String password, MLogListener logListener) throws MDatabaseConnectionFailureException {
+    public MDatabaseConnection(String driver, String url, String username, String password, boolean localTypesMode, MLogListener logListener) throws MDatabaseConnectionFailureException {
         super();
         //
         if (MText.isBlank(driver)) {
@@ -56,6 +57,7 @@ public class MDatabaseConnection extends MObject {
         this.url = url;
         this.username = username;
         this.password = password;
+        this.localTypesMode = localTypesMode;
         this.setLogListener(logListener);
         // Load driver.
         try {
@@ -142,6 +144,12 @@ public class MDatabaseConnection extends MObject {
         return this.password;
     }
 
+    /* Local types mode. */
+
+    public boolean getLocalTypesMode() {
+        return this.localTypesMode;
+    }
+
     /* Connection. */
 
     protected Connection getConnection() {
@@ -246,7 +254,7 @@ public class MDatabaseConnection extends MObject {
             }
             // Execute the statement and parse the results.
             preparedStatement.execute();
-            results = new MSqlStatementResults(preparedStatement);
+            results = new MSqlStatementResults(preparedStatement, this.getLocalTypesMode());
             this.logStatement(preparedStatement.toString());
         }
         catch (SQLException exception) {
index 3e09502dd81f7fb33095b15d6065096668876e54..263ec6957236a5deaf67f82892048b5313bbf537 100644 (file)
@@ -16,11 +16,12 @@ public class MDatabaseConnectionGenerator extends MObject {
     protected String url = null;
     protected String username = null;
     protected String password = null;
+    protected boolean localTypesMode = false;
     protected MLogListener logListener = null;
 
     /* */
 
-    public MDatabaseConnectionGenerator(String driver, String url, String username, String password, MLogListener logListener) {
+    public MDatabaseConnectionGenerator(String driver, String url, String username, String password, boolean localTypesMode, MLogListener logListener) {
         super();
         //
         if (MText.isBlank(driver)) {
@@ -40,6 +41,7 @@ public class MDatabaseConnectionGenerator extends MObject {
         this.url = url;
         this.username = username;
         this.password = password;
+        this.localTypesMode = localTypesMode;
         this.setLogListener(logListener);
     }
 
@@ -67,6 +69,12 @@ public class MDatabaseConnectionGenerator extends MObject {
         return this.password;
     }
 
+    /* Local types mode. */
+
+    public boolean getLocalTypesMode() {
+        return this.localTypesMode;
+    }
+
     /* Log listener. */
 
     public MLogListener getLogListener() {
@@ -80,7 +88,7 @@ public class MDatabaseConnectionGenerator extends MObject {
     /* Generator. */
 
     public MDatabaseConnection getNewDatabaseConnection() throws MDatabaseConnectionFailureException {
-        return new MDatabaseConnection(this.getDriver(), this.getUrl(), this.getUsername(), this.getPassword(), this.getLogListener());
+        return new MDatabaseConnection(this.getDriver(), this.getUrl(), this.getUsername(), this.getPassword(), this.getLocalTypesMode(), this.getLogListener());
     }
 
     public boolean isGeneratorFor(MDatabaseConnection databaseConnection) {
@@ -100,6 +108,9 @@ public class MDatabaseConnectionGenerator extends MObject {
         if (!databaseConnection.getPassword().equals(this.getPassword())) {
             return false;
         }
+        if (databaseConnection.getLocalTypesMode() != this.getLocalTypesMode()) {
+            return false;
+        }
         if (databaseConnection.getLogListener() != this.getLogListener()) {
             return false;
         }
index bbb032343038bf72fbf5b7df2d1886979f038905..2ac0b3868eb01976bfaadf92d45f264ed59138fc 100644 (file)
@@ -20,7 +20,7 @@ public class MDatabaseConnectionPool extends MObject {
 
     /* */
 
-    public MDatabaseConnectionPool(String driver, String url, String username, String password, int minimumSize, int maximumSize, MLogListener logListener) throws MDatabaseConnectionFailureException {
+    public MDatabaseConnectionPool(String driver, String url, String username, String password, boolean localTypesMode, int minimumSize, int maximumSize, MLogListener logListener) throws MDatabaseConnectionFailureException {
         super();
         //
         if (0 > minimumSize) {
@@ -33,7 +33,7 @@ public class MDatabaseConnectionPool extends MObject {
             throw new IllegalArgumentException(String.format("Invalid 'maximumSize': %s < %s ('minimumSize').", maximumSize, minimumSize));
         }
         //
-        this.databaseConnectionGenerator = new MDatabaseConnectionGenerator(driver, url, username, password, logListener);
+        this.databaseConnectionGenerator = new MDatabaseConnectionGenerator(driver, url, username, password, localTypesMode, logListener);
         this.minimumSize = minimumSize;
         this.maximumSize = maximumSize;
         //
index 6b2eb49cae670d35d2fe1e9ba8a9fce30ed20cf8..e7c57fd97a730458eb741fb6451818a10a3ea763 100644 (file)
@@ -20,6 +20,8 @@ import java.util.LinkedList;
 public class MSqlStatementResults extends MObject {
 
     protected PreparedStatement preparedStatement = null;
+    protected boolean localTypesMode = false;
+
     protected ResultSet resultSet = null;
 
     protected LinkedList<LinkedHashMap<String, Object>> records = new LinkedList<LinkedHashMap<String, Object>>();
@@ -28,7 +30,7 @@ public class MSqlStatementResults extends MObject {
 
     /* */
 
-    public MSqlStatementResults(PreparedStatement preparedStatement) throws MSqlStatementException {
+    public MSqlStatementResults(PreparedStatement preparedStatement, boolean localTypesMode) throws MSqlStatementException {
         super();
         //
         if (null == preparedStatement) {
@@ -36,6 +38,7 @@ public class MSqlStatementResults extends MObject {
         }
         //
         this.preparedStatement = preparedStatement;
+        this.localTypesMode = localTypesMode;
         //
         try {
             this.resultSet = this.getPreparedStatement().getResultSet();
@@ -43,10 +46,24 @@ public class MSqlStatementResults extends MObject {
                 while (this.getResultSet().next()) {
                     ResultSetMetaData resultSetMetaData = this.getResultSet().getMetaData();
                     LinkedHashMap<String, Object> record = new LinkedHashMap<String, Object>();
+                    //
                     for (int f = 0; resultSetMetaData.getColumnCount() > f; f++) {
                         String field = resultSetMetaData.getColumnLabel(f + 1);
-                        record.put(field, this.getResultSet().getObject(field));
+                        Object value = this.getResultSet().getObject(field);
+                        //
+                        if ((value instanceof java.sql.Date) && (this.getLocalTypesMode())) {
+                            value = ((java.sql.Date)value).toLocalDate();
+                        }
+                        else if ((value instanceof java.sql.Time) && (this.getLocalTypesMode())) {
+                            value = ((java.sql.Time)value).toLocalTime();
+                        }
+                        else if ((value instanceof java.sql.Timestamp) && (this.getLocalTypesMode())) {
+                            value = ((java.sql.Timestamp)value).toLocalDateTime();
+                        }
+                        //
+                        record.put(field, value);
                     }
+                    //
                     this.getRecords().add(record);
                 }
             }
@@ -97,6 +114,10 @@ public class MSqlStatementResults extends MObject {
         return this.resultSet;
     }
 
+    public boolean getLocalTypesMode() {
+        return this.localTypesMode;
+    }
+
     /* Records. */
 
     public LinkedList<LinkedHashMap<String, Object>> getRecords() {