[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: PgSQLPreparedStatement.php,v 1.14 2005/04/16 18:55:28 hlellelid Exp $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://creole.phpdb.org>. 20 */ 21 22 require_once 'creole/PreparedStatement.php'; 23 require_once 'creole/common/PreparedStatementCommon.php'; 24 25 /** 26 * PgSQL subclass for prepared statements. 27 * 28 * @author Hans Lellelid <hans@xmpl.org> 29 * @version $Revision: 1.14 $ 30 * @package creole.drivers.pgsql 31 */ 32 class PgSQLPreparedStatement extends PreparedStatementCommon implements PreparedStatement { 33 34 /** 35 * Quotes string using native pgsql function (pg_escape_string). 36 * @param string $str 37 * @return string 38 */ 39 protected function escape($str) 40 { 41 return pg_escape_string($str); 42 } 43 44 /** 45 * Recursive function to turn multi-dim array into str representation. 46 * @param array $arr 47 * @return string Array in pgsql-friendly string notation: {val1, val2} or {{sub1,sub2}, {sub3, sub4}} 48 */ 49 private function arrayToStr($arr) 50 { 51 $parts = array(); 52 foreach((array)$arr as $el) { 53 if (is_array($el)) { 54 $parts[] = $this->arrayToStr($el); 55 } else { 56 if (is_string($el)) { 57 $parts[] = '"' . $this->escape($el) . '"'; 58 } else { 59 $parts[] = $el; 60 } 61 } 62 } 63 return '{' . implode(',', $parts) . '}'; 64 } 65 66 /** 67 * Sets an array. 68 * Unless a driver-specific method is used, this means simply serializing 69 * the passed parameter and storing it as a string. 70 * @param int $paramIndex 71 * @param array $value 72 * @return void 73 * @see PreparedStatement::setArray() 74 */ 75 function setArray($paramIndex, $value) 76 { 77 if( $paramIndex > $this->positionsCount || $paramIndex < 1) { 78 throw new SQLException('Cannot bind to invalid param index: '.$paramIndex); 79 } 80 if ($value === null) 81 $this->setNull($paramIndex); 82 else 83 $this->boundInVars[$paramIndex] = "'" . $this->arrayToStr($value) . "'"; 84 } 85 86 /** 87 * For setting value of Postgres BOOLEAN column. 88 * @param int $paramIndex 89 * @param boolean $value 90 * @return void 91 */ 92 function setBoolean($paramIndex, $value) 93 { 94 if( $paramIndex > $this->positionsCount || $paramIndex < 1) { 95 throw new SQLException('Cannot bind to invalid param index: '.$paramIndex); 96 } 97 if ($value === null) 98 $this->setNull($paramIndex); 99 else 100 $this->boundInVars[$paramIndex] = ($value ? "'t'" : "'f'"); 101 } 102 103 /** 104 * Applies sqlite_udf_encode_binary() to ensure that binary contents will be handled correctly by sqlite. 105 * @param int $paramIndex 106 * @param mixed $blob Blob object or string containing data. 107 * @return void 108 */ 109 function setBlob($paramIndex, $blob) 110 { 111 if ($blob === null) { 112 $this->setNull($paramIndex); 113 } else { 114 // they took magic __toString() out of PHP5.0.0; this sucks 115 if (is_object($blob)) { 116 $blob = $blob->__toString(); 117 } 118 $this->boundInVars[$paramIndex] = "'" . pg_escape_bytea( $blob ) . "'"; 119 } 120 121 } 122 123 /** 124 * @param int $paramIndex 125 * @param string $value 126 * @return void 127 */ 128 function setTime($paramIndex, $value) 129 { 130 if ($value === null) { 131 $this->setNull($paramIndex); 132 } else { 133 if ( is_numeric ( $value ) ) { 134 $value = date ( "H:i:s O", $value ); 135 } elseif ( is_object ( $value ) ) { 136 $value = date ( "H:i:s O", $value->getTime ( ) ); 137 } 138 $this->boundInVars [ $paramIndex ] = "'" . $this->escape ( $value ) . "'"; 139 } 140 } 141 142 /** 143 * @param int $paramIndex 144 * @param string $value 145 * @return void 146 */ 147 function setTimestamp($paramIndex, $value) 148 { 149 if ($value === null) { 150 $this->setNull($paramIndex); 151 } else { 152 if (is_numeric($value)) $value = date('Y-m-d H:i:s O', $value); 153 elseif (is_object($value)) $value = date("Y-m-d H:i:s O", $value->getTime()); 154 $this->boundInVars[$paramIndex] = "'".$this->escape($value)."'"; 155 } 156 } 157 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |