Execute the request of type delete insert update

@param string $sqlstatement
@param array $data
@return mixed
480421
private static function executePrepareQuery($sqlstatement, array $data = [])
    {
        $pdostatement = static::$adapter
            ->getConnection()
            ->prepare($sqlstatement);

        static::$adapter->bind(
            $pdostatement,
            Sanitize::make($data, true)
        );

        $pdostatement->execute();

        $r = $pdostatement->rowCount();

        return $r;
    }
702960
public function throwExceptionIfErrors($sqlStatement)
    {
        if ((int) $sqlStatement->errorCode()) {
            throw new \Exception(
                print_r($sqlStatement->errorInfo(), true)
            );
        }

        return false;
    }
567356
public static function execute($conn, $sql) {
    $conn = is_array($conn) ? self::connect($conn) : $conn;
    $result = $conn->query($sql);
    if (!$result) {
      throw new SqlException("Cannot execute $sql: " . $conn->error);
    }

    if ($result && $result !== TRUE) {
      $result->free_result();
    }

  }
378819
public function insert( $query, array $data = [ ] )
    {
        $id = NULL;

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

        if ( FALSE === $statement )
        {
            $this->close();

            throw new DatabaseException( 'Failed to create prepared statement.' );
        }

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

        $this->query = $statement->queryString;

        if ( FALSE !== $result )
        {
            $result = ( int ) $this->database->lastInsertId();
        }

        return $result;
    }
353911
private function doInsert(array $statements, array $data, $type): array
    {
        $table = end($statements['tables']);

        $bindings = $keys = $values = [];

        foreach ($data as $key => $value) {
            $keys[] = $key;
            if ($value instanceof Raw) {
                $values[] = (string)$value;
            } else {
                $values[] = '?';
                $bindings[] = $value;
            }
        }

        $sqlArray = [
            $type . ' INTO',
            $this->wrapSanitizer($table),
            '(' . $this->arrayStr($keys) . ')',
            'VALUES',
            '(' . $this->arrayStr($values, ',', false) . ')',
        ];

        if (isset($statements['onduplicate']) === true) {

            if (\count($statements['onduplicate']) < 1) {
                throw new Exception('No data given.', 4);
            }

            [$updateStatement, $updateBindings] = $this->getUpdateStatement($statements['onduplicate']);
            $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement;
            $bindings = array_merge($bindings, $updateBindings);

        }

        $sql = $this->concatenateQuery($sqlArray);

        return compact('sql', 'bindings');
    }
447302
public static function insert(array $data)
    {
        $self = static::getInstance();

        $data = static::filterColumns($data);

        if (!\count($data)) {
            throw new DbException(
                "Invalid field names of table `{$self->name}`. Please check use of `insert()` method"
            );
        }

        $table = DbProxy::quoteIdentifier($self->name);

        $sql = "INSERT INTO $table SET " . implode(',', self::prepareStatement($data));
        $result = DbProxy::query($sql, array_values($data));
        if (!$result) {
            return null;
        }

        /**
         * If a sequence name was not specified for the name parameter, PDO::lastInsertId()
         * returns a string representing the row ID of the last row that was inserted into the database.
         *
         * If a sequence name was specified for the name parameter, PDO::lastInsertId()
         * returns a string representing the last value retrieved from the specified sequence object.
         *
         * If the PDO driver does not support this capability, PDO::lastInsertId() triggers an IM001 SQLSTATE.
         */
        return DbProxy::handler()->lastInsertId($self->sequence);
    }
1296889
protected function doInsert(array $data = [])
    {
        if (empty($data)) {
            $this->insertEmptyRecord();
            return $this->db->lastInsertId();
        }

        if (!is_int(key($data))) {
            // single record is inserted
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], array_keys($data)));
            $params   = implode(', ', array_fill(0, count($data), '?'));
            $bindings = array_values($data);
        }
        else {
            // Bulk insert...
            $keys = [];
            foreach ($data as $row) {
                if (empty($row)) {
                    throw new InvalidArgumentException('Cannot insert an empty record on bulk mode.');
                }
                $keys = array_merge($keys, $row);
            }
            $keys     = array_keys($keys);
            $columns  = implode(', ', array_map([$this->db, 'quoteName'], $keys));
            $params   = implode(', ', array_fill(0, count($keys), '?'));
            $bindings = [];
            $temp     = [];
            foreach ($data as $i => $row) {
                foreach ($keys as $key) {
                    $bindings[] = isset($row[$key]) ? $row[$key] : null;
                }
                $temp[] = $params;
            }
            $params = implode('), (', $temp);
        }

        $table = implode(', ', $this->from);

        /** @noinspection SqlDialectInspection */
        $query = "INSERT INTO $table ($columns) VALUES ($params)";

        $this->db->exec($query, $bindings);

        return $this->db->lastInsertId();
    }
1347873
public static function selectOne($sqlstatement, array $data = [])
    {
        static::verifyConnection();

        if (!preg_match("/^select\s.+?\sfrom\s.+;?$/i", $sqlstatement)) {
            throw new DatabaseException(
                'Syntax Error on the Request',
                E_USER_ERROR
            );
        }

        // Prepare query
        $pdostatement = static::$adapter
            ->getConnection()
            ->prepare($sqlstatement);

        // Bind data
        static::$adapter->bind($pdostatement, $data);

        // Execute query
        $pdostatement->execute();

        return Sanitize::make($pdostatement->fetch());
    }
961249
public function insert($table_name, array $data) {
            Event::dispatch(EventData::forBefore(Event::BEFORE_INSERT, $table_name, $data), $data);

            $q = $this->config->getSystemIdentifierQuote();
            $fields = [];
            $data_values = "";
            $statement_params = [];
            $null_params = [];

            foreach ($data as $field_name => $value) {
                $fields[] = "\n$q" . $field_name . $q;
                if (strtolower($value) == 'null' || is_null($value)) {
                    $data_values .= 'NULL,';
                    $null_params[ $field_name ] = 'NULL';
                } else {
                    $data_values .= ':' . $field_name . ',';
                    $statement_params[ ':' . $field_name ] = $value;
                }
            }

            $full_param_list = array_merge($statement_params, $null_params);

            $fields = implode(',', $fields);
            $data_values = substr($data_values, 0, -1);
            $sql_query = "INSERT INTO \n$q" . $table_name . "$q ( " . $fields . " )\n VALUES (" . $data_values . ")";
            if ($this->config->db_type === 'pgsql') {
                $sql_query .= "\nRETURNING id;\n";
            }

            try {
                $stmt = $this->db_connection->prepare($sql_query);
                if ($statement_params) {
                    foreach ($statement_params as $k => $v) {
                        $stmt->bindValue($k, $v);
                    }
                }
                $stmt->execute();
            } catch (Exception $ex) {
                Event::dispatch(EventData::forException($ex, $sql_query, $full_param_list));
                throw new QueryException($ex->getMessage(), $ex->getCode(), $ex);
            }

            if ($this->config->db_type === 'pgsql') {
                $insert_id = $stmt->fetchColumn(0);
            } else {
                $insert_id = $this->db_connection->lastInsertId();
            }

            $rows_affected = empty($insert_id) ? 0 : 1;
            Event::dispatch(EventData::forAfter(Event::AFTER_INSERT, $table_name, $full_param_list, $sql_query, $rows_affected, $insert_id));

            return $insert_id;
        }
1068321
public static function statement($sqlstatement)
    {
        static::verifyConnection();

        if (!preg_match(
            "/^((drop|alter|create)\s+(?:(?:temp|temporary)\s+)?table|truncate|call)(\s+)?(.+?);?$/i",
            $sqlstatement
        )) {
            throw new DatabaseException(
                'Syntax Error on the Request',
                E_USER_ERROR
            );
        }

        return static::$adapter
            ->getConnection()
            ->exec($sqlstatement) === 0;
    }
351649
public function insert($table, array $data)
    {
        $nameFields = join(',', array_keys($data));
        $preparedValues = $this->prepareValues($data);
        $keyValues = join(",", array_keys($preparedValues));
        $queryString = "INSERT INTO {$table} ({$nameFields}) VALUES ({$keyValues});";

        $this->query($queryString, $preparedValues);

        return $this->getInsertId();
    }
1326744
public function update(array $data) {
        $statement = new UpdateStatement($data);
        $statement->setStatementContext($this->statementContext);
        $statement->setCondition($this->condition);
        $sql = $statement->parse();

        $rowCount = $this->connection->execute($sql, $this->statementContext->getParameters());

        $this->clear();

        return $rowCount;
    }
1062748
public static function select($sqlstatement, array $data = [])
    {
        static::verifyConnection();

        if (!preg_match(
            "/^(select\s.+?\sfrom\s.+;?|desc\s.+;?)$/i",
            $sqlstatement
        )) {
            throw new DatabaseException(
                'Syntax Error on the Request',
                E_USER_ERROR
            );
        }

        $pdostatement = static::$adapter
            ->getConnection()
            ->prepare($sqlstatement);

        static::$adapter->bind(
            $pdostatement,
            Sanitize::make($data, true)
        );

        $pdostatement->execute();

        return Sanitize::make($pdostatement->fetchAll());
    }
95187
public function update(array $statements, array $data): array
    {
        if (\count($data) === 0) {
            throw new Exception('No data given.', 4);
        }

        $table = end($statements['tables']);

        // UPDATE
        [$updateStatement, $bindings] = $this->getUpdateStatement($data);

        // WHERE
        [$whereCriteria, $whereBindings] = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE');

        $sqlArray = [
            'UPDATE',
            $this->wrapSanitizer($table),
            $this->buildQueryPart(static::QUERY_PART_JOIN, $statements),
            'SET ' . $updateStatement,
            $whereCriteria,
            $this->buildQueryPart(static::QUERY_PART_GROUPBY, $statements),
            $this->buildQueryPart(static::QUERY_PART_ORDERBY, $statements),
            $this->buildQueryPart(static::QUERY_PART_LIMIT, $statements),
            $this->buildQueryPart(static::QUERY_PART_OFFSET, $statements),
        ];

        $sql = $this->concatenateQuery($sqlArray);

        $bindings = array_merge($bindings, $whereBindings);

        return compact('sql', 'bindings');
    }
998182
public function insertRecord($table, array $data)
    {
        $dbHandle = $this->getDatabaseConnection();

        $fieldSet = array_keys($data);

        // Prepare sets
        $valueSet = array();
        foreach ($data as $key => $value) {
            $valueSet[':' . $key] = $value;
        }

        if (count($fieldSet) == 0 || count($valueSet) == 0) {
            throw new \Exception('Missing data for insert query', 1363894664);
        }

        $fieldSet[] = 'tstamp';
        $valueSet[':tstamp'] = time();

        $fieldSet[] = 'crdate';
        $valueSet[':crdate'] = time();

        $query = 'INSERT INTO ' . $table . ' (`' . implode('`,`', $fieldSet) . '`)
                  VALUES (' . implode(', ', array_keys($valueSet)) . ')';

        $statement = $dbHandle->prepare($query);
        $executeResult = $statement->execute($valueSet);

        $this->checkQueryError($statement, $executeResult, $valueSet);
        return $dbHandle->lastInsertId();
    }
1255321
public function write($id, $data)
    {
        $this->app['db']
            ->delete(self::TABLENAME)
            ->where('id', $id)
            ->execute();

        return $this->app['db']
            ->insert([
                'id' => $id,
                'access' => time(),
                'session_data' => $data, ])
            ->into(self::TABLENAME)
            ->execute() instanceof PDOStatement;
    }
130656
protected function _insert(array $data)
    {
        $data = $this->_setData($data);
        $sql = Sql::createSql($this->getAdapter())->insert($this->getTableName());
        $event = new Save([
            'action' => Save::INSERT,
            'data' => $data,
            'abort' => false
        ]);
        $this->getEventManager()->trigger(Save::BEFORE_SAVE, $this, $event);
        if ($event->abort) {
            return false;
        }
        $result = $sql->set($event->data)->execute();

        if ($result > 0) {
            $pmk = $this->getPrimaryKey();
            $this->$pmk = $this->getAdapter()->getLastInsertId();
            $this->getEventManager()->trigger(Save::AFTER_SAVE, $this, $event);
        }
        return $result > 0;
    }