Created
January 19, 2017 13:07
-
-
Save annibuliful/e84763da3bfda6f077a190fac96e3f01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require dirname(__DIR__).'\config\DB.php'; | |
require dirname(__DIR__).'\util\array.php'; | |
class buildquery extends DB_config | |
{ | |
private $pdo; | |
private $array_tool; | |
private $param; | |
public function __construct() | |
{ | |
$this->pdo = new PDO($this->dsn, $this->user, $this->password); | |
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$this->array_tool = new array_tool(); | |
} | |
/* | |
* ฟังก์ชั่นในการเลือกข้อมูล | |
* @param string $table ชื่อ table ที่ต้องการ | |
* @param array $columns ชื่อ columns ที่ต้องการ | |
* @return string $sql คือ SQL command | |
*/ | |
public function select(string $table, array $columns = null) | |
{ | |
$sql = 'SELECT '; | |
if ($columns == null) { | |
$sql .= "* FROM {$table}"; | |
} elseif ($columns != null) { | |
$columns_size = (int) count($columns); | |
$columns_num = (int) count($columns) - 1; | |
for ($i = 0; $i < $columns_size; ++$i) { | |
if ($i < $columns_num) { | |
$sql .= "{$columns[$i]},"; | |
} else { | |
$sql .= "{$columns[$i]} FROM {$table}"; | |
} | |
} | |
} | |
return (string) $sql; | |
} | |
/* | |
* ฟังก์ชั่นกำหนดเงื่อนไข | |
* @param $sql คือ SQL command | |
* @param array $condition เป็นเงื่อนไขที่ต้องการ | |
*/ | |
public function where(array $condition) | |
{ | |
$param = array(); | |
$sql .= " WHERE "; | |
$condition_size = (int)count($condition); | |
$condition_num = (int)count($condition) - 1; | |
for ($i=0; $i < $condition_size ; $i++) { | |
if ($i < $condition_num) { | |
$sql .= "{$condition[$i][0]} :? ,"; | |
}else { | |
$sql.= "{$condition[$i][0]} :? ;"; | |
} | |
} | |
for ($i=0; $i < $condition_size ; $i++) { | |
array_push($param,$condition[$i][1]); | |
} | |
print_r($param); | |
} | |
/* | |
* ฟังก์ชั่นสำหรับ execute SQL command | |
* @param string $sql คำสั่ง SQL ที่ต้องการ*/ | |
public function exec(string $sql,array $param) | |
{ | |
$param_size = (int)count($param); | |
$param_num = (int)count($param) - 1; | |
for ($i=0; $i <$param_size ; $i++) { | |
} | |
} | |
/* | |
* ฟังก์ชั่นในการเพิ่มข้อมูลเข้า DB | |
* @param string $table คือ table ที่เราต้องการจะเพิ่มข้อมูล | |
* @param array $columns คือ columns ที่ต้องการจะเพิ่มข้อมูล | |
* @param array $values คือค่าที่ต้องการเพิ่มลง | |
* @return $sql | |
* @assert insert('test','',array('xxx','yyy')); | |
* @assert insert('test',array('test1','test2'),array('xxx','yyy')); | |
*/ | |
public function insert(string $table, array $columns = null, array $values) | |
{ | |
$sql = ''; | |
if ($columns == null) { | |
$sql = "INSERT INTO {$table} VALUES ("; | |
$values_size = (int) count($values); | |
$values_num = (int) count($values) - 1; | |
for ($i = 0; $i < $values_size; ++$i) { | |
if ($i < $values_num) { | |
$sql .= ":{$i} ,"; | |
} elseif ($i == $values_num) { | |
$sql .= ":{$i} );"; | |
} | |
} | |
$pdo = $this->pdo->prepare($sql); | |
for ($i = 0; $i < $values_size; ++$i) { | |
$param = ':'.$i; | |
$pdo->bindParam($param, $values[$i]); | |
} | |
$pdo->execute(); | |
} elseif ($columns != null) { | |
$sql = "INSERT INTO {$table}("; | |
$columns_size = (int) count($columns); | |
$columns_num = (int) count($columns) - 1; | |
for ($i = 0; $i < $columns_size; ++$i) { | |
if ($i < $columns_num) { | |
$sql .= "{$columns[$i]},"; | |
} elseif ($i == $columns_num) { | |
$sql .= "{$columns[$i]}) VALUES ("; | |
} | |
} | |
$values_size = (int) count($columns); | |
$values_num = (int) count($columns) - 1; | |
for ($i = 0; $i < $values_size; ++$i) { | |
if ($i < $values_size_num) { | |
$sql .= ":{$i},"; | |
} elseif ($i == $values_num) { | |
$sql .= ":{$i});"; | |
} | |
} | |
$pdo = $this->pdo->prepare($sql); | |
for ($i = 0; $i < $columns_size; ++$i) { | |
$param = ':'.$i; | |
$pdo->bindParam($param, $values[$i]); | |
} | |
$pdo->execute(); | |
} | |
} | |
} | |
require dirname(__DIR__).'\database\FluentPDO\FluentPDO.php'; | |
$s = new PDO('mysql:dbname=test;host=127.0.0.1','root','@PeNtesterMYSQL'); | |
$s1 = new FluentPDO($s); | |
$s2 = $s1->from('test1')->execute()->fetchAll(); | |
print_r($s2); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment