function test_translateDeleteQuery()
 {
     $app =& Dataface_Application::getInstance();
     // Try to insert only values into both the base table and the
     // translated table.
     $sql = 'Delete FROM PeopleIntl where PersonID=5';
     $translator = new Dataface_QueryTranslator('en');
     $tsql = $translator->translateQuery($sql);
     //print_r($tsql);exit;
     $affected_rows = array();
     foreach ($tsql as $q) {
         $res = xf_db_query($q, $app->db());
         if (!$res) {
             die(xf_db_error($app->db()));
         }
         $affected_rows[$q] = xf_db_affected_rows($app->db());
     }
     $this->assertEquals(array("delete from `PeopleIntl` where `PersonID` = 5", "delete from `PeopleIntl_en` where `PersonID` = 5", "delete from `PeopleIntl_fr` where `PersonID` = 5"), $tsql);
     $this->assertEquals(array("delete from `PeopleIntl` where `PersonID` = 5" => 1, "delete from `PeopleIntl_en` where `PersonID` = 5" => 0, "delete from `PeopleIntl_fr` where `PersonID` = 5" => 0), $affected_rows);
 }
Exemple #2
0
 /**
  * Translates a select SQL query into a multilanguage query.  It will compute the
  * appropriate joins to the translation tables and swap the original column for
  * its translated column in the join table.
  * @param $query The query.
  * @param $lang The 2 digit language code for the translation we wish to obtain.
  *
  */
 function translate_query($query, $lang = null)
 {
     //echo "Dirty flag: ".$this->_cacheDirtyFlag;
     if ($lang === null) {
         // If no language is provided use the language in the conf.ini file
         $lang = $this->app->_conf['lang'];
     }
     $this->_loadCache();
     if (strtolower(substr($query, 0, strlen('DELETE '))) == 'delete ') {
         return $query;
     }
     $original_query = $query;
     $prepared_query = $this->prepareQuery($query);
     if (isset($this->_cache[$lang][$prepared_query[0]])) {
         // we have already translated this select query and cached it!
         // just load the query from the cache and fill in the appropriate
         // values:
         $prepared_query[0] = $this->_cache[$lang][$prepared_query[0]];
         return $this->compilePreparedQuery($prepared_query);
     }
     $query = $prepared_query[0];
     import('Dataface/QueryTranslator.php');
     $translator = new Dataface_QueryTranslator($lang);
     $output = $translator->translateQuery($prepared_query[0]);
     if (PEAR::isError($output)) {
         //echo $output->toString();
         throw new Exception(df_translate('scripts.Dataface.DB.translate_query.FAILED_TO_TRANSLATE', "Failed to translate query: {$query}.: ", array('query' => $query)) . $output->toString(), E_USER_ERROR);
     }
     $this->cache($prepared_query[0], $output, $lang);
     $prepared_query[0] = $output;
     return $this->compilePreparedQuery($prepared_query);
 }
Exemple #3
0
 function translateUnaryClause(&$clause)
 {
     if (!isset($clause['type'])) {
         $this->translateClause($clause);
     } else {
         switch ($clause['type']) {
             case 'ident':
                 $new_value = $this->translateColumn($clause['value'], null, false);
                 if (is_array($new_value) and isset($new_value['set_function'])) {
                     // the translation is a function
                     $clause['type'] = 'function';
                     $clause['value'] = $new_value['set_function'][0];
                 } else {
                     $clause['value'] = $new_value['column_names'][0];
                 }
                 break;
             case 'function':
                 $this->translateFunction($clause['value']);
                 break;
             case 'subclause':
                 $this->translateClause($clause['value']);
                 break;
             case 'command':
             case 'subquery':
                 $translator = new Dataface_QueryTranslator($this->_lang);
                 $translator->setParentContext($this);
                 $clause['value'] = $translator->translateQuery($clause['value'], $this->_lang, false);
                 break;
                 //case 'match':
                 //	if ( isset($clause['value']) and is_array($clause['value']) ){
                 //		$numClauses = count($clause['value']);
                 //		for ($i=0; $i<$numClauses; $i++){
                 //
                 //
                 //
                 //		}
                 //	}
                 //	break;
         }
     }
 }