Ejemplo n.º 1
0
        /// <summary>
        /// Creates a PDO instance representing a connection to a database
        /// </summary>
        /// <param name="dsn">The DSN.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="options">The options.</param>
        public void __construct(string dsn, string username = null, string password = null, PhpArray options = null)
        {
            int doublecolon = dsn.IndexOf(':');

            if (doublecolon < 0)
            {
                // lookup dsn alias
                var dsn2 = _ctx.Configuration.Get <PDOConfiguration>()?.Dsn[dsn];
                if (dsn2 == null)
                {
                    throw new PDOException("Invalid DSN Alias.");
                }

                dsn         = dsn2;
                doublecolon = dsn.IndexOf(':');

                if (doublecolon <= 0)
                {
                    throw new PDOException("Invalid DSN.");
                }
            }

            //
            var driver     = dsn.Remove(doublecolon);
            var connstring = dsn.AsSpan(doublecolon + 1);

            // resolve the driver:
            Driver = PDOEngine.TryGetDriver(driver) ?? throw new PDOException($"could not find driver: '{driver}'"); // TODO: resources

            if (options != null && options.TryGetValue((int)PDO_ATTR.ATTR_PERSISTENT, out var persistent))
            {
                // TODO: lookup for persistent connection in `ConnectionManager`, mark as persistent
            }

            try
            {
                // create connection object:
                _connection = new PdoConnectionResource(this, Driver.OpenConnection(connstring, username, password, options));
            }
            catch (Exception e)
            {
                // PDO construct always throws PDOException on error:
                throw new PDOException(e.Message);
            }

            // set attributes
            if (options != null)
            {
                var enumerator = options.GetFastEnumerator();
                while (enumerator.MoveNext())
                {
                    var key = enumerator.CurrentKey;
                    if (key.IsInteger &&
                        key.Integer != ATTR_PERSISTENT)
                    {
                        setAttribute((PDO_ATTR)key.Integer, enumerator.CurrentValue);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a PDO instance representing a connection to a database
        /// </summary>
        /// <param name="dsn">The DSN.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="options">The options.</param>
        public void __construct(string dsn, string username = null, string password = null, PhpArray options = null)
        {
            this.SetDefaultAttributes();

            string driver;
            ReadOnlySpan <char> connstring;

            int doublecolon = dsn.IndexOf(':');

            if (doublecolon >= 0)
            {
                driver     = dsn.Remove(doublecolon);
                connstring = dsn.AsSpan(doublecolon + 1);
            }
            else
            {
                // Alias mode
                throw new NotImplementedException("PDO DSN alias not implemented");
                // replace DSN alias with value
            }

            if (driver == "uri") // TODO: move to a driver "UriDriver"
            {
                // Uri mode
                if (Uri.TryCreate(connstring.ToString(), UriKind.Absolute, out var uri))
                {
                    if (uri.Scheme.Equals("file", StringComparison.Ordinal))
                    {
                        throw new NotImplementedException("PDO uri DSN not implemented");
                        //return
                    }
                    else
                    {
                        throw new PDOException("PDO DSN as URI does not support other schemes than 'file'");
                    }
                }
                else
                {
                    throw new PDOException("Invalid uri in DSN");
                }
            }

            // DSN mode
            Driver = PDOEngine.TryGetDriver(driver)
                     ?? throw new PDOException($"Driver '{driver}' not found"); // TODO: resources

            try
            {
                this.m_con = Driver.OpenConnection(connstring, username, password, options);
            }
            catch (Exception e)
            {
                throw new PDOException(e.Message);
            }

            this.m_attributes[PDO_ATTR.ATTR_SERVER_VERSION] = (PhpValue)this.m_con.ServerVersion;
            this.m_attributes[PDO_ATTR.ATTR_DRIVER_NAME]    = (PhpValue)Driver.Name;
            this.m_attributes[PDO_ATTR.ATTR_CLIENT_VERSION] = (PhpValue)Driver.ClientVersion;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a PDO instance representing a connection to a database
        /// </summary>
        /// <param name="dsn">The DSN.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="options">The options.</param>
        public void __construct(string dsn, string username = null, string password = null, PhpArray options = null)
        {
            int doublecolon = dsn.IndexOf(':');

            if (doublecolon < 0)
            {
                // lookup dsn alias
                var dsn2 = _ctx.Configuration.Get <PDOConfiguration>()?.Dsn[dsn];
                if (dsn2 == null)
                {
                    throw new PDOException("Invalid DSN Alias.");
                }

                dsn         = dsn2;
                doublecolon = dsn.IndexOf(':');

                if (doublecolon <= 0)
                {
                    throw new PDOException("Invalid DSN.");
                }
            }

            //
            var driver     = dsn.Remove(doublecolon);
            var connstring = dsn.AsSpan(doublecolon + 1);

            // resolve the driver:
            Driver = PDOEngine.TryGetDriver(driver) ?? throw new PDOException($"Driver '{driver}' not found"); // TODO: resources

            try
            {
                // create connection object:
                _connection = new PdoConnectionResource(this, Driver.OpenConnection(connstring, username, password, options));
            }
            catch (Exception e)
            {
                // PDO construct always throws PDOException on error:
                throw new PDOException(e.Message);
            }
        }