/**
  * Creates the custom table with an id of id_c.
  *
  * @param bool $execute
  * @return string
  */
 public function createCustomTable($execute = true)
 {
     $out = '';
     if (!$this->db->tableExists($this->bean->table_name . '_cstm')) {
         $GLOBALS['log']->debug('creating custom table for ' . $this->bean->table_name);
         $idDef = array('id_c' => array('name' => 'id_c', 'type' => 'id', 'required' => 1));
         $idIdx = array('id' => array('name' => $this->bean->table_name . '_cstm_pk', 'type' => 'primary', 'fields' => array('id_c')));
         $query = $this->db->createTableSQLParams($this->bean->table_name . '_cstm', $idDef, $idIdx);
         if (!$this->db->supports('inline_keys')) {
             $indicesArr = $this->db->getConstraintSql($idIdx, $this->bean->table_name . '_cstm');
         } else {
             $indicesArr = array();
         }
         if ($execute) {
             $this->db->query($query);
             if (!empty($indicesArr)) {
                 foreach ($indicesArr as $idxq) {
                     $this->db->query($idxq);
                 }
             }
         }
         $out = $query . "\n";
         if (!empty($indicesArr)) {
             $out .= implode("\n", $indicesArr) . "\n";
         }
         $out .= $this->add_existing_custom_fields($execute);
     }
     return $out;
 }
 public function testModifyIndexByMultiQuery()
 {
     $tableName = 'test24_' . mt_rand();
     $this->created[$tableName] = true;
     $fields = array('foo' => array('name' => 'foo', 'type' => 'varchar', 'len' => '255'), 'foobar' => array('name' => 'foobar', 'type' => 'varchar', 'len' => '255'));
     $indexes = array();
     $queries = array();
     $queries[] = $this->_db->createTableSQLParams($tableName, $fields, $indexes);
     $indexes = array('idx_foo' => array('name' => 'idx_foo', 'type' => 'index', 'fields' => array('foo')));
     $tQueries = $this->_db->addIndexes($tableName, $indexes, false);
     $queries = array_merge($queries, explode(";\n", rtrim($tQueries, ";\n")));
     $indexesNew = $indexes;
     $indexesNew['idx_foo']['fields'] = array('foobar');
     $tQueries = $this->_db->modifyIndexes($tableName, $indexesNew, false);
     $queries = array_merge($queries, explode(";\n", rtrim($tQueries, ";\n")));
     $this->_db->query($queries);
     $indexesDB = $this->_db->get_indices($tableName);
     $this->assertEquals($indexesNew, $indexesDB, 'Indexes are incorrect');
 }
 /**
  * If auditing is enabled, create the audit table.
  *
  * Function is used by the install scripts and a repair utility in the admin panel.
  *
  * Internal function, do not override.
  */
 function create_audit_table()
 {
     global $dictionary;
     $table_name = $this->get_audit_table_name();
     require 'metadata/audit_templateMetaData.php';
     $fieldDefs = $dictionary['audit']['fields'];
     $indices = $dictionary['audit']['indices'];
     // '0' stands for the first index for all the audit tables
     $indices[0]['name'] = 'idx_' . strtolower($this->getTableName()) . '_' . $indices[0]['name'];
     $indices[1]['name'] = 'idx_' . strtolower($this->getTableName()) . '_' . $indices[1]['name'];
     $engine = null;
     if (isset($dictionary['audit']['engine'])) {
         $engine = $dictionary['audit']['engine'];
     } else {
         if (isset($dictionary[$this->getObjectName()]['engine'])) {
             $engine = $dictionary[$this->getObjectName()]['engine'];
         }
     }
     $sql = $this->db->createTableSQLParams($table_name, $fieldDefs, $indices, $engine);
     $msg = "Error creating table: " . $table_name . ":";
     $this->db->query($sql, true, $msg);
 }