2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 /*********************************************************************************
40 * Description: This file handles the Data base functionality for the application specific
41 * to oracle database. It is called by the DBManager class to generate various sql statements.
43 * All the functions in this class will work with any bean which implements the meta interface.
44 * Please refer the DBManager documentation for the details.
46 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
47 * All Rights Reserved.
48 * Contributor(s): ______________________________________..
49 ********************************************************************************/
50 require_once('include/database/DBHelper.php');
52 class MysqlHelper extends DBHelper
55 * @see DBHelper::createTableSQL()
57 public function createTableSQL(
61 $tablename = $bean->getTableName();
62 $fieldDefs = $bean->getFieldDefinitions();
63 $indices = $bean->getIndices();
64 $engine = $this->getEngine($bean);
65 return $this->createTableSQLParams($tablename, $fieldDefs, $indices, $engine);
69 * Generates sql for create table statement for a bean.
71 * @param string $tablename
72 * @param array $fieldDefs
73 * @param array $indices
74 * @param string $engine optional, MySQL engine to use
75 * @return string SQL Create Table statement
77 public function createTableSQLParams(
84 if ( empty($engine) && isset($fieldDefs['engine']))
85 $engine = $fieldDefs['engine'];
86 if ( !$this->isEngineEnabled($engine) )
89 $sql = parent::createTableSQLParams($tablename,$fieldDefs,$indices);
91 $sql.= " ENGINE=$engine";
97 * Returns the name of the engine to use or null if we are to use the default
99 * @param object $bean SugarBean instance
102 private function getEngine($bean)
106 if (isset($dictionary[$bean->getObjectName()]['engine'])) {
107 $engine = $dictionary[$bean->getObjectName()]['engine'];
113 * Returns true if the engine given is enabled in the backend
115 * @param string $engine
118 private function isEngineEnabled(
122 $engine = strtoupper($engine);
124 $r = $this->db->query("SHOW ENGINES");
126 while ( $row = $this->db->fetchByAssoc($r) )
127 if ( strtoupper($row['Engine']) == $engine )
128 return ($row['Support']=='YES' || $row['Support']=='DEFAULT');
134 * @see DBHelper::getColumnType()
136 public function getColumnType(
144 'double' => 'double',
146 'uint' => 'int unsigned',
147 'ulong' => 'bigint unsigned',
149 'short' => 'smallint',
150 'varchar' => 'varchar',
152 'longtext' => 'longtext',
155 'relate' => 'varchar',
156 'multienum'=> 'text',
158 'datetime' => 'datetime',
159 'datetimecombo' => 'datetime',
162 'tinyint' => 'tinyint',
165 'longblob' => 'longblob',
166 'currency' => 'decimal(26,6)',
167 'decimal' => 'decimal',
168 'decimal2' => 'decimal',
171 'encrypt'=>'varchar',
175 // Bug 44291 - If requested type is in array, returns it. Otherwise return requested type, so devs could see exactly what went wrong in log.
176 if (isset($map[$type]))
183 * @see DBHelper::oneColumnSQLRep()
185 protected function oneColumnSQLRep(
187 $ignoreRequired = false,
189 $return_as_array = false
192 $ref = parent::oneColumnSQLRep($fieldDef, $ignoreRequired, $table, true);
194 if ( $ref['colType'] == 'int'
195 && !empty($fieldDef['len']) )
196 $ref['colType'] .= "(".$fieldDef['len'].")";
198 // bug 22338 - don't set a default value on text or blob fields
199 if ( isset($ref['default']) &&
200 ($ref['colType'] == 'text' || $ref['colType'] == 'blob'
201 || $ref['colType'] == 'longtext' || $ref['colType'] == 'longblob' ))
202 $ref['default'] = '';
204 if ( $return_as_array )
207 return "{$ref['name']} {$ref['colType']} {$ref['default']} {$ref['required']} {$ref['auto_increment']}";
211 * @see DBHelper::changeColumnSQL()
213 protected function changeColumnSQL(
217 $ignoreRequired = false
220 if ($this->isFieldArray($fieldDefs)){
221 foreach ($fieldDefs as $def){
222 if ($action == 'drop')
223 $columns[] = $def['name'];
225 $columns[] = $this->oneColumnSQLRep($def, $ignoreRequired);
228 if ($action == 'drop')
229 $columns[] = $fieldDefs['name'];
231 $columns[] = $this->oneColumnSQLRep($fieldDefs);
234 return "alter table $tablename $action column ".implode(",$action column ", $columns);
238 * @see DBHelper::deleteColumnSQL()
240 public function deleteColumnSQL(
245 if ($this->isFieldArray($fieldDefs))
246 foreach ($fieldDefs as $fieldDef)
247 $columns[] = $fieldDef['name'];
249 $columns[] = $fieldDefs['name'];
251 return "alter table ".$bean->getTableName()." drop column ".implode(", drop column ", $columns);
255 * @see DBHelper::keysSQL
257 public function keysSQL(
259 $alter_table = false,
263 // check if the passed value is an array of fields.
264 // if not, convert it into an array
265 if (!$this->isFieldArray($indices))
266 $indices[] = $indices;
269 foreach ($indices as $index) {
270 if(!empty($index['db']) && $index['db'] != 'mysql')
272 if (isset($index['source']) && $index['source'] != 'db')
275 $type = $index['type'];
276 $name = $index['name'];
278 if (is_array($index['fields']))
279 $fields = implode(", ", $index['fields']);
281 $fields = $index['fields'];
285 $columns[] = " UNIQUE $name ($fields)";
288 $columns[] = " PRIMARY KEY ($fields)";
293 case 'alternate_key':
295 * @todo here it is assumed that the primary key of the foreign
296 * table will always be named 'id'. It must be noted though
297 * that this can easily be fixed by referring to db dictionary
298 * to find the correct primary field name
301 $columns[] = " INDEX $name ($fields)";
303 $columns[] = " KEY $name ($fields)";
306 if ($this->full_text_indexing_enabled())
307 $columns[] = " FULLTEXT ($fields)";
309 $GLOBALS['log']->debug('MYISAM engine is not available/enabled, full-text indexes will be skipped. Skipping:',$name);
313 $columns = implode(", $alter_action ", $columns);
314 if(!empty($alter_action)){
315 $columns = $alter_action . ' '. $columns;
321 * @see DBHelper::setAutoIncrement()
323 protected function setAutoIncrement(
328 return "auto_increment";
332 * Sets the next auto-increment value of a column to a specific value.
334 * @param string $table tablename
335 * @param string $field_name
337 public function setAutoIncrementStart(
343 $this->db->query( "ALTER TABLE $table AUTO_INCREMENT = $start_value;");
349 * Returns the next value for an auto increment
351 * @param string $table tablename
352 * @param string $field_name
355 public function getAutoIncrement(
361 $result = $this->db->query("SHOW TABLE STATUS LIKE '$table'");
362 $row = $this->db->fetchByAssoc($result);
363 if (!empty($row['Auto_increment']))
364 return $row['Auto_increment'];
370 * @see DBHelper::get_indices()
372 public function get_indices(
376 //find all unique indexes and primary keys.
377 $result = $this->db->query("SHOW INDEX FROM $tablename");
380 while (($row=$this->db->fetchByAssoc($result)) !=null) {
382 if ($row['Key_name'] =='PRIMARY') {
383 $index_type='primary';
385 elseif ( $row['Non_unique'] == '0' ) {
386 $index_type='unique';
388 $name = strtolower($row['Key_name']);
389 $indices[$name]['name']=$name;
390 $indices[$name]['type']=$index_type;
391 $indices[$name]['fields'][]=strtolower($row['Column_name']);
397 * @see DBHelper::get_columns()
399 public function get_columns(
403 //find all unique indexes and primary keys.
404 $result = $this->db->query("DESCRIBE $tablename");
407 while (($row=$this->db->fetchByAssoc($result)) !=null) {
408 $name = strtolower($row['Field']);
409 $columns[$name]['name']=$name;
411 preg_match_all("/(\w+)(?:\(([0-9]+,?[0-9]*)\)|)( unsigned)?/i", $row['Type'], $matches);
412 $columns[$name]['type']=strtolower($matches[1][0]);
413 if ( isset($matches[2][0]) && in_array(strtolower($matches[1][0]),array('varchar','char','varchar2','int','decimal','float')) )
414 $columns[$name]['len']=strtolower($matches[2][0]);
415 if ( stristr($row['Extra'],'auto_increment') )
416 $columns[$name]['auto_increment'] = '1';
417 if ($row['Null'] == 'NO' && !stristr($row['Key'],'PRI'))
418 $columns[$name]['required'] = 'true';
419 if (!empty($row['Default']) )
420 $columns[$name]['default'] = $row['Default'];
426 * @see DBHelper::add_drop_constraint()
428 public function add_drop_constraint(
434 $type = $definition['type'];
435 $fields = implode(',',$definition['fields']);
436 $name = $definition['name'];
437 $foreignTable = isset($definition['foreignTable']) ? $definition['foreignTable'] : array();
443 case 'alternate_key':
445 $sql = "DROP INDEX {$name} ";
447 $sql = "CREATE INDEX {$name} ON {$table} ({$fields})";
449 // constraints as indices
452 $sql = "ALTER TABLE {$table} DROP INDEX $name";
454 $sql = "ALTER TABLE {$table} ADD CONSTRAINT UNIQUE {$name} ({$fields})";
458 $sql = "ALTER TABLE {$table} DROP PRIMARY KEY";
460 $sql = "ALTER TABLE {$table} ADD CONSTRAINT PRIMARY KEY ({$fields})";
464 $sql = "ALTER TABLE {$table} DROP FOREIGN KEY ({$fields})";
466 $sql = "ALTER TABLE {$table} ADD CONSTRAINT FOREIGN KEY {$name} ({$fields}) REFERENCES {$foreignTable}({$foreignfields})";
473 * @see DBHelper::number_of_columns()
475 public function number_of_columns(
479 $result = $this->db->query("DESCRIBE $table_name");
481 return ($this->db->getRowCount($result));
485 * @see DBHelper::full_text_indexing_enabled()
487 protected function full_text_indexing_enabled(
491 return $this->isEngineEnabled('MyISAM');
495 * @see DBHelper::massageFieldDef()
497 public function massageFieldDef(
502 DBHelper::massageFieldDef($fieldDef,$tablename);
504 if ( isset($fieldDef['default']) &&
505 ($fieldDef['dbType'] == 'text'
506 || $fieldDef['dbType'] == 'blob'
507 || $fieldDef['dbType'] == 'longtext'
508 || $fieldDef['dbType'] == 'longblob' ))
509 unset($fieldDef['default']);
510 if ($fieldDef['dbType'] == 'uint')
511 $fieldDef['len'] = '10';
512 if ($fieldDef['dbType'] == 'ulong')
513 $fieldDef['len'] = '20';
514 if ($fieldDef['dbType'] == 'bool')
515 $fieldDef['type'] = 'tinyint';
516 if ($fieldDef['dbType'] == 'bool' && empty($fieldDef['default']) )
517 $fieldDef['default'] = '0';
518 if (($fieldDef['dbType'] == 'varchar' || $fieldDef['dbType'] == 'enum') && empty($fieldDef['len']) )
519 $fieldDef['len'] = '255';
520 if ($fieldDef['dbType'] == 'uint')
521 $fieldDef['len'] = '10';
522 if ($fieldDef['dbType'] == 'int' && empty($fieldDef['len']) )
523 $fieldDef['len'] = '11';