Merge branch 'master' of https://git.cuchulu.com/phangoapp/wpdo
This commit is contained in:
commit
78e1422a9d
4 changed files with 429 additions and 4 deletions
300
src/WPDI.php
Normal file
300
src/WPDI.php
Normal file
|
|
@ -0,0 +1,300 @@
|
|||
<?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=='') {
|
||||
|
||||
$this->txt_error='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 {
|
||||
|
||||
$this->txt_error='Cannot connect to the mysqldb';
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function query($query, $values) {
|
||||
|
||||
/*$this->sth=$this->conn->prepare($query);
|
||||
|
||||
//$this->sth->execute([$values]);
|
||||
|
||||
return $this->sth->execute($values);*/
|
||||
//echo $this->conn;
|
||||
return $this->conn->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, $values);
|
||||
|
||||
}
|
||||
|
||||
public function select_to_array($fields=[], $where_sql='', $values=[]) {
|
||||
|
||||
if(count($fields)==0) {
|
||||
|
||||
$fields=$this->table->fields;
|
||||
|
||||
}
|
||||
|
||||
|
||||
$result=$this->select($fields, $where_sql, $values);
|
||||
|
||||
if($result) {
|
||||
|
||||
//return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
return $rows;
|
||||
|
||||
}
|
||||
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, $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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -142,7 +142,7 @@ class WPDO {
|
|||
|
||||
if($this->select($fields, $where_sql.' limit 1', $values)) {
|
||||
|
||||
$rows=$this->sth->fetchAll();
|
||||
$rows=$this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
if(count($rows)>0) {
|
||||
|
||||
|
|
@ -165,7 +165,7 @@ class WPDO {
|
|||
|
||||
if($this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values)) {
|
||||
|
||||
$rows=$this->sth->fetchAll();
|
||||
$rows=$this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
if(count($rows)>0) {
|
||||
|
||||
|
|
|
|||
125
tests/WPDITest.php
Normal file
125
tests/WPDITest.php
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
<?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 WPDITest extends TestCase {
|
||||
|
||||
public function testConnect()
|
||||
{
|
||||
|
||||
global $pdi;
|
||||
|
||||
$table=new WTable('table_test', ['name', 'last_name', 'type']);
|
||||
|
||||
$pdi=new WPDI($table);
|
||||
|
||||
$this->assertTrue($pdi->connect());
|
||||
|
||||
}
|
||||
|
||||
public function testCreateTable()
|
||||
{
|
||||
global $pdi;
|
||||
|
||||
/*CREATE TABLE MyGuests (
|
||||
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
firstname VARCHAR(30) NOT NULL,
|
||||
lastname VARCHAR(30) NOT NULL,
|
||||
email VARCHAR(50),
|
||||
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
)*/
|
||||
|
||||
//$sql_table="DROP TABLE IF EXISTS table_test;CREATE TABLE table_test ( id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, type INT NOT NULL )";
|
||||
|
||||
$sql_table="DROP TABLE IF EXISTS table_test;";
|
||||
|
||||
$this->assertTrue($pdi->query($sql_table, []));
|
||||
|
||||
$sql_table="CREATE TABLE table_test ( \n".
|
||||
"id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n".
|
||||
"name VARCHAR(30) NOT NULL,\n".
|
||||
"last_name VARCHAR(30) NOT NULL,\n".
|
||||
"type INT NOT NULL\n".
|
||||
" )";
|
||||
|
||||
$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);
|
||||
|
||||
$result=$pdi->select(['name'], 'WHERE name=? AND type=?', ['first', 1]);
|
||||
|
||||
$arr_result=$result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$this->assertEquals([['name' => 'first']], $arr_result);
|
||||
|
||||
$this->assertEquals([['name' => 'first']], $pdi->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||
|
||||
$this->assertEquals(['name' => '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']));
|
||||
|
||||
$result=$pdi->select(['name'], 'WHERE name=? AND type=?', ['first_updated', 1]);
|
||||
|
||||
$arr_result=$result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$this->assertEquals([['name' => 'first_updated']], $arr_result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreateTable
|
||||
*/
|
||||
|
||||
public function testDropTable()
|
||||
{
|
||||
global $pdi;
|
||||
|
||||
$this->assertTrue($pdi->query('drop table table_test', []));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -70,9 +70,9 @@ final class WPDOTest extends TestCase {
|
|||
|
||||
$this->assertEquals([['name' => 'first', 0 => 'first']], $arr_result);
|
||||
|
||||
$this->assertEquals([['name' => 'first', 0 => 'first']], $pdo->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||
$this->assertEquals([['name' => 'first']], $pdo->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||
|
||||
$this->assertEquals(['name' => 'first', 0 => 'first'], $pdo->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||
$this->assertEquals(['name' => 'first'], $pdo->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1]));
|
||||
|
||||
$this->assertEquals(1, $pdo->select_count('WHERE name=?', ['first']));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue