예제 #1
0
 /// <summary>
 /// Checks whether a dereferenced variable is boolean.
 /// </summary>
 /// <param name="variable">The variable.</param>
 /// <returns>Whether <paramref name="variable"/> is boolean.</returns>
 public static bool is_bool(PhpValue variable) => variable.IsBoolean();
예제 #2
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);
        }
예제 #3
0
파일: Ftp.cs 프로젝트: softearth/peachpie
        /// <summary>
        /// This function controls various runtime options for the specified FTP stream.
        /// </summary>
        /// <param name="ftp_stream">The link identifier of the FTP connection.</param>
        /// <param name="option">Currently, the following options are supported:FTP_TIMEOUT_SEC,
        /// FTP_AUTOSEEK, FTP_USEPASVADDRESS</param>
        /// <param name="value">This parameter depends on which option is chosen to be altered.</param>
        /// <returns>Returns TRUE if the option could be set; FALSE if not. A warning message will be thrown
        /// if the option is not supported or the passed value doesn't match the expected value for the given option.</returns>
        public static bool ftp_set_option(PhpResource ftp_stream, int option, PhpValue value)
        {
            var resource = ValidateFtpResource(ftp_stream);

            if (resource == null)
            {
                return(false);
            }

            switch (option)
            {
            case FTP_AUTOSEEK:
                if (value.IsBoolean(out var result))
                {
                    resource.Autoseek = result;
                    return(true);
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, Resources.Resources.unexpected_arg_given,
                                       nameof(value), PhpVariable.TypeNameBoolean, PhpVariable.GetTypeName(value));
                    return(false);
                }

            case FTP_TIMEOUT_SEC:
                if (value.IsLong(out var time))
                {
                    if (time <= 0)
                    {
                        PhpException.Throw(PhpError.Warning, Resources.Resources.sleep_seconds_less_zero);
                        return(false);
                    }

                    resource.Client.ReadTimeout    = (int)time * 1000;
                    resource.Client.ConnectTimeout = resource.Client.ReadTimeout;
                    resource.Client.DataConnectionConnectTimeout = resource.Client.ReadTimeout;
                    resource.Client.DataConnectionReadTimeout    = resource.Client.ReadTimeout;
                    return(true);
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, Resources.Resources.unexpected_arg_given,
                                       nameof(value), PhpVariable.TypeNameInteger, PhpVariable.GetTypeName(value));
                    return(false);
                }

            case FTP_USEPASVADDRESS:
                if (value.IsBoolean(out result))
                {
                    resource.UsePASVAddress = result;
                    return(true);
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, Resources.Resources.unexpected_arg_given,
                                       nameof(value), PhpVariable.TypeNameBoolean, PhpVariable.GetTypeName(value));
                    return(false);
                }

            default:
                PhpException.Throw(PhpError.Warning, Resources.Resources.arg_invalid_value, option.ToString(), nameof(option));
                return(false);
            }
        }