Skip to content

Commit

Permalink
Fixed: SQL Server syntax for fetch rows
Browse files Browse the repository at this point in the history
  • Loading branch information
dongnl committed Sep 5, 2024
1 parent aabec06 commit cd16c2e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## Version 3.4.1
1. Fixed: SQL Server syntax for fetch rows

## Version 3.4.0
1. Added: new line before keyword sql output mode

Expand Down
70 changes: 39 additions & 31 deletions SQLServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

class SQLServer extends SQL
{
protected $identifierQuotes=array('"','"');//For table name and column name
protected $identifierQuotes = array('"', '"'); //For table name and column name


protected function buildProcedureQuery($options = [])
{
$sql = "";
foreach($this->query->procedures as $proc) {
$statement = "EXEC ".$proc[0]." @params ;";
foreach ($this->query->procedures as $proc) {
$statement = "EXEC " . $proc[0] . " @params ;";
$params = [];
foreach($proc[1] as $value) {
if(gettype($value)==="string") {
array_push($params,$this->coverValue($this->escapeString($value)));
foreach ($proc[1] as $value) {
if (gettype($value) === "string") {
array_push($params, $this->coverValue($this->escapeString($value)));
} else {
array_push($params,$value);
array_push($params, $value);
}
}
$statement = str_replace("@params",implode(",",$params),$statement);
$sql.=$statement;
$statement = str_replace("@params", implode(",", $params), $statement);
$sql .= $statement;
}
return $sql;
}
Expand All @@ -31,52 +31,60 @@ protected function buildSelectQuery($options = [])
{
$sql = "SELECT ";
if ($this->query->distinct) {
$sql.="DISTINCT ";
$sql .= "DISTINCT ";
}
if (count($this->query->columns)>0) {
$sql.=$this->getSelect($this->query->columns);
if (count($this->query->columns) > 0) {
$sql .= $this->getSelect($this->query->columns);
} else {
$sql.="*";
$sql .= "*";
}
if (count($this->query->tables)>0) {
$sql.=" FROM ".$this->getFrom($this->query->tables);
if (count($this->query->tables) > 0) {
$sql .= " FROM " . $this->getFrom($this->query->tables);
} else {
throw new \Exception("No table available in SQL Query");
}

if (count($this->query->joins)>0) {
$sql.=$this->getJoin($this->query->joins);
if (count($this->query->joins) > 0) {
$sql .= $this->getJoin($this->query->joins);
}

if (count($this->query->conditions)>0) {
$sql.=" WHERE ".$this->getWhere($this->query->conditions);
if (count($this->query->conditions) > 0) {
$sql .= " WHERE " . $this->getWhere($this->query->conditions);
}

if (count($this->query->groups)>0) {
$sql.=" GROUP BY ".$this->getGroupBy($this->query->groups);
if (count($this->query->groups) > 0) {
$sql .= " GROUP BY " . $this->getGroupBy($this->query->groups);
}

if ($this->query->having) {
$sql.=" HAVING ".$this->getHaving($this->query->having);
$sql .= " HAVING " . $this->getHaving($this->query->having);
}

/*
SQL Server requires ORDER BY and OFFET ROWS if using FETCH ROWS
*/
if (isset($this->query->limit)) {
if (empty($this->query->orders)) $this->query->orders = [['[{raw}]', 1]];
if (empty($this->query->offset)) $this->query->offset = 0;
}

if (count($this->query->orders)>0) {
$sql.=" ORDER BY ".$this->getOrderBy($this->query->orders);
if (count($this->query->orders) > 0) {
$sql .= " ORDER BY " . $this->getOrderBy($this->query->orders);
}

if ($this->query->offset!==null) {
$sql.=" OFFSET ".$this->query->offset." ROWS";
if ($this->query->offset !== null) {
$sql .= " OFFSET " . $this->query->offset . " ROWS";
}

if ($this->query->limit!==null) {
$sql.=" FETCH NEXT ".$this->query->limit." ROWS ONLY";
if ($this->query->limit !== null) {
$sql .= " FETCH NEXT " . $this->query->limit . " ROWS ONLY";
}


if (count($this->query->unions)>0) {
$sql.=$this->getUnions($this->query->unions);
if (count($this->query->unions) > 0) {
$sql .= $this->getUnions($this->query->unions);
}
// echo "sql: $sql<br>";
return $sql;
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koolreport/querybuilder",
"version":"3.4.0",
"version":"3.4.1",
"description": "Create query by php code",
"keywords": ["PHP","php reprting tools","php reporting framework","mysql reporting tools","charts","graphs","query builder","sql query generator"],
"homepage": "https://www.koolreport.com",
Expand Down

0 comments on commit cd16c2e

Please sign in to comment.