/// <summary> /// Sets <see cref="PDO.PDO_ATTR.ATTR_STRINGIFY_FETCHES"/> attribute value. /// </summary> /// <param name="pdo">Containing <see cref="PDO"/> object reference.</param> /// <param name="stringify">Whether to stringify fetched values.</param> /// <returns>Value indicating the attribute was set succesfuly.</returns> public virtual bool TrySetStringifyFetches(PDO pdo, bool stringify) { // NOTE: this method should be removed and stringify handled when actually used in PdoResultResource.GetValues() pdo.Stringify = stringify; return(true); }
/// <summary> /// Empty ctor. /// </summary> protected PDOStatement() { m_pdo = null; m_stmt = null; m_options = PhpArray.Empty; m_cmd = null; }
/// <summary> /// Executes a prepared statement /// </summary> /// <param name="input_parameters">An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.</param> /// <returns>Returns TRUE on success or FALSE on failure</returns> public virtual bool execute(PhpArray input_parameters = null) { if (Result != null) { // reusing command Connection.ClosePendingReader(); } // parameters BindParameters(_cmd.Parameters, input_parameters); // execute Result = Connection.ExecuteCommand(_cmd, convertTypes: true, parameters: null /*already set*/, skipResults: false); // handle error: if (Connection.LastException == null) { // TODO: write-back output parameters from _cmd: } else { PDO.HandleError(Connection.LastException); return(false); } return(Result != null); }
/// <summary> /// Initializes a new instance of the <see cref="PDOStatement" /> class. /// </summary> /// <param name="pdo">The pdo.</param> /// <param name="statement">The statement.</param> /// <param name="driver_options">The driver options.</param> internal PDOStatement(PDO pdo, string statement, PhpArray driver_options) { this.m_pdo = pdo; this.m_stmt = statement; this.m_options = driver_options ?? PhpArray.Empty; this.m_cmd = pdo.CreateCommand(this.m_stmt); this.SetDefaultAttributes(); }
/// <inheritDoc /> public override string GetLastInsertId(PDO pdo, string name) { //http://php.net/manual/en/function.db2-last-insert-id.php#98361 //https://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.sql.ref.doc/doc/r0004231.html using (var cmd = pdo.CreateCommand("SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1")) { object value = cmd.ExecuteScalar(); return(value?.ToString()); } }
protected PDOStatement(Context ctx) { Debug.Assert(ctx != null); _ctx = ctx; m_pdo = null; m_stmt = null; m_options = PhpArray.Empty; m_cmd = null; }
public DbCommand CreateCommand(string commandText) { var dbCommand = Connection.CreateCommand(); dbCommand.CommandText = commandText; dbCommand.Transaction = PDO.CurrentTransaction; dbCommand.CommandTimeout = (PDO.TryGetAttribute(PDO_ATTR.ATTR_TIMEOUT, out var timeout) ? (int)timeout : 30) * 1000; LastCommand = dbCommand; return(dbCommand); }
/// <summary> /// 2 phase ctor. /// Prepares the command. /// </summary> /// <param name="pdo"></param> /// <param name="queryString"></param> /// <param name="options">Driver options. Optional.</param> internal void Prepare(PdoConnectionResource pdo, string queryString, PhpArray options) { pdo.ClosePendingReader(); // initialize properties this.Connection = pdo ?? throw new ArgumentNullException(nameof(pdo)); this.queryString = queryString; // var actualQuery = Driver.RewriteCommand(queryString, options, out bound_param_map); _cmd = PDO.CreateCommand(actualQuery); //_cmd.Prepare(); // <-- compiles the query, needs parameters to be bound }
/// <summary> /// Initializes a new instance of the <see cref="PDOStatement" /> class. /// </summary> /// <param name="ctx">The php context.</param> /// <param name="pdo">The PDO statement is created for.</param> /// <param name="statement">The statement.</param> /// <param name="driver_options">The driver options.</param> internal PDOStatement(Context ctx, PDO pdo, string statement, PhpArray driver_options) { if (pdo.HasExecutedQuery) { if (!pdo.StoreLastExecutedQuery()) { pdo.HandleError(new PDOException("Last executed PDOStatement result set could not be saved correctly.")); } } this.m_pdo = pdo; this._ctx = ctx; this.m_stmt = statement; this.m_options = driver_options ?? PhpArray.Empty; this.m_cmd = pdo.CreateCommand(this.m_stmt); PrepareStatement(); this.SetDefaultAttributes(); }
/// <summary> /// Opens a DataReader. /// </summary> /// <param name="pdo">The pdo.</param> /// <param name="cmd">The command.</param> /// <param name="cursor">The cursor configuration.</param> /// <returns></returns> public virtual DbDataReader OpenReader(PDO pdo, DbCommand cmd, PDO.PDO_CURSOR cursor) { return(cmd.ExecuteReader()); }
/// <summary> /// Gets the driver specific attribute value /// </summary> /// <param name="pdo">The pdo.</param> /// <param name="attribute">The attribute.</param> /// <returns></returns> public virtual PhpValue GetAttribute(PDO pdo, PDO.PDO_ATTR attribute) { return(PhpValue.Null); }
/// <summary> /// Gets the last insert identifier. /// </summary> /// <param name="pdo">Reference to corresponding <see cref="PDO"/> instance.</param> /// <param name="name">The name.</param> /// <returns></returns> public abstract string GetLastInsertId(PDO pdo, string name);
private protected void HandleError(ErrorInfo error) { _lastError = error; PDO.HandleError(error); }
/// <summary> /// Tries to set a driver specific attribute value. /// </summary> /// <param name="pdo">Containing <see cref="PDO"/> object reference.</param> /// <param name="attributes">The current attributes collection.</param> /// <param name="attribute">The attribute to set.</param> /// <param name="value">The value.</param> /// <returns>true if value is valid, or false if value can't be set.</returns> public virtual bool TrySetAttribute(PDO pdo, Dictionary <PDO.PDO_ATTR, PhpValue> attributes, int attribute, PhpValue value) { return(false); }
public PdoConnectionResource(PDO pdo, DbConnection connection) : base(connection.ConnectionString, nameof(PdoConnectionResource)) { this.PDO = pdo; this.Connection = connection ?? throw new ArgumentNullException(nameof(connection)); }
/// <summary> /// Fetches the specified fetch style. /// </summary> /// <param name="fetch_style">Controls how the next row will be returned to the caller. This value must be one of the PDO::FETCH_* constants.</param> /// <param name="cursor_orientation">This value determines which row will be returned to the caller.</param> /// <param name="cursor_offet">Relative or absolute position move for the cursor.</param> /// <returns>The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure.</returns> public virtual PhpValue fetch(PDO.PDO_FETCH fetch_style = PDO_FETCH.Default /*0*/, PDO_FETCH_ORI cursor_orientation = PDO_FETCH_ORI.FETCH_ORI_NEXT /*0*/, int cursor_offet = 0) { PDO.ClearError(); if (cursor_orientation != PDO_FETCH_ORI.FETCH_ORI_NEXT) // 0 { throw new NotImplementedException(cursor_orientation.ToString()); } if (Result == null) { throw new InvalidOperationException(); } try { var how = fetch_style != PDO_FETCH.Default ? fetch_style : _default_fetch_type; var flags = how & PDO_FETCH.Flags; switch (how & ~PDO_FETCH.Flags) { case PDO_FETCH.Default: case PDO_FETCH.FETCH_BOTH: return(Result.FetchArray(true, true) ?? PhpValue.False); case PDO_FETCH.FETCH_ASSOC: return(Result.FetchAssocArray() ?? PhpValue.False); case PDO_FETCH.FETCH_NUM: return(Result.FetchArray(true, false) ?? PhpValue.False); case PDO_FETCH.FETCH_OBJ: return(ObjectOrFalse(Result.FetchStdClass())); case PDO_FETCH.FETCH_BOUND: return(FetchBound()); case PDO_FETCH.FETCH_COLUMN: return(fetchColumn(_fetch_column)); case PDO.PDO_FETCH.FETCH_CLASS: return(ObjectOrFalse(FetchClass(_default_fetch_class, _default_fetch_class_args))); case PDO_FETCH.FETCH_NAMED: return(this.ReadNamed()); //case PDO_FETCH.FETCH_LAZY: // return new PDORow( ... ) reads columns lazily //case PDO_FETCH.FETCH_INTO: //case PDO_FETCH.FETCH_FUNC: //case PDO_FETCH.FETCH_KEY_PAIR: default: throw new NotImplementedException($"fetch {how}"); } } catch (System.Exception ex) { PDO.HandleError(ex); return(PhpValue.False); } }
/// <summary> /// Gets the driver specific attribute value /// </summary> /// <param name="pdo">The pdo.</param> /// <param name="attribute">The attribute.</param> /// <returns></returns> public virtual PhpValue GetAttribute(PDO pdo, int attribute) { return(PhpValue.Null); }
private protected void ClearError() { _lastError = default; PDO.ClearError(); }
/// <inheritDoc /> public virtual PDOStatement PrepareStatement(PDO pdo, string statement, PhpArray driver_options) { PDOStatement stmt = new PDOStatement(pdo, statement, driver_options); return(stmt); }