示例#1
0
        /// <summary>
        /// Opens a connection to a mysql server.
        /// </summary>
        public bool real_connect(Context ctx, string host = null, string username = null, string passwd = null, string dbname = "", int port = -1, string socket = null, int flags = 0)
        {
            var config            = ctx.Configuration.Get <MySqlConfiguration>();
            int connectiontimeout = 0;

            if (_lazyoptions != null)
            {
                PhpValue value;
                if (_lazyoptions.TryGetValue(Constants.MYSQLI_OPT_CONNECT_TIMEOUT, out value))
                {
                    connectiontimeout = (int)value.ToLong();
                }
            }

            // string $host = ini_get("mysqli.default_host")
            // string $username = ini_get("mysqli.default_user")
            // string $passwd = ini_get("mysqli.default_pw")
            // string $dbname = ""
            // int $port = ini_get("mysqli.default_port")
            // string $socket = ini_get("mysqli.default_socket")

            // p:
            if (host != null && host.StartsWith("p:", StringComparison.Ordinal))
            {
                host   = host.Substring(2);
                flags |= (int)MySql.ConnectFlags.Pooling;
            }

            //
            var connection_string = MySql.BuildConnectionString(config, ref host, port, username, passwd,
                                                                flags: (MySql.ConnectFlags)flags,
                                                                connectiontimeout: connectiontimeout);

            _connection = MySqlConnectionManager.GetInstance(ctx)
                          .CreateConnection(connection_string, false, -1, out bool success);

            if (success)
            {
                _connection.Server = host;

                if (!string.IsNullOrEmpty(dbname))
                {
                    _connection.SelectDb(dbname);
                }
            }
            else
            {
                MySqliContextData.GetContextData(ctx).LastConnectionError
                      = connect_error
                      = _connection.GetLastErrorMessage();
            }

            //
            return(success);
        }
示例#2
0
        /// <summary>
        /// Opens a connection to a mysql server.
        /// </summary>
        public bool real_connect(Context ctx, string host = null, string username = null, string passwd = null, string dbname = "", int port = -1, string socket = null, int flags = 0)
        {
            var    config            = ctx.Configuration.Get <MySqlConfiguration>();
            int    connectiontimeout = 0;
            string characterset      = "utf8mb4"; // default characterset that solves all the problems

            if (_lazyoptions != null)
            {
                PhpValue value;
                if (_lazyoptions.TryGetValue(Constants.MYSQLI_OPT_CONNECT_TIMEOUT, out value))
                {
                    connectiontimeout = (int)value.ToLong();
                }
                if (_lazyoptions.TryGetValue(Constants.MYSQLI_SET_CHARSET_NAME, out value))
                {
                    characterset = value.ToStringOrThrow(ctx);
                }
            }

            // string $host = ini_get("mysqli.default_host")
            // string $username = ini_get("mysqli.default_user")
            // string $passwd = ini_get("mysqli.default_pw")
            // string $dbname = ""
            // int $port = ini_get("mysqli.default_port")
            // string $socket = ini_get("mysqli.default_socket")

            var connection_string = MySql.BuildConnectionString(config, ref host, username, passwd,
                                                                flags: (MySql.ConnectFlags)flags,
                                                                connectiontimeout: connectiontimeout,
                                                                characterset: characterset);

            _connection = MySqlConnectionManager.GetInstance(ctx)
                          .CreateConnection(connection_string, false, -1, out bool success);

            if (success)
            {
                _connection.Server = host;

                if (!string.IsNullOrEmpty(dbname))
                {
                    _connection.SelectDb(dbname);
                }
            }
            else
            {
                MySqliContextData.GetContextData(ctx).LastConnectionError
                      = connect_error
                      = _connection.GetLastErrorMessage();
            }

            //
            return(success);
        }