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 2019/07/09 13:13:50 UTC

[empire-db] branch master updated: EMPIREDB-290: enum DataMode has been made obsolete.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b0fccc4  EMPIREDB-290: enum DataMode has been made obsolete.
b0fccc4 is described below

commit b0fccc474bf5be7da3b1f05349d535817ae65408
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Tue Jul 9 15:13:45 2019 +0200

    EMPIREDB-290: enum DataMode has been made obsolete.
---
 .../main/java/org/apache/empire/data/DataMode.java |  2 +
 .../main/java/org/apache/empire/db/DBTable.java    | 72 ++++++++++++++--------
 .../java/org/apache/empire/db/DBTableColumn.java   | 55 ++++++++++-------
 3 files changed, 79 insertions(+), 50 deletions(-)

diff --git a/empire-db/src/main/java/org/apache/empire/data/DataMode.java b/empire-db/src/main/java/org/apache/empire/data/DataMode.java
index 76c9bd4..70cb0ba 100644
--- a/empire-db/src/main/java/org/apache/empire/data/DataMode.java
+++ b/empire-db/src/main/java/org/apache/empire/data/DataMode.java
@@ -20,7 +20,9 @@ package org.apache.empire.data;
 
 /**
  * DataMode is an enumeration that specifies whether the value of a column is readonly, optional (nullable), required (NotNull), or auto-generated 
+ * @deprecated will be removed in a future release 
  */
+@Deprecated
 public enum DataMode {
     /**
      * Optional = value may be null
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBTable.java b/empire-db/src/main/java/org/apache/empire/db/DBTable.java
index ebdbb3f..b09d463 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBTable.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBTable.java
@@ -200,9 +200,6 @@ public class DBTable extends DBRowSet implements Cloneable
 
     /**
      * Adds a column to this table's column list.
-     * DO NOT CALL!
-     * This method is internally called from the constructor of DBTableColumn
-     * 
      * @param column a column object
      */
     protected void addColumn(DBTableColumn column)
@@ -213,6 +210,23 @@ public class DBTable extends DBRowSet implements Cloneable
             throw new ItemExistsException(column.getName());
         // add now
         columns.add(column);
+    }
+
+    /**
+     * Creates a new Column object and appends it to the column list
+     * @param columnName the column name
+     * @param type the type of the column e.g. integer, text, date
+     * @param size the column width
+     * @param required true if not null column
+     * @param defValue a Object object
+     * @return the new column object
+     */
+    protected DBTableColumn crateAndAppendColumn(String columnName, DataType type, double size, boolean required, Object defValue)
+    { 
+        // Make sure (DataType.INTEGER & DataMode.AutoGenerated) = DataType.AUTOINC
+        boolean autoGenerated = (type==DataType.AUTOINC);
+        DBTableColumn column = new DBTableColumn(this, type, columnName, size, required, autoGenerated, defValue);
+        addColumn(column);
         // auto-set primary key
         if (column.getDataType()==DataType.AUTOINC)
         {   // Automatically set primary key
@@ -229,26 +243,34 @@ public class DBTable extends DBRowSet implements Cloneable
             else
                 log.warn("Table {} already has a Timestamp column. DataType of column {} should be DATETIME.", getName(), column.getName());
         }
+        return column;
     }
     
     /**
-     * Creates a new DBTableColumn object and adds it to the column collection.
-     * 
-     * @param columnName the column name
-     * @param type the type of the column e.g. integer, text, date
-     * @param size the column width
-     * @param dataMode determines whether this column is optional, required or auto-generated 
-     * @param defValue a Object object
-     * @return the created DBTableColumn object
+     * Deprecacted function to add a table column.
+     * This function will be removed in a further release.
+     * @deprecated use {@link #addColumn(String columnName, DataType type, double size, boolean required, Object defValue)} instead. 
+     * @return the new column object 
      */
-    protected DBTableColumn addColumn(String columnName, DataType type, double size, DataMode dataMode, Object defValue)
+    @Deprecated
+    public final DBTableColumn addColumn(String columnName, DataType type, double size, DataMode dataMode, Object defValue)
     { 
-        DBTableColumn col = new DBTableColumn(this, type, columnName, size, dataMode, defValue);
-        addColumn(col);
-        return col;
+        return this.addColumn(columnName, type, size, (dataMode!=DataMode.Nullable), defValue);
     }
 
     /**
+     * Deprecacted function to add a table column
+     * This function will be removed in a further release.
+     * @deprecated use {@link #addColumn(String columnName, DataType type, double size, boolean required)} instead. 
+     * @return the new column object 
+     */
+    @Deprecated
+    public final DBTableColumn addColumn(String columnName, DataType type, double size, DataMode dataMode) 
+    {
+        return this.addColumn(columnName, type, size, (dataMode!=DataMode.Nullable));
+    }
+    
+    /**
      * Creates a new DBTableColumn object and adds it to the column collection.
      * Instead of the data mode enum, a boolean flag is used to indicate whether the column is required or optional.
      * 
@@ -257,7 +279,7 @@ public class DBTable extends DBRowSet implements Cloneable
      * @param size the column width
      * @param required true if not null column
      * @param defValue a Object object
-     * @return the created DBTableColumn object
+     * @return the new column object 
      */
     public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Object defValue)
     {
@@ -265,8 +287,7 @@ public class DBTable extends DBRowSet implements Cloneable
         {
             log.warn("Column {}: a class object of type \"{}\" has been passed as default value. Please check!", columnName, ((Class<?>)defValue).getName());
         }
-        DataMode dm = (required ? DataMode.NotNull : DataMode.Nullable);
-        return this.addColumn(columnName, type, size, dm, defValue);
+        return this.crateAndAppendColumn(columnName, type, size, required, defValue);
     }
 
     /**
@@ -276,12 +297,11 @@ public class DBTable extends DBRowSet implements Cloneable
      * @param type the type of the column e.g. integer, text, date
      * @param size the column width
      * @param required true if not null column
-     * @return the created DBTableColumn object
+     * @return the new column object 
      */
     public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required)
     { 
-        DataMode dm = (required ? DataMode.NotNull : DataMode.Nullable);
-        return this.addColumn(columnName, type, size, dm, null);
+        return this.crateAndAppendColumn(columnName, type, size, required, null);
     }
 
     /**
@@ -293,7 +313,7 @@ public class DBTable extends DBRowSet implements Cloneable
      * @param size the column width
      * @param required true if not null column
      * @param enumType  the class of the enum type
-     * @return the created DBTableColumn object
+     * @return the new column object 
      */
     public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Class<?> enumType)
     {
@@ -301,8 +321,7 @@ public class DBTable extends DBRowSet implements Cloneable
         {   // Class must be an enum type
             throw new InvalidArgumentException("enumType", enumType);
         }
-        DataMode dm = (required ? DataMode.NotNull : DataMode.Nullable);
-        DBTableColumn col = this.addColumn(columnName, type, size, dm, null);
+        DBTableColumn col = this.crateAndAppendColumn(columnName, type, size, required, null);
         col.setEnumOptions(enumType);
         return col;
     }
@@ -316,12 +335,11 @@ public class DBTable extends DBRowSet implements Cloneable
      * @param size the column width
      * @param required true if not null column
      * @param enumType  defValue the default value
-     * @return the created DBTableColumn object
+     * @return the new column object 
      */
     public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Enum<?> defValue)
     { 
-        DataMode dm = (required ? DataMode.NotNull : DataMode.Nullable);
-        DBTableColumn col = this.addColumn(columnName, type, size, dm, defValue);
+        DBTableColumn col = this.crateAndAppendColumn(columnName, type, size, required, defValue);
         col.setEnumOptions(defValue.getClass());
         return col;
     }
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 916a413..1599384 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
@@ -58,10 +58,11 @@ public class DBTableColumn extends DBColumn
     // Column Information
     protected DataType  type;
     protected double    size;
-    protected DataMode  dataMode;
-    protected Object    defValue;
-    protected int 		decimalScale = 0;
+    protected boolean   required;
+    protected boolean   autoGenerated;
     protected boolean   readOnly;
+    protected Object    defaultValue;
+    protected int 		decimalScale = 0;
 
     /**
      * Constructs a DBTableColumn object set the specified parameters to this object.
@@ -73,20 +74,16 @@ public class DBTableColumn extends DBColumn
      * @param dataMode determines whether this column is optional, required or auto-generated 
      * @param defValue the object value
      */
-    public DBTableColumn(DBTable table, DataType type, String name, double size, DataMode dataMode, Object defValue)
+    public DBTableColumn(DBTable table, DataType type, String name, double size, boolean required, boolean autoGenerated, Object defValue)
     {
         super(table, name);
         // check properties
-        // Make sure (DataType.INTEGER & DataMode.AutoGenerated) = DataType.AUTOINC
-        if (type==DataType.AUTOINC && dataMode!=DataMode.AutoGenerated)
-            dataMode=DataMode.AutoGenerated;
-        if (type==DataType.INTEGER && dataMode==DataMode.AutoGenerated)
-            type=DataType.AUTOINC;           
         // set column properties
         this.type = type;
-        this.dataMode = dataMode;
-        this.defValue = defValue;
-        this.readOnly =(dataMode == DataMode.AutoGenerated);
+        this.required = required;
+        this.autoGenerated = autoGenerated;
+        this.readOnly = autoGenerated;
+        this.defaultValue = defValue;
         // xml
         this.attributes = new Attributes();
         this.options = null;
@@ -95,6 +92,16 @@ public class DBTableColumn extends DBColumn
     }
 
     /**
+     * Old constructor which will be removed in a further release.
+     * @deprecated use {@link #DBTableColumn(DBTable table, DataType type, String name, double size, boolean required, boolean autoGenerated, Object defValue)} instead. 
+     */
+    @Deprecated
+    public DBTableColumn(DBTable table, DataType type, String name, double size, DataMode dataMode, Object defValue)
+    {
+        this(table, type, name, size, (dataMode!=DataMode.Nullable), (dataMode==DataMode.AutoGenerated), defValue);
+    }
+    
+    /**
      * Clone Constructor - use clone()
      */
     protected DBTableColumn(DBTable newTable, DBTableColumn other)
@@ -103,8 +110,10 @@ public class DBTableColumn extends DBColumn
         // Copy
         this.type = other.type;
         this.size = other.size;
-        this.dataMode = other.dataMode;
-        this.defValue = other.defValue;
+        this.required = other.required;
+        this.autoGenerated = other.autoGenerated;
+        this.readOnly = other.readOnly;
+        this.defaultValue = other.defaultValue;
         this.attributes = new Attributes();
         this.attributes.addAll(other.attributes);
         this.options = other.options;
@@ -122,7 +131,7 @@ public class DBTableColumn extends DBColumn
      */
     public Object getDefaultValue()
     {
-        return defValue;
+        return defaultValue;
     }
 
     /**
@@ -132,7 +141,7 @@ public class DBTableColumn extends DBColumn
      */
     public void setDefaultValue(Object defValue)
     {
-        this.defValue = defValue;
+        this.defaultValue = defValue;
     }
     
     /**
@@ -146,7 +155,7 @@ public class DBTableColumn extends DBColumn
     public Object getRecordDefaultValue(Connection conn)
     {	// Check params   
         if (rowset==null)
-            return defValue;
+            return defaultValue;
         // Detect default value
         DBDatabase db = rowset.getDatabase();
         if (isAutoGenerated())
@@ -158,7 +167,7 @@ public class DBTableColumn extends DBColumn
             return driver.getColumnAutoValue(db, this, conn);
         }
         // Normal value
-        return defValue;
+        return defaultValue;
     }
 
     /**
@@ -244,7 +253,7 @@ public class DBTableColumn extends DBColumn
     @Override
     public boolean isRequired()
     {
-        return (dataMode==DataMode.NotNull);
+        return this.required;
     }
     
     /**
@@ -255,7 +264,7 @@ public class DBTableColumn extends DBColumn
     @Override
     public boolean isAutoGenerated()
     {
-        return (dataMode==DataMode.AutoGenerated);
+        return this.autoGenerated;
     }
     
     /**
@@ -295,7 +304,7 @@ public class DBTableColumn extends DBColumn
         }
         else
         {	// Set DataMode
-        	dataMode=(required ? DataMode.NotNull : DataMode.Nullable);
+        	this.required = required;
         }
     }
 
@@ -306,8 +315,8 @@ public class DBTableColumn extends DBColumn
      */
     @Override
     public boolean isReadOnly()
-    {   // Check DataMode
-        return (this.readOnly || dataMode==DataMode.AutoGenerated);
+    {   
+        return this.readOnly;
     }
 
     /**