/**
  * 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;
 }
Beispiel #2
0
 public function testGetAffectedRowCount()
 {
     if (!$this->_db->supports("affected_rows")) {
         $this->markTestSkipped('Skipping, backend doesn\'t support affected rows');
     }
     $beanIds = $this->_createRecords(1);
     $result = $this->_db->query("DELETE From contacts where id = '{$beanIds[0]}'");
     $this->assertEquals(1, $this->_db->getAffectedRowCount($result));
 }
Beispiel #3
0
 /**
  * Prefixes column names with this bean's table name.
  *
  * @param string $order_by  Order by clause to be processed
  * @param SugarBean $submodule name of the module this order by clause is for
  * @param boolean $suppress_table_name Whether table name should be suppressed
  * @param array $field_map Map of bean fields to query columns
  * @return string Processed order by clause
  *
  * Internal function, do not override.
  * @deprecated Use SugarQuery & $this->fetchFromQuery() instead
  */
 public function process_order_by($order_by, $submodule = null, $suppress_table_name = false, $field_map = array())
 {
     if (empty($order_by)) {
         return $order_by;
     }
     //submodule is empty,this is for list object in focus
     if (empty($submodule)) {
         $bean_queried = $this;
     } else {
         //submodule is set, so this is for subpanel, use submodule
         $bean_queried = $submodule;
     }
     $raw_elements = explode(',', $order_by);
     $valid_elements = array();
     foreach ($raw_elements as $key => $value) {
         $is_valid = false;
         //value might have ascending and descending decorations
         $list_column = preg_split('/\\s/', trim($value), 2);
         $list_column = array_map('trim', $list_column);
         $list_column_name = $list_column[0];
         // check if it contains table name, eg tasks.name
         if (($pos = strpos($list_column_name, ".")) !== false) {
             $list_column_name = substr($list_column_name, $pos + 1);
         }
         if (isset($bean_queried->field_defs[$list_column_name])) {
             $field_defs = $bean_queried->field_defs[$list_column_name];
             $source = isset($field_defs['source']) ? $field_defs['source'] : 'db';
             if (empty($field_defs['table']) && !$suppress_table_name) {
                 if ($source == 'db') {
                     $list_column[0] = $bean_queried->table_name . '.' . $list_column_name;
                 } elseif ($source == 'custom_fields') {
                     $list_column[0] = $bean_queried->table_name . '_cstm.' . $list_column_name;
                 }
             }
             // Bug 38803 - Use CONVERT() function when doing an order by on ntext, text, and image fields
             if ($source != 'non-db' && $this->db->isTextType($this->db->getFieldType($bean_queried->field_defs[$list_column_name]))) {
                 $list_column[0] = $this->db->convert($list_column[0], "text2char");
             }
             $is_valid = true;
             if (isset($list_column[1])) {
                 switch (strtolower($list_column[1])) {
                     case 'asc':
                     case 'desc':
                         break;
                     default:
                         $GLOBALS['log']->debug("process_order_by: ({$list_column['1']}) is not a valid order.");
                         unset($list_column[1]);
                         break;
                 }
             }
         } else {
             $GLOBALS['log']->debug("process_order_by: ({$list_column['0']}) does not have a vardef entry.");
         }
         if ($is_valid) {
             // Determine order by direction. Will be the same for multiple columns.
             $order = isset($list_column[1]) ? $list_column[1] : '';
             if (isset($field_map[$list_column_name])) {
                 foreach ($field_map[$list_column_name] as $field) {
                     $valid_elements[$field] = $field . ' ' . $order;
                 }
             } else {
                 $valid_elements[$list_column[0]] = implode(' ', $list_column);
             }
             // Apply `ORDER BY` stability if not implied by db backend
             if (!$this->db->supports('order_stability')) {
                 if ($suppress_table_name) {
                     $stableCol = 'id';
                 } else {
                     $stableCol = $bean_queried->getTableName() . '.id';
                 }
                 $valid_elements[$stableCol] = "{$stableCol} {$order}";
             }
         }
     }
     return implode(', ', $valid_elements);
 }
Beispiel #4
0
 /**
  * Check if this DB supports certain capability
  * See $this->capabilities for the list
  * @param string $cap
  * @return bool
  */
 public function supports($cap)
 {
     if ($cap == 'case_insensitive') {
         return !empty($GLOBALS['sugar_config']['oracle_enable_ci']);
     }
     return parent::supports($cap);
 }
Beispiel #5
0
 function execute_query($query_name = 'query', $result_name = 'result', $row_count_name = 'row_count', $row_start_name = 'row_start', $row_end_name = 'row_end', $limit = false)
 {
     // FIXME: needs DB-independent code here
     if ($limit) {
         $start_offset = $this->report_offset;
         if (!$this->db->supports('select_rows')) {
             if ($start_offset > 0) {
                 $start_offset++;
             }
         }
         $this->{$result_name} = $this->db->limitQuery($this->{$query_name}, $start_offset, $this->report_max, true, "Error executing query ");
     } else {
         $this->{$result_name} = $this->db->query($this->{$query_name}, true, "Error executing query ");
     }
     if (!empty($row_count_name) && empty($this->{$row_count_name})) {
         $this->{$row_count_name} = $this->report_offset;
         $this->{$row_end_name} = $this->report_max;
         if ($limit && $this->total_count < $this->{$row_end_name} + $this->{$row_count_name}) {
             $this->{$row_end_name} = $this->total_count - $this->{$row_count_name};
         }
         if ($this->{$row_count_name} > 0) {
             $this->{$row_start_name} = 1;
         }
     }
 }