2009年7月26日星期日

[fw-db] insert method refactoring

Hi,
I have small idea for insert method refactoring. I have script witch insert
nearly 50 000 rows to DB. So now I do something like this:

// taken from insert method
$cols = array();
$vals = array();
foreach ($bind as $col => $val) {
$cols[] = $Db->quoteIdentifier($col, true);
if ($val instanceof Zend_Db_Expr) {
$vals[] = $val->__toString();
unset($bind[$col]);
} else {
$vals[] = '?';
}
}

// build the statement
$sql = "INSERT INTO "
. $Db->quoteIdentifier($table, true)
. ' (' . implode(', ', $cols) . ') '
. 'VALUES (' . implode(', ', $vals) . ')';


$stmt = $Db->prepare($sql);

foreach($data as $value ){
$stmt->execute($value);
}

I think that that you are familiar with first part as I actualy take it from
insert method. As it's ugly to have same code on two places my idea is
write public method
prepareInsert($table,$columns) which will encapsulate it. This method can by
quite handy in some situation and I think that is quite easy to implement it
without breaking BC.


--
View this message in context: http://www.nabble.com/insert-method-refactoring-tp24673217p24673217.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: