Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin Pranatha committed Jun 24, 2023
1 parent 707c551 commit 71fee7f
Show file tree
Hide file tree
Showing 15 changed files with 756 additions and 24 deletions.
48 changes: 47 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function paginator($items, $total, $perPage, $currentPage = null, array $
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);

//var_dump($this->total);
//var_dump($this->perPage);
//$this->perPage);
//var_dump($this->lastPage);
//var_dump($this->path);
//var_dump($this->currentPage);
Expand Down Expand Up @@ -516,5 +516,51 @@ protected function isValidPageNumber($page)
return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
}

/**
* Prepare the value and operator for a where clause.
*
* @param string $value
* @param string $operator
* @param bool $useDefault
* @return array
*
* @throws \InvalidArgumentException
*/
public function prepareValueAndOperator($value, $operator, $useDefault = false)
{
if ($useDefault) {
return [$operator, '='];
} elseif ($this->invalidOperatorAndValue($operator, $value)) {
throw new \InvalidArgumentException('Illegal operator and value combination.');
}

return [$value, $operator];
}

/**
* Determine if the given operator and value combination is legal.
*
* Prevents using Null values with invalid operators.
*
* @param string $operator
* @param mixed $value
* @return bool
*/
protected function invalidOperatorAndValue($operator, $value)
{
return is_null($value) && in_array($operator, $this->operators) &&
! in_array($operator, ['=', '<>', '!=']);
}

/**
* Get a scalar type value from an unknown type of input.
*
* @param mixed $value
* @return mixed
*/
protected function flattenValue($value)
{
return is_array($value) ? head(Arr::flatten($value)) : $value;
}

}
2 changes: 1 addition & 1 deletion src/Connectors/SQLiteConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function connect(array $config)
// as the developer probably wants to know if the database exists and this
// SQLite driver will not throw any exception if it does not by default.
if ($path === false) {
throw new \InvalidArgumentException("Neko\Database does not exist.");
throw new \InvalidArgumentException("Database file does not exist path = '".$path."'");
}

return $this->createConnection("sqlite:{$path}", $config, $options);
Expand Down
5 changes: 2 additions & 3 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public static function connection($con_string) {
*/
public static function getConnection() {
global $pdo;

if ( is_null( static::$connection ) ) {
static::$connection = new Connection( $pdo );
}
Expand All @@ -85,13 +84,13 @@ public static function getConnection() {
*
* @param \Database\ConnectionInterface $connection
*/
public static function setConnection($con_string) {
public static function setConnection($con_string=null) {
global $app;
$factory = new ConnectionFactory();

if($con_string == null)
{
$con_string = $app->config['db']["default"];
$con_string = $app->config['db'][$app->config['db_connection']];
}else{
$con_string = $app->config['db'][$con_string];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setMaxQueryLength($maxQueryLength)
* @param array $bindings
* @param \Exception $previousException
*/
public function handle($query = '', array $bindings = array(), \Exception $previousException)
public function handle($query, array $bindings, \Exception $previousException)
{
$parameters = $this->parameters;

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ExceptionHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

interface ExceptionHandlerInterface
{
public function handle($query, array $bindings = array(), \Exception $previousException);
public function handle($query, array $bindings, \Exception $previousException);
}
26 changes: 26 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Closure;
use Neko\Database\ConnectionInterface;
use Neko\Database\Query\Grammars\Grammar;
use Neko\Framework\Util\Date;

class Builder
{
Expand Down Expand Up @@ -1684,6 +1685,7 @@ public function insertUpdate(array $values, array $updateValues)
$sql = $this->grammar->compileInsertOnDuplicateKeyUpdate($this, $values, $updateValues);
return $this->connection->query($sql, $bindings);
}


/**
* Alias for insertOnDuplicateKeyUpdate
Expand Down Expand Up @@ -1982,4 +1984,28 @@ public function __call($method, $parameters)

throw new \BadMethodCallException("Call to undefined method {$className}::{$method}()");
}

/**
* Add a "where date" statement to the query.
*
* @param string $column
* @param string $operator
* @param \DateTimeInterface|string|null $value
* @param string $boolean
* @return $this
*/
public function whereDate($column, $operator, $value = null, $boolean = 'and')
{
[$value, $operator] = $this->prepareValueAndOperator(
$value, $operator, func_num_args() === 2
);

$value = $this->flattenValue($value);

if (Date::verifyDate($value)) {
$value = $value->format('Y-m-d');
}

return $this->addDateBasedWhere('Date', $column, $operator, $value, $boolean);
}
}
12 changes: 12 additions & 0 deletions src/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1023,4 +1023,16 @@ protected function wrap_value($value)
return ('*' === $value) ? '*' : sprintf($this->wrapper, $value);
}

/**
* Compile a "where date" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereDate(Builder $query, $where)
{
return $this->dateBasedWhere('date', $query, $where);
}

}
45 changes: 44 additions & 1 deletion src/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostgresGrammar extends Grammar
*
* @var string
*/
public $wrapper = '%s';
public $wrapper = '"%s"';

/**
* All of the available clause operators.
Expand Down Expand Up @@ -173,4 +173,47 @@ public function compileTruncate(Builder $query)
return array('truncate ' . $this->wrapTable($query->from) . ' restart identity' => array());
}

/**
* Wrap a single string in keyword identifiers.
*
* @param string $value
* @return string
*/
protected function wrapValue($value)
{
// if ($value === '*') return $value;

// return '"' . str_replace('"', '""', $value) . '"';
return ('*' === $value) ? '*' : sprintf($this->wrapper, $value);
}

/**
* Compile the "group by" portions of the query.
*
* @param \Neko\Database\Query\Builder $query
* @param array $groups
* @return string
*/
protected function compileGroups(Builder $query, $groups)
{
return parent::compileGroups($query, $groups) . ($query->rollup ? ' with rollup' : '');
}


/**
* Compile an insert statement into SQL.
*
* @param \Neko\Database\Query\Builder $query
* @param array $values
* @param array $updateValues
* @return string
*/
public function compileInsertOnDuplicateKeyUpdate(Builder $query, array $values, array $updateValues)
{
$insert = $this->compileInsert($query, $values);

$update = $this->getUpdateColumns($updateValues);
return $insert.' ON CONFLICT('.array_keys(reset($values))[0].') DO UPDATE SET '.$update;
}

}
18 changes: 17 additions & 1 deletion src/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SQLiteGrammar extends Grammar
*
* @var string
*/
public $wrapper = '%s';
public $wrapper = '`%s`';

/**
* All of the available clause operators.
Expand Down Expand Up @@ -154,5 +154,21 @@ protected function dateBasedWhere($type, Builder $query, $where)

return 'strftime(\'' . $type . '\', ' . $this->wrap($where['column']) . ') ' . $where['operator'] . ' ' . $value;
}

/**
* Compile an insert statement into SQL.
*
* @param \Neko\Database\Query\Builder $query
* @param array $values
* @param array $updateValues
* @return string
*/
public function compileInsertOnDuplicateKeyUpdate(Builder $query, array $values, array $updateValues)
{
$insert = $this->compileInsert($query, $values);

$update = $this->getUpdateColumns($updateValues);
return $insert.' ON CONFLICT('.array_keys(reset($values))[0].') DO UPDATE SET '.$update;
}

}
Loading

0 comments on commit 71fee7f

Please sign in to comment.