commit d74fb5895385a09ba749802fcb0b64f842b76fa1
parent b5d74cc8ab92f8cd023a0ec0136cb7ecaf45b939
Author: Friedel Schon <[email protected]>
Date: Wed, 12 Apr 2023 19:00:36 +0200
refactor and fix reset-table
Diffstat:
2 files changed, 70 insertions(+), 110 deletions(-)
diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php
@@ -27,6 +27,11 @@ namespace Lollipop {
$this->data = $data;
}
+ public function getData()
+ {
+ return $this->data;
+ }
+
public function load(string $id): bool
{
$sql = "SELECT * FROM {$this->table} WHERE {$this->primary} LIKE ?";
@@ -87,131 +92,29 @@ namespace Lollipop {
$this->changed_keys[] = $name;
}
- public function getData()
- {
- return $this->data;
- }
- function where(array $vars) : string
- {
- if (!sizeof($vars)) {
- return false;
- }
-
- $sql = "SELECT * FROM {$this->get_table()} WHERE ";
- $params = [];
-
- $i = 0;
- foreach ($vars as $key => $value) {
- if ($i > 0) {
- $sql .= ' AND ';
- }
- $sql .= " $key = ?";
- $params[] = $value;
- $i++;
- }
-
- $stmt = $this->db->conn->prepare($sql);
- $stmt->execute($params);
- $result = $stmt->get_result();
-
- if (!$result || $result->num_rows == 0) {
- return $sql;
- }
-
- while ($row = $result->fetch_assoc()){
- $this->setData($row);
- }
- return true;
- }
-
- function all_where(array $vars): bool
- /*
- chat gpt look at this
- */
- {
- $sql = "SELECT * FROM {$this->get_table()} WHERE ";
- $params = [];
-
- $i = 0;
- foreach ($vars as $key => $value) {
- if ($i > 0) {
- $sql .= ' AND ';
- }
- $sql .= " $key LIKE ?";
- $params[] = $value;
- $i++;
- }
-
- $result = $this->db->conn->prepare($sql);
-
- if (!$result) {
- return false;
- }
-
- // Bind parameters to the prepared statement
- $types = str_repeat('s', count($params));
- $result->bind_param($types, ...$params);
-
- // Execute the prepared statement and get the result set
- $result->execute();
- $result_set = $result->get_result();
-
- if (!$result_set || $result_set->num_rows == 0) {
- return false;
- }
-
- // Get an array of mysqli_field objects representing the columns in the result set
- $fields = $result_set->fetch_fields();
-
- // Create an array to hold the column names
- $column_names = array();
-
- // Loop through the mysqli_field objects and get the column names
- foreach ($fields as $field) {
- $column_names[] = $field->name;
- }
-
- // Loop through the rows and add their column values to the PHP array
- while ($row = $result_set->fetch_assoc()) {
- $this->data[] = array_intersect_key($row, array_flip($column_names));
- }
-
- return true;
- }
-
function insert(): bool
- {
+ {
//first check if this primary key exists
$sql = "SELECT {$this->primary} FROM {$this->table} WHERE {$this->primary} = ?";
$stmt = $this->db->conn->prepare($sql);
$stmt->bind_param('s', $this->data['email']);
$stmt->execute();
$result = $stmt->get_result();
- if ($result->num_rows > 0) {
+ if ($result && $result->num_rows > 0)
return false;
- }
-
+
//if this primay key does not exist add the data
$keys = implode(", ", array_keys($this->data));
- $values = '';
- $count = count($this->data);
- $i = 0;
- foreach ($this->data as $index => $data) {
- $values .= '?';
- if ($i < $count - 1) {
- $values .= ', ';
- }
- $i++;
- }
-
- $sql = "INSERT INTO {$this->get_table()} ({$keys}) VALUES ({$values})";
+ $values = implode(", ", array_fill(0, sizeof($this->data), '?'));
+
+ $sql = "INSERT INTO {$this->table} ({$keys}) VALUES ({$values})";
$stmt = $this->db->conn->prepare($sql);
$stmt->execute(array_values($this->data));
$result = $stmt->get_result();
-
- return true;
+
+ return $result && $result->num_rows > 0;
}
}
}
\ No newline at end of file
diff --git a/Lollipop/SQLDatabase.php b/Lollipop/SQLDatabase.php
@@ -17,6 +17,63 @@ namespace Lollipop {
$cls = new $table_class($this);
return $cls;
}
+
+ function all(string $table_class): array
+ {
+ $cls = new $table_class($this);
+
+ $sql = "SELECT * FROM {$cls->table}";
+
+ $stmt = $this->conn->prepare($sql);
+ $result = $stmt->get_result();
+
+ if (!$result || $result->num_rows == 0) {
+ return [];
+ }
+
+ $rows = [];
+ while ($row = $result->fetch_assoc()) {
+ $r = new $table_class($this);
+ $r->setData($row);
+ $rows[] = $r;
+ }
+ return $rows;
+ }
+
+ function where(string $table_class, array $vars, bool $like = false): array
+ {
+ $cls = new $table_class($this);
+
+ if (!sizeof($vars))
+ return [];
+
+ $sql = "SELECT * FROM {$cls->table} WHERE ";
+
+ $i = 0;
+ foreach ($vars as $key => $value) {
+ if ($i > 0) {
+ $sql .= ' AND ';
+ }
+ $sql .= $like ? " $key LIKE ?" : " $key = ?";
+ $i++;
+ }
+
+ $stmt = $this->conn->prepare($sql);
+ $stmt->execute(array_values($vars));
+ $result = $stmt->get_result();
+
+ if (!$result || $result->num_rows == 0) {
+ return [];
+ }
+
+ $rows = [];
+ while ($row = $result->fetch_assoc()) {
+ $r = new $table_class($this);
+ $r->setData($row);
+ $rows[] = $r;
+ }
+ return $rows;
+ }
}
}
?>
\ No newline at end of file