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);
162 if ( !isset($this->dialect) ) {
163 trigger_error(
"Unsupported database dialect", E_USER_ERROR);
166 switch ( $this->dialect ) {
168 list( $schema, $table ) = explode(
'.', $tablename_string, 2);
169 if ( empty($table) ) {
170 $table = $tablename_string;
174 $sql =
'SELECT f.attname AS fieldname, t.typname AS typename, f.atttypmod AS precision FROM pg_attribute f';
175 $sql .=
' JOIN pg_class c ON ( f.attrelid = c.oid )';
176 $sql .=
' JOIN pg_type t ON ( f.atttypid = t.oid )';
177 $sql .=
' JOIN pg_namespace ns ON ( c.relnamespace = ns.oid )';
178 $sql .=
' WHERE relname = '.$this->Quote($table,PDO::PARAM_STR).
' AND attnum >= 0 ';
179 if ( isset($schema) ) $sql .=
' AND ns.nspname = '.$this->Quote($schema,PDO::PARAM_STR);
180 $sql .=
' ORDER BY f.attnum';
181 dbg_error_log(
'AwlDBDialect', $sql);
214 function Quote( $value, $value_type =
null ) {
215 if ( isset($value_type) && $value_type ==
'identifier' ) {
216 if ( $this->dialect ==
'mysql' ) {
218 $rv =
'`' . str_replace(
'`',
'\\`', $value ) .
'`';
221 $rv =
'"' . str_replace(
'"',
'\\"', $value ) .
'"';
226 if ( !isset($value_type) ) {
227 if ( !isset($value) ) $value_type = PDO::PARAM_NULL;
228 elseif ( is_bool($value) ) $value_type = PDO::PARAM_BOOL;
229 elseif ( is_float($value) ) $value_type = PDO::PARAM_INT;
230 elseif ( is_numeric($value)) {
231 if ( preg_match(
'{^(19|20)\d\d(0[1-9]|1[012])([012]\d|30|31)$}', $value) )
232 $value_type = PDO::PARAM_STR;
233 elseif ( preg_match(
'{^0x}i', $value) )
234 $value_type = PDO::PARAM_STR;
235 elseif ( preg_match(
'{^[0-9+-]+e[0-9+-]+$}i', $value) )
236 $value_type = PDO::PARAM_STR;
237 elseif ( preg_match(
'/^[01]{6,}$/i', $value) )
238 $value_type = PDO::PARAM_STR;
240 $value_type = PDO::PARAM_INT;
243 $value_type = PDO::PARAM_STR;
246 if ( is_string($value_type) ) {
247 switch( $value_type ) {
249 $value_type = PDO::PARAM_NULL;
253 $value_type = PDO::PARAM_INT;
256 $value_type = PDO::PARAM_BOOL;
259 $value_type = PDO::PARAM_STR;
264 switch ( $value_type ) {
265 case PDO::PARAM_NULL:
271 case PDO::PARAM_BOOL:
272 $rv = ($value ?
'TRUE' :
'FALSE');
282 $rv =
"'".str_replace(
"'",
"''", str_replace(
':',
'\\x3a', str_replace(
'\\',
'\\x5c', $value))).
"'";
284 if ( $this->dialect ==
'pgsql' && strpos( $rv,
'\\' ) !==
false ) {
289 $rv =
'E'.str_replace(
'?',
'\\x3f', $rv);
373 $argc = func_num_args();
374 $args = func_get_args();
376 if ( is_array($args[0]) ) {
381 $argc = count($args);
383 $querystring = array_shift($args);
385 if ( is_array($args[0]) ) {
387 $argc = count($args);
390 foreach( $args AS $name => $value ) {
391 if ( substr($name, 0, 1) !=
':' ) {
392 dbg_error_log(
"ERROR",
"AwlDBDialect: Named parameter '%s' does not begin with a colon.", $name);
394 $replacement = str_replace(
'$',
'\\$', $this->
Quote($value));
395 $querystring = preg_replace(
'{\Q'.$name.
'\E\b}s', $replacement, $querystring );