Adding query class

This commit is contained in:
Tristan Ancelet 2023-05-27 14:28:06 -05:00
parent 8b5782c5de
commit 52c65cea85
4 changed files with 69 additions and 3 deletions

53
Libraries/db/query.php Normal file
View File

@ -0,0 +1,53 @@
<?php
class Query {
private $table_name;
private array $columns_querying = array();
private int $limit=0;
public function __construct(string $table_name){
$this->table_name = $table_name;
}
public function set_columns($columns){
if (is_array($columns)){
$this->columns_querying = $columns;
}
}
public function set_limit(int $limit){
if ($limit > 0){
$this->limit = $limit;
}
}
private function get_columns(){
$column_string="";
if (count($this->columns_querying) === 0){
$column_string="*";
} else {
$columns = implode(',', $this->columns_querying);
$column_string=$columns;
}
return $column_string;
}
private function get_limit() {
$modifier="";
if ($this->limit > 0){
$modifier=" LIMIT {$this->limit}";
}
return $modifier;
}
private function get_where() {
$where = "";
return $where;
}
public function get_query_string(){
$query = "SELECT {$this->get_columns()} FROM {$this->table_name}{$this->get_where()}{$this->get_limit()};";
return $query;
}
}
?>

View File

@ -1,6 +1,9 @@
<?php
include("../../Libraries/test.php");
include("../Libraries/table/class.php");
$db = new SQLite3("../../webcron.db");
$table = new Table("crontabs");
test_func();
$table->Load($db, "");
echo $table->get_html();
?>

View File

@ -50,7 +50,7 @@ function loadCrontab (id){
}
function test (){
url = "/api/crontab_view.php?name=0hourly";
url = "/api/test.php";
setAreaByUrl(url);
}

10
test_query.php Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/php
<?php
require("Libraries/db/query.php");
$query = new Query("crontabs");
$query->set_limit(-1);
echo $query->get_query_string();
?>