empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From der...@apache.org
Subject empire-db git commit: EMPIREDB-247
Date Tue, 09 Aug 2016 05:55:22 GMT
Repository: empire-db
Updated Branches:
  refs/heads/EMPIREDB-247 [created] e89108aa2


EMPIREDB-247

Project: http://git-wip-us.apache.org/repos/asf/empire-db/repo
Commit: http://git-wip-us.apache.org/repos/asf/empire-db/commit/e89108aa
Tree: http://git-wip-us.apache.org/repos/asf/empire-db/tree/e89108aa
Diff: http://git-wip-us.apache.org/repos/asf/empire-db/diff/e89108aa

Branch: refs/heads/EMPIREDB-247
Commit: e89108aa2e31e88eced42d1c56cdf283d3751c08
Parents: e769f0f
Author: Jan Glaubitz <jan@glaubitz.org>
Authored: Tue Aug 9 07:55:00 2016 +0200
Committer: Jan Glaubitz <jan@glaubitz.org>
Committed: Tue Aug 9 07:55:00 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/empire/db/DBCommand.java    | 14 ++++++++++
 .../java/org/apache/empire/db/DBDatabase.java   | 28 ++++++++++++++++++++
 .../org/apache/empire/db/DBDriverFeature.java   |  3 ++-
 .../empire/db/mysql/DBDatabaseDriverMySQL.java  | 12 +++++++++
 4 files changed, 56 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/empire-db/blob/e89108aa/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
index f3eff01..6941cc8 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
@@ -42,6 +42,7 @@ import org.apache.empire.db.expr.set.DBSetExpr;
 import org.apache.empire.exceptions.InternalException;
 import org.apache.empire.exceptions.MiscellaneousErrorException;
 import org.apache.empire.exceptions.ObjectNotValidException;
+import org.apache.empire.exceptions.NotSupportedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -1131,6 +1132,19 @@ public abstract class DBCommand extends DBCommandExpr
     }
     
     /**
+     * Creates the Insert or Update ("UPSERT") SQL-Command.
+     * 
+     * This depends on your Database (driver) and might not be supported.
+     * Check DBDatabaseDriver.isSupported(PERFORM_UPSERT)
+     * 
+     * @return the SQL-Command
+     */
+    public synchronized String getInsertOrUpdate()
+    {
+    	throw new NotSupportedException(this, "getInsertOrUpdate");
+    }
+    
+    /**
      * Creates the delete SQL-Command.
      * 
      * @param table the table object 

http://git-wip-us.apache.org/repos/asf/empire-db/blob/e89108aa/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
index bd4a754..5d84c30 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
@@ -1387,6 +1387,34 @@ public abstract class DBDatabase extends DBObject
     }
 
     /**
+     * Executes an Insert or Update Statement from a command object
+     * 
+     * This depends on your Database (driver) and might not be supported.
+     * Check DBDatabaseDriver.isSupported(PERFORM_UPSERT)
+     * 
+     * @param cmd the command object containing the update command
+     * @param conn a valid connection to the database.
+     * @return the number of records that have been updated or inserted with the supplied
statement
+     */
+    public final int executeInsertOrUpdate(DBCommand cmd, Connection conn)
+    {
+    	if (getDriver().isSupported(DBDriverFeature.PERFORM_UPSERT))
+    	{
+    		return executeSQL(cmd.getInsertOrUpdate(), cmd.getParamValues(), conn); 
+    	}
+    	else
+    	{
+    		log.warn("{} is not supported by your Database(driver). Falling back to two statements");
+    		int count = executeUpdate(cmd, conn);
+    		if (count < 1) {
+    			// nothing updated -> INSERT
+    			count += executeInsert(cmd, conn);
+    		}
+    		return count;
+    	}
+    }
+    
+    /**
      * Executes a Delete statement from a command object
      * @param from the database table from which to delete records
      * @param cmd the command object containing the delete constraints

http://git-wip-us.apache.org/repos/asf/empire-db/blob/e89108aa/empire-db/src/main/java/org/apache/empire/db/DBDriverFeature.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDriverFeature.java b/empire-db/src/main/java/org/apache/empire/db/DBDriverFeature.java
index c0004e4..7cb7758 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBDriverFeature.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBDriverFeature.java
@@ -28,5 +28,6 @@ public enum DBDriverFeature {
     CREATE_SCHEMA,
     SEQUENCES,
     QUERY_LIMIT_ROWS,
-    QUERY_SKIP_ROWS
+    QUERY_SKIP_ROWS,
+    PERFORM_UPSERT
 }

http://git-wip-us.apache.org/repos/asf/empire-db/blob/e89108aa/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
b/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
index b758b74..2429660 100644
--- a/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
+++ b/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
@@ -121,6 +121,17 @@ public class DBDatabaseDriverMySQL extends DBDatabaseDriver
             addWhere(buf);
             return buf.toString();
         }
+        
+        @Override
+        public synchronized String getInsertOrUpdate()
+        {
+        	StringBuilder buf = new StringBuilder(getInsert());
+        	buf.append(" ON DUPLICATE KEY UPDATE ");
+            long context = CTX_NAME | CTX_VALUE;
+            addListExpr(buf, set, context, ", ");
+        	return buf.toString();
+        }
+        
     }
     
     // Properties
@@ -571,6 +582,7 @@ public class DBDatabaseDriverMySQL extends DBDatabaseDriver
             case SEQUENCES:         return useSequenceTable;
             case QUERY_LIMIT_ROWS:  return true;
             case QUERY_SKIP_ROWS:   return true;
+            case PERFORM_UPSERT:    return true;
             default:
                 // All other features are not supported by default
                 return false;


Mime
View raw message