/*
 * 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.util.Set;

import org.apache.empire.commons.DateUtils;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.data.DataType;
import org.apache.empire.db.expr.column.DBValueExpr;
import org.apache.empire.exceptions.InvalidOperationException;

/**
 * This class defines a parameter for a prepared statement query.
 * Do not create instances of this class yourself, rather use DBCommand.addParam(...)
 * @author Rainer
 */
public class DBCmdParam extends DBExpr
{
    // *Deprecated* private static final long serialVersionUID = 1L;
    protected DBCommand cmd;
    protected DataType  type;
    protected Object    value;
    protected Object    reference;

    /**
     * Protected constructor used e.g. by DBCommand.addParam(...) 
     * @param cmd the command to which this DBCommand belongs to
     * @param type the parameter data type
     * @param value the initial value (can be changed any time by calling setValue(...))
     */
    protected DBCmdParam(DBCommand cmd, DataType type, Object value)
    {
        this.cmd = cmd;
        this.type = type;
        this.value = getCmdParamValue(value);
    }

    /**
     * Returns a value expression for this param
     * @param db the database
     * @return the value expression 
     */
    public DBValueExpr getValueExpr(DBDatabase db)
    {
        return db.getValueExpr(this, type);
    }

    /**
     * Returns the internal parameter value for a given "real" value.  
     * Depending on the data type this might involve wrapping the real value with another class.
     * This is done e.g. for CLOBs and BLOBs 
     * @param value the "real" value
     * @return the (possibly wrapped) value
     */
    protected Object getCmdParamValue(Object value)
    {
        if (value instanceof DBExpr)
            throw new InvalidOperationException("DBCmdParam does not accept DBExpr");
        if (value!=null)
            value = ObjectUtils.convertValue(type, value);
        if (value==null)
            return value;
        // If still an Enum, convert now (Lazy conversion)
        if (value instanceof Enum<?>)
            value = ObjectUtils.getEnumValue((Enum<?>)value, type.isNumeric());
        // Check CLOB and BLOB
        switch (type)
        {
            case BLOB:
                if (value instanceof DBBlobData)
                    return value;
                if (value instanceof byte[])
                    return new DBBlobData((byte[])value);
                // create a blob data
                return new DBBlobData(value.toString());
            case CLOB:
                if (value instanceof DBClobData)
                    return value;
                // create a clob data
                return new DBClobData(value.toString());
            case DATE:
                if (DBDatabase.SYSDATE.equals(value))
                    value = DateUtils.getDateNow();
                return value;
            case DATETIME:
                if (DBDatabase.SYSDATE.equals(value))
                    value = DateUtils.getTimeNow();
                return value;
            case TIMESTAMP:
            case UNKNOWN:
                if (DBDatabase.SYSDATE.equals(value))
                    value = DateUtils.getTimestamp();
                return value;
            default:
                // the value
                return value;
        }
    }
    
    @Override
    public void addSQL(DBSQLBuilder sql, long context)
    {
        sql.append("?"); //$NON-NLS-1$
        // Move to current usage position
        cmd.cmdParams.notifyParamUsage(this);
    }
    
    /**
     * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
     */
    @Override
    public void addReferencedColumns(Set<DBColumn> list)
    {
        // Nothing to add
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public final DBDatabase getDatabase()
    {
        return cmd.getDatabase();
    }
    
    /**
     * Returns the command this parameter belongs to
     * @return the command
     */
    public DBCommand getCmd()
    {
        return cmd;
    }

    /**
     * Returns the data type of the command parameter
     * @return the data type
     */
    public DataType getDataType()
    {
        return type;
    }
    
    /**
     * Returns the current value of the parameter.
     * In some cases (as for CLOBs and BLOBs) this might return a Wrapper class for the underlying value.  
     * @return the current (possibly wrapped) value
     */
    public Object getValue()
    {
        return value;
    }
    
    /**
     * Sets the current value of the parameter
     * @param value the new value
     */
    public void setValue(Object value)
    {
        this.value = getCmdParamValue(value);
    }

    /**
     * Returns the reference object associated with this param
     * @return the reference object or null
     */
    public Object getReference()
    {
        return reference;
    }

    /**
     * Sets a reference object associated with this param
     * @param reference the reference object
     * @return this object 
     */
    public DBCmdParam setReference(Object reference)
    {
        this.reference = reference;
        return this;
    }
}
