00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once 'creole/PreparedStatement.php';
00023 require_once 'creole/common/PreparedStatementCommon.php';
00024
00032 class PgSQLPreparedStatement extends PreparedStatementCommon implements PreparedStatement {
00033
00039 protected function escape($str)
00040 {
00041 return pg_escape_string($str);
00042 }
00043
00049 private function arrayToStr($arr)
00050 {
00051 $parts = array();
00052 foreach((array)$arr as $el) {
00053 if (is_array($el)) {
00054 $parts[] = $this->arrayToStr($el);
00055 } else {
00056 if (is_string($el)) {
00057 $parts[] = '"' . $this->escape($el) . '"';
00058 } else {
00059 $parts[] = $el;
00060 }
00061 }
00062 }
00063 return '{' . implode(',', $parts) . '}';
00064 }
00065
00075 function setArray($paramIndex, $value)
00076 {
00077 if( $paramIndex > $this->positionsCount || $paramIndex < 1) {
00078 throw new SQLException('Cannot bind to invalid param index: '.$paramIndex);
00079 }
00080 if ($value === null)
00081 $this->setNull($paramIndex);
00082 else
00083 $this->boundInVars[$paramIndex] = "'" . $this->arrayToStr($value) . "'";
00084 }
00085
00092 function setBoolean($paramIndex, $value)
00093 {
00094 if( $paramIndex > $this->positionsCount || $paramIndex < 1) {
00095 throw new SQLException('Cannot bind to invalid param index: '.$paramIndex);
00096 }
00097 if ($value === null)
00098 $this->setNull($paramIndex);
00099 else
00100 $this->boundInVars[$paramIndex] = ($value ? "'t'" : "'f'");
00101 }
00102
00109 function setBlob($paramIndex, $blob)
00110 {
00111 if ($blob === null) {
00112 $this->setNull($paramIndex);
00113 } else {
00114
00115 if (is_object($blob)) {
00116 $blob = $blob->__toString();
00117 }
00118 $this->boundInVars[$paramIndex] = "'" . pg_escape_bytea( $blob ) . "'";
00119 }
00120
00121 }
00122
00128 function setTime($paramIndex, $value)
00129 {
00130 if ($value === null) {
00131 $this->setNull($paramIndex);
00132 } else {
00133 if ( is_numeric ( $value ) ) {
00134 $value = date ( "H:i:s O", $value );
00135 } elseif ( is_object ( $value ) ) {
00136 $value = date ( "H:i:s O", $value->getTime ( ) );
00137 }
00138 $this->boundInVars [ $paramIndex ] = "'" . $this->escape ( $value ) . "'";
00139 }
00140 }
00141
00147 function setTimestamp($paramIndex, $value)
00148 {
00149 if ($value === null) {
00150 $this->setNull($paramIndex);
00151 } else {
00152 if (is_numeric($value)) $value = date('Y-m-d H:i:s O', $value);
00153 elseif (is_object($value)) $value = date("Y-m-d H:i:s O", $value->getTime());
00154 $this->boundInVars[$paramIndex] = "'".$this->escape($value)."'";
00155 }
00156 }
00157 }