/** * Create a from statement * * @param SugarBean|array $bean * @return string */ protected function compileFrom($bean) { $alias = ""; $return = array(); if (is_array($bean)) { list($bean, $alias) = $bean; $this->from_alias = $alias; } $this->from_bean = $bean; $table = $bean->getTableName(); $table_cstm = ''; $from_clause = "{$table}"; if (!empty($alias)) { $from_clause .= " {$alias}"; } //SugarQuery will determine if we actually need to add the table or not. $this->sugar_query->joinCustomTable($bean, $alias); if (!empty($this->from_alias)) { $this->primary_table = $this->from_alias; $this->primary_custom_table = $this->from_alias . '_c'; } else { $this->primary_table = $this->from_bean->getTableName(); $this->primary_custom_table = $this->from_bean->get_custom_table_name(); } $return = $from_clause; return $return; }
/** * This method generates sql that deletes a column identified by fieldDef. * * @param SugarBean $bean Sugarbean instance * @param array $fieldDefs * @return string SQL statement */ public function deleteColumnSQL(SugarBean $bean, $fieldDefs) { return $this->dropColumnSQL($bean->getTableName(), $fieldDefs); }
/** * This method generates sql that deletes a column identified by fieldDef. * * @param object $bean Sugarbean instance * @param array $fieldDefs * @return string SQL statement */ public function deleteColumnSQL(SugarBean $bean, $fieldDefs) { if ($this->isFieldArray($fieldDefs)) { foreach ($fieldDefs as $fieldDef) { $columns[] = $fieldDef['name']; } } else { $columns[] = $fieldDefs['name']; } return "alter table " . $bean->getTableName() . " drop (" . implode(", ", $columns) . ")"; }
/** * @see DBManager::createTableSQL() */ public function createTableSQL(SugarBean $bean) { $tablename = $bean->getTableName(); $fieldDefs = $bean->getFieldDefinitions(); $indices = $bean->getIndices(); $engine = $this->getEngine($bean); return $this->createTableSQLParams($tablename, $fieldDefs, $indices, $engine); }
/** * Drops the table associated with a bean * * @param object $bean SugarBean instance */ public function dropTable(SugarBean $bean) { $this->tableName = $bean->getTableName(); $this->dropTableName($this->tableName); }
/** * @see DBHelper::deleteColumnSQL() */ public function deleteColumnSQL(SugarBean $bean, $fieldDefs) { if ($this->isFieldArray($fieldDefs)) { foreach ($fieldDefs as $fieldDef) { $columns[] = $fieldDef['name']; } } else { $columns[] = $fieldDefs['name']; } return "ALTER TABLE " . $bean->getTableName() . " DROP COLUMN " . implode(", DROP COLUMN ", $columns); }
/** * Drops the table associated with a bean * * @param SugarBean $bean SugarBean instance * * @return bool query result */ public function dropTable(SugarBean $bean) { // If we want drop table then we have to drop all FTS indexes if they are present foreach ($this->get_indices($bean->getTableName()) as $index) { if ($index['type'] == 'fulltext') { $this->dropIndexes($bean->getTableName(), array($index), true); } } return parent::dropTable($bean); }
/** * @param string $_rel_name use this relationship key. * @param SugarBean $_bean reference of the bean that instantiated this class. * @param array $fieldDef vardef entry for the field. * @param string $_table_name optional, fetch from the bean's table name property. * @param string $_key_name optional, name of the primary key column for _table_name */ public function Link($_rel_name, SugarBean &$_bean, $fieldDef, $_table_name = '', $_key_name = '') { global $dictionary; require_once DOCROOT . "modules/TableDictionary.php"; Log::debug("Link Constructor, relationship name: [{$_rel_name}], Table name [{$_table_name}], Key name [{$_key_name}]"); $this->_relationship_name = $_rel_name; $this->relationship_fields = !empty($fieldDef['rel_fields']) ? $fieldDef['rel_fields'] : []; $this->_bean =& $_bean; $this->_relationship = new Relationship(); //$this->_relationship->retrieve_by_string_fields(array('relationship_name'=>$this->_relationship_name)); $this->_relationship->retrieve_by_name($this->_relationship_name); $this->_db = DBManagerFactory::getInstance(); // Following behavior is tied to a property(ignore_role) value in the vardef. // It alters the values of 2 properties, ignore_role_filter and add_distinct. // the property values can be altered again before any requests are made. if (!empty($fieldDef) && is_array($fieldDef)) { if (array_key_exists('ignore_role', $fieldDef)) { if ($fieldDef['ignore_role'] == true) { $this->ignore_role_filter = true; $this->add_distinct = true; } } } $this->_bean_table_name = !empty($_table_name) ? $_table_name : $_bean->getTableName(); if (!empty($key_name)) { $this->_bean_key_name = $_key_name; } else { if ($this->_relationship->lhs_table != $this->_relationship->rhs_table) { if ($_bean->table_name == $this->_relationship->lhs_table) { $this->_bean_key_name = $this->_relationship->lhs_key; } if ($_bean->table_name == $this->_relationship->rhs_table) { $this->_bean_key_name = $this->_relationship->rhs_key; } } } if ($this->_relationship->lhs_table == $this->_relationship->rhs_table && isset($fieldDef['side']) && $fieldDef['side'] == 'right') { $this->_swap_sides = true; } if (!empty($fieldDef['rhs_key_override'])) { $this->_rhs_key_override = true; } if (!empty($fieldDef['bean_filter_field'])) { $this->_bean_filter_field = $fieldDef['bean_filter_field']; } //default to id if not set. if (empty($this->_bean_key_name)) { $this->_bean_key_name = 'id'; } Log::debug("Link Constructor, _bean_table_name: [{$this->_bean_table_name}], _bean_key_name: [{$this->_bean_key_name}]"); if (!empty($this->_relationship->id)) { Log::debug("Link Constructor, relationship record found."); } else { Log::debug("Link Constructor, No relationship record."); } }
/** * Generic method for un-deleting records of all types * * @param SugarBean $parentBean Parent bean module to get field definitions from * @param array $where_data Array of fields to filter on in the where clause * @param bool $updateDateModified Boolean to indicate if the date_modified should be updated in the query or not * @param string $tableName Table name to run the query from. If excluded will use the parentBean->table_name passed * @return Array $results Results of query * */ private function unmarkDeletedRecord($parentBean, $where_data, $updateDateModified = false, $tableName = "") { if ($tableName == "") { $tableName = $parentBean->getTableName(); } $usePreparedStatements = false; $dataFields['deleted'] = $parentBean->getFieldDefinition('deleted'); $dataValues['deleted'] = '0'; if ($updateDateModified) { $dataFields['date_modified'] = $parentBean->getFieldDefinition('date_modified'); $dataValues['date_modified'] = $this->db->timedate->nowDb(); } $sql = $this->db->updateParams($tableName, $dataFields, $dataValues, $where_data, null, false, $usePreparedStatements); $sqlResults = $this->db->query($sql); //All supported dbs have affected_rows, but lets check just to be sure if (isset($this->db->capabilities["affected_rows"]) && $this->db->capabilities["affected_rows"] == true) { $unDeletedResults = $this->db->getAffectedRowCount($sqlResults); } else { $unDeletedResults = 1; } return $unDeletedResults; }
/** * Joins the custom table to the current query (if possible) * @param SugarBean $bean * @param string $alias */ public function joinCustomTable($bean, $alias = "") { if ($bean->hasCustomFields() && !$this->customJoined) { $table = $bean->getTableName(); $table_cstm = $bean->get_custom_table_name(); if (!empty($table_cstm)) { // TODO: CLEAN THIS UP if (!empty($alias)) { $sql = "LEFT JOIN {$table_cstm} {$alias}_c ON {$alias}_c.id_c = {$alias}.id"; } else { $sql = "LEFT JOIN {$table_cstm} ON {$table_cstm}.id_c = {$table}.id"; } // can do a join here because we haven't got to the joins yet in the compile sequence. $this->joinRaw($sql); } } }
/** * @see DBManager::insert() */ public function insert(SugarBean $bean) { $this->tableName = $bean->getTableName(); $msg = "Error inserting into table: " . $this->tableName; if ($this->usePreparedStatements) { list($sql, $data, $lobs) = $this->insertSQL($bean, true); return $this->preparedQuery($sql, $data, $lobs, $msg); } else { $sql = $this->insertSQL($bean); $ret = $this->AltlobExecute($this->tableName, $bean->getFieldDefinitions(), get_object_vars($bean), $sql); $this->checkError($msg . ' Query Failed: ' . $sql, true); } return $ret; }