Skip to content

Commit

Permalink
fix a few issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zegenie committed Dec 12, 2018
1 parent aeebb84 commit 4d04212
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 38 deletions.
14 changes: 9 additions & 5 deletions src/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public function getValues()

foreach ($this->parts as $part) {
if ($part instanceof Criterion) {
$values[] = $part->getValue();
if ($part->getValue() !== null) {
$values[] = $part->getValue();
}
} else {
foreach ($part->getValues() as $value) {
$values[] = $value;
if ($value !== null) {
$values[] = $value;
}
}
}
}
Expand Down Expand Up @@ -133,10 +137,10 @@ public function or($column, $value = null, $operator = Criterion::EQUALS, $varia
/**
* Returns the SQL string for the current criteria
*
* @param bool $strip
* @param bool $strip_table_name
* @return string
*/
public function getSQL($strip = false)
public function getSQL($strip_table_name = false)
{
$sql_parts = [];
foreach ($this->parts as $part) {
Expand All @@ -146,7 +150,7 @@ public function getSQL($strip = false)
$part->setQuery($this->query);
}

$sql_parts[] = $part->getSql($strip);
$sql_parts[] = $part->getSql($strip_table_name);
}

if (count($sql_parts) > 1) {
Expand Down
10 changes: 7 additions & 3 deletions src/Criterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function __construct($column, $value = '', $operator = self::EQUALS, $var
$this->column = $column;
$this->value = $value;
if ($operator !== null) {
if ($operator == self::IN && !$value) {
throw new Exception('Cannot use an empty value for WHERE IN criteria');
}

$this->operator = $operator;
}
if ($variable) {
Expand Down Expand Up @@ -232,13 +236,13 @@ public function isInTypeOperator()
return in_array($this->operator, [self::IN, self::NOT_IN]);
}

public function getSql($strip = false)
public function getSql($strip_table_name = false)
{
if ($this->sql !== null) {
return $this->sql;
}

$column = ($strip) ? Table::getColumnName($this->column) : $this->getQuery()->getSelectionColumn($this->column);
$column = ($strip_table_name) ? Table::getColumnName($this->column) : $this->getQuery()->getSelectionColumn($this->column);
$initial_sql = Query::quoteIdentifier($column);

if ($this->special) {
Expand All @@ -247,7 +251,7 @@ public function getSql($strip = false)
$sql = $initial_sql;
}

if (is_null($this->value) && !$this->isNullTypeOperator()) {
if ($this->value === null && !$this->isNullTypeOperator()) {
$this->operator = ($this->operator == self::EQUALS) ? self::IS_NULL : self::IS_NOT_NULL;
} elseif (is_array($this->value) && $this->operator != self::NOT_IN) {
$this->operator = self::IN;
Expand Down
4 changes: 3 additions & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ public function addValue($value)
$this->addValue($single_value);
}
} else {
$this->values[] = $this->getDatabaseValue($value);
if ($value !== null) {
$this->values[] = $this->getDatabaseValue($value);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/RawQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RawQuery implements QueryInterface

protected $columns;

protected $values;
protected $values = [];

protected $sql;

Expand Down
17 changes: 16 additions & 1 deletion src/Resultset.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
class Resultset implements \Countable
{

protected $rows = array();
/**
* @var Row[]
*/
protected $rows = [];

/**
* @var QueryInterface
Expand Down Expand Up @@ -62,6 +65,9 @@ public function __construct(Statement $statement)
}
}

/**
* @return bool
*/
protected function _next()
{
if ($this->int_ptr == $this->max_ptr) {
Expand All @@ -72,6 +78,9 @@ protected function _next()
}
}

/**
* @return int
*/
public function getCount()
{
return $this->max_ptr;
Expand Down Expand Up @@ -119,6 +128,9 @@ public function get($column, $foreign_key = null)
}
}

/**
* @return Row[]
*/
public function getAllRows()
{
return $this->rows;
Expand Down Expand Up @@ -154,6 +166,9 @@ public function rewind()
$this->int_ptr = 0;
}

/**
* @return Row
*/
public function current()
{
$row = $this->getCurrentRow();
Expand Down
13 changes: 7 additions & 6 deletions src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Row implements \ArrayAccess
{

protected $fields = array();
protected $fields = [];

/**
* Statement
Expand All @@ -32,11 +32,12 @@ class Row implements \ArrayAccess

protected $id_col = null;

/**
* Constructor
*
* @param Statement $statement
*/
/**
* Constructor
*
* @param mixed[] $row
* @param Statement $statement
*/
public function __construct($row, $statement)
{
foreach ($row as $key => $val) {
Expand Down
26 changes: 10 additions & 16 deletions src/Saveable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,16 @@
/**
* B2DB Saveable class, active record implementation for B2DB
*
* @author Daniel Andre Eikeland <zegenie@zegeniestudios.net>
* @version 2.0
* @license http://www.opensource.org/licenses/mozilla1.1.php Mozilla Public License 1.1 (MPL 1.1)
* @package b2db
* @subpackage core
*/

/**
* B2DB Saveable class, active record implementation for B2DB
* Can be implemented by objects to allow them to be passed to a B2DB table
* class for saving
*
* @package b2db
* @subpackage core
*/
class Saveable
{

protected $b2db_initial_values = array();
protected $b2db_initial_values = [];

/**
* Return the associated B2DBTable for this class
* Return the associated Table class for this class
*
* @return Table
*/
Expand All @@ -36,6 +24,12 @@ public static function getB2DBTable()
return $b2db_table_name::getTable();
}

/**
* @param $id
* @param $classname
* @param Row|null $row
* @return Saveable
*/
public static function getB2DBCachedObjectIfAvailable($id, $classname, $row = null)
{
$has_cached = self::getB2DBTable()->hasCachedB2DBObject($id);
Expand All @@ -48,7 +42,7 @@ public static function getB2DBCachedObjectIfAvailable($id, $classname, $row = nu
return $object;
}

protected function _b2dbLazycount($property)
protected function _b2dbLazyCount($property)
{
$relation_details = Core::getCachedEntityRelationDetails('\\'.get_class($this), $property);
if (array_key_exists('manytomany', $relation_details) && $relation_details['manytomany']) {
Expand All @@ -60,7 +54,7 @@ protected function _b2dbLazycount($property)
return $count;
}

protected function _b2dbLazyload($property, $use_cache = true)
protected function _b2dbLazyLoad($property, $use_cache = true)
{
$relation_details = Core::getCachedEntityRelationDetails('\\'.get_class($this), $property);
if ($relation_details['collection']) {
Expand Down
11 changes: 7 additions & 4 deletions src/SqlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ protected function getTable()
* Add a specified value
*
* @param mixed $value
* @param bool $force_null
*/
protected function addValue($value)
protected function addValue($value, $force_null = false)
{
if (is_array($value)) {
foreach ($value as $single_value) {
$this->addValue($single_value);
}
} else {
$this->values[] = $this->query->getDatabaseValue($value);
if ($value !== null || $force_null) {
$this->values[] = $this->query->getDatabaseValue($value);
}
}
}

Expand Down Expand Up @@ -338,7 +341,7 @@ protected function generateUpdateSQL(Update $update)
$prefix = Query::quoteIdentifier($column);
$updates[] = $prefix . Criterion::EQUALS . '?';

$this->addValue($value);
$this->addValue($value, true);
}
$sql = 'UPDATE ' . $this->getTable()->getSqlTableName() . ' SET ' . implode(', ', $updates);
return $sql;
Expand Down Expand Up @@ -387,7 +390,7 @@ public function getInsertSQL(Insertion $insertion)
$values[] = '@' . $insertion->getVariable($column);
} else {
$values[] = '?';
$this->addValue($value);
$this->addValue($value, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final public function __construct()

protected function initialize()
{
throw new Exception('The table "\\' . get_class($this) . '" has no corresponding entity class. You must override the _initialize() method to set up the table details.');
throw new Exception('The table "\\' . get_class($this) . '" has no corresponding entity class. You must override the initialize() method to set up the table details.');
}

protected function setup($b2db_name, $id_column)
Expand Down

0 comments on commit 4d04212

Please sign in to comment.