Add bindings to a statement. Cast integers, null, bool and string.

@param PDOStatement $statement
@param array $bindings
320768
public static function bindToStatement(PDOStatement $statement, array $bindings=[])
    {
        foreach ($bindings as $key=>$value) {

            $casted = is_bool($value) ? (int)$value : $value;

            $statement->bindValue(
                is_string($key) ? $key : $key + 1,
                $casted,
                is_int($casted) ? PDO::PARAM_INT : ($casted === null ? PDO::PARAM_NULL : PDO::PARAM_STR)
            );

        }
    }
563387
public function statement($query, $bindings = [])
    {
        $new_query = $this->bindParams($query, $bindings, true);

        return $this->unprepared($new_query);
    }
1298376
public function prepareBindingsForUpdate(array $bindings, array $values)
	{
		$index = 0;

		foreach ($values as $column => $value) {
			if ($this->isJsonSelector($column) && is_bool($value)) {
				unset($bindings[$index]);
			}

			$index++;
		}

		return $bindings;
	}
1309318
public function affectingStatement( $query, $bindings = [] )
	{
		$new_query = $this->bind_params( $query, $bindings, true );

		$result = $this->db->query( $new_query );

		if ( $result === false || $this->db->last_error ) {
			throw new QueryException( $new_query, $bindings, new \Exception( $this->db->last_error ) );
		}

		return intval( $result );
	}
1255365
public function criteriaOnly(array $statements, $bindValues = true): array
    {
        $sql = $bindings = [];
        if (isset($statements['criteria']) === false) {
            return compact('sql', 'bindings');
        }

        [$sql, $bindings] = $this->buildCriteria($statements['criteria'], $bindValues);

        return compact('sql', 'bindings');
    }
954997
public function execute($query, $bindings = [])
    {
        if (strtolower(substr($query, 0, 6)) === "select" && $this->forcePrefix) {
            $query = $this->forcePrefix . $query;
        }

        return $this->run($query, $bindings, function ($query, $bindings) {
            if ($this->pretending()) {
                return true;
            }

            $statement = $this->getPdo()->prepare($query);

            $this->bindValues($statement, $bindings);

            $statement->execute();
            return $statement;
        });
    }
1255282
protected function prepared(PDOStatement $statement)
    {
        $statement->setFetchMode($this->fetchMode);

        $this->event(new Events\StatementPrepared(
            $this, $statement
        ));

        return $statement;
    }
994671
protected function insertExecute(string $statement, array $values): ?array {
		
		// our insert method should return either a single ID or a list of created IDs.
		// we can use the ExtendedPdo's fetchAffected method to determine how many were
		// inserted and then use that to determine our return value.  if nothing at all
		// was inserted, we return null.
		
		$affected = $this->dbConn->fetchAffected($statement, array_values($values));
		
		if ($affected === 0) {
			return null;
		}
		
		if ($affected === 1) {
			return [$this->getInsertedId()];
		}
		
		// if we haven't returned, then we inserted more than 1 row.  we can construct a
		// list of the IDs that were created by using the last ID and range as follows.
		// to identify our first ID, it would seem like $lastId - $affected would be enough,
		// but if we inserted 5 rows ending at 10, 10 - 5 = 5 and we don't want a range to
		// be [10,9,8,7,6,5] because that's six IDs.  so, we want to do 10 - (5 - 1) which
		// would result in [10,9,8,7,6] which is the correct response.
		
		$lastId = $this->getInsertedId();
		$firstId = $lastId - ($affected - 1);
		return range($lastId, $firstId);
	}
1098028
protected function execute($sql, $bindings = array())
		{
			$bindings = (array) $bindings;

			// Since expressions are injected into the query as strings, we need to
			// remove them from the array of bindings. After we have removed them,
			// we'll reset the array so there aren't gaps in the keys.
			$bindings = array_values(array_filter($bindings, function($binding) {
				return ! $binding instanceof Expression;
			}));

			$sql = $this->grammar()->shortcut($sql, $bindings);

			$statement = $this->pdo->prepare($sql);

			// Every query is timed so that we can log the executinon time along
			// with the query SQL and array of bindings. This should be make it
			// convenient for the developer to profile performance.
			$time = microtime(true);

			$result = $statement->execute($bindings);

			$time = number_format((microtime(true) - $time) * 1000, 2);

			// Once we have execute the query, we log the SQL, bindings, and
			// execution time in a static array that is accessed by all of
			// the connections used by the application.
			$this->log($sql, $bindings, $time);

			return array($statement, $result);
		}
1055009
public function fetchAssocs(string $statement, array $bindings = [], $indexKey = null): array
    {
        $data = [];
        $sth = $this->execute($statement, $bindings);

        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
            if ($indexKey) {
                $data[$row[$indexKey]] = $row;
            } else {
                $data[] = $row;
            }
            // $data[current($row)] = $row;
        }

        $this->freeResource($sth);

        return $data;
    }
1071588
public function addBinding($field, $values) {
        if (!is_array($values)) {
            $values = [$values];
        }

        foreach ($values as $value) {
            if ($value instanceof Predicate) {
                $this->resolvePredicate($value);

            } else if ($value instanceof SubQuery) {
                $this->resolveSubQuery($value);

            } else {
                $this->_bindings[] = [
                    'field' => $field,
                    'value' => $value
                ];
            }
        }

        return $this;
    }
859881
protected function bindValues(\PDOStatement $statement, array $values)
    {
        $number = 1;

        foreach ($values as $name => $value) {
            $this->bindValue($statement, is_string($name) ? $name : $number, $value);
            $number += 1;
        }
    }
51704
public function insertNotExists(array $bindings, $value, $ttl = null)
    {
        return $this->insert($bindings, $value, $ttl, false);
    }
1098861
public function prepareBindingsForUpdate(array $bindings, array $values)
    {
        $bindingsWithoutJoin = $bindings;
        unset($bindingsWithoutJoin['join']);

        return array_values(
            array_merge($bindings['join'], $values, array_flatten($bindingsWithoutJoin))
        );
    }
867594
public function executeProcedure($procedureName, array $bindings = [])
    {
        $stmt = $this->createStatementFromProcedure($procedureName, $bindings);

        $stmt = $this->addBindingsToStatement($stmt, $bindings);

        return $stmt->execute();
    }
554201
protected function bindParameter(PDOStatement $statement, int $key, $value): void
	{
		if(is_string($value))
		{
			$type = PDO::PARAM_STR;
		}
		elseif(is_int($value))
		{
			$type = PDO::PARAM_INT;
		}
		elseif(is_bool($value))
		{
			$type = PDO::PARAM_BOOL;
		}
		elseif(is_null($value))
		{
			$type = PDO::PARAM_NULL;
		}
		elseif(is_object($value) && $value instanceof TypeInterface)
		{
			$value = $value->getValue();

			$type = $value->getType();
		}
		elseif(is_resource($value))
		{
			$type = PDO::PARAM_LOB;
		}
		else
		{
			$type = PDO::PARAM_STR;
		}

		$statement->bindValue($key + 1, $value, $type);
	}
1362423
public static function executeStatement(
        PDOStatement $statement,
        array $params = null
    ) {
        //Add the statement to the list of those run
        static::addStatement($statement);

        //Add bindings to the statement
        if (is_array($params)) {
            $statement->getBindings()
                ->hydrateFromArray($params);
        }

        $result = static::getOrCreateResultAt(static::numQueries());

        //Add the result to the statement
        $statement->setResult($result);

        //If a transaction is open, add the statement to it
        if (static::inTransaction()) {
            static::currentTransaction()->addStatement($statement);
        }

        //Increment number of queries run
        static::incrementNumQueries();

        if (static::shouldLogBacktrace()) {
            $statement->setBacktrace(
                static::makeBacktraceCollection(
                    debug_backtrace(
                        DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS,
                        static::resolveBacktraceLimit()
                    )
                )
            );
        }
    }