给CodeIgniter 2.0.1中的PostgreSQL 数据库驱动打两个补丁

近期帮朋友用CodeIgniter基于PostgreSQL做了个小东西,发现CodeIgniter2.0.0在实现PostgreSQL数据库驱动是有两个小缺陷。

一个是CI的数据库通用接口insert_batch,CI2.0发布包中的PosgreSQL驱动实现,并没有实现该接口,可以在system\database\drivers\postgre\postgre_driver.php文件中的_insert函数下添加如下函数。

<!–

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

–> // ——————————————————————–
/

 * Insert_batch statement
 *
 * Generates a platform-specific insert string from the supplied data
 *
 * @access    public
 * @param    string    the table name
 * @param    array    the insert keys
 * @param    array    the insert values
 * @return    string
 </span><span style="color: #008000;">*/</span><span style="color: #000000;">
</span><span style="color: #0000FF;">function</span><span style="color: #000000;"> _insert_batch(</span><span style="color: #800080;">$table</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$keys</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$values</span><span style="color: #000000;">)
{
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">INSERT INTO </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">.</span><span style="color: #800080;">$table</span><span style="color: #000000;">.</span><span style="color: #000000;">&quot;</span><span style="color: #000000;"> (</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">.</span><span style="color: #008080;">implode</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">, </span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$keys</span><span style="color: #000000;">)</span><span style="color: #000000;">.</span><span style="color: #000000;">&quot;</span><span style="color: #000000;">) VALUES </span><span style="color: #000000;">&quot;</span><span style="color: #000000;">.</span><span style="color: #008080;">implode</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">, </span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$values</span><span style="color: #000000;">);
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> --------------------------------------------------------------------</span><span style="color: #008000;">

另一个是insert_id()函数,该函数如果在数据表没有serial字段可能无法返回数值,导致问题,特别是主从表而从表不需要serial字段的情况下。

如果碰到这个问题可以把原来的:

elseif ($table != null && $column != null && $v >= 8.0)
{
$sql = sprintf("SELECT pg_get_serial_sequence(‘%s’,’%s’) as seq", $table, $column);
$query = $this->query($sql);
$row = $query->row();
$sql = sprintf("SELECT CURRVAL(‘%s’) as ins_id", $row->seq);
}

修改成:

elseif ($table != null && $column != null && $v >= 8.0)
{
$sql = sprintf("SELECT pg_get_serial_sequence(‘%s’,’%s’) as seq", $table, $column);
$query = $this->query($sql);
$row = $query->row();
if($row->seq!=‘’)
$sql = sprintf("SELECT CURRVAL(‘%s’) as ins_id", $row->seq);
else
$sql=SELECT LASTVAL() as ins_id;
}