cvs: pear /Translation2/Admin/Container db.php mdb.php mdb2.php
| From: | Lorenzo Alberton | Date: | Wed, 27 Oct 2004 19:03:38 +0000 |
| Subject: | cvs: pear /Translation2/Admin/Container db.php mdb.php mdb2.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-25298@lists.php.net to get a copy of this message | ||
quipo Wed Oct 27 15:03:38 2004 EDT
Modified files:
/pear/Translation2/Admin/Container db.php mdb.php mdb2.php
Log:
check if an UPDATE is more appropriate, before doing an INSERT
http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/db.php?r1=1.14&r2=1.15&ty=u Index: pear/Translation2/Admin/Container/db.php diff -u pear/Translation2/Admin/Container/db.php:1.14 pear/Translation2/Admin/Container/db.php:1.15 --- pear/Translation2/Admin/Container/db.php:1.14 Wed Oct 20 12:43:43 2004 +++ pear/Translation2/Admin/Container/db.php Wed Oct 27 15:03:38 2004 @@ -16,11 +16,11 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.14 2004/10/20 16:43:43 quipo Exp $ +// $Id: db.php,v 1.15 2004/10/27 19:03:38 quipo Exp $ // /** * @package Translation2 - * @version $Id: db.php,v 1.14 2004/10/20 16:43:43 quipo Exp $ + * @version $Id: db.php,v 1.15 2004/10/27 19:03:38 quipo Exp $ */ /** @@ -35,7 +35,7 @@ * by the PEAR DB abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.14 $ + * @version $Revision: 1.15 $ */ class Translation2_Admin_Container_db extends Translation2_Container_db { @@ -206,16 +206,29 @@ // Langs may be in different tables - we need to split up queries along // table lines, so we can keep DB traffic to a minimum. + $unquoted_stringID = $stringID; + $unquoted_pageID = $pageID; $stringID = $this->db->quote($stringID); $pageID = is_null($pageID) ? 'NULL' : $this->db->quote($pageID); // Loop over the tables we need to insert into. foreach ($this->_tableLangs($langs) as $table => $tableLangs) { + //before INSERTing a new record, check if it exists already. + $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table); + if (PEAR::isError($exists)) { + return $exists; + } + if ($exists) { + $this->update($unquoted_stringID, $unquoted_pageID, + $this->_filterStringsByTable($stringArray, $table)); + continue; + } + $tableCols = $this->_getLangCols($tableLangs); $langData = array(); foreach ($tableLangs as $lang) { $langData[$lang] =& $this->db->quote($stringArray[$lang]); } - + $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)', $table, $this->options['string_id_col'], @@ -406,5 +419,59 @@ } // }}} + // {{{ + + /** + * Check if there's already a record in the table with the + * given (pageID, stringID) pair. + * + * @param string $stringID + * @param string $pageID + * @param string $table + * @return boolean + * @access private + */ + function _recordExists($stringID, $pageID, $table) + { + $stringID = $this->db->quote($stringID, 'text'); + $pageID = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->quote($pageID); + $query = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s', + $table, + $this->options['string_id_col'], + $stringID, + $this->options['string_page_id_col'], + $pageID + ); + ++$this->_queries; + $res = $this->db->getOne($query); + if (PEAR::isError($res)) { + return $res; + } + return ($res > 0); + } + + // }}} + // {{{ + + /** + * Get only the strings for the langs in the given table + * + * @param string $pageID + * @param array $stringArray Associative array with string translations. + * Sample format: array('en' => 'sample', 'it' => 'esempio') + * @access private + */ + function &_filterStringsByTable($stringArray, $table) + { + $strings = array(); + foreach ($stringArray as $lang => $string) { + if ($table == $this->_getLangTable($lang)) { + $strings[$lang] = $string; + } + } + return $strings; + } + + // }}} } ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/mdb.php?r1=1.11&r2=1.12&ty=u Index: pear/Translation2/Admin/Container/mdb.php diff -u pear/Translation2/Admin/Container/mdb.php:1.11 pear/Translation2/Admin/Container/mdb.php:1.12 --- pear/Translation2/Admin/Container/mdb.php:1.11 Wed Oct 20 12:46:25 2004 +++ pear/Translation2/Admin/Container/mdb.php Wed Oct 27 15:03:38 2004 @@ -16,11 +16,11 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: mdb.php,v 1.11 2004/10/20 16:46:25 quipo Exp $ +// $Id: mdb.php,v 1.12 2004/10/27 19:03:38 quipo Exp $ // /** * @package Translation2 - * @version $Id: mdb.php,v 1.11 2004/10/20 16:46:25 quipo Exp $ + * @version $Id: mdb.php,v 1.12 2004/10/27 19:03:38 quipo Exp $ */ /** @@ -35,7 +35,7 @@ * by the PEAR MDB abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.11 $ + * @version $Revision: 1.12 $ */ class Translation2_Admin_Container_mdb extends Translation2_Container_mdb { @@ -207,10 +207,23 @@ // Langs may be in different tables - we need to split up queries along // table lines, so we can keep DB traffic to a minimum. + $unquoted_stringID = $stringID; + $unquoted_pageID = $pageID; $stringID = $this->db->getTextValue($stringID); $pageID = is_null($pageID) ? 'NULL' : $this->db->getTextValue($pageID); // Loop over the tables we need to insert into. foreach ($this->_tableLangs($langs) as $table => $tableLangs) { + //before INSERTing a new record, check if it exists already. + $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table); + if (PEAR::isError($exists)) { + return $exists; + } + if ($exists) { + $this->update($unquoted_stringID, $unquoted_pageID, + $this->_filterStringsByTable($stringArray, $table)); + continue; + } + $tableCols = $this->_getLangCols($tableLangs); $langData = array(); foreach ($tableLangs as $lang) { @@ -407,5 +420,59 @@ } // }}} + // {{{ + + /** + * Check if there's already a record in the table with the + * given (pageID, stringID) pair. + * + * @param string $stringID + * @param string $pageID + * @param string $table + * @return boolean + * @access private + */ + function _recordExists($stringID, $pageID, $table) + { + $stringID = $this->db->getTextValue($stringID); + $pageID = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->getTextValue($pageID); + $query = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s', + $table, + $this->options['string_id_col'], + $stringID, + $this->options['string_page_id_col'], + $pageID + ); + ++$this->_queries; + $res = $this->db->getOne($query); + if (PEAR::isError($res)) { + return $res; + } + return ($res > 0); + } + + // }}} + // {{{ + + /** + * Get only the strings for the langs in the given table + * + * @param string $pageID + * @param array $stringArray Associative array with string translations. + * Sample format: array('en' => 'sample', 'it' => 'esempio') + * @access private + */ + function &_filterStringsByTable($stringArray, $table) + { + $strings = array(); + foreach ($stringArray as $lang => $string) { + if ($table == $this->_getLangTable($lang)) { + $strings[$lang] = $string; + } + } + return $strings; + } + + // }}} } ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/mdb2.php?r1=1.10&r2=1.11&ty=u Index: pear/Translation2/Admin/Container/mdb2.php diff -u pear/Translation2/Admin/Container/mdb2.php:1.10 pear/Translation2/Admin/Container/mdb2.php:1.11 --- pear/Translation2/Admin/Container/mdb2.php:1.10 Wed Oct 20 12:46:25 2004 +++ pear/Translation2/Admin/Container/mdb2.php Wed Oct 27 15:03:38 2004 @@ -16,11 +16,11 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: mdb2.php,v 1.10 2004/10/20 16:46:25 quipo Exp $ +// $Id: mdb2.php,v 1.11 2004/10/27 19:03:38 quipo Exp $ // /** * @package Translation2 - * @version $Id: mdb2.php,v 1.10 2004/10/20 16:46:25 quipo Exp $ + * @version $Id: mdb2.php,v 1.11 2004/10/27 19:03:38 quipo Exp $ */ /** @@ -35,7 +35,7 @@ * by the PEAR MDB2 abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.10 $ + * @version $Revision: 1.11 $ */ class Translation2_Admin_Container_mdb2 extends Translation2_Container_mdb2 { @@ -221,10 +221,23 @@ // Langs may be in different tables - we need to split up queries along // table lines, so we can keep DB traffic to a minimum. + $unquoted_stringID = $stringID; + $unquoted_pageID = $pageID; $stringID = $this->db->quote($stringID, 'text'); $pageID = is_null($pageID) ? 'NULL' : $this->db->quote($pageID, 'text'); // Loop over the tables we need to insert into. foreach ($this->_tableLangs($langs) as $table => $tableLangs) { + //before INSERTing a new record, check if it exists already. + $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table); + if (PEAR::isError($exists)) { + return $exists; + } + if ($exists) { + $this->update($unquoted_stringID, $unquoted_pageID, + $this->_filterStringsByTable($stringArray, $table)); + continue; + } + $tableCols = $this->_getLangCols($tableLangs); $langData = array(); foreach ($tableLangs as $lang) { @@ -294,7 +307,6 @@ $this->options['string_page_id_col'], $pageID ); - ++$this->_queries; $res = $this->db->query($query); if (PEAR::isError($res)) { @@ -421,5 +433,59 @@ } // }}} + // {{{ + + /** + * Check if there's already a record in the table with the + * given (pageID, stringID) pair. + * + * @param string $stringID + * @param string $pageID + * @param string $table + * @return boolean + * @access private + */ + function _recordExists($stringID, $pageID, $table) + { + $stringID = $this->db->quote($stringID, 'text'); + $pageID = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->quote($pageID, 'text'); + $query = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s', + $table, + $this->options['string_id_col'], + $stringID, + $this->options['string_page_id_col'], + $pageID + ); + ++$this->_queries; + $res = $this->db->queryOne($query); + if (PEAR::isError($res)) { + return $res; + } + return ($res > 0); + } + + // }}} + // {{{ + + /** + * Get only the strings for the langs in the given table + * + * @param string $pageID + * @param array $stringArray Associative array with string translations. + * Sample format: array('en' => 'sample', 'it' => 'esempio') + * @access private + */ + function &_filterStringsByTable($stringArray, $table) + { + $strings = array(); + foreach ($stringArray as $lang => $string) { + if ($table == $this->_getLangTable($lang)) { + $strings[$lang] = $string; + } + } + return $strings; + } + + // }}} } ?> \ No newline at end of file
http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/db.php?r1=1.14&r2=1.15&ty=u Index: pear/Translation2/Admin/Container/db.php diff -u pear/Translation2/Admin/Container/db.php:1.14 pear/Translation2/Admin/Container/db.php:1.15 --- pear/Translation2/Admin/Container/db.php:1.14 Wed Oct 20 12:43:43 2004 +++ pear/Translation2/Admin/Container/db.php Wed Oct 27 15:03:38 2004 @@ -16,11 +16,11 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: db.php,v 1.14 2004/10/20 16:43:43 quipo Exp $ +// $Id: db.php,v 1.15 2004/10/27 19:03:38 quipo Exp $ // /** * @package Translation2 - * @version $Id: db.php,v 1.14 2004/10/20 16:43:43 quipo Exp $ + * @version $Id: db.php,v 1.15 2004/10/27 19:03:38 quipo Exp $ */ /** @@ -35,7 +35,7 @@ * by the PEAR DB abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.14 $ + * @version $Revision: 1.15 $ */ class Translation2_Admin_Container_db extends Translation2_Container_db { @@ -206,16 +206,29 @@ // Langs may be in different tables - we need to split up queries along // table lines, so we can keep DB traffic to a minimum. + $unquoted_stringID = $stringID; + $unquoted_pageID = $pageID; $stringID = $this->db->quote($stringID); $pageID = is_null($pageID) ? 'NULL' : $this->db->quote($pageID); // Loop over the tables we need to insert into. foreach ($this->_tableLangs($langs) as $table => $tableLangs) { + //before INSERTing a new record, check if it exists already. + $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table); + if (PEAR::isError($exists)) { + return $exists; + } + if ($exists) { + $this->update($unquoted_stringID, $unquoted_pageID, + $this->_filterStringsByTable($stringArray, $table)); + continue; + } + $tableCols = $this->_getLangCols($tableLangs); $langData = array(); foreach ($tableLangs as $lang) { $langData[$lang] =& $this->db->quote($stringArray[$lang]); } - + $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)', $table, $this->options['string_id_col'], @@ -406,5 +419,59 @@ } // }}} + // {{{ + + /** + * Check if there's already a record in the table with the + * given (pageID, stringID) pair. + * + * @param string $stringID + * @param string $pageID + * @param string $table + * @return boolean + * @access private + */ + function _recordExists($stringID, $pageID, $table) + { + $stringID = $this->db->quote($stringID, 'text'); + $pageID = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->quote($pageID); + $query = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s', + $table, + $this->options['string_id_col'], + $stringID, + $this->options['string_page_id_col'], + $pageID + ); + ++$this->_queries; + $res = $this->db->getOne($query); + if (PEAR::isError($res)) { + return $res; + } + return ($res > 0); + } + + // }}} + // {{{ + + /** + * Get only the strings for the langs in the given table + * + * @param string $pageID + * @param array $stringArray Associative array with string translations. + * Sample format: array('en' => 'sample', 'it' => 'esempio') + * @access private + */ + function &_filterStringsByTable($stringArray, $table) + { + $strings = array(); + foreach ($stringArray as $lang => $string) { + if ($table == $this->_getLangTable($lang)) { + $strings[$lang] = $string; + } + } + return $strings; + } + + // }}} } ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/mdb.php?r1=1.11&r2=1.12&ty=u Index: pear/Translation2/Admin/Container/mdb.php diff -u pear/Translation2/Admin/Container/mdb.php:1.11 pear/Translation2/Admin/Container/mdb.php:1.12 --- pear/Translation2/Admin/Container/mdb.php:1.11 Wed Oct 20 12:46:25 2004 +++ pear/Translation2/Admin/Container/mdb.php Wed Oct 27 15:03:38 2004 @@ -16,11 +16,11 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: mdb.php,v 1.11 2004/10/20 16:46:25 quipo Exp $ +// $Id: mdb.php,v 1.12 2004/10/27 19:03:38 quipo Exp $ // /** * @package Translation2 - * @version $Id: mdb.php,v 1.11 2004/10/20 16:46:25 quipo Exp $ + * @version $Id: mdb.php,v 1.12 2004/10/27 19:03:38 quipo Exp $ */ /** @@ -35,7 +35,7 @@ * by the PEAR MDB abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.11 $ + * @version $Revision: 1.12 $ */ class Translation2_Admin_Container_mdb extends Translation2_Container_mdb { @@ -207,10 +207,23 @@ // Langs may be in different tables - we need to split up queries along // table lines, so we can keep DB traffic to a minimum. + $unquoted_stringID = $stringID; + $unquoted_pageID = $pageID; $stringID = $this->db->getTextValue($stringID); $pageID = is_null($pageID) ? 'NULL' : $this->db->getTextValue($pageID); // Loop over the tables we need to insert into. foreach ($this->_tableLangs($langs) as $table => $tableLangs) { + //before INSERTing a new record, check if it exists already. + $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table); + if (PEAR::isError($exists)) { + return $exists; + } + if ($exists) { + $this->update($unquoted_stringID, $unquoted_pageID, + $this->_filterStringsByTable($stringArray, $table)); + continue; + } + $tableCols = $this->_getLangCols($tableLangs); $langData = array(); foreach ($tableLangs as $lang) { @@ -407,5 +420,59 @@ } // }}} + // {{{ + + /** + * Check if there's already a record in the table with the + * given (pageID, stringID) pair. + * + * @param string $stringID + * @param string $pageID + * @param string $table + * @return boolean + * @access private + */ + function _recordExists($stringID, $pageID, $table) + { + $stringID = $this->db->getTextValue($stringID); + $pageID = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->getTextValue($pageID); + $query = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s', + $table, + $this->options['string_id_col'], + $stringID, + $this->options['string_page_id_col'], + $pageID + ); + ++$this->_queries; + $res = $this->db->getOne($query); + if (PEAR::isError($res)) { + return $res; + } + return ($res > 0); + } + + // }}} + // {{{ + + /** + * Get only the strings for the langs in the given table + * + * @param string $pageID + * @param array $stringArray Associative array with string translations. + * Sample format: array('en' => 'sample', 'it' => 'esempio') + * @access private + */ + function &_filterStringsByTable($stringArray, $table) + { + $strings = array(); + foreach ($stringArray as $lang => $string) { + if ($table == $this->_getLangTable($lang)) { + $strings[$lang] = $string; + } + } + return $strings; + } + + // }}} } ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/mdb2.php?r1=1.10&r2=1.11&ty=u Index: pear/Translation2/Admin/Container/mdb2.php diff -u pear/Translation2/Admin/Container/mdb2.php:1.10 pear/Translation2/Admin/Container/mdb2.php:1.11 --- pear/Translation2/Admin/Container/mdb2.php:1.10 Wed Oct 20 12:46:25 2004 +++ pear/Translation2/Admin/Container/mdb2.php Wed Oct 27 15:03:38 2004 @@ -16,11 +16,11 @@ // | Author: Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: mdb2.php,v 1.10 2004/10/20 16:46:25 quipo Exp $ +// $Id: mdb2.php,v 1.11 2004/10/27 19:03:38 quipo Exp $ // /** * @package Translation2 - * @version $Id: mdb2.php,v 1.10 2004/10/20 16:46:25 quipo Exp $ + * @version $Id: mdb2.php,v 1.11 2004/10/27 19:03:38 quipo Exp $ */ /** @@ -35,7 +35,7 @@ * by the PEAR MDB2 abstraction layer to fetch data. * * @package Translation2 - * @version $Revision: 1.10 $ + * @version $Revision: 1.11 $ */ class Translation2_Admin_Container_mdb2 extends Translation2_Container_mdb2 { @@ -221,10 +221,23 @@ // Langs may be in different tables - we need to split up queries along // table lines, so we can keep DB traffic to a minimum. + $unquoted_stringID = $stringID; + $unquoted_pageID = $pageID; $stringID = $this->db->quote($stringID, 'text'); $pageID = is_null($pageID) ? 'NULL' : $this->db->quote($pageID, 'text'); // Loop over the tables we need to insert into. foreach ($this->_tableLangs($langs) as $table => $tableLangs) { + //before INSERTing a new record, check if it exists already. + $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table); + if (PEAR::isError($exists)) { + return $exists; + } + if ($exists) { + $this->update($unquoted_stringID, $unquoted_pageID, + $this->_filterStringsByTable($stringArray, $table)); + continue; + } + $tableCols = $this->_getLangCols($tableLangs); $langData = array(); foreach ($tableLangs as $lang) { @@ -294,7 +307,6 @@ $this->options['string_page_id_col'], $pageID ); - ++$this->_queries; $res = $this->db->query($query); if (PEAR::isError($res)) { @@ -421,5 +433,59 @@ } // }}} + // {{{ + + /** + * Check if there's already a record in the table with the + * given (pageID, stringID) pair. + * + * @param string $stringID + * @param string $pageID + * @param string $table + * @return boolean + * @access private + */ + function _recordExists($stringID, $pageID, $table) + { + $stringID = $this->db->quote($stringID, 'text'); + $pageID = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->quote($pageID, 'text'); + $query = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s', + $table, + $this->options['string_id_col'], + $stringID, + $this->options['string_page_id_col'], + $pageID + ); + ++$this->_queries; + $res = $this->db->queryOne($query); + if (PEAR::isError($res)) { + return $res; + } + return ($res > 0); + } + + // }}} + // {{{ + + /** + * Get only the strings for the langs in the given table + * + * @param string $pageID + * @param array $stringArray Associative array with string translations. + * Sample format: array('en' => 'sample', 'it' => 'esempio') + * @access private + */ + function &_filterStringsByTable($stringArray, $table) + { + $strings = array(); + foreach ($stringArray as $lang => $string) { + if ($table == $this->_getLangTable($lang)) { + $strings[$lang] = $string; + } + } + return $strings; + } + + // }}} } ?> \ No newline at end of file