PDO4You\PDO4You::executeQuery PHP Method

executeQuery() private static method

Method for manipulation of records in the database
private static executeQuery ( string $jarr, string $type, string $use ) : array
$jarr string SQL statement in JSON/ARRAY format
$type string Type of operation in the database
$use string OPTIONAL Name of the database defined as a new connection instance
return array Returns an array with the number of rows affected by the operation
    private static function executeQuery($jarr, $type, $use)
    {
        $total = null;
        try {
            if (is_null($jarr)) {
                throw new \PDOException(self::$exception['no-instruction']);
            }
            if (!is_null($use)) {
                self::setInstance($use);
            }
            $pdo = self::$instance;
            if (!$pdo instanceof \PDO) {
                throw new \PDOException(self::$exception['no-instance']);
            } else {
                $pdo->beginTransaction();
                try {
                    $jarr = is_array($jarr) ? $jarr : self::parseJSON($jarr);
                    if ($type == 'insert') {
                        foreach ($jarr['query'] as $field) {
                            $sql = 'INSERT INTO ' . $field['table'] . ' (';
                            foreach ($field['values'] as $key => $val) {
                                $sql .= ', ' . $key;
                            }
                            $sql = preg_replace('/, /', '', $sql, 1);
                            $sql .= ') VALUES (';
                            foreach ($field['values'] as $key => $val) {
                                $sql .= ', ?';
                            }
                            $sql .= ')';
                            $sql = preg_replace('/\\(, /', '(', $sql, 1);
                            $pre = $pdo->prepare($sql);
                            $k = 1;
                            foreach ($field['values'] as $key => $val) {
                                $pre->bindValue($k++, $val);
                            }
                            $pre->execute();
                            $total[] = $pre->rowCount();
                        }
                    }
                    if ($type == 'update') {
                        foreach ($jarr['query'] as $index => $field) {
                            $sql = 'UPDATE ' . $field['table'] . ' SET ';
                            foreach ($field['values'] as $key => $val) {
                                $sql .= ', ' . $key . ' = ?';
                            }
                            $sql = preg_replace('/, /', '', $sql, 1);
                            $sql .= ' WHERE ';
                            foreach ($field['where'] as $key => $val) {
                                $sql .= ' AND ' . $key . ' = ?';
                            }
                            $sql = preg_replace('/ AND /', '', $sql, 1);
                            $pre = $pdo->prepare($sql);
                            $k = 1;
                            foreach ($field['values'] as $key => $val) {
                                $pre->bindValue($k++, $val);
                            }
                            $j = $k;
                            foreach ($field['where'] as $key => $val) {
                                $pre->bindValue($j++, $val);
                            }
                            $pre->execute();
                            $total[] = $pre->rowCount();
                        }
                    }
                    if ($type == 'delete') {
                        foreach ($jarr['query'] as $index => $field) {
                            $sql = 'DELETE FROM ' . $field['table'] . ' WHERE ';
                            foreach ($field['where'] as $key => $val) {
                                $sql .= ' AND ' . $key . ' = ?';
                            }
                            $sql = preg_replace('/ AND /', '', $sql, 1);
                            $pre = $pdo->prepare($sql);
                            $k = 1;
                            foreach ($field['where'] as $key => $val) {
                                $pre->bindValue($k++, $val);
                            }
                            $pre->execute();
                            $total[] = $pre->rowCount();
                        }
                    }
                    self::$rowCount = $total;
                    $pdo->commit();
                } catch (\PDOException $e) {
                    $pdo->rollback();
                    throw $e;
                }
            }
        } catch (\PDOException $e) {
            self::getErrorInfo($e);
            self::stackTrace($e);
        }
        $pdo = null;
        return $total;
    }