/// <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); } } } }
static bool TryGetUriDriver(string driverName, out PDODriver driver) { if (driverName == "uri") { throw new NotImplementedException("PDO uri DSN not implemented"); //// Uri mode //if (Uri.TryCreate(connstring.ToString(), UriKind.Absolute, out var uri)) //{ // if (uri.Scheme.Equals("file", StringComparison.Ordinal)) // { // //return // } // else // { // throw new PDOException("PDO DSN as URI does not support other schemes than 'file'"); // } //} //else //{ // throw new PDOException("Invalid uri in DSN"); //} } driver = null; return(false); }
/// <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); } }
public PDORegistrator() { Context.RegisterConfiguration(new PDOConfiguration()); PDODriver.RegisterAllDrivers(); }