CodeIgniter Forums
PostGreSQL $this->db->insert_id() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: PostGreSQL $this->db->insert_id() (/thread-11497.html)



PostGreSQL $this->db->insert_id() - El Forum - 09-10-2008

[eluser]Greg Aker[/eluser]
Hey guys,

I have run into a problem with postgres 7.4.7 and getting the insert_id properly returned. I came up with a workaround, but it's not going to work with php4.

I thought I'd share my findings and see if anyone can add to it.

./system/database/drivers/postgre/postgre_driver.php

_version()

was not working for me. a var_dump or print_r gave me the value of "S".

so I changed it to:

Code:
function _version()
{
    return pg_version();
}

So that is where the php4 vs php5 issues come in to play.

Then, in the MSSQL driver, there is a function to grab the major version. I used a modified version of that to give me the major and minor version, and this is what I came up with.

Code:
function _parse_version($version)
{
    $v = $version['server'];

    preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $v, $ver_info);
    $ver = $ver_info[1] . '.' . $ver_info[2];
    return $ver;
}

The I changed the top of insert_id() to:

Code:
function insert_id()
{
    $v = $this->_parse_version($this->_version());

So that's what I have and it's working nicely for me on pg 7.4.7. I'd like to see what you guys think, and I appreciate any feedback.

Thanks,

-greg


PostGreSQL $this->db->insert_id() - El Forum - 09-10-2008

[eluser]Pascal Kriete[/eluser]
I don't have a postgre setup to test on, but that looks good to me. It could check for php5 and return 0 for php 4 (anything smaller than 8 should work).

A quick look at how cake and zend do it makes me think that CI might to be taking the complicated (read: ├╝bercompatible) route again.

The other two essentially do:
Code:
$seq = $column ? $table.'_'.$column.'_seq' : $table.'_seq';

$query = "SELECT CURRVAL({$seq}) as ins_id";
$query = $this->query($sql);
$row = $query->row();
return $row->ins_id;