Added wtable in other file

This commit is contained in:
absurdo 2023-10-27 23:12:08 +02:00
parent e0b8e8e8e3
commit 731bdfcf82
3 changed files with 65 additions and 37 deletions

View file

@ -2,21 +2,6 @@
namespace PhangoApp\WPDO; namespace PhangoApp\WPDO;
class WTable {
public $table;
public $fields;
public function __construct($table, $fields) {
$this->name=$table;
$this->fields=$fields;
}
}
class WPDO { class WPDO {
static public $host_db=''; static public $host_db='';
@ -49,19 +34,21 @@ class WPDO {
echo 'Cannot connect to the mysqldb'; echo 'Cannot connect to the mysqldb';
exit(); return false;
} }
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db); $this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
return true;
} }
public function query($query, $values) { public function query($query, $values) {
$this->sth=$this->conn->prepare($query); $this->sth=$this->conn->prepare($query);
$this->sth->execute([$values]); //$this->sth->execute([$values]);
return $this->sth->execute($values); return $this->sth->execute($values);
@ -74,8 +61,6 @@ class WPDO {
$query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql; $query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql;
$this->sth=$this->conn->prepare($query); $this->sth=$this->conn->prepare($query);
$this->sth->execute($values);
return $this->sth->execute($values); return $this->sth->execute($values);
@ -128,7 +113,7 @@ class WPDO {
} }
public function check_fields($fields) { public function check_fields($fields) {
if($fields[0]!='*') { if($fields[0]!='*') {
$final_fields=[]; $final_fields=[];
@ -142,19 +127,17 @@ class WPDO {
} }
} }
$str_fields=implode(', ', $final_fields);
} }
else { else {
$str_fields='*'; $final_fields=['*'];
} }
if(count($final_fields)==0) { if(count($final_fields)==0) {
throw new Exception('No valid fields selected in '.$this->table->name); throw new \Exception('No valid fields selected in '.$this->table->name.' Valid fields are '.implode(', ', $this->table->fields).' . Fields passed are: '.implode(', ', $fields));
} }
@ -163,3 +146,4 @@ class WPDO {
} }
} }

19
src/WTable.php Normal file
View file

@ -0,0 +1,19 @@
<?php
namespace PhangoApp\WPDO;
class WTable {
public $table;
public $fields;
public function __construct($table, $fields) {
$this->name=$table;
$this->fields=$fields;
}
}

View file

@ -1,6 +1,7 @@
<?php <?php
use PhangoApp\WPDO; use PhangoApp\WPDO\WPDO;
use PhangoApp\WPDO\WTable;
use PhangoApp\PhaUtils\Utils; use PhangoApp\PhaUtils\Utils;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -9,26 +10,50 @@ include("libraries/Utils.php");
Utils::load_config('config_test', 'settings'); Utils::load_config('config_test', 'settings');
$sql_table="CREATE TABLE table_test (
`name` VARCHAR(255) NOT NULL default '',
`last_name` VARCHAR(255) NOT NULL default '',
`type` INT NOT NULL
);";
$pdo=new WPDO('table_test', ['name', 'last_name', 'type']);
$pdo->connect();
final class WPDOTest extends TestCase { final class WPDOTest extends TestCase {
public function testConnect()
{
global $pdo;
$table=new WTable('table_test', ['name', 'last_name', 'type']);
$pdo=new WPDO($table);
$this->assertTrue($pdo->connect());
}
public function testCreateTable() public function testCreateTable()
{ {
global $pdo; global $pdo;
$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($pdo->query($sql_table)); $this->assertTrue($pdo->query($sql_table, []));
} }
/**
* @depends testCreateTable
*/
public function testInsert() {
global $pdo;
$this->assertEquals('table_test', $pdo->table->name);
$this->assertTrue($pdo->insert(['name', 'last_name', 'type'], ['first', 'last', '1']));
}
/** /**
* @depends testCreateTable * @depends testCreateTable
*/ */
@ -37,7 +62,7 @@ final class WPDOTest extends TestCase {
{ {
global $pdo; global $pdo;
$this->assertTrue($pdo->query('drop table table_test')); $this->assertTrue($pdo->query('drop table table_test', []));
} }