Req #54861 [Opn->Fbk]: query() optionaly prepared and PDO::PARAM_FIELDNAME(quoting)
| From: | johannes@php.net | Date: | Thu, 26 May 2011 12:54:26 +0000 |
| Subject: | Req #54861 [Opn->Fbk]: query() optionaly prepared and PDO::PARAM_FIELDNAME(quoting) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-160974@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54861&edit=1
ID: 54861
Updated by: johannes@php.net
Reported by: harrieva at gmx dot de
Summary: query() optionaly prepared and
PDO::PARAM_FIELDNAME(quoting)
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: PDO related
Operating System: linux
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
I do not understand what you want. Could you be more precise please?
About the " vs. ` thing: You can set the SQL mode in MySQL to be more
standards compliant. The MySQL developers are conservative in changing
the default as it will break many applications unfortunately.
http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html
(can be set
per session if you don't want/can change it globally)
Previous Comments:
------------------------------------------------------------------------
[2011-05-19 13:33:54] harrieva at gmx dot de
Description:
------------
I like prepared statements and its templating. Since query returns a
statement, why not making it a prepared one, when the second parameter
is an array, and execute it directly... Example: See my exPDO-Class at
the bottom.
Since mysql quotes fieldnames(and tablenames) different then
standardconform sqlservers, it is not easy to write/generate sql that
work everywhere... Eg. postgre lowercases fieldnames when they are not
quoted in "... Mysql wants `...
I help my self by deriving from PDO and overwrite quote...
public function quote($txt,$parameter_type = PDO::PARAM_STR ){
if($parameter_type == "12345"){
if($this->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql'){
return '
' . $txt . '';
}else{
return '"' . $txt . '"';
}
}else{
return parent::quote($txt,$parameter_type);
}
}
By the way: Here is my hole extention.... Now it is possible to see all
the executed querys, and the time it took to get the result....
<?php
class extPDO extends PDO{
public $query_count = 0;
public $exec_count = 0;
public $prepared_count = 0;
public $query_time = 0;
public $sqls = array();
public function __construct($dsn, $username, $passwd,
$options=array()){
parent::__construct($dsn, $username, $passwd, $options);
self::setAttribute(PDO::ATTR_STATEMENT_CLASS,
array("extPDOStatement",array($this)));
}
public function query($statement,$args = array()){
$this->query_count++;
if(is_array($args)){
if(empty($args)){
$this->sqls[] = 'q: '.$statement;
$start = microtime(true);
$res = parent::query($statement);
$this->query_time += microtime(true) - $start;
return $res;
}else{
//keine zeitmessung da diese durchs statement übernomen wird
$res = self::prepare($statement);
$res->execute($args);
$this->prepared_count--;
return $res;
}
}else{
$res = parent::prepare($statement);
$res->execute(array($args));
$this->prepared_count--;
return $res;
}
}
public function exec($statement,$args = array()){
$this->exec_count++;
if(is_array($args)){
if(empty($args)){
$this->sqls[] = 'e: '. $statement;
$start = microtime(true);
$res = parent::exec($statement);
$this->query_time += microtime(true) - $start;
return $res;
}else{
$res = self::prepare($statement);
$res->execute($args);
$this->prepared_count--;
return $res->rowCount();
}
}else{
$res = self::prepare($statement);
$res->execute( array($args) );
$this->prepared_count--;
return $res->rowCount();
}
}
public function quote($txt,$parameter_type = PDO::PARAM_STR ){
if($parameter_type == "12345"){
if($this->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql'){
return '' . $txt . '';
}else{
return '"' . $txt . '"';
}
}else{
return parent::quote($txt,$parameter_type);
}
}
public function prepare($statement,array $options = array()){
return parent::prepare($statement,$options);
}
}
class extPDOStatement extends PDOStatement{
private $db;
protected function __construct($db){
$this->db = $db;
}
public function execute(array $input_parameters = array()){
$this->db->sqls[] = 'p: '. $this->queryString;
$this->db->prepared_count++;
$start = microtime(true);
$res = parent::execute($input_parameters);
$this->db->query_time += microtime(true) - $start;
return $res;
}
}
?>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54861&edit=1