PDA

View Full Version : کلاس یهینه سازی PDO



nimoosh
یک شنبه 10 خرداد 1394, 13:08 عصر
سلام دوستان . یک کلاس طراحی کردم که قابل ویرایش هم است .
100% ایرادات زیادی داره اما برای من کار را خیلی راه انداخت . مثلا هنوز bind رو اجرا نکردم که در ورژن بعدی با یک سری تابع دیگر ای کار رو خواهم کرد . ممنون .
تقاضا دارم با review کردن این کلاس کمک کنید تا کلاس بالا بیاد . توضبحات مربوط به کلاس :

LINK : https://sourceforge.net/projects/nimapdoclass/files/

How to use This class



in this class we have 7 functions as ( Insert , select , select one row , select with Like statement , Delete , Update , Select random ) with just one line coding .
the main thing is very good is that , i create this class with Pdo connection that has a good security for your web site.





Example of insert

$obj= new nimaPDo;
$table='tableNAme';
$params=array('col1','col2','col3');
$values=array('val1','val2','val3');
$obj->insert($table,$params,$values);


Example of Update

$obj= new nimaPDo;
$table='tableNAme';
$ceriteria="id='5'";
$params=array('col1','col2','col3');
$values=array('val1','val2','val3');
$obj->update($table,$params,$values,$ceriteria);


Example of select

$obj= new nimaPDo;
$table='tableNAme';
$params=array('col1','col2','col3');
$values=array('val1','val2','val3');
$obj->select($table,$params,$values);


Example of select like

$obj= new nimaPDo;
$table='tableNAme';
$params=array('col1','col2','col3');
$values=array('val1','val2','val3');
$obj->select_like($table,$params,$values);


Example of select One

$obj= new nimaPDo;
$table='tableNAme';
$params=array('col1','col2','col3');
$values=array('val1','val2','val3');
$obj->select_one($table,$params,$values);


Example of select random

$obj= new nimaPDo;
$table='tableNAme';
$params=array('col1','col2','col3');
$values=array('val1','val2','val3');
$obj->select_rand($table,$params,$values);

H:Shojaei
یک شنبه 10 خرداد 1394, 14:23 عصر
ببخشید رک میگم کلاس ضعیفیه عملا با کدهای معمولی عملیات رو انجام بدیم بهتر از این کلاس استفاده کردنه هیچ مزیتی واسه ساده کردن عملیات یا سرعت بخشیدن یا حتی ذره ای امنیت بیشتر نسبت به حالت عادی بدون کلاس اختصاصی کد زدن ندارید!!!
شما بخواید یک کلاس، تابع یا هر چیزی رو بسازید و گسترش بدین اول خودتون باید توی اون موضوع طوری کار کنید که اولین ورژن اون حتی از عملیاتی که اون کار به صورت عادی انجام میشه یه چیزی بیشتر داشته باشه...

rezaonline.net
یک شنبه 10 خرداد 1394, 16:47 عصر
کلاست خوبه ولی خب از این کلاس بنده یه خورده الگو بگیری بدی نیست


class RxDb
{
private $sql = NULL;
private $params = array();

private $method = '';

public static $log = array();


public $pdo = NULL;

function __construct($_1,$_2,$_3)
{
$this->pdo = new PDO($_1,$_2,$_3);

$version = 5.3;
$encoding = ($version >= 5.5) ? 'utf8mb4' : 'utf8';
$this->pdo->query("set names {$encoding}")->execute();
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
public function query($sql,$params=array())
{
$this->sql = $sql ;
$this->params = $params;
$this->method = __FUNCTION__;

return $this;
}

function get($tbl='',$condition='',$params=array())
{
if(is_array($tbl))
{
$f = $tbl[0];
$tbl = $tbl[1];
}
else
{
$f = '*';
}
if(empty($condition))
$c = '';
else
$c = " WHERE $condition ";
$this->sql = "SELECT {$f} FROM `{$tbl}` {$c}";
$this->params = $params;
return $this;
}

function row($fetch='object')
{
$_key = array(
'object'=> PDO::FETCH_OBJ ,
'array'=> PDO::FETCH_ASSOC ,
'num'=> PDO::FETCH_NUM
);


$stmt = $this->pdo->prepare($this->sql);
foreach($this->params as $key=>$val)
{
if(is_int($key))
{
$key = $key+1;
$stmt->bindValue($key, $val);
}
else
$stmt->bindParam($key, $val);
}
$stmt->execute();

$this->logQuery();

return $stmt->fetch($_key[$fetch]);


}

function all($fetch='object')
{
$_key = array(
'object'=> PDO::FETCH_OBJ ,
'array'=> PDO::FETCH_ASSOC ,
'num'=> PDO::FETCH_NUM
);

$stmt = $this->pdo->prepare($this->sql);

foreach($this->params as $key=>$val)
{
if(is_int($key))
{
$key = $key+1;
$stmt->bindValue($key, $val);
}
else
$stmt->bindParam($key, $val);
}
$stmt->execute();

$this->logQuery();
return $stmt->fetchAll($_key[$fetch]);

}


function execute($params=array())
{
if( ! empty($params))
$this->params = $params;

if(empty($this->params))
{
$ret = $this->pdo->prepare($this->sql);

$this->logQuery();
return $ret->execute();
}
else
{
$stmt = $this->pdo->prepare($this->sql);
foreach($this->params as $key=>$val)
{
if(is_int($key))
{
$key = $key+1;
$stmt->bindValue($key, $val);
}
else
$stmt->bindParam($key, $val);
}

$ret =$stmt->execute();

$this->logQuery();
return $ret;
}
}




protected function logQuery()
{
$qr = $this->sql ;
if( ! empty($this->params))
{
$qr .=" | ";
foreach($this->params as $key=>$val)
$qr .= " {$key}=>{$val}";
}

self::$log[] = $qr;
$this->params = array();
$this->sql = '';
}



//query Builder
function insert($tbl,$data=array())
{
$key = join(',',array_keys($data));

$val = str_repeat('?,', intval(count(array_keys($data))));

$val = trim($val,',');

$qr = "INSERT INTO `{$tbl}` ($key) values($val) ";
return $this->query($qr)->execute(array_values($data));
}


function update($tbl='',$data=array(),$condition=' 1=1',$params = array())
{

$qr = "update `{$tbl}` set ";

$newData = array();
foreach($data as $key=>$vals)
{
$qr .="`{$key}`=?,";
$newData[] = $vals;
}

foreach($params as $row)
$newData[] = $row;

$qr = rtrim($qr,',');
$qr .= " WHERE {$condition} ";

return $this->query($qr)->execute(array_values($newData));
}


function delete($tbl='',$condition='',$params=array())
{
if( ! empty($condition))
$wh = " where ({$condition})";
else
$wh = '';
return $this->query("delete from `{$tbl}` {$wh}")->execute($params);
}

function __destruct()
{
unset($this->pdo);
$this->pdo = NULL;
}
}


اینم مثال

$db = new RxDB('mysql:dbname=mydatabase;host=localhost', 'db_username', 'db_password');

//insert
$af = $db->insert('tbl',array(
'name'=>'reza',
'age'=>30
));

if($af>0)
echo true;

//update
$af = $db->update('tbl',array(
'name'=>'reza',
'age'=>30
),
'id=?' ,
array(
1
)
);


//delete
$af = $db->delete('tbl','id=?',array($id));


//read
$row = $db->find('id=1');
$all = $db->findAll('name=?',array('reza')) ;

$row = $db->query('select * from users where id>5')->row();

$all = $db->query("select * from users where `id`>? limit 10",array($id))->all();

$all = $db->get("users","`id` < ? limit 10",array($id))->all();

$all = $db->get(array('id,username,password',"users"),"`id` < ? limit 10",array($id))->all();


//query

$row = $db->query("select * from user where id=? and title=?",array($id,$title))->row();

$all = $db->query("select * from user")->all();


//update , delete
$db->query("update users set username=? where id=?")->execute(array($username,$id));



// log

$log = $db::$log;
print_r($log);

nimoosh
یک شنبه 10 خرداد 1394, 18:55 عصر
مرسی از انتقادات سازنده دوستان . همینکه زمان گذاشتید و استفاده کردید و نظر دادید یک دنیا متشکرم .
این کلاس رو من در حدود نیم ساعت برای یک پروژه طراحی کردم . گفتم ارایه بدم تا بشه بیشتر هم روش کار کرد و راحترش کرد . باز هم روش کار میکنم .تشکر از دوستان عزیز