commit bdab68afd33ce6e1ac9ae374d27e8d3864346e14 Author: absurdo Date: Fri Oct 27 14:41:39 2023 +0200 Added files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9300fb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +vendor/* diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3b392a5 --- /dev/null +++ b/composer.json @@ -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" + } + } +} diff --git a/src/WPDO.php b/src/WPDO.php new file mode 100644 index 0000000..af39391 --- /dev/null +++ b/src/WPDO.php @@ -0,0 +1,165 @@ +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; + + } + +} diff --git a/tests/WPDOTest.php b/tests/WPDOTest.php new file mode 100644 index 0000000..e70f9e0 --- /dev/null +++ b/tests/WPDOTest.php @@ -0,0 +1,43 @@ +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')); + + } + +}