Skip to content

Commit

Permalink
Change Readme + test for update method
Browse files Browse the repository at this point in the history
  • Loading branch information
AgelxNash authored and vagrant committed Jun 3, 2018
1 parent 227237e commit 2863853
Show file tree
Hide file tree
Showing 10 changed files with 854 additions and 145 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion README.md
@@ -1,5 +1,5 @@
## DBAPI Evolution
[![CMS MODX Evolution](https://img.shields.io/badge/CMS-MODX%20Evolution-brightgreen.svg)](https://github.com/modxcms/evolution) [![Build Status](https://api.travis-ci.org/AgelxNash/modx-evo-database.svg?branch=master)](https://travis-ci.org/AgelxNash/modx-evo-database) [![Code quality](https://img.shields.io/scrutinizer/g/AgelxNash/modx-evo-database.svg?maxAge=2592000)](https://scrutinizer-ci.com/g/AgelxNash/modx-evo-database/) [![Code Coverage](https://scrutinizer-ci.com/g/AgelxNash/modx-evo-database/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/AgelxNash/modx-evo-database/?branch=master) [![Total Downloads](https://poser.pugx.org/agelxnash/modx-evo-database/d/total.png)](https://packagist.org/packages/agelxnash/modx-evo-database) [![License](https://img.shields.io/github/license/AgelxNash/modx-evo-database.svg?maxAge=2592000)](https://github.com/AgelxNash/modx-evo-database/blob/master/LICENSE)
[![CMS MODX Evolution](https://img.shields.io/badge/CMS-MODX%20Evolution-brightgreen.svg)](https://github.com/modxcms/evolution) [![Build Status](https://api.travis-ci.org/AgelxNash/modx-evo-database.svg?branch=master)](https://travis-ci.org/AgelxNash/modx-evo-database) [![Code quality](https://img.shields.io/scrutinizer/g/AgelxNash/modx-evo-database.svg?maxAge=2592000)](https://scrutinizer-ci.com/g/AgelxNash/modx-evo-database/) [![Code Coverage](https://scrutinizer-ci.com/g/AgelxNash/modx-evo-database/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/AgelxNash/modx-evo-database/?branch=master) [![Total Downloads](https://poser.pugx.org/agelxnash/modx-evo-database/d/total.png)](https://packagist.org/packages/agelxnash/modx-evo-database) [![License](https://gitlicense.com/badge/AgelxNash/modx-evo-database)](https://github.com/AgelxNash/modx-evo-database/blob/master/LICENSE.md)

### Example
---------
Expand Down Expand Up @@ -47,6 +47,7 @@ foreach ($DB->makeArray($result) as $item) {
<br />
<bt />
<br />
<br />
<small>
<a href="https://agel-nash.ru">https://agel-nash.ru</a>
<br />
Expand Down
113 changes: 60 additions & 53 deletions src/Database.php
@@ -1,6 +1,5 @@
<?php namespace AgelxNash\Modx\Evo\Database;

use AgelxNash\Modx\Evo\Database\Exceptions\QueryException;
use mysqli;
use mysqli_result;
use mysqli_sql_exception;
Expand All @@ -11,14 +10,13 @@ class Database implements DatabaseInterface
use Traits\DebugTrait,
Traits\SupportTrait;

protected const SAFE_LOOP_COUNT = 1000;

/**
* @var mysqli
*/
public $conn;
public $config = [];

protected $safeLoopCount = 1000;

/**
* @param string $host
Expand Down Expand Up @@ -161,7 +159,7 @@ public function isConnected() : bool
public function escape($data, $safeCount = 0)
{
$safeCount++;
if (self::SAFE_LOOP_COUNT < $safeCount) {
if ($this->safeLoopCount < $safeCount) {
throw new Exceptions\TooManyLoopsException("Too many loops '{$safeCount}'");
}
if (\is_array($data)) {
Expand Down Expand Up @@ -191,24 +189,24 @@ public function query($sql)
if (\is_array($sql)) {
$sql = implode("\n", $sql);
}
$this->lastQuery = $sql;

try {
$result = $this->getConnect()->query($sql);
$this->lastQuery = $sql;
$result = $this->getConnect()->query($this->getLastQuery());
} catch (mysqli_sql_exception $exception) {
throw new Exceptions\QueryException($exception->getMessage(), $exception->getCode());
throw (new Exceptions\QueryException($exception->getMessage(), $exception->getCode()))
->setQuery($this->getLastQuery());
}

if ($result === false) {
$this->checkLastError($sql);
$this->checkLastError($this->getLastQuery());
} else {
$tend = microtime(true);
$totalTime = $tend - $tStart;
$this->queryTime += $totalTime;
if ($this->isDebug()) {
$this->collectQuery(
$result,
$sql,
$this->getLastQuery(),
$this->executedQueries + 1,
$totalTime
);
Expand All @@ -229,6 +227,7 @@ public function query($sql)
* @throws Exceptions\ConnectException
* @throws Exceptions\QueryException
* @throws Exceptions\TableNotDefinedException
* @throws Exceptions\InvalidFieldException
*/
public function delete($table, $where = '', $orderBy = '', $limit = '')
{
Expand All @@ -250,6 +249,7 @@ public function delete($table, $where = '', $orderBy = '', $limit = '')
* @throws Exceptions\ConnectException
* @throws Exceptions\QueryException
* @throws Exceptions\TableNotDefinedException
* @throws Exceptions\InvalidFieldException
*/
public function select($fields, $tables, $where = '', $orderBy = '', $limit = '')
{
Expand All @@ -270,21 +270,25 @@ public function select($fields, $tables, $where = '', $orderBy = '', $limit = ''
* @throws Exceptions\ConnectException
* @throws Exceptions\QueryException
* @throws Exceptions\TableNotDefinedException
* @throws Exceptions\InvalidFieldException
*/
public function update($values, string $table, $where = '')
{
$table = $this->prepareFrom($table);
$values = $this->prepareValuesSet($values);
if (mb_strtoupper(mb_substr($values, 0, 4)) !== 'SET ') {
$values = 'SET ' . $values;
}
$where = $this->prepareWhere($where);

return $this->query("UPDATE {$table} SET {$values} {$where}");
return $this->query("UPDATE {$table} {$values} {$where}");
}

/**
* @param array|string $fields
* @param string $table
* @param string $fromfields
* @param string $fromtable
* @param array|string $fromFields
* @param string $fromTable
* @param array|string $where
* @param string $limit
* @return mixed
Expand All @@ -293,56 +297,78 @@ public function update($values, string $table, $where = '')
* @throws Exceptions\QueryException
* @throws Exceptions\TableNotDefinedException
* @throws Exceptions\TooManyLoopsException
* @throws Exceptions\InvalidFieldException
*/
public function insert(
$fields,
string $table,
string $fromfields = '*',
string $fromtable = '',
$fromFields = '*',
string $fromTable = '',
$where = '',
string $limit = ''
) {
$table = $this->prepareFrom($table);

if (is_scalar($fields)) {
$this->query("INSERT INTO {$table} {$fields}");
$useFields = null;
$lid = null;

if (\is_array($fields)) {
$useFields = empty($fromTable) ?
$this->prepareValues($fields) :
$this->prepareFields($fields, true);
} else {
$useFields = $fields;
}

if (empty($useFields) || ! \is_scalar($useFields) || ($useFields === '*' && ! empty($fromTable))) {
throw (new Exceptions\InvalidFieldException('Invalid insert fields'))
->setData($fields);
}

if (empty($fromTable)) {
$this->query("INSERT INTO {$table} {$useFields}");
} else {
if (empty($fromtable) && \is_array($fields)) {
$fields = $this->prepareValues($fields);
$this->query("INSERT INTO {$table} {$fields}");
if (empty($fromFields) || $fromFields === '*') {
$fromFields = $this->prepareFields($fields, true);
} else {
$fields = $this->prepareValues($fields, 2, true)['values'];
$where = $this->prepareWhere($where);
$limit = $this->prepareLimit($limit);
$this->query("INSERT INTO {$table} {$fields} SELECT {$fromfields} FROM {$fromtable} {$where} {$limit}");
$fromFields = $this->prepareFields($fromFields, true);
}

$where = $this->prepareWhere($where);
$limit = $this->prepareLimit($limit);

$lid = $this->query(
"INSERT INTO {$table} ({$useFields}) SELECT {$fromFields} FROM {$fromTable} {$where} {$limit}"
);
}
if (($lid = $this->getInsertId()) === false) {

if ($lid === null && ($lid = $this->getInsertId()) === false) {
throw new Exceptions\GetDataException("Couldn't get last insert key!");
}

return $lid;
return $this->convertValue($lid);
}

/**
* @param string|array $fields
* @param string $table
* @param string $where
* @param array|string $where
* @return bool|mixed|mysqli_result
* @throws Exceptions\ConnectException
* @throws Exceptions\GetDataException
* @throws Exceptions\QueryException
* @throws Exceptions\TableNotDefinedException
* @throws Exceptions\TooManyLoopsException
* @throws Exceptions\InvalidFieldException
*/
public function save($fields, string $table, string $where = '')
public function save($fields, string $table, $where = '')
{
if ($where === '') {
$mode = 'insert';
} else {
$result = $this->select('*', $table, $where);
if (! $result instanceof mysqli_result) {
throw (new QueryException('Need mysqli_result'))
throw (new Exceptions\QueryException('Need mysqli_result'))
->setQuery($this->getLastQuery());
}
if ($this->getRecordCount($result) === 0) {
Expand All @@ -364,17 +390,6 @@ public function isResult($result) : bool
return $result instanceof mysqli_result;
}

/**
* @param mysqli_result $result
* @return $this
*/
public function freeResult(mysqli_result $result) : self
{
$result->free_result();

return $this;
}

/**
* @param mysqli_result $result
* @return int
Expand All @@ -401,7 +416,7 @@ public function fieldName(mysqli_result $result, $col = 0) :? string
* @param string|null $method
* @return bool
* @throws Exceptions\ConnectException
* @throws QueryException
* @throws Exceptions\QueryException
*/
public function setCharset(string $charset, $method = null) : bool
{
Expand Down Expand Up @@ -544,7 +559,7 @@ public function getValue($result)
$out = $result[0] ?? false;
}

return $out;
return $this->convertValue($out);
}

/**
Expand Down Expand Up @@ -590,16 +605,6 @@ public function makeArray(mysqli_result $result, bool $index = false) : array
return $rsArray;
}

/**
* @param mysqli_result $result
* @param int $row
* @return bool
*/
public function dataSeek(mysqli_result $result, int $row) : bool
{
return $result->data_seek($row);
}

/**
* @return string
* @throws Exceptions\ConnectException
Expand Down Expand Up @@ -644,7 +649,9 @@ public function truncate(string $table)
*/
public function getInsertId()
{
return $this->getConnect()->insert_id;
return $this->convertValue(
$this->getConnect()->insert_id
);
}

/**
Expand Down
Expand Up @@ -2,7 +2,7 @@

use Exception;

class InvaludFieldException extends Exception
class InvalidFieldException extends Exception
{
protected $data;

Expand Down

0 comments on commit 2863853

Please sign in to comment.