Fixes for complete test

This commit is contained in:
absurdo 2023-10-28 01:31:28 +02:00
parent 731bdfcf82
commit 43dc5639b3
3 changed files with 111 additions and 19 deletions

View file

@ -14,18 +14,28 @@ class WPDO {
static public $driver='mysql';
static public $mconn=null;
public $conn;
public $sth;
public $table;
public $last_query;
//static public $conn=false;
public function __construct($table) {
$this->table=$table;
if(WPDO::$mconn!=null) {
$this->conn=WPDO::$mconn;
}
}
public function connect() {
@ -38,9 +48,29 @@ class WPDO {
}
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
if(WPDO::$mconn==null) {
return true;
$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;
}
}
@ -58,35 +88,74 @@ class WPDO {
$fields=$this->check_fields($fields);
$query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql;
$str_fields=implode('`, `', $fields);
$query='select `'.$str_fields.'` from '.$this->table->name.' '.$where_sql;
$this->sth=$this->conn->prepare($query);
return $this->sth->execute($values);
$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) {
$this->sth=$this->select($fields, $where_sql, $values);
if($this->select($fields, $where_sql, $values)) {
return $this->sth->fetchAll();
return $this->sth->fetchAll();
}
else {
return false;
}
}
public function select_a_row($fields, $where_sql, $values) {
$this->sth=$this->select($fields, $where_sql, $values);
if($this->select($fields, $where_sql.' limit 1', $values)) {
$rows=$this->sth->fetchAll();
$rows=$this->sth->fetchAll();
if(count($rows)>0) {
if(count($rows)>0) {
return $rows[0];
return $rows[0];
}
}
else {
return false;
}
return [];
}
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;
}
}
@ -100,12 +169,10 @@ class WPDO {
}
$final_fields=implode('`', $fields);
$quest=implode(',', $fields);
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.array_fill(0, count($final_fields), '?').')';
$final_fields=implode('`, `', $fields);
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.implode(',', array_fill(0, count($fields), '?')).')';
//echo $query;
$this->sth=$this->conn->prepare($query);
return $this->sth->execute($values);
@ -118,7 +185,7 @@ class WPDO {
$final_fields=[];
foreach($field as $fields) {
foreach($fields as $field) {
if(in_array($field, $this->table->fields)) {

View file

@ -6,6 +6,7 @@ namespace PhangoApp\WPDO;
class WTable {
public $table;
public $name;
public $fields;
public function __construct($table, $fields) {

View file

@ -54,6 +54,30 @@ final class WPDOTest extends TestCase {
}
/**
* @depends testInsert
*/
public function testSelect() {
global $pdo;
$this->assertEquals('table_test', $pdo->table->name);
$this->assertTrue($pdo->select(['name'], 'WHERE name=? AND type=?', ['first', 1]));
$arr_result=$pdo->sth->fetchAll();
$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', 0 => 'first'], $pdo->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1]));
$this->assertEquals(1, $pdo->select_count('WHERE name=?', ['first']));
}
/**
* @depends testCreateTable
*/