예제 #1
0
        /// <inheritDoc />
        public bool bindValue(PhpValue parameter, PhpValue value, PDO.PARAM data_type = (PDO.PARAM) PDO.PARAM_STR)
        {
            Debug.Assert(this.m_cmd != null);

            IDbDataParameter param = null;

            if (m_namedAttr)
            {
                // Mixed parameters not allowed
                if (m_positionalAttr)
                {
                    m_pdo.HandleError(new PDOException("Mixed parameters mode not allowed. Use either only positional, or only named parameters."));
                    return(false);
                }

                string key = parameter.AsString();
                if (key == null)
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter name must be a string."));
                    return(false);
                }

                if (key.Length > 0 && key[0] == ':')
                {
                    key = key.Substring(1);
                }

                param = m_cmd.Parameters[key];

                //rewrite the bounded params dictionary
                if (HasParamsBound)
                {
                    if (m_boundParams.ContainsKey(key))
                    {
                        m_boundParams.Remove(key);
                    }
                }
            }
            else if (m_positionalAttr)
            {
                if (!parameter.IsInteger())
                {
                    m_pdo.HandleError(new PDOException("Supplied parameter index must be an integer."));
                    return(false);
                }
                int paramIndex = ((int)parameter) - 1;

                if (paramIndex < m_positionalPlaceholders.Count)
                {
                    param = m_cmd.Parameters[paramIndex];
                }

                //rewrite the bounded params dictionary
                if (HasParamsBound)
                {
                    if (m_boundParams.ContainsKey(paramIndex.ToString()))
                    {
                        m_boundParams.Remove(paramIndex.ToString());
                    }
                }
            }
            else
            {
                m_pdo.HandleError(new PDOException("No parameter mode set yet for this Statement. Possibly no parameters required?"));
                return(false);
            }

            if (param == null)
            {
                m_pdo.HandleError(new PDOException("No matching parameter found."));
                return(false);
            }

            switch (data_type)
            {
            case PDO.PARAM.PARAM_INT:
                if (value.IsInteger())
                {
                    param.DbType = DbType.Int32;
                    param.Value  = (int)value;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_STR:
                string str = null;
                if ((str = value.ToStringOrNull()) != null)
                {
                    param.DbType = DbType.String;
                    param.Value  = str;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_BOOL:
                if (value.IsBoolean())
                {
                    param.DbType = DbType.Boolean;
                    param.Value  = value.ToBoolean();
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            case PDO.PARAM.PARAM_LOB:
                byte[] bytes = null;
                if ((bytes = value.ToBytesOrNull()) != null)
                {
                    param.DbType = DbType.Binary;
                    param.Value  = bytes;
                }
                else
                {
                    m_pdo.HandleError(new PDOException("Parameter type does not match the declared type."));
                    return(false);
                }
                break;

            // Currently not supported by any drivers
            case PDO.PARAM.PARAM_NULL:
            case PDO.PARAM.PARAM_STMT:
            default:
                throw new NotImplementedException();
            }

            return(true);
        }