You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2022/01/23 22:00:13 UTC

[empire-db] branch version3 updated: EMPIREDB-362 bugfix DBRowSet.initRecordDefaultValues

This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch version3
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/version3 by this push:
     new f3233f3  EMPIREDB-362 bugfix DBRowSet.initRecordDefaultValues
f3233f3 is described below

commit f3233f398cf1241fc880eef1ae6f0c6188b44b5f
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Sun Jan 23 23:00:12 2022 +0100

    EMPIREDB-362 bugfix DBRowSet.initRecordDefaultValues
---
 empire-db-maven-plugin/pom.xml                     |  51 +++---
 .../org/apache/empire/db/maven/CodeGenMojo.java    |   3 +-
 .../main/java/org/apache/empire/db/DBIndex.java    |  10 ++
 .../main/java/org/apache/empire/db/DBRowSet.java   |  38 ++++
 .../java/org/apache/empire/db/DBTableColumn.java   |   5 +-
 .../java/org/apache/empire/db/SerializeTest.java   | 199 +++++++++++++++++++++
 6 files changed, 277 insertions(+), 29 deletions(-)

diff --git a/empire-db-maven-plugin/pom.xml b/empire-db-maven-plugin/pom.xml
index 8376867..ff19b2a 100644
--- a/empire-db-maven-plugin/pom.xml
+++ b/empire-db-maven-plugin/pom.xml
@@ -22,9 +22,12 @@
 		<groupId>org.apache.empire-db</groupId>
 		<version>3.0.0-SNAPSHOT</version>
 	</parent>
+	
 	<artifactId>empire-db-maven-plugin</artifactId>
 	<packaging>maven-plugin</packaging>
+
 	<name>Apache Empire-db Maven Plugin</name>
+	
 	<dependencies>
 		<dependency>
 			<groupId>org.apache.empire-db</groupId>
@@ -55,7 +58,7 @@
         <dependency>
             <groupId>org.apache.maven.plugin-tools</groupId>
             <artifactId>maven-plugin-annotations</artifactId>
-            <version>3.5.2</version>
+            <version>3.6.2</version>
             <scope>provided</scope><!-- annotations are needed only to build the plugin -->
         </dependency>
 		<dependency>
@@ -76,28 +79,28 @@
 	</dependencies>
 
     <build>
-        <pluginManagement>
-            <plugins>
-                <plugin>
-                    <groupId>org.apache.maven.plugins</groupId>
-                    <artifactId>maven-plugin-plugin</artifactId>
-                    <version>3.5.2</version>
-                    <configuration>
-                        <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
-                    </configuration>
-                    <!-- not necessary for version 3.3 
-                    <executions>
-                        <execution>
-                            <id>mojo-descriptor</id>
-                            <phase>process-classes</phase>
-                            <goals>
-                                <goal>descriptor</goal>
-                            </goals>
-                        </execution>
-                    </executions>
-                    -->
-                </plugin>
-            </plugins>
-        </pluginManagement>
+		<!-- maven-plugin-plugin 
+		see	https://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html
+		-->
+		<plugins>
+		    <plugin>
+		        <groupId>org.apache.maven.plugins</groupId>
+		        <artifactId>maven-plugin-plugin</artifactId>
+		        <version>3.6.2</version>
+		        <configuration>
+		            <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+		        </configuration>
+				<!-- if you want to generate help goal 
+				<executions>
+					<execution>
+						<id>help-goal</id>
+						<goals>
+							<goal>helpmojo</goal>
+						</goals>
+					</execution>
+				</executions>
+				-->
+		    </plugin>
+		</plugins>
     </build>
 </project>
diff --git a/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java b/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
index 80e7512..f9da89e 100644
--- a/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
+++ b/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
@@ -42,8 +42,7 @@ import org.codehaus.plexus.util.StringUtils;
 /**
  * Empire-DB Code generation by reading an existing database schema
  */
-@Mojo(
-        name = "codegen",
+@Mojo(  name = "codegen",
         defaultPhase = LifecyclePhase.GENERATE_SOURCES,
         requiresDependencyResolution = ResolutionScope.RUNTIME)
 public class CodeGenMojo extends AbstractMojo {
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBIndex.java b/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
index 8739ec9..5331922 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
@@ -114,6 +114,16 @@ public class DBIndex extends DBObject
     {
         return columns;
     }
+
+    /**
+     * Returns the columns belonging to this index.
+     * 
+     * @return the columns belonging to this index
+     */
+    public DBColumn getColumn(int index)
+    {
+        return columns[index];
+    }
     
     /**
      * Returns the number of columns belonging to this index.
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
index 0d27422..3382cfc 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
@@ -549,6 +549,8 @@ public abstract class DBRowSet extends DBExpr
                 fields[field] = keyValues[i];
             }
         }
+        // Set defaults (don't provide connection here)
+        initRecordDefaultValues(rec, null);
         // Init
         completeInitRecord(rec);
     }
@@ -597,6 +599,42 @@ public abstract class DBRowSet extends DBExpr
     }
     
     /**
+     * initializes the Record Default Values
+     * @param rec the record
+     * @param conn (optional) to allow the driver handle autogenerated fields
+     */
+    protected void initRecordDefaultValues(DBRecord rec, Connection conn)
+    {
+        Object[] fields = rec.getFields();
+        /*
+         *  Connection Auto-Detect (disabled!)
+         *  Use derived class to add connection if necessary
+         *         
+        DBColumn pkColumn = (primaryKey!=null ? primaryKey.getColumn(0) : null);
+        if (conn==null && pkColumn!=null && pkColumn.isAutoGenerated() && fields[getColumnIndex(pkColumn)]==null)
+        {   // Init AutoGenerated Key
+            conn = rec.getContext().getConnection();
+        }
+        */
+        // Set Default values
+        for (int i = 0; i < fields.length; i++)
+        {   // already set ?
+            if (fields[i]!=null)
+                continue; 
+            // check default
+            DBColumn column = columns.get(i);
+            if (column instanceof DBTableColumn)
+            {   // getDefaultValue
+                Object value = ((DBTableColumn)column).getRecordDefaultValue(conn);
+                if (value==null)
+                    continue;
+                // Initial value
+                fields[i] = value;
+            }
+        }
+    }
+    
+    /**
      * Completes the record initialization.<BR>
      * Override this function to do post initialization processing.
      * <P>
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java b/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
index 0b1a3f6..163fa57 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
@@ -139,14 +139,13 @@ public class DBTableColumn extends DBColumn
         if (rowset==null)
             return defaultValue;
         // Detect default value
-        DBDatabase db = rowset.getDatabase();
         if (isAutoGenerated())
         {   // If no connection is supplied defer till later
         	if (conn==null)
         		return null; // Create Later
             // Other auto-generated values
-        	DBDatabaseDriver driver = db.getDriver();
-            return driver.getColumnAutoValue(db, this, conn);
+	        DBDatabase db = rowset.getDatabase();
+            return db.getDriver().getColumnAutoValue(db, this, conn);
         }
         // Normal value
         return defaultValue;
diff --git a/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java b/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java
new file mode 100644
index 0000000..e3493f0
--- /dev/null
+++ b/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.empire.db;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.sql.Connection;
+
+import org.apache.empire.commons.ClassUtils;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.context.DBContextBase;
+import org.apache.empire.db.context.DBContextStatic;
+import org.apache.empire.db.context.DBRollbackManager;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author rainer
+ * @since 23.01.22
+ */
+public class SerializeTest
+{
+    private static final MockDriver driver = new MockDriver();
+
+    private static final DBContext staticContext = new DBContextStatic(driver, null);
+    
+    private static DBContext getStaticContext()
+    {
+        return staticContext;
+    }
+
+    private static Connection getDBConnection()
+    {
+        return null; /* No real Connection used */ 
+    }
+
+    /**
+     * Test Standard DBRecord with Serializable Context
+     */
+    @Test
+    public void testSerializationWithSerializableContext()
+        throws Exception
+    {
+        final TestDatabase db = new TestDatabase();
+        
+        DBContext context = new SerializableContext(driver, null);
+        db.open(context);
+        
+        DBRecord rec1Original = new DBRecord(context, db.T_TEST);
+        rec1Original.init(DBRecord.key(1), true);
+        DBRecord rec1Serial = ClassUtils.testSerialization(DBRecord.class, rec1Original);
+        
+        Assert.assertTrue(rec1Serial.isValid());
+        Assert.assertNotNull(rec1Serial.getContext());
+        Assert.assertNotNull(rec1Serial.getContext().getDriver());
+        Assert.assertNotSame(rec1Original.getContext(), rec1Serial.getContext());
+    }
+    
+    /**
+     * Test Serializable Record with Static Context
+     */
+    @Test
+    public void testSerializationWithSerializableRecord()
+        throws Exception
+    {
+        final TestDatabase db = new TestDatabase();
+        
+        DBContext context = getStaticContext();
+        db.open(context);
+        
+        DBRecord rec2Original = new SerializableRecord(context, db.T_TEST);
+        rec2Original.init(DBRecord.key(1), true);
+        DBRecord rec2Serial = ClassUtils.testSerialization(DBRecord.class, rec2Original);
+
+        Assert.assertTrue(rec2Serial.isValid());
+        Assert.assertNotNull(rec2Serial.getContext());
+        Assert.assertNotNull(rec2Serial.getContext().getDriver());
+        Assert.assertEquals(rec2Original.getContext(), rec2Serial.getContext());
+    }
+    
+    /**
+     * Databae
+     */
+    static class TestDatabase extends DBDatabase
+    {
+        public final TestTable T_TEST = new TestTable(this);
+        static class TestTable extends DBTable
+        {
+            public final DBTableColumn C_ID;
+            public final DBTableColumn C_TEXT;
+
+            TestTable(DBDatabase db)
+            {
+                super("testtable", db);
+                this.C_ID = addColumn("id", DataType.INTEGER, 0, true);
+                this.C_TEXT = addColumn("text", DataType.VARCHAR, 255, false);
+                setPrimaryKey(C_ID);
+            }
+        }
+    }
+    
+    /**
+     * SerializableContext
+     */
+    static class SerializableContext extends DBContextBase implements Serializable
+    {
+        private static final long serialVersionUID = 1L;
+        
+        private final transient DBDatabaseDriver driver;
+        private final transient Connection conn;
+        
+        /**
+         * Custom deserialization for transient fields.
+         */
+        private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException 
+        {   // Driver
+            ClassUtils.setPrivateFieldValue(SerializableContext.class, this, "driver", SerializeTest.driver);
+            // Context
+            ClassUtils.setPrivateFieldValue(SerializableContext.class, this, "conn", getDBConnection());
+            // read the rest
+            strm.defaultReadObject();
+        }
+        
+        
+        public SerializableContext(DBDatabaseDriver driver, Connection conn)
+        {
+            this.driver = driver;
+            this.conn = conn;
+        }
+
+        @Override
+        public DBDatabaseDriver getDriver()
+        {
+            return driver;
+        }
+    
+        @Override
+        public boolean isRollbackHandlingEnabled()
+        {
+            return false;
+        }
+    
+        @Override
+        protected Connection getConnection(boolean create)
+        {
+            return conn;
+        }
+    
+        @Override
+        protected DBRollbackManager getRollbackManager(boolean create)
+        {
+            return null;
+        }
+    }
+    
+    /**
+     * SerializableRecord
+     */
+    static class SerializableRecord extends DBRecord
+    {
+        private static final long serialVersionUID = 1L;
+        
+        public SerializableRecord(DBContext context, DBRowSet rowset)
+        {
+            super(context, rowset);
+        }
+    
+        @Override
+        protected void writeContext(ObjectOutputStream strm) throws IOException
+        {
+            /* Nothing */
+        }
+    
+        @Override
+        protected DBContext readContext(ObjectInputStream strm)  throws IOException, ClassNotFoundException
+        {
+            return getStaticContext();
+        }
+    }
+}