DataObjects insert() for pgsql
| From: | Rezende Rodrigo | Date: | Mon, 06 Oct 2003 16:36:59 +0000 |
| Subject: | DataObjects insert() for pgsql | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-22410@lists.php.net to get a copy of this message | ||
Hello
Actually the insert method in DataObjet suport only Mysql and Mssql (CVS).
Bellow is sugestion for insert method (postgresql)
Thanks, Rodrigo Rezende.
function insert()
{
global $_DB_DATAOBJECT;
// connect will load the config!
$this->_connect();
$__DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
$items = $this->table();
if (!$items) {
DB_DataObject::raiseError("insert:No table definition for
{$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG);
return false;
}
$options= &$_DB_DATAOBJECT['CONFIG'];
// turn the sequence keys into an array
if ((@$options['ignore_sequence_keys']) &&
(@$options['ignore_sequence_keys'] != 'ALL') &&
(!is_array($options['ignore_sequence_keys']))) {
$options['ignore_sequence_keys'] = explode(',',
$options['ignore_sequence_keys']);
}
$datasaved = 1;
$leftq = '';
$rightq = '';
$key = false;
$keys = $this->keys();
$dbtype =
$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn["phptype"];
if ( ($key = @$keys[0]) &&
($dbtype == 'pgsql') &&
(@$options['ignore_sequence_keys'] != 'ALL') &&
(!is_array(@$options['ignore_sequence_keys']) ||
@!in_array($this->__table,$options['ignore_sequence_keys'])) )
{
if (!($seq = @$options['sequence_'. $this->__table])) {
$dummy = explode('.',$this->__table);
$schema_name = $dummy[0];
$table_name = $dummy[1];
$sql_seq = "
SELECT
a.attname as attname, adef.adsrc as sequence
FROM
pg_catalog.pg_attribute a LEFT JOIN
pg_catalog.pg_attrdef adef
ON a.attrelid=adef.adrelid
AND a.attnum=adef.adnum
WHERE
a.atthasdef = 't' AND
a.attname = '$key' AND
a.attrelid = (SELECT oid FROM pg_catalog.pg_class
WHERE relname='$table_name'
AND relnamespace = (SELECT oid FROM
pg_catalog.pg_namespace WHERE
nspname = '$schema_name'))
AND a.attnum > 0 AND NOT a.attisdropped
;
";
$r = $__DB->getRow($sql_seq,DB_FETCHMODE_ASSOC);
$attrname_seq = $r['attname'];
$sequence_name = explode('\'',$r['sequence']);
$sequence_name =
(isset($sequence_name[1]))?$sequence_name[1]:'';
$seq = $sequence_name;
}
$this->$key = $__DB->nextId($seq);
}
foreach($items as $k => $v) {
if (!isset($this->$k) || $this->$k == '' || $this->$k ===
'null') {
continue;
}
if ($leftq) {
$leftq .= ', ';
$rightq .= ', ';
}
$leftq .= "$k ";
if (strtolower($this->$k) === 'null') {
$rightq .= " NULL ";
continue;
}
if ($v & DB_DATAOBJECT_STR) {
$rightq .= $__DB->quote($this->$k) . " ";
continue;
}
if (is_numeric($this->$k)) {
$rightq .=" {$this->$k} ";
continue;
}
// at present we only cast to integers
// - V2 may store additional data about float/int
$rightq .= ' ' . intval($this->$k) . ' ';
}
if ($leftq) {
$r = $this->_query("INSERT INTO {$this->__table} ($leftq) VALUES
($rightq) ");
if (PEAR::isError($r)) {
DB_DataObject::raiseError($r);
return false;
}
if ($r < 1) {
DB_DataObject::raiseError('No Data Affected By
insert',DB_DATAOBJECT_ERROR_NOAFFECTEDROWS);
return false;
}
if ($key &&
($items[$key] & DB_DATAOBJECT_INT) &&
($dbtype == 'mysql') &&
(@$options['ignore_sequence_keys'] != 'ALL') &&
(
!@$options['ignore_sequence_keys'] ||
(is_array(@$options['ignore_sequence_keys']) &&
in_array($this->__table,@$options['ignore_sequence_keys']))
)
)
{
$this->$key =
mysql_insert_id($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->co
nnection);
}
$this->_clear_cache();
if ($key) {
return $this->$key;
}
return true;
}
DB_DataObject::raiseError("insert: No Data specifed for query",
DB_DATAOBJECT_ERROR_NODATA);
return false;
}