Added files
This commit is contained in:
commit
bdab68afd3
5 changed files with 230 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
*~
|
||||||
|
vendor/*
|
||||||
0
README.md
Normal file
0
README.md
Normal file
20
composer.json
Normal file
20
composer.json
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"name": "phangoapp/WPDO",
|
||||||
|
"description": "A simple class for create models",
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "~4.8@dev"
|
||||||
|
},
|
||||||
|
"license": "GPL3",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Antonio de la Rosa",
|
||||||
|
"email": "webmaster@web-t-sys.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"PhangoApp\\PhaModels\\": "src"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
165
src/WPDO.php
Normal file
165
src/WPDO.php
Normal file
|
|
@ -0,0 +1,165 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
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='';
|
||||||
|
|
||||||
|
static public $db='';
|
||||||
|
|
||||||
|
static public $login_db='';
|
||||||
|
|
||||||
|
static public $pass_db='';
|
||||||
|
|
||||||
|
static public $driver='mysql';
|
||||||
|
|
||||||
|
public $conn;
|
||||||
|
|
||||||
|
public $sth;
|
||||||
|
|
||||||
|
public $table;
|
||||||
|
|
||||||
|
//static public $conn=false;
|
||||||
|
|
||||||
|
public function __construct($table) {
|
||||||
|
|
||||||
|
$this->table=$table;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function connect() {
|
||||||
|
|
||||||
|
if(WPDO::$host_db=='' || WPDO::$db=='') {
|
||||||
|
|
||||||
|
echo 'Cannot connect to the mysqldb';
|
||||||
|
|
||||||
|
exit();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function query($query, $values) {
|
||||||
|
|
||||||
|
$this->sth=$this->conn->prepare($query);
|
||||||
|
|
||||||
|
$this->sth->execute([$values]);
|
||||||
|
|
||||||
|
return $this->sth->execute($values);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select($fields, $where_sql, $values) {
|
||||||
|
|
||||||
|
$fields=$this->check_fields($fields);
|
||||||
|
|
||||||
|
$query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql;
|
||||||
|
|
||||||
|
$this->sth=$this->conn->prepare($query);
|
||||||
|
|
||||||
|
$this->sth->execute($values);
|
||||||
|
|
||||||
|
return $this->sth->execute($values);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select_to_array($fields, $where_sql, $values) {
|
||||||
|
|
||||||
|
$this->sth=$this->select($fields, $where_sql, $values);
|
||||||
|
|
||||||
|
return $this->sth->fetchAll();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function select_a_row($fields, $where_sql, $values) {
|
||||||
|
|
||||||
|
$this->sth=$this->select($fields, $where_sql, $values);
|
||||||
|
|
||||||
|
$rows=$this->sth->fetchAll();
|
||||||
|
|
||||||
|
if(count($rows)>0) {
|
||||||
|
|
||||||
|
return $rows[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function insert($fields, $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);
|
||||||
|
|
||||||
|
$quest=implode(',', $fields);
|
||||||
|
|
||||||
|
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.array_fill(0, count($final_fields), '?').')';
|
||||||
|
|
||||||
|
$this->sth=$this->conn->prepare($query);
|
||||||
|
|
||||||
|
return $this->sth->execute($values);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function check_fields($fields) {
|
||||||
|
|
||||||
|
if($fields[0]!='*') {
|
||||||
|
|
||||||
|
$final_fields=[];
|
||||||
|
|
||||||
|
foreach($field as $fields) {
|
||||||
|
|
||||||
|
if(in_array($field, $this->table->fields)) {
|
||||||
|
|
||||||
|
$final_fields[]=$field;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$str_fields=implode(', ', $final_fields);
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
$str_fields='*';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($final_fields)==0) {
|
||||||
|
|
||||||
|
throw new Exception('No valid fields selected in '.$this->table->name);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return $final_fields;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
43
tests/WPDOTest.php
Normal file
43
tests/WPDOTest.php
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PhangoApp\WPDO;
|
||||||
|
use PhangoApp\PhaUtils\Utils;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
include("vendor/autoload.php");
|
||||||
|
|
||||||
|
Utils::load_config('config', '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 testCreateTable()
|
||||||
|
{
|
||||||
|
global $pdo;
|
||||||
|
|
||||||
|
$this->assertTrue($pdo->query($sql_table));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testCreateTable
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testDropTable()
|
||||||
|
{
|
||||||
|
global $pdo;
|
||||||
|
|
||||||
|
$this->assertTrue($pdo->query('drop table table_test'));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue