Skip to content

Commit 45b7bc1

Browse files
committed
add createView() method
1 parent 924775f commit 45b7bc1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/QueryBuilder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,26 @@ public function __toString(): string
904904
return $this->getSql();
905905
}
906906

907+
public function createView(string $viewName, bool $addExists = true)
908+
{
909+
// this method will be moved to another class
910+
if (empty($viewName)) {
911+
$this->setError('Empty $viewName in ' . __METHOD__);
912+
return $this;
913+
}
914+
915+
$exists = $addExists ? "IF NOT EXISTS " : "";
916+
917+
if (mb_strpos(mb_strtolower($this->sql), 'select') === false) {
918+
$this->setError('No SELECT found in ' . __METHOD__);
919+
return $this;
920+
}
921+
922+
$this->sql = "CREATE VIEW {$exists}`{$viewName}` AS " . $this->sql;
923+
924+
return $this;
925+
}
926+
907927
public function dropView(string $viewName, bool $addExists = true)
908928
{
909929
// this method will be moved to another class

0 commit comments

Comments
 (0)