18 if ( !defined(
'E_USER_ERROR') ) define(
'E_USER_ERROR',256);
90 function __construct( $connection_string, $dbuser=null, $dbpass=null, $options=null ) {
91 if ( preg_match(
'/^(pgsql):/', $connection_string, $matches ) ) {
92 $this->dialect = $matches[1];
95 error_log(
"Unable to connect to database: ". $e->getMessage() );
96 trigger_error(
"Unsupported database connection '".$connection_string.
"'",E_USER_ERROR);
99 $this->db =
new PDO( $connection_string, $dbuser, $dbpass, $options );
100 }
catch (PDOException $e) {
101 error_log(
"Unable to connect to database: ". $e->getMessage() );
102 if ( function_exists(
'trigger_error') )
103 trigger_error(
"PDO connection error '".$connection_string.
"': ".$e->getMessage(),E_USER_ERROR);
114 if ( !isset($this->dialect) ) {
115 trigger_error(
"Unsupported database dialect",E_USER_ERROR);
118 switch ( $this->dialect ) {
120 if ( $search_path == null ) $search_path =
'public';
121 $sql =
"SET search_path TO " . $this->
Quote( $search_path,
'identifier' );
133 if ( !isset($this->dialect) ) {
134 trigger_error(
"Unsupported database dialect", E_USER_ERROR);
139 switch ( $this->dialect ) {
141 $sql =
"SELECT version()";
142 if ( $sth = $this->db->query($sql) ) {
143 $row = $sth->fetch(PDO::FETCH_NUM);
144 $version .= preg_replace(
'/^PostgreSQL (\d+\.\d+)\..*$/i',
'$1', $row[0]);
161 if ( !isset($this->dialect) ) {
162 trigger_error(
"Unsupported database dialect", E_USER_ERROR);
165 switch ( $this->dialect ) {
167 list( $schema, $table ) = explode(
'.', $tablename_string, 2);
168 if ( empty($table) ) {
169 $table = $tablename_string;
173 $sql =
'SELECT f.attname AS fieldname, t.typname AS typename, f.atttypmod AS precision FROM pg_attribute f';
174 $sql .=
' JOIN pg_class c ON ( f.attrelid = c.oid )';
175 $sql .=
' JOIN pg_type t ON ( f.atttypid = t.oid )';
176 $sql .=
' JOIN pg_namespace ns ON ( c.relnamespace = ns.oid )';
177 $sql .=
' WHERE relname = '.$this->Quote($table,PDO::PARAM_STR).
' AND attnum >= 0 ';
178 if ( isset($schema) ) $sql .=
' AND ns.nspname = '.$this->Quote($schema,PDO::PARAM_STR);
179 $sql .=
' ORDER BY f.attnum';
213 function Quote( $value, $value_type = null ) {
214 if ( isset($value_type) && $value_type ==
'identifier' ) {
215 if ( $this->dialect ==
'mysql' ) {
217 $rv =
'`' . str_replace(
'`',
'\\`', $value ) .
'`';
220 $rv =
'"' . str_replace(
'"',
'\\"', $value ) .
'"';
225 if ( !isset($value_type) ) {
226 if ( !isset($value) ) $value_type = PDO::PARAM_NULL;
227 elseif ( is_bool($value) ) $value_type = PDO::PARAM_BOOL;
228 elseif ( is_float($value) ) $value_type = PDO::PARAM_INT;
229 elseif ( is_numeric($value)) {
230 if ( preg_match(
'{^(19|20)\d\d(0[1-9]|1[012])([012]\d|30|31)$}', $value) )
231 $value_type = PDO::PARAM_STR;
232 elseif ( preg_match(
'{^0x}i', $value) )
233 $value_type = PDO::PARAM_STR;
234 elseif ( preg_match(
'{^[0-9+-]+e[0-9+-]+$}i', $value) )
235 $value_type = PDO::PARAM_STR;
236 elseif ( preg_match(
'/^[01]{6,}$/i', $value) )
237 $value_type = PDO::PARAM_STR;
239 $value_type = PDO::PARAM_INT;
242 $value_type = PDO::PARAM_STR;
245 if ( is_string($value_type) ) {
246 switch( $value_type ) {
248 $value_type = PDO::PARAM_NULL;
252 $value_type = PDO::PARAM_INT;
255 $value_type = PDO::PARAM_BOOL;
258 $value_type = PDO::PARAM_STR;
263 switch ( $value_type ) {
264 case PDO::PARAM_NULL:
270 case PDO::PARAM_BOOL:
271 $rv = ($value ?
'TRUE' :
'FALSE');
281 $rv =
"'".str_replace(
"'",
"''", str_replace(
':',
'\\x3a', str_replace(
'\\',
'\\x5c', $value))).
"'";
283 if ( $this->dialect ==
'pgsql' && strpos( $rv,
'\\' ) !==
false ) {
288 $rv =
'E'.str_replace(
'?',
'\\x3f', $rv);
316 $argc = func_num_args();
317 $args = func_get_args();
319 if ( is_array($args[0]) ) {
324 $argc = count($args);
326 $qry = array_shift($args);
328 if ( is_array($args[0]) ) {
330 $argc = count($args);
339 $parts = explode(
'?', $qry, $argc + 1 );
340 $querystring = $parts[0];
343 for( $i = 0; $i < $argc; $i++ ) {
345 $querystring .= $this->
Quote($arg);
347 if ( isset($parts[$z]) ) $querystring .= $parts[$z];
372 $argc = func_num_args();
373 $args = func_get_args();
375 if ( is_array($args[0]) ) {
380 $argc = count($args);
382 $querystring = array_shift($args);
384 if ( is_array($args[0]) ) {
386 $argc = count($args);
389 foreach( $args AS $name => $value ) {
390 if ( substr($name, 0, 1) !=
':' ) {
391 dbg_error_log(
"ERROR",
"AwlDBDialect: Named parameter '%s' does not begin with a colon.", $name);
393 $replacement = str_replace(
'$',
'\\$', $this->
Quote($value));
394 $querystring = preg_replace(
'{\Q'.$name.
'\E\b}s', $replacement, $querystring );
TranslateSQL($sql_string)
__construct($connection_string, $dbuser=null, $dbpass=null, $options=null)
SetSearchPath($search_path=null)
GetFields($tablename_string)
Quote($value, $value_type=null)