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 2025/10/03 18:19:02 UTC

(empire-db) branch master updated: EMPIREDB-477 ColumnUtils: allow to set a sort expression for ech column

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 79853c03 EMPIREDB-477 ColumnUtils: allow to set a sort expression for ech column
79853c03 is described below

commit 79853c030e351d11a7d10fdd8a7bfb58caa33678
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Fri Oct 3 20:18:58 2025 +0200

    EMPIREDB-477
    ColumnUtils: allow to set a sort expression for ech column
---
 .../org/apache/empire/commons/ColumnUtils.java     | 47 +++++++++++++++++
 .../main/java/org/apache/empire/data/Column.java   |  1 +
 .../src/main/java/org/apache/empire/db/DBView.java | 59 +++++++++++++---------
 3 files changed, 83 insertions(+), 24 deletions(-)

diff --git a/empire-db/src/main/java/org/apache/empire/commons/ColumnUtils.java b/empire-db/src/main/java/org/apache/empire/commons/ColumnUtils.java
index fc4eead6..7530faed 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ColumnUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ColumnUtils.java
@@ -26,7 +26,11 @@ import java.util.regex.Pattern;
 import org.apache.empire.data.Column;
 import org.apache.empire.data.ColumnExpr;
 import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.expr.column.DBFuncExpr;
 import org.apache.empire.exceptions.NotSupportedException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * ColumnUtils
@@ -35,6 +39,8 @@ import org.apache.empire.exceptions.NotSupportedException;
  */
 public class ColumnUtils
 {
+    private static final Logger log = LoggerFactory.getLogger(ColumnUtils.class);
+    
     /**
      * The instance of ValueUtils to be used for value type conversion
      */
@@ -171,6 +177,47 @@ public class ColumnUtils
             columnExpr.setAttribute(Column.COLATTR_CASESENSITIVE, false);
         }
     }
+    
+    /**
+     * Returns the sort expression for a given column
+     * If no sort expression is explicitly set then the columnExpr itself is returned
+     * The returned expression should be assigned to an DBCommand.orderBy() function.
+     * 
+     * @param columnExpr the column for which to retrieve the sort expression
+     * @return the sort expression or the columnExp if not sort expression is set
+     */
+    public static DBColumnExpr getSortExpr(DBColumnExpr columnExpr)
+    {
+        Object value = instance.getColumnAttribute(columnExpr, Column.COLATTR_SORTEXPRESSION);
+        // create a sort expression
+        if (value instanceof DBColumnExpr)
+        {   // return expression
+            return ((DBColumnExpr)value); 
+        }
+        else if ((value instanceof String) && ((String)value).indexOf('?')>=0)
+        {   // create a sort function expression
+            String sortFunctionTemplate = StringUtils.toString(value);
+            return new DBFuncExpr(columnExpr, sortFunctionTemplate, null, false, columnExpr.getDataType());
+        }
+        else if (value!=null)
+        {   // unknown value
+            log.warn("Invalid value for {}: {}", Column.COLATTR_SORTEXPRESSION, value);
+        }
+        // not changed
+        return columnExpr; 
+    }
+    
+    /**
+     * Sets a sort function template for a particular DBColumExpr
+     * @param columnExpr the column expression
+     * @param sortFunctionTemplate the template which must contain a ? which will be replaced by the sort expression
+     * @return return the columnExpr
+     */
+    public static <T extends DBColumnExpr> T setSortExpr(T columnExpr, DBColumnExpr sortExpression)
+    {   // set sort expression
+        columnExpr.setAttribute(Column.COLATTR_SORTEXPRESSION, sortExpression);
+        return columnExpr;
+    }
 
     /**
      * Returns true if the columnExpr is a numeric columnExpr
diff --git a/empire-db/src/main/java/org/apache/empire/data/Column.java b/empire-db/src/main/java/org/apache/empire/data/Column.java
index 9af0a1fc..df88ec8c 100644
--- a/empire-db/src/main/java/org/apache/empire/data/Column.java
+++ b/empire-db/src/main/java/org/apache/empire/data/Column.java
@@ -42,6 +42,7 @@ public interface Column extends ColumnExpr
     public static final String COLATTR_NORMCOLUMN      = "normColumn";           // normalized column
     public static final String COLATTR_REFCOLUMNS      = "refColumns";           // reference columns
     public static final String COLATTR_CASESENSITIVE   = "caseSensitive";        // true/false
+    public static final String COLATTR_SORTEXPRESSION  = "sortExpression";       // sort function template for DBFunc
 
     public static final String COLATTR_MINLENGTH       = "minLength";
     public static final String COLATTR_MINVALUE        = "minValue";
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBView.java b/empire-db/src/main/java/org/apache/empire/db/DBView.java
index 85289433..7699678e 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBView.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBView.java
@@ -73,14 +73,11 @@ public abstract class DBView extends DBRowSet implements Cloneable
             // Source Column
             this.updateColumn = expr.getUpdateColumn();
             // from update column
-            this.size = (updateColumn!=null ? updateColumn.getSize() : size);
+            this.size = (size==0.0d && updateColumn!=null ? updateColumn.getSize() : size);
             // Copy enumType
             Class<Enum<?>> enumType = expr.getEnumType();
             if (enumType!=null)
                 setAttribute(Column.COLATTR_ENUMTYPE, enumType);
-            // Add to view
-            if (view != null)
-                view.addViewColumn(this);
         }
 
         /**
@@ -100,9 +97,6 @@ public abstract class DBView extends DBRowSet implements Cloneable
             Class<Enum<?>> enumType = other.getEnumType();
             if (enumType!=null)
                 setAttribute(Column.COLATTR_ENUMTYPE, enumType);
-            // Add to view
-            if (view != null)
-                view.addViewColumn(this);
         }
         
         public DBColumnExpr getSourceColumnExpr()
@@ -346,6 +340,7 @@ public abstract class DBView extends DBRowSet implements Cloneable
     protected DBColumn cloneColumn(DBRowSet clone, DBColumn sourceColumn)
     {
         DBViewColumn newCol = new DBViewColumn((DBView)clone, (DBViewColumn)sourceColumn);
+        addViewColumn(newCol);
         return newCol;
     }
 
@@ -464,63 +459,79 @@ public abstract class DBView extends DBRowSet implements Cloneable
         columns.add(col);
     }
 
+    /**
+     * Adds a column to the view.
+     * 
+     * @param columnName the column name
+     * @param sourceExpression the source column expression
+     * @param size the size
+     * @return the new column
+     */
+    protected DBViewColumn createAndAppendColumn(String columnName, DBColumnExpr sourceExpression, double size)
+    {
+        DBViewColumn viewColumn = new DBViewColumn(this, columnName, sourceExpression, size);
+        // add now
+        addViewColumn(viewColumn);
+        return viewColumn;
+    }
+
     /**
      * Adds a column to the view.
      * 
      * @param columnName name of the column in the view
-     * @param dataType the data type of the column
-     * @param size the size of the column
-     * @return true if the column was successfully added or false otherwise
+     * @param columnExpr the column expression that builds the column
+     * @return the new column
      */
-    protected final DBViewColumn addColumn(String columnName, DataType dataType, double size)
+    protected final DBViewColumn addColumn(String columnName, DBColumnExpr columnExpr)
     {   // find column by name
-        return new DBViewColumn(this, columnName, db.getValueExpr(null, dataType), size);
+        return createAndAppendColumn(columnName, columnExpr, 0.0d);
     }
-
+    
     /**
      * Adds a column to the view.
      * 
      * @param columnName name of the column in the view
      * @param dataType the data type of the column
-     * @return true if the column was successfully added or false otherwise
+     * @param size the size of the column
+     * @return the new column
      */
-    protected final DBViewColumn addColumn(String columnName, DataType dataType)
+    protected final DBViewColumn addColumn(String columnName, DataType dataType, double size)
     {   // find column by name
-        return new DBViewColumn(this, columnName, db.getValueExpr(null, dataType), 0.0d);
+        return createAndAppendColumn(columnName, db.getValueExpr(null, dataType), size);
     }
 
     /**
      * Adds a column to the view.
      * 
      * @param columnName name of the column in the view
-     * @param columnExpr the column expression that builds the column
-     * @return true if the column was successfully added or false otherwise
+     * @param dataType the data type of the column
+     * @return the new column
      */
-    protected final DBViewColumn addColumn(String columnName, DBColumnExpr columnExpr)
+    protected final DBViewColumn addColumn(String columnName, DataType dataType)
     {   // find column by name
-        return new DBViewColumn(this, columnName, columnExpr, 0.0d);
+        return createAndAppendColumn(columnName, db.getValueExpr(null, dataType), 0.0d);
     }
 
     /**
      * Adds a column to the view based on an existing column in another table.
      * 
      * @param sourceColumn existing column in another table  
-     * @return the view column object
+     * @return the new column
      */
     protected final DBViewColumn addColumn(DBTableColumn sourceColumn)
     {   // find column by name
-        return new DBViewColumn(this, sourceColumn.getName(), sourceColumn, sourceColumn.getSize());
+        return createAndAppendColumn(sourceColumn.getName(), sourceColumn, sourceColumn.getSize());
     }
 
     /**
      * Adds a column to the view based on an existing column in another view.
      * 
      * @param sourceColumn existing column in another view  
-     * @return the view column object
+     * @return the new column
      */
     protected final DBViewColumn addColumn(DBViewColumn sourceColumn)
     {   // find column by name
-        return new DBViewColumn(this, sourceColumn.getName(), sourceColumn, sourceColumn.getSize());
+        return createAndAppendColumn(sourceColumn.getName(), sourceColumn, sourceColumn.getSize());
     }
 
     /**