示例#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);
                    }
                }
            }
        }
示例#2
0
 /// <summary>
 /// Registers all referenced PDO drivers.
 /// </summary>
 public static void RegisterAllDrivers()
 {
     foreach (var driver in Context.CompositionContext.GetExports <IPDODriver>())
     {
         PDOEngine.RegisterDriver(driver);
     }
 }
示例#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)
        {
            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;
        }
示例#4
0
文件: PDO.cs 项目: tobihille/peachpie
        /// <inheritDoc />
        public void __construct(string dsn, string username = null, string password = null, PhpArray options = null)
        {
            this.SetDefaultAttributes();

            string[] items = dsn.Split(new[] { ':' }, 2);

            if (items[0].Equals("uri", StringComparison.Ordinal))
            {
                //Uri mode
                Uri uri;
                if (Uri.TryCreate(items[1], UriKind.Absolute, out 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");
                }
            }

            if (items.Length == 1)
            {
                //Alias mode
                throw new NotImplementedException("PDO DSN alias not implemented");
                //replace DSN alias with value
            }

            //DSN mode
            this.m_driver = PDOEngine.GetDriver(items[0]);
            if (this.m_driver == null)
            {
                throw new PDOException(string.Format("Driver '{0}' not found", items[0]));
            }

            this.m_extensionMethods = this.m_driver.GetPDObjectExtensionMethods();

            if (this.m_driver.Name == "mysql")
            {
                items[1] = PDOMySQLWrapper.convertDSN(items[1]);
            }

            this.m_con = this.m_driver.OpenConnection(items[1], username, password, options);
            this.m_attributes.Set(PDO_ATTR.ATTR_SERVER_VERSION, (PhpValue)this.m_con.ServerVersion);
            this.m_attributes.Set(PDO_ATTR.ATTR_DRIVER_NAME, (PhpValue)this.m_driver.Name);
            this.m_attributes.Set(PDO_ATTR.ATTR_CLIENT_VERSION, (PhpValue)this.m_driver.ClientVersion);
        }
示例#5
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);
            }
        }
示例#6
0
 /// <summary>
 /// Get the known PDO drivers
 /// </summary>
 /// <returns></returns>
 public static PhpArray pdo_drivers()
 {
     return(PhpArray.New(PDOEngine.GetDriverNames()));
 }
        /// <summary>
        /// Get the known PDO drivers
        /// </summary>
        /// <returns></returns>
        public static PhpArray pdo_drivers()
        {
            var phpNames = PDOEngine.GetDriverNames().Select(d => PhpValue.Create(d)).ToArray();

            return(PhpArray.New(phpNames));
        }
 /// <summary>
 /// Get the known PDO drivers
 /// </summary>
 /// <returns></returns>
 public static PhpArray pdo_drivers() => new PhpArray(PDOEngine.GetDriverNames());