Added wtable in other file
This commit is contained in:
parent
e0b8e8e8e3
commit
731bdfcf82
3 changed files with 65 additions and 37 deletions
30
src/WPDO.php
30
src/WPDO.php
|
|
@ -2,21 +2,6 @@
|
|||
|
||||
namespace PhangoApp\WPDO;
|
||||
|
||||
|
||||
class WTable {
|
||||
|
||||
public $table;
|
||||
public $fields;
|
||||
|
||||
public function __construct($table, $fields) {
|
||||
|
||||
$this->name=$table;
|
||||
$this->fields=$fields;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class WPDO {
|
||||
|
||||
static public $host_db='';
|
||||
|
|
@ -49,19 +34,21 @@ class WPDO {
|
|||
|
||||
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);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public function query($query, $values) {
|
||||
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
$this->sth->execute([$values]);
|
||||
//$this->sth->execute([$values]);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
|
||||
|
|
@ -75,8 +62,6 @@ class WPDO {
|
|||
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
$this->sth->execute($values);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
|
||||
}
|
||||
|
|
@ -143,18 +128,16 @@ class WPDO {
|
|||
|
||||
}
|
||||
|
||||
$str_fields=implode(', ', $final_fields);
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$str_fields='*';
|
||||
$final_fields=['*'];
|
||||
|
||||
}
|
||||
|
||||
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
19
src/WTable.php
Normal 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use PhangoApp\WPDO;
|
||||
use PhangoApp\WPDO\WPDO;
|
||||
use PhangoApp\WPDO\WTable;
|
||||
use PhangoApp\PhaUtils\Utils;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
|
@ -9,23 +10,47 @@ include("libraries/Utils.php");
|
|||
|
||||
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 {
|
||||
|
||||
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()
|
||||
{
|
||||
global $pdo;
|
||||
|
||||
$this->assertTrue($pdo->query($sql_table));
|
||||
$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, []));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @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']));
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +62,7 @@ final class WPDOTest extends TestCase {
|
|||
{
|
||||
global $pdo;
|
||||
|
||||
$this->assertTrue($pdo->query('drop table table_test'));
|
||||
$this->assertTrue($pdo->query('drop table table_test', []));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue