273 lines
6.2 KiB
PHP
273 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace PhangoApp\WPDO;
|
|
|
|
class WPDO {
|
|
|
|
static public $host_db='';
|
|
|
|
static public $db='';
|
|
|
|
static public $login_db='';
|
|
|
|
static public $pass_db='';
|
|
|
|
static public $driver='mysql';
|
|
|
|
static public $mconn=null;
|
|
|
|
public $conn;
|
|
|
|
public $sth;
|
|
|
|
public $table;
|
|
|
|
public $last_query;
|
|
|
|
public $field_delimiter='`';
|
|
|
|
//static public $conn=false;
|
|
|
|
public function __construct($table) {
|
|
|
|
$this->table=$table;
|
|
|
|
if(WPDO::$mconn!=null) {
|
|
|
|
$this->conn=WPDO::$mconn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function connect() {
|
|
|
|
if(WPDO::$host_db=='' || WPDO::$db=='') {
|
|
|
|
echo 'Cannot connect to the mysqldb';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(WPDO::$mconn==null) {
|
|
|
|
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
|
|
|
|
WPDO::$mconn=$this->conn;
|
|
|
|
}
|
|
else {
|
|
|
|
$this->conn=WPDO::$mconn;
|
|
|
|
}
|
|
|
|
if($this->conn) {
|
|
|
|
return true;
|
|
|
|
}
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function query($query, $values) {
|
|
|
|
$this->sth=$this->conn->prepare($query);
|
|
|
|
//$this->sth->execute([$values]);
|
|
|
|
return $this->sth->execute($values);
|
|
|
|
}
|
|
|
|
public function get_result() {
|
|
|
|
return $this->sth->fetchAll();
|
|
|
|
}
|
|
|
|
public function select($fields, $where_sql, $values) {
|
|
|
|
$fields=$this->check_fields($fields);
|
|
|
|
$str_fields=implode("{$this->field_delimiter}, {$this->field_delimiter}", $fields);
|
|
|
|
$query="select {$this->field_delimiter}".$str_fields."{$this->field_delimiter} from ".$this->table->name." ".$where_sql;
|
|
|
|
$this->sth=$this->conn->prepare($query);
|
|
|
|
$result=$this->sth->execute($values);
|
|
|
|
//$this->last_query=var_dump( $this->sth->queryString, $values);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
public function select_to_array($fields=[], $where_sql='', $values=[]) {
|
|
|
|
if(count($fields)==0) {
|
|
|
|
$fields=$this->table->fields;
|
|
|
|
}
|
|
|
|
|
|
if($this->select($fields, $where_sql, $values)) {
|
|
|
|
return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
}
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function select_a_row($fields, $where_sql='', $values=[]) {
|
|
|
|
if(count($fields)==0) {
|
|
|
|
$fields=$this->table->fields;
|
|
|
|
}
|
|
|
|
if($this->select($fields, $where_sql.' limit 1', $values)) {
|
|
|
|
$rows=$this->sth->fetchAll();
|
|
|
|
if(count($rows)>0) {
|
|
|
|
return $rows[0];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function select_count($where_sql, $values) {
|
|
|
|
if($this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values)) {
|
|
|
|
$rows=$this->sth->fetchAll();
|
|
|
|
if(count($rows)>0) {
|
|
|
|
return $rows[0]['count(*)'];
|
|
|
|
}
|
|
|
|
}
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function insert($values) {
|
|
|
|
$fields=array_keys($values);
|
|
|
|
$values=array_values($values);
|
|
|
|
$fields=$this->check_fields($fields);
|
|
|
|
if(count($fields)!=count($values)) {
|
|
|
|
throw new \Exception('No valid values for insert in '.$this->table->name);
|
|
|
|
}
|
|
|
|
$final_fields=implode("{$this->field_delimiter}, {$this->field_delimiter}", $fields);
|
|
|
|
$query='insert into '.$this->table->name." ({$this->field_delimiter}".$final_fields."{$this->field_delimiter}) VALUES (".implode(',', array_fill(0, count($fields), '?')).')';
|
|
|
|
$this->sth=$this->conn->prepare($query);
|
|
|
|
return $this->sth->execute($values);
|
|
|
|
}
|
|
|
|
public function update($fields, $values_update, $where_sql, $values) {
|
|
|
|
$fields=$this->check_fields($fields);
|
|
|
|
if(count($fields)!=count($values_update)) {
|
|
|
|
throw new \Exception('No valid values for insert in '.$this->table->name);
|
|
|
|
}
|
|
|
|
$final_fields=[];
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$final_fields[]="{$this->field_delimiter}".$field."{$this->field_delimiter}=?";
|
|
|
|
}
|
|
|
|
$str_fields=implode(' AND ', $final_fields);
|
|
|
|
$query='update '.$this->table->name.' SET '.$str_fields.' '.$where_sql;
|
|
//echo $query;
|
|
$final_values=array_merge($values_update, $values);
|
|
|
|
$this->sth=$this->conn->prepare($query);
|
|
|
|
return $this->sth->execute($final_values);
|
|
|
|
}
|
|
|
|
public function check_fields($fields) {
|
|
|
|
if($fields[0]!='*') {
|
|
|
|
$final_fields=[];
|
|
|
|
foreach($fields as $field) {
|
|
|
|
if(in_array($field, $this->table->fields)) {
|
|
|
|
$final_fields[]=$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
else {
|
|
|
|
$final_fields=['*'];
|
|
|
|
}
|
|
|
|
if(count($final_fields)==0) {
|
|
|
|
throw new \Exception('No valid fields selected in '.$this->table->name.' Valid fields are '.implode(', ', $this->table->fields).' . Fields passed are: '.implode(', ', $fields));
|
|
|
|
}
|
|
|
|
return $final_fields;
|
|
|
|
}
|
|
|
|
}
|
|
|