Skip to content

Instantly share code, notes, and snippets.

@mavitm
Last active January 12, 2017 23:14
Show Gist options
  • Save mavitm/4bfa4edc746b97a0c8c783fc7201fc44 to your computer and use it in GitHub Desktop.
Save mavitm/4bfa4edc746b97a0c8c783fc7201fc44 to your computer and use it in GitHub Desktop.
<?php
/*
cms4 < cms4 system entegration illuminate framework
// composer.json
{
"name": "test/test",
"require": {
"illuminate/database": "5.2.*"
},
"autoload": {
"psr-4": {
"App\\": "yourClassDir/"
}
}
}
*/
<?php
namespace App;
use Illuminate\Database\Capsule\Manager as Capsule;
class dbMngr extends Capsule{
private $dbOpt = array();
private $sqlStr = '';
private $setArg = array();
private $queryResult = null;
public $insertID = 0, $affRows = 0, $numRows = 0, $queryCount = 0, $queryTime = 0;
public function setOpt(Array $opt){
$this->dbOpt = $opt;
return $this;
}
public function connect_db() {
$this->addConnection([
'driver' => $this->dbOpt['driver'],
'host' => $this->dbOpt['host'],
'database' => $this->dbOpt['database'],
'username' => $this->dbOpt['username'],
'password' => $this->dbOpt['password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $this->dbOpt['prefix'],
]);
$this->setAsGlobal();
$this->bootEloquent();
}
public function setSql($sql){
$this->args = null;
$this->numRows = 0;
$this->queryResult = false;
$this->queryTime = 0;
$this->sqlStr = preg_replace('/\s\s+|\t\t+/', ' ', trim($sql));;
return $this;
}
public function setArg() {
// argümanları al
$args = func_get_args();
$this->setArg = is_array($args[0]) ? $args[0] : $args;
//execute
return $this;
}
public function runSql(){
$prev = microtime(true);
if(empty($this->setArg)){
$this->queryResult = Capsule::select($this->sqlStr);
}else{
$args = $this->setArg;
if(strpos(end($this->setArg),":") !== false){
$args = array();
foreach($this->args as $k=>$v){
$nk = str_replace(':','',$k);
$args[$nk] = $v;
}
}
$this->queryResult = Capsule::select($this->sqlStr, $args);
}
$next = microtime(true);
$this->queryTime = number_format(($next - $prev), 20);
$this->numRows = count($this->queryResult);
$this->insertID = 0;
if(preg_match('/^(insert)\s/i', $this->sqlStr)){
$this->insertID = Capsule::getPdo()->lastInsertId();
}
$this->affRows = Capsule::getPdo()->rowCount();
$this->numRows = Capsule::getPdo()->rowCount();
$this->queryCount++;
}
public function result(){
return ($this->queryResult === false) ? false : true;
}
public function insertID() {
return $this->insertID;
}
public function numRows() {
return $this->numRows;
}
public function affRows() {
return $this->affRows;
}
public function queryCount() {
return $this->queryCount;
}
public function queryTime() {
return $this->queryTime;
}
public function getAll($mod = "obj"){
if (empty($this->queryResult) || is_scalar($this->queryResult)) {
return array();
}
if(!in_array($mod, array("obj","arr","num"))){
$mod = "obj";
}
if ($mod === 'arr') {
$func = create_function('&$val', 'return (array) $val;');
return array_map($func, $this->queryResult);
}elseif ($mod === 'num') {
return array_map('array_values', $this->queryResult);
}
return $this->queryResult;
}
public function getRow($sno = 1, $mod = null) {
if (empty($this->queryResult) || is_scalar($this->queryResult)) {
return array();
}
$sno = ($sno > 0 ? ($sno - 1) : 0);
if (!is_int($sno) || $sno < 0){ return array();}
if ($sno >= $this->numRows){return array();}
if(!in_array($mod, array("obj","arr","num"))){
$mod = "obj";
}
if ($mod === 'num')
return array_values((array) $this->queryResult[$sno]);
// dizi
if ($mod === 'arr')
return (array) $this->queryResult[$sno];
// nesne
if ($mod === 'obj')
return (object) $this->queryResult[$sno];
}
public function getOne() {
// sorgu boşsa veya sonuç olarak geriye bir Array/Object dönmediyse
if (empty($this->queryResult) || is_scalar($this->queryResult)) {
return NULL;
}
$arr = array_values($this->queryResult[0]);
return $arr[0];
}
public function getSql($arg = 0) {
if(!$arg){
return $this->sqlStr;
}
$a = '';
if(is_array($this->setArg)){
foreach($this->setArg as $k=>$v){
$a .= $k.':'.$v;
}
}
return $this->sqlStr.' -Arg- '.$a;
}
}
/****############################################****/
$db = new dbMngr();
$db->setOpt(
[
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]
)->connect_db();
$db->setSql("SELECT * FROM users WHERE userid = 1")->runSql();
/*
$db->setSql("SELECT * FROM users WHERE userid = :userid")->setArg([":userid"=>1])->runSql();
*/
$data = $db->getAll();
var_dump($data);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment