summaryrefslogtreecommitdiff
path: root/src/web/core
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-05-20 19:26:59 -0400
committerFreya Murphy <freya@freyacat.org>2024-05-20 19:26:59 -0400
commit29f7c5ea41d36509d8e5961f40a7af0a934a7ca1 (patch)
tree722c420750b16c9ef25eb77410813942f146446f /src/web/core
parenta (diff)
downloadxssbook2-29f7c5ea41d36509d8e5961f40a7af0a934a7ca1.tar.gz
xssbook2-29f7c5ea41d36509d8e5961f40a7af0a934a7ca1.tar.bz2
xssbook2-29f7c5ea41d36509d8e5961f40a7af0a934a7ca1.zip
aaaa
Diffstat (limited to 'src/web/core')
-rw-r--r--src/web/core/database.php161
1 files changed, 130 insertions, 31 deletions
diff --git a/src/web/core/database.php b/src/web/core/database.php
index e9b8109..25cb5ba 100644
--- a/src/web/core/database.php
+++ b/src/web/core/database.php
@@ -19,19 +19,19 @@ class DatabaseQuery {
$this->param = array();
}
- private function in($array) {
- $in = 'IN (';
- foreach ($array as $idx => $item) {
- if ($idx != 0) {
- $in .= ",";
- }
- $in .= "?";
- array_push($this->param, $item);
- }
- $in .= ")";
- return $in;
+ ///
+ /// ARBITRARY QUERY
+ ///
+
+ public function query($query) {
+ $this->query .= $query;
+ return $this;
}
+ ///
+ /// SELECT
+ ///
+
public function select($select) {
$this->query .= "SELECT $select\n";
return $this;
@@ -42,6 +42,39 @@ class DatabaseQuery {
return $this;
}
+ ///
+ /// INSERT
+ ///
+
+ public function insert_into($insert, ...$columns) {
+ $this->query .= "INSERT INTO $insert\n (";
+ foreach ($columns as $idx => $column) {
+ if ($idx !== 0) {
+ $this->query .= ",";
+ }
+ $this->query .= $column;
+ }
+ $this->query .= ")\n";
+ return $this;
+ }
+
+ public function values(...$values) {
+ $this->query .= "VALUES (";
+ foreach ($values as $idx => $value) {
+ if ($idx !== 0) {
+ $this->query .= ",";
+ }
+ $this->query .= "?";
+ array_push($this->param, $value);
+ }
+ $this->query .= ")\n";
+ return $this;
+ }
+
+ ///
+ /// WHERE
+ ///
+
public function where($cond) {
if (!$this->where) {
$this->where = TRUE;
@@ -53,6 +86,39 @@ class DatabaseQuery {
return $this;
}
+ public function where_in($column, $array) {
+ if (!$this->where) {
+ $this->where = TRUE;
+ $this->query .= "WHERE ";
+ } else {
+ $this->query .= "AND ";
+ }
+ if (empty($array)) {
+ $this->query .= "FALSE\n";
+ return $this;
+ }
+ $in = $this->in($array);
+ $this->query .= "$column $in\n";
+ return $this;
+ }
+
+ private function in($array) {
+ $in = 'IN (';
+ foreach ($array as $idx => $item) {
+ if ($idx != 0) {
+ $in .= ",";
+ }
+ $in .= "?";
+ array_push($this->param, $item);
+ }
+ $in .= ")";
+ return $in;
+ }
+
+ ///
+ /// OPERATORS
+ ///
+
public function like($item) {
$this->query .= "LIKE ?\n";
array_push($this->param, $item);
@@ -83,27 +149,19 @@ class DatabaseQuery {
return $this;
}
- public function where_in($column, $array) {
- if (!$this->where) {
- $this->where = TRUE;
- $this->query .= "WHERE ";
- } else {
- $this->query .= "AND ";
- }
- if (empty($array)) {
- $this->query .= "FALSE\n";
- return $this;
- }
- $in = $this->in($array);
- $this->query .= "$column $in\n";
- return $this;
- }
+ ///
+ /// JOINS
+ ///
public function join($table, $on, $type = 'LEFT') {
$this->query .= "$type JOIN $table ON $on\n";
return $this;
}
+ ///
+ /// LIMIT, OFFSET, ORDER
+ ///
+
public function limit($limit) {
$this->query .= "LIMIT ?\n";
array_push($this->param, $limit);
@@ -121,10 +179,18 @@ class DatabaseQuery {
return $this;
}
- public function rows() {
+ ///
+ /// COLLECT
+ ///
+
+ public function rows(...$params) {
+ $args = $this->param;
+ foreach ($params as $param) {
+ array_push($args, $param);
+ }
$stmt = $this->conn->prepare($this->query);
try {
- $stmt->execute($this->param);
+ $stmt->execute($args);
} catch (Exception $ex) {
echo $ex;
echo '<br> >> caused by <<<br>';
@@ -133,17 +199,38 @@ class DatabaseQuery {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
- public function row() {
+ public function row(...$params) {
+ $args = $this->param;
+ foreach ($params as $param) {
+ array_push($args, $param);
+ }
$stmt = $this->conn->prepare($this->query);
- $stmt->execute($this->param);
+ $stmt->execute($args);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
+
+ public function execute(...$params) {
+ $args = $this->param;
+ foreach ($params as $param) {
+ array_push($args, $param);
+ }
+ $stmt = $this->conn->prepare($this->query);
+ try {
+ $stmt->execute($args);
+ return TRUE;
+ } catch (Exception $_e) {
+ echo $_e;
+ echo '<br> >> caused by <<<br>';
+ echo str_replace("\n", "<br>", $this->query);
+ return FALSE;
+ }
+ }
}
/**
* DatabaseHelper
* allows queries on the
- * xssbook postgres database
+ * postgres database
*/
class DatabaseHelper {
@@ -180,4 +267,16 @@ class DatabaseHelper {
return $query->select($select);
}
+ public function insert_into($insert, ...$columns) {
+ $conn = $this->connect();
+ $query = new DatabaseQuery($conn);
+ return $query->insert_into($insert, ...$columns);
+ }
+
+ public function query($query_str) {
+ $conn = $this->connect();
+ $query = new DatabaseQuery($conn);
+ return $query->query($query_str);
+ }
}
+