Added WPDI for mysqli directly
This commit is contained in:
parent
a1717228bc
commit
bea8e57a33
2 changed files with 407 additions and 0 deletions
296
src/WPDI.php
Normal file
296
src/WPDI.php
Normal file
|
|
@ -0,0 +1,296 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhangoApp\WPDO;
|
||||||
|
|
||||||
|
class WPDI {
|
||||||
|
|
||||||
|
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 $txt_error='';
|
||||||
|
|
||||||
|
//static public $conn=false;
|
||||||
|
|
||||||
|
public function __construct($table) {
|
||||||
|
|
||||||
|
$this->table=$table;
|
||||||
|
|
||||||
|
if(WPDI::$mconn!=null) {
|
||||||
|
|
||||||
|
$this->conn=WPDI::$mconn;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function connect() {
|
||||||
|
|
||||||
|
if(WPDI::$host_db=='' || WPDI::$db=='') {
|
||||||
|
|
||||||
|
echo 'Cannot connect to the mysqldb';
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(WPDI::$mconn==null) {
|
||||||
|
|
||||||
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||||
|
|
||||||
|
//$this->conn=new \PDO(WPDI::$driver.':host='.WPDI::$host_db.';dbname='.WPDI::$db, WPDI::$login_db, WPDI::$pass_db);
|
||||||
|
$this->conn=new mysqli(WPDI::$host_db, WPDI::$login_db, WPDI::$pass_db, WPDI::$db);
|
||||||
|
|
||||||
|
WPDI::$mconn=$this->conn;
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
$this->conn=WPDI::$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);*/
|
||||||
|
|
||||||
|
return $mysqli->execute_query($query, $values);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_result() {
|
||||||
|
|
||||||
|
return $this->sth->fetchAll();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select($fields, $where_sql, $values) {
|
||||||
|
|
||||||
|
$fields=$this->check_fields($fields);
|
||||||
|
|
||||||
|
$str_fields=implode('`, `', $fields);
|
||||||
|
|
||||||
|
$query='select `'.$str_fields.'` 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;*/
|
||||||
|
|
||||||
|
return $this->conn->execute_query($query, $value);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select_to_array($fields=[], $where_sql='', $values=[]) {
|
||||||
|
|
||||||
|
if(count($fields)==0) {
|
||||||
|
|
||||||
|
$fields=$this->table->fields;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$result=$this->conn->select($fields, $where_sql, $values);
|
||||||
|
|
||||||
|
if($result) {
|
||||||
|
|
||||||
|
//return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
$this->txt_error=$this->conn->info();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select_a_row($fields, $where_sql='', $values=[]) {
|
||||||
|
|
||||||
|
if(count($fields)==0) {
|
||||||
|
|
||||||
|
$fields=$this->table->fields;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$result=$this->select($fields, $where_sql.' limit 1', $values);
|
||||||
|
|
||||||
|
if($result) {
|
||||||
|
|
||||||
|
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||||
|
|
||||||
|
if(count($rows)>0) {
|
||||||
|
|
||||||
|
return $rows[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->txt_error=$this->conn->info();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
$this->txt_error=$this->conn->info();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select_count($where_sql, $values) {
|
||||||
|
|
||||||
|
$result=$this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values);
|
||||||
|
|
||||||
|
if($result) {
|
||||||
|
|
||||||
|
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||||
|
|
||||||
|
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('`, `', $fields);
|
||||||
|
|
||||||
|
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.implode(',', array_fill(0, count($fields), '?')).')';
|
||||||
|
|
||||||
|
//$this->sth=$this->conn->prepare($query);
|
||||||
|
|
||||||
|
//return $this->sth->execute($values);
|
||||||
|
return $this->query($query, $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[]='`'.$field.'`=?';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$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);*/
|
||||||
|
return $this->query($query, $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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
111
tests/WPDITest.php
Normal file
111
tests/WPDITest.php
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PhangoApp\WPDO\WPDI;
|
||||||
|
use PhangoApp\WPDO\WTable;
|
||||||
|
use PhangoApp\PhaUtils\Utils;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
include("vendor/autoload.php");
|
||||||
|
include("libraries/Utils.php");
|
||||||
|
|
||||||
|
Utils::load_config('config_test', 'settings');
|
||||||
|
|
||||||
|
final class WPDOTest extends TestCase {
|
||||||
|
|
||||||
|
public function testConnect()
|
||||||
|
{
|
||||||
|
|
||||||
|
global $pdi;
|
||||||
|
|
||||||
|
$table=new WTable('table_test', ['name', 'last_name', 'type']);
|
||||||
|
|
||||||
|
$pdi=new WPDO($table);
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->connect());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateTable()
|
||||||
|
{
|
||||||
|
global $pdi;
|
||||||
|
|
||||||
|
$sql_table="DROP TABLE IF EXISTS table_test;
|
||||||
|
CREATE TABLE table_test (
|
||||||
|
`name` VARCHAR(255) NOT NULL default '',
|
||||||
|
`last_name` VARCHAR(255) NOT NULL default '',
|
||||||
|
`type` INT NOT NULL
|
||||||
|
);";
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->query($sql_table, []));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testCreateTable
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testInsert() {
|
||||||
|
|
||||||
|
global $pdi;
|
||||||
|
|
||||||
|
$this->assertEquals('table_test', $pdi->table->name);
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->insert(['name' => 'first', 'last_name' => 'last', 'type' => 1]));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testInsert
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testSelect() {
|
||||||
|
|
||||||
|
global $pdi;
|
||||||
|
|
||||||
|
$this->assertEquals('table_test', $pdi->table->name);
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->select(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||||
|
|
||||||
|
$arr_result=$pdi->sth->fetchAll();
|
||||||
|
|
||||||
|
$this->assertEquals([['name' => 'first', 0 => 'first']], $arr_result);
|
||||||
|
|
||||||
|
$this->assertEquals([['name' => 'first', 0 => 'first']], $pdi->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||||
|
|
||||||
|
$this->assertEquals(['name' => 'first', 0 => 'first'], $pdi->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||||
|
|
||||||
|
$this->assertEquals(1, $pdi->select_count('WHERE name=?', ['first']));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testInsert
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testUpdate() {
|
||||||
|
|
||||||
|
global $pdi;
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->update(['name'], ['first_updated'], 'WHERE name=?', ['first']));
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->select(['name'], 'WHERE name=? AND type=?', ['first_updated', 1]));
|
||||||
|
|
||||||
|
$arr_result=$pdi->sth->fetchAll();
|
||||||
|
|
||||||
|
$this->assertEquals([['name' => 'first_updated', 0 => 'first_updated']], $arr_result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testCreateTable
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testDropTable()
|
||||||
|
{
|
||||||
|
global $pdi;
|
||||||
|
|
||||||
|
$this->assertTrue($pdi->query('drop table table_test', []));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue