Skip to content

Commit

Permalink
SC_DB_DBFactory::addLimitOffset() 修正
Browse files Browse the repository at this point in the history
- MySQL で実行できない OFFSET 句の生成を回避。
- LIMIT 0 に対応する。
  • Loading branch information
seasoftjapan committed Jan 10, 2025
1 parent 5ae14f6 commit e2dac78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 7 additions & 7 deletions data/class/db/SC_DB_DBFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,20 @@ public function listTables(SC_Query &$objQuery)
* SQL 文に OFFSET, LIMIT を付加する。
*
* @param string 元の SQL 文
* @param int LIMIT
* @param int OFFSET
* @param int|string|null LIMIT
* @param int|string|null OFFSET
*
* @return string 付加後の SQL 文
*/
public function addLimitOffset($sql, $limit = 0, $offset = 0)
public function addLimitOffset($sql, $limit = null, $offset = null)
{
if ($limit != 0) {
// 以下の is_numeric() は、`!is_null()` と `!== ''` の評価と、SQL インジェクション対策を兼ねる。
if (is_numeric($limit)) {
$sql .= " LIMIT $limit";
}
if (strlen($offset) === 0) {
$offset = 0;
if (is_numeric($offset)) {
$sql .= " OFFSET $offset";
}
$sql .= " OFFSET $offset";

return $sql;
}
Expand Down
10 changes: 10 additions & 0 deletions data/class/db/dbfactory/SC_DB_DBFactory_MYSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,14 @@ public function isSkipDeleteIfNotExists()
{
return $this->getTransactionIsolationLevel() >= static::ISOLATION_LEVEL_REPEATABLE_READ;
}

public function addLimitOffset($sql, $limit = null, $offset = null)
{
// MySQL は OFFSET のみの指定はできないため、LIMIT が指定されていない場合は最大値を指定する。
if (!is_numeric($limit) && is_numeric($offset)) {
$limit = '18446744073709551615';
}

return parent::addLimitOffset($sql, $limit, $offset);
}
}

0 comments on commit e2dac78

Please sign in to comment.