001package com.randomnoun.common.jexl.sql;
002
003/** An SQL column which represents a transformed value ( the value in the expression is not the stored value ).
004 * 
005 * <p>Examples of TransformedSqlColumns:
006 * <ul>
007 * <li>enum values ( 1-char in database exposed as enum strings )
008 * <li>yes/no values ( 'Y'/'N' in database exposed as booleans )
009 * <li>api-prefixed values ( 1234 in database exposed as "obj-1234" )
010 * </ul>
011 * 
012 * <p>If the transformation is reversible, we can convert some expressions to use the raw column 
013 * which can then leverage indexes on that column.
014 * 
015 */
016public abstract class TransformedSqlColumn extends SqlColumn {
017
018        public TransformedSqlColumn(String name, String table, int dataType) {
019                super(name, table, dataType);
020        }
021
022        /** If true, we can perform null checks on the source column instead of the transformed value */
023        public abstract boolean isNullPreserved();        // if nulls are preserved through transformation
024        
025        /** If true, we can reverse transformations for equals and not-equals comparisons */
026        public abstract boolean isReversableEquals();     // if we can convert col == "someValue" to a simplified form
027        
028        /** If true, we can reverse transformations for comparison operations; we can only do this if transformations preserve ordering */
029        public abstract boolean isReversableComparison(); // if we can convert col > "someValue" to a simplified form
030        
031        /** The source column */
032        public abstract SqlColumn getSourceSqlColumn();    // name of column to use for reverse comparisons
033        
034        /** Reverse the transformation, i.e. convert a user-supplied value back into a database value.
035         * The user-supplied value must be a literal (String, Boolean, Long etc), not an expression. 
036         * 
037         * @param value literal value in expression
038         * 
039         * @value the raw value stored in the database
040         */
041        public abstract Object reverseTransformLiteral(Object value) throws CannotReverseTransformationException;
042        
043}