Skip to content

Commit

Permalink
SC_Query setOrder() と setLimit() の有効範囲を揃える #1116
Browse files Browse the repository at this point in the history
- getSql() LIMIT, OFFSET に対応。処理は SC_DB_DBFactory::addLimitOffset() に委ねる。
- getSql() 末尾で、句に関わる設定をリセットする。
- setLimitOffset()、setLimit()、setOffset() いずれも、MDB2 のラッパーではなく、getSql() に依存させる。
  • Loading branch information
seasoftjapan committed Jan 10, 2025
1 parent e2dac78 commit 5559b6a
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions data/class/SC_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class SC_Query
/** @var SC_DB_DBFactory */
public $dbFactory;

/**
* LIMIT 句 の値
*
* string は、uint64 対策。
*
* @var int|string|null
*/
public $limit;

/**
* OFFSET 句 の値
*
* string は、uint64 対策。
*
* @var int|string|null
*/
public $offset;

/** シングルトン動作のためのインスタンスプール配列。キーは DSN の識別情報。 */
public static $arrPoolInstance = [];

Expand Down Expand Up @@ -353,7 +371,7 @@ public function getAll($sql, $arrVal = [], $fetchmode = MDB2_FETCHMODE_ASSOC)
*
* @return string 構築済みの SELECT 文
*/
public function getSql($cols, $from = '', $where = '', &$arrWhereVal = null)
public function getSql($cols, $from = '', $where = '', &$arrWhereVal = null, $preserve_additional_clauses = false)
{
$dbFactory = SC_DB_DBFactory_Ex::getInstance();

Expand All @@ -377,7 +395,14 @@ public function getSql($cols, $from = '', $where = '', &$arrWhereVal = null)
}
}

$sqlse .= ' '.$this->groupby.' '.$this->order.' '.$this->option;
$sqlse .= ' '.$this->groupby;
$sqlse .= ' '.$this->order;
$sqlse = $this->dbFactory->addLimitOffset($sqlse, $this->limit, $this->offset);
$sqlse .= ' '.$this->option;

if (!$preserve_additional_clauses) {
$this->resetAdditionalClauses();
}

return $sqlse;
}
Expand All @@ -403,16 +428,15 @@ public function setOption($str)
*
* この関数で設定した値は SC_Query::getSql() で使用されます.
*
* @param int $limit LIMIT 句に付与する値
* @param int $offset OFFSET 句に付与する値
* @param int|string|null $limit LIMIT 句に付与する値
* @param int|string|null $offset OFFSET 句に付与する値
*
* @return SC_Query 自分自身のインスタンス
*/
public function setLimitOffset($limit, $offset = 0)
public function setLimitOffset($limit = null, $offset = null)
{
if (is_numeric($limit) && is_numeric($offset)) {
$this->conn->setLimit($limit, $offset);
}
$this->limit = $limit;
$this->offset = $offset;

return $this;
}
Expand All @@ -426,7 +450,7 @@ public function setLimitOffset($limit, $offset = 0)
*
* @return SC_Query 自分自身のインスタンス
*/
public function setGroupBy($str)
public function setGroupBy($str = '')
{
if (strlen($str) == 0) {
$this->groupby = '';
Expand Down Expand Up @@ -504,7 +528,7 @@ public function setWhere($where = '', $arrWhereVal = [])
*
* @return SC_Query 自分自身のインスタンス
*/
public function setOrder($str)
public function setOrder($str = '')
{
if (strlen($str) == 0) {
$this->order = '';
Expand All @@ -520,15 +544,13 @@ public function setOrder($str)
*
* この関数で設定した値は SC_Query::getSql() で使用されます.
*
* @param int $limit LIMIT 句に設定する値
* @param int|string|null $limit LIMIT 句に設定する値
*
* @return SC_Query 自分自身のインスタンス
*/
public function setLimit($limit)
public function setLimit($limit = null)
{
if (is_numeric($limit)) {
$this->conn->setLimit($limit);
}
$this->limit = $limit;

return $this;
}
Expand All @@ -544,9 +566,7 @@ public function setLimit($limit)
*/
public function setOffset($offset)
{
if (is_numeric($offset)) {
$this->conn->setLimit($this->conn->limit, $offset);
}
$this->offset = $offset;

return $this;
}
Expand Down Expand Up @@ -1293,4 +1313,18 @@ public static function getPoolInstance($dsn = '')
return SC_Query_Ex::$arrPoolInstance[$key_str];
}
}

/**
* 句に関わる設定をリセットする。
*
* TODO: WHERE 句に関しても扱うべきか検討する。
*
* @return void
*/
public function resetAdditionalClauses()
{
$this->setGroupBy();
$this->setOrder();
$this->setLimitOffset();
}
}

0 comments on commit 5559b6a

Please sign in to comment.