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.
41 * It acts as the DB abstraction layer for the application. It depends on helper classes
42 * which generate the necessary SQL. This sql is then passed to PEAR DB classes.
43 * The helper class is chosen in DBManagerFactory, which is driven by 'db_type' in 'dbconfig' under config.php.
45 * All the functions in this class will work with any bean which implements the meta interface.
46 * The passed bean is passed to helper class which uses these functions to generate correct sql.
48 * The meta interface has the following functions:
49 * getTableName() Returns table name of the object.
50 * getFieldDefinitions() Returns a collection of field definitions in order.
51 * getFieldDefintion(name) Return field definition for the field.
52 * getFieldValue(name) Returns the value of the field identified by name.
53 * If the field is not set, the function will return boolean FALSE.
54 * getPrimaryFieldDefinition() Returns the field definition for primary key
56 * The field definition is an array with the following keys:
58 * name This represents name of the field. This is a required field.
59 * type This represents type of the field. This is a required field and valid values are:
73 * length This is used only when the type is varchar and denotes the length of the string.
74 * The max value is 255.
75 * enumvals This is a list of valid values for an enum separated by "|".
76 * It is used only if the type is �enum�;
77 * required This field dictates whether it is a required value.
78 * The default value is �FALSE�.
79 * isPrimary This field identifies the primary key of the table.
80 * If none of the fields have this flag set to �TRUE�,
81 * the first field definition is assume to be the primary key.
82 * Default value for this field is �FALSE�.
83 * default This field sets the default value for the field definition.
86 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
87 * All Rights Reserved.
88 * Contributor(s): ______________________________________..
89 ********************************************************************************/
94 abstract class DBManager
97 * DBHelper object instance for this class
102 * Name of database table we are dealing with
104 protected $tableName;
109 public $database = null;
112 * Indicates whether we should die when we get an error from the DB
114 protected $dieOnError = false;
117 * Indicates whether we should html encode the results from a query by default
119 protected $encode = true;
122 * Records the execution time of the last query
124 protected $query_time = 0;
127 * Number of the last row fetched from the query result set
129 protected $lastmysqlrow = -1;
132 * Last error message from the DB backend
134 protected $last_error = '';
137 * Registry of available result sets
139 protected $lastResult = array();
142 * Current query count
144 private static $queryCount = 0;
147 * Query threshold limit
149 private static $queryLimit = 0;
152 * Array of common backend functions and what the PHP they map to is
154 protected $backendFunctions = array();
157 * Array of prepared statements and their correspoding parsed tokens
159 protected $preparedTokens = array();
162 * Wrapper for those trying to access the private and protected class members directly
164 public function __get($p)
166 $GLOBALS['log']->info('call to DBManagerFactory::$'.$p.' is deprecated');
170 public function __construct()
175 * Returns the current tablename
179 public function getTableName()
181 return $this->tableName;
185 * Returns the current database handle
189 public function getDatabase()
191 $this->checkConnection();
192 return $this->database;
196 * Returns this instance's DBHelper
198 * @return object DBHelper instance
200 public function getHelper()
202 if ( !($this->helper instanceof DBHelper) ) {
203 global $sugar_config;
205 switch ( $sugar_config['dbconfig']['db_type'] ) {
207 $my_db_helper = 'MysqlHelper';
208 if ( (!isset($sugar_config['mysqli_disabled'])
209 || $sugar_config['mysqli_disabled'] == false)
210 && function_exists('mysqli_connect') )
211 $my_db_helper = 'MysqliHelper';
214 if ( function_exists('sqlsrv_connect')
215 && (empty($config['db_mssql_force_driver']) || $config['db_mssql_force_driver'] == 'sqlsrv' ))
216 $my_db_helper = 'SqlsrvHelper';
218 && (empty($config['db_mssql_force_driver']) || $config['db_mssql_force_driver'] == 'freetds' ))
219 $my_db_helper = 'FreeTDSHelper';
221 $my_db_helper = 'MssqlHelper';
224 $my_db_helper = 'MysqlHelper';
226 $GLOBALS['log']->info("using $my_db_helper DBHelper backend");
227 require_once("include/database/{$my_db_helper}.php");
228 $this->helper = new $my_db_helper();
229 $this->helper->db = $this;
232 return $this->helper;
236 * Checks for database not being connected
238 * @param string $msg message to prepend to the error message
239 * @param bool $dieOnError true if we want to die immediately on error
242 public function checkError(
246 $userMsg = inDeveloperMode()?"$msg: ":"";
248 if (!isset($this->database)) {
249 $GLOBALS['log']->error("Database Is Not Connected");
250 if($this->dieOnError || $dieOnError)
251 sugar_die ($userMsg."Database Is Not Connected");
253 $this->last_error = $userMsg."Database Is Not Connected";
260 * This method is called by every method that runs a query.
261 * If slow query dumping is turned on and the query time is beyond
262 * the time limit, we will log the query. This function may do
263 * additional reporting or log in a different area in the future.
265 * @param string $query query to log
266 * @return boolean true if the query was logged, false otherwise
268 protected function dump_slow_queries(
272 global $sugar_config;
274 $do_the_dump = isset($sugar_config['dump_slow_queries'])
275 ? $sugar_config['dump_slow_queries'] : false;
276 $slow_query_time_msec = isset($sugar_config['slow_query_time_msec'])
277 ? $sugar_config['slow_query_time_msec'] : 5000;
280 if($slow_query_time_msec < ($this->query_time * 1000)) {
281 // Then log both the query and the query time
282 $GLOBALS['log']->fatal('Slow Query (time:'.$this->query_time."\n".$query);
290 * Scans order by to ensure that any field being ordered by is.
292 * It will throw a warning error to the log file - fatal if slow query logging is enabled
294 * @param string $sql query to be run
295 * @param bool $object_name optional, object to look up indices in
296 * @return bool true if an index is found false otherwise
298 protected function checkQuery(
304 preg_match_all("'.* FROM ([^ ]*).* ORDER BY (.*)'is", $sql, $match);
306 if (!empty($match[1][0]))
307 $table = $match[1][0];
311 if (!empty($object_name) && !empty($GLOBALS['dictionary'][$object_name]))
312 $indices = $GLOBALS['dictionary'][$object_name]['indices'];
314 if (empty($indices)) {
315 foreach ( $GLOBALS['dictionary'] as $current ) {
316 if ($current['table'] == $table){
317 $indices = $current['indices'];
322 if (empty($indices)) {
323 $GLOBALS['log']->warn('CHECK QUERY: Could not find index definitions for table ' . $table);
326 if (!empty($match[2][0])) {
327 $orderBys = explode(' ', $match[2][0]);
328 foreach ($orderBys as $orderBy){
329 $orderBy = trim($orderBy);
332 $orderBy = strtolower($orderBy);
333 if ($orderBy == 'asc' || $orderBy == 'desc')
336 $orderBy = str_replace(array($table . '.', ','), '', $orderBy);
338 foreach ($indices as $index)
339 if (empty($index['db']) || $index['db'] == $this->dbType)
340 foreach ($index['fields'] as $field)
341 if ($field == $orderBy)
344 $warning = 'Missing Index For Order By Table: ' . $table . ' Order By:' . $orderBy ;
345 if (!empty($GLOBALS['sugar_config']['dump_slow_queries']))
346 $GLOBALS['log']->fatal('CHECK QUERY:' .$warning);
348 $GLOBALS['log']->warn('CHECK QUERY:' .$warning);
356 * Returns the time the last query took to execute
360 public function getQueryTime()
362 return $this->query_time;
366 * Checks the current connection; if it is not connected then reconnect
368 public function checkConnection()
370 $this->last_error = '';
371 if (!isset($this->database))
376 * Sets the dieOnError value
380 public function setDieOnError(
384 $this->dieOnError = $value;
388 * Implements a generic insert for any bean.
390 * @param object $bean SugarBean instance
392 public function insert(
396 $sql = $this->getHelper()->insertSQL($bean);
397 $this->tableName = $bean->getTableName();
398 $msg = "Error inserting into table: ".$this->tableName;
399 $this->query($sql,true,$msg);
403 * Implements a generic update for any bean
405 * @param object $bean Sugarbean instance
406 * @param array $where values with the keys as names of fields.
407 * If we want to pass multiple values for a name, pass it as an array
408 * If where is not passed, it defaults to id of table
410 public function update(
412 array $where = array()
415 $sql = $this->getHelper()->updateSQL($bean, $where);
416 $this->tableName = $bean->getTableName();
417 $msg = "Error updating table: ".$this->tableName. ":";
418 $this->query($sql,true,$msg);
422 * Implements a generic delete for any bean identified by id
424 * @param object $bean Sugarbean instance
425 * @param array $where values with the keys as names of fields.
426 * If we want to pass multiple values for a name, pass it as an array
427 * If where is not passed, it defaults to id of table
429 public function delete(
431 array $where = array()
434 $sql = $this->getHelper()->deleteSQL($bean, $where);
435 $this->tableName = $bean->getTableName();
436 $msg = "Error deleting from table: ".$this->tableName. ":";
437 $this->query($sql,true,$msg);
441 * Implements a generic retrieve for any bean identified by id
443 * If we want to pass multiple values for a name, pass it as an array
444 * If where is not passed, it defaults to id of table
446 * @param object $bean Sugarbean instance
447 * @param array $where values with the keys as names of fields.
448 * @return resource result from the query
450 public function retrieve(
452 array $where = array()
455 $sql = $this->getHelper()->retrieveSQL($bean, $where);
456 $this->tableName = $bean->getTableName();
457 $msg = "Error retriving values from table:".$this->tableName. ":";
458 return $this->query($sql,true,$msg);
462 * Implements a generic retrieve for a collection of beans.
464 * These beans will be joined in the sql by the key attribute of field defs.
465 * Currently, this function does support outer joins.
467 * @param array $beans Sugarbean instance(s)
468 * @param array $cols columns to be returned with the keys as names of bean as identified by
469 * get_class of bean. Values of this array is the array of fieldDefs to be returned for a bean.
470 * If an empty array is passed, all columns are selected.
471 * @param array $where values with the keys as names of bean as identified by get_class of bean
472 * Each value at the first level is an array of values for that bean identified by name of fields.
473 * If we want to pass multiple values for a name, pass it as an array
474 * If where is not passed, all the rows will be returned.
477 public function retrieveView(
479 array $cols = array(),
480 array $where = array()
483 $sql = $this->getHelper()->retrieveViewSQL($beans, $cols, $where);
484 $this->tableName = "View Collection"; // just use this string for msg
485 $msg = "Error retriving values from table:".$this->tableName. ":";
486 $this->query($sql,true,$msg);
491 * Implements creation of a db table for a bean.
493 * @param object $bean Sugarbean instance
495 public function createTable(
499 $sql = $this->getHelper()->createTableSQL($bean);
500 $this->tableName = $bean->getTableName();
501 $msg = "Error creating table: ".$this->tableName. ":";
502 $this->query($sql,true,$msg);
506 * Implements creation of a db table
508 * @param string $tablename
509 * @param array $fieldDefs
510 * @param array $indices
511 * @param string $engine MySQL engine to use
513 public function createTableParams(
520 if (!empty($fieldDefs)) {
521 $sql = $this->getHelper()
522 ->createTableSQLParams($tablename, $fieldDefs, $indices,$engine);
523 $this->tableName = $tablename;
525 $msg = "Error creating table: ".$this->tableName. ":";
526 $this->query($sql,true,$msg);
532 * Implements repair of a db table for a bean.
534 * @param object $bean SugarBean instance
535 * @param bool $execute true if we want the action to take place, false if we just want the sql returned
536 * @return string SQL statement or empty string, depending upon $execute
538 public function repairTable(SugarBean $bean, $execute = true)
540 $indices = $bean->getIndices();
541 $fielddefs = $bean->getFieldDefinitions();
542 $tablename = $bean->getTableName();
544 //Clean the indicies to prevent duplicate definitions
545 $new_Indecies = array();
546 foreach($indices as $ind_def){
547 $new_Indecies[$ind_def['name']] = $ind_def;
549 //jc: added this for beans that do not actually have a table, namely
550 //ForecastOpportunities
551 if($tablename == 'does_not_exist' || $tablename == '')
556 if (isset($dictionary[$bean->getObjectName()]['engine']) && !empty($dictionary[$bean->getObjectName()]['engine']) )
557 $engine = $dictionary[$bean->getObjectName()]['engine'];
559 return $this->repairTableParams($tablename, $fielddefs,$new_Indecies,$execute,$engine);
563 * Builds the SQL commands that repair a table structure
565 * @param string $tablename
566 * @param array $fielddefs
567 * @param array $indices
568 * @param bool $execute optional, true if we want the queries executed instead of returned
569 * @param string $engine optional, MySQL engine
571 public function repairTableParams(
579 global $table_descriptions;
581 //jc: had a bug when running the repair if the tablename is blank the repair will
582 //fail when it tries to create a repair table
583 if ($tablename == '')
585 if (empty($fielddefs))
588 //if the table does not exist create it and we are done
589 $sql = "/* Table : $tablename */\n";
590 if (!$this->tableExists($tablename)){
592 $createtablesql = $this->getHelper()
593 ->createTableSQLParams($tablename,$fielddefs,$indices,$engine);
594 if($execute && $createtablesql){
595 $this->createTableParams($tablename,$fielddefs,$indices,$engine);
598 $sql .= "/* MISSING TABLE: {$tablename} */\n";
599 $sql .= $createtablesql . "\n";
603 $compareFieldDefs = $this->getHelper()->get_columns($tablename);
604 $compareIndices = $this->getHelper()->get_indices($tablename);
606 $take_action = false;
608 // do column comparisions
609 $sql .= "/*COLUMNS*/\n";
610 foreach ($fielddefs as $value) {
611 if (isset($value['source']) && $value['source'] != 'db')
614 $name = $value['name'];
615 // add or fix the field defs per what the DB is expected to give us back
616 $this->getHelper()->massageFieldDef($value,$tablename);
618 $ignorerequired=false;
620 //Do not track requiredness in the DB, auto_increment, ID, and deleted fields are always required in the DB, so don't force those
621 if (empty($value['auto_increment']) && !isset($value['isnull'])
622 && (empty($value['type']) || $value['type'] != 'id')
623 && (empty($value['dbType']) || $value['dbType'] != 'id')
624 && (empty($value['name']) || ($value['name'] != 'id' && $value['name'] != 'deleted'))
626 $value['required'] = false;
628 //Should match the conditions in DBHelper::oneColumnSQLRep for DB required fields, type='id' fields will sometimes
629 //come into this function as 'type' = 'char', 'dbType' = 'id' without required set in $value. Assume they are correct and leave them alone.
630 else if (($name == 'id' || $value['type'] == 'id' || (isset($value['dbType']) && $value['dbType'] == 'id'))
631 && (!isset($value['required']) && isset($compareFieldDefs[$name]['required'])))
633 $value['required'] = $compareFieldDefs[$name]['required'];
636 if ( !isset($compareFieldDefs[$name]) ) {
637 // ok we need this field lets create it
638 $sql .= "/*MISSING IN DATABASE - $name - ROW*/\n";
639 $sql .= $this->getHelper()->addColumnSQL($tablename, $value) . "\n";
641 $this->addColumn($tablename, $value);
644 elseif ( !$this->compareVarDefs($compareFieldDefs[$name],$value)) {
645 //fields are different lets alter it
646 $sql .= "/*MISMATCH WITH DATABASE - $name - ROW ";
647 foreach($compareFieldDefs[$name] as $rKey => $rValue)
648 $sql .= "[$rKey] => '$rValue' ";
650 $sql .= "/* VARDEF - $name - ROW";
651 foreach($value as $rKey => $rValue)
652 $sql .= "[$rKey] => '$rValue' ";
655 //jc: oracle will complain if you try to execute a statement that sets a column to (not) null
656 //when it is already (not) null
657 if ( isset($value['isnull']) && isset($compareFieldDefs[$name]['isnull']) ) {
658 if ($value['isnull'] === $compareFieldDefs[$name]['isnull']) {
659 unset($value['required']);
660 $ignorerequired=true;
664 //dwheeler: Once a column has been defined as null, we cannot try to force it back to !null
665 if ((isset($value['required']) && ($value['required'] === true || $value['required'] == 'true' || $value['required'] === 1))
666 && (empty($compareFieldDefs[$name]['required']) || $compareFieldDefs[$name]['required'] != 'true'))
668 $ignorerequired = true;
671 $sql .= $this->getHelper()->alterColumnSQL($tablename, $value,$ignorerequired) . "\n";
673 $this->alterColumn($tablename, $value,$ignorerequired);
679 // do index comparisions
680 $sql .= "/* INDEXES */\n";
681 $correctedIndexs = array();
682 foreach ($indices as $value) {
683 if (isset($value['source']) && $value['source'] != 'db')
687 $validDBName = $this->helper->getValidDBName($name, true, 'index', true);
688 if (isset($compareIndices[$validDBName])) {
689 $value['name'] = $validDBName;
691 $name = $value['name'];
693 //Don't attempt to fix the same index twice in one pass;
694 if (isset($correctedIndexs[$name]))
697 //don't bother checking primary nothing we can do about them
698 if (isset($value['type']) && $value['type'] == 'primary')
701 //database helpers do not know how to handle full text indices
702 if ($value['type']=='fulltext')
705 if ( in_array($value['type'],array('alternate_key','foreign')) )
706 $value['type'] = 'index';
708 if ( !isset($compareIndices[$name]) ) {
709 //First check if an index exists that doens't match our name, if so, try to rename it
711 foreach ($compareIndices as $ex_name => $ex_value)
713 if($this->compareVarDefs($ex_value, $value, true))
721 $sql .= "/*MISSNAMED INDEX IN DATABASE - $name - $ex_name */\n";
722 $sql .= $this->renameIndex($tablename, $ex_name, $name, $execute) . "\n";
726 // ok we need this field lets create it
727 $sql .= "/*MISSING INDEX IN DATABASE - $name -{$value['type']} ROW */\n";
728 $sql .= $this->addIndexes($tablename,array($value), $execute) . "\n";
731 $correctedIndexs[$name] = true;
733 elseif ( !$this->compareVarDefs($compareIndices[$name],$value) ) {
734 // fields are different lets alter it
735 $sql .= "/*INDEX MISMATCH WITH DATABASE - $name - ROW ";
736 foreach ($compareIndices[$name] as $n1 => $t1) {
738 if ( $n1 == 'fields' )
739 foreach($t1 as $rKey => $rValue)
740 $sql .= "[$rKey] => '$rValue' ";
745 $sql .= "/* VARDEF - $name - ROW";
746 foreach ($value as $n1 => $t1) {
748 if ( $n1 == 'fields' )
749 foreach ($t1 as $rKey => $rValue)
750 $sql .= "[$rKey] => '$rValue' ";
755 $sql .= $this->modifyIndexes($tablename,array($value), $execute) . "\n";
757 $correctedIndexs[$name] = true;
761 return ($take_action === true) ? $sql : "";
765 * Compares two vardefs
767 * @param array $fielddef1 This is from the database
768 * @param array $fielddef2 This is from the vardef
769 * @return bool true if they match, false if they don't
771 public function compareVarDefs(
777 foreach ( $fielddef1 as $key => $value ) {
778 if ( $key == 'name' && ( strtolower($fielddef1[$key]) == strtolower($fielddef2[$key]) || $ignoreName) )
780 if ( isset($fielddef2[$key]) && $fielddef1[$key] == $fielddef2[$key] )
782 //Ignore len if its not set in the vardef
783 if ($key == 'len' && empty($fielddef2[$key]))
792 * Compare a field in two tables
794 * @param string $name field name
795 * @param string $table1
796 * @param string $table2
797 * @return array array with keys 'msg','table1','table2'
799 public function compareFieldInTables(
805 $row1 = $this->describeField($name, $table1);
806 $row2 = $this->describeField($name, $table2);
807 $returnArray = array(
813 $ignore_filter = array('Key'=>1);
816 // Exists on table1 but not table2
817 $returnArray['msg'] = 'not_exists_table2';
820 if (sizeof($row1) != sizeof($row2)) {
821 $returnArray['msg'] = 'no_match';
824 $returnArray['msg'] = 'match';
825 foreach($row1 as $key => $value){
826 //ignore keys when checking we will check them when we do the index check
827 if( !isset($ignore_filter[$key]) && $row1[$key] !== $row2[$key]){
828 $returnArray['msg'] = 'no_match';
835 $returnArray['msg'] = 'not_exists_table1';
842 * Compare an index in two different tables
844 * @param string $name index name
845 * @param string $table1
846 * @param string $table2
847 * @return array array with keys 'msg','table1','table2'
849 public function compareIndexInTables(
855 $row1 = $this->describeIndex($name, $table1);
856 $row2 = $this->describeIndex($name, $table2);
857 $returnArray = array(
862 $ignore_filter = array('Table'=>1, 'Seq_in_index'=>1,'Cardinality'=>1, 'Sub_part'=>1, 'Packed'=>1, 'Comment'=>1);
866 //Exists on table1 but not table2
867 $returnArray['msg'] = 'not_exists_table2';
870 if (sizeof($row1) != sizeof($row2)) {
871 $returnArray['msg'] = 'no_match';
874 $returnArray['msg'] = 'match';
875 foreach ($row1 as $fname => $fvalue) {
876 if (!isset($row2[$fname])) {
877 $returnArray['msg'] = 'no_match';
879 foreach($fvalue as $key => $value) {
880 //ignore keys when checking we will check them when we do the index check
881 if(!isset($ignore_filter[$key]) && $row1[$fname][$key] != $row2[$fname][$key]){
882 $returnArray['msg'] = 'no_match';
890 $returnArray['msg'] = 'not_exists_table1';
898 * Creates an index identified by name on the given fields.
900 * @param object $bean SugarBean instance
901 * @param array $fieldDefs
902 * @param string $name index name
903 * @param bool $unique optional, true if we want to create an unique index
905 public function createIndex(
912 $sql = $this->getHelper()->createIndexSQL($bean, $fieldDefs, $name, $unique);
913 $this->tableName = $bean->getTableName();
914 $msg = "Error creating index $name on table: ".$this->tableName. ":";
915 $this->query($sql,true,$msg);
921 * @param string $tablename
922 * @param array $indexes indexes to add
923 * @param bool $execute true if we want to execute the returned sql statement
924 * @return string SQL statement
926 public function addIndexes(
932 $alters = $this->getHelper()->keysSQL($indexes,true,'ADD');
933 $sql = "ALTER TABLE $tablename $alters";
939 public function renameIndex($tablename, $oldName, $newName, $execute = true)
947 * @param string $tablename
948 * @param array $indexes indexes to drop
949 * @param bool $execute true if we want to execute the returned sql statement
950 * @return string SQL statement
952 public function dropIndexes(
959 foreach ($indexes as $index) {
960 $name =$index['name'];
962 unset($GLOBALS['table_descriptions'][$tablename]['indexes'][$name]);
963 if ($index['type'] == 'primary')
964 $name = 'PRIMARY KEY';
966 $name = "INDEX $name";
968 $sql .= " DROP $name ";
970 $sql .= ", DROP $name ";
973 $sql = "ALTER TABLE $tablename $sql";
983 * @param string $tablename
984 * @param array $indexes indexes to modify
985 * @param bool $execute true if we want to execute the returned sql statement
986 * @return string SQL statement
988 public function modifyIndexes(
994 return $this->dropIndexes($tablename, $indexes, $execute)."\n".
995 $this->addIndexes($tablename, $indexes, $execute);
999 * Adds a column to table identified by field def.
1001 * @param string $tablename
1002 * @param array $fieldDefs
1004 public function addColumn(
1009 $this->tableName = $tablename;
1010 $sql = $this->getHelper()->addColumnSQL($this->tableName, $fieldDefs);
1011 if ($this->getHelper()->isFieldArray($fieldDefs)){
1012 foreach ($fieldDefs as $fieldDef) $columns[] = $fieldDef['name'];
1013 $columns = implode(",", $columns);
1016 $columns = $fieldDefs['name'];
1018 $msg = "Error adding column(s) ".$columns." on table: ".$this->tableName. ":";
1019 $this->query($sql,true,$msg);
1023 * Alters old column identified by oldFieldDef to new fieldDef.
1025 * @param string $tablename
1026 * @param array $newFieldDef
1027 * @param bool $ignoreRequired optional, true if we are ignoring this being a required field
1029 public function alterColumn(
1032 $ignoreRequired = false
1035 $this->tableName = $tablename;
1036 $sql = $this->getHelper()->alterColumnSQL($this->tableName, $newFieldDef,$ignoreRequired);
1037 if ($this->getHelper()->isFieldArray($newFieldDef)){
1038 foreach ($newFieldDef as $fieldDef) {
1039 unset($GLOBALS['table_descriptions'][$tablename][$fieldDef['name']]);
1040 $columns[] = $fieldDef['name'];
1042 $columns = implode(",", $columns);
1045 unset($GLOBALS['table_descriptions'][$tablename][$newFieldDef['name']]);
1046 $columns = $newFieldDef['name'];
1049 $msg = "Error altering column(s) ".$columns." on table: ".$this->tableName. ":";
1050 $this->query($sql,true,$msg);
1054 * Drops the table associated with a bean
1056 * @param object $bean SugarBean instance
1058 public function dropTable(
1062 $this->tableName = $bean->getTableName();
1063 $this->dropTableName( $this->tableName);
1067 * Drops the table by name
1069 * @param string $name SugarBean instance
1071 public function dropTableName(
1075 $sql = $this->getHelper()->dropTableNameSQL($name);
1076 $msg = "Error dropping table ".$this->tableName. ":";
1077 $this->query($sql,true,$msg);
1081 * Deletes a column identified by fieldDef.
1083 * @param string $name SugarBean instance
1084 * @param array $fieldDefs
1086 public function deleteColumn(
1091 $sql = $this->getHelper()->deleteColumnSQL($bean, $fieldDefs);
1092 $this->tableName = $bean->getTableName();
1093 $msg = "Error deleting column(s) ".$columns." on table: ".$this->tableName. ":";
1094 $this->query($sql,true,$msg);
1098 * Fetches all the rows for a select query. Returns FALSE if query failed and
1099 * DB_OK for all other queries
1103 * @param resource $result
1104 * @return array All rows in result set
1106 private function setResult(
1110 $GLOBALS['log']->info('call to DBManager::setResult() is deprecated');
1111 if (PEAR::isError($result) === true) {
1112 $GLOBALS['log']->error($msg);
1115 elseif ($result != DB_OK) {
1117 $GLOBALS['log']->fatal("setResult:".print_r($result,true));
1120 while (ocifetchinto($result, $row, OCI_ASSOC|OCI_RETURN_NULLS|OCI_RETURN_LOBS)){
1121 $err = ocierror($result);
1122 if ($err == false) $rows[] = $row;
1127 $GLOBALS['log']->fatal("setResult: returning rows from setResult");
1132 * Private function to handle most of the sql statements which go as queries
1136 * @param string $sql
1139 * @param boolean $dieOnError
1140 * @param string $msg
1141 * @return array All rows in result set
1143 public function executeLimitQuery(
1147 $dieOnError = false,
1150 $GLOBALS['log']->info('call to DBManager::executeLimitQuery() is deprecated');
1151 $result = $this->limitQuery($query,$start,$count, $dieOnError, $msg);
1152 return $this->setResult($result);
1156 * Private function to handle most of the sql statements which go as queries
1160 * @param string $query
1161 * @param string $msg
1162 * @param bool $getRows
1163 * @return array All rows in result set
1165 public function executeQuery(
1171 $GLOBALS['log']->info('call to DBManager::executeQuery() is deprecated');
1172 $result = $this->query($query,true,$msg);
1174 return $this->setResult($result);
1175 // dd not get rows. Simply go on.
1179 * Given a db_type return the correct DBHelper
1183 * @param string $db_type the type of database being used
1184 * @return object DBHelper instance corresponding to the db_type
1186 private function configureHelper(
1190 $GLOBALS['log']->info('call to DBManager::configureHelper() is deprecated');
1191 global $sugar_config;
1193 $my_db_helper = 'MysqlHelper';
1194 if( $sugar_config['dbconfig']['db_type'] == "mysql" ) {
1195 if (!isset($sugar_config['mysqli_disabled']) or $sugar_config['mysqli_disabled']==false) {
1196 if (function_exists('mysqli_connect')) {
1197 $my_db_helper = 'MysqliHelper';
1202 if($db_type == "oci8" ){
1203 }else if($db_type == "mssql"){
1204 require_once('include/database/MssqlHelper.php');
1205 $my_db_helper = 'MssqlHelper';
1207 if($my_db_helper == 'MysqlHelper'){
1208 require_once('include/database/MysqlHelper.php');
1210 return new $my_db_helper();
1214 * Generate a set of Insert statements based on the bean given
1218 * @param object $bean the bean from which table we will generate insert stmts
1219 * @param string $select_query the query which will give us the set of objects we want to place into our insert statement
1220 * @param int $start the first row to query
1221 * @param int $count the number of rows to query
1222 * @param string $table the table to query from
1223 * @param string $db_type the client db type
1224 * @return string SQL insert statement
1226 public function generateInsertSQL(
1233 $is_related_query = false
1236 $GLOBALS['log']->info('call to DBManager::generateInsertSQL() is deprecated');
1237 global $sugar_config;
1239 $count_query = $bean->create_list_count_query($select_query);
1240 if(!empty($count_query))
1242 // We have a count query. Run it and get the results.
1243 $result = $this->query($count_query, true, "Error running count query for $this->object_name List: ");
1244 $assoc = $this->fetchByAssoc($result);
1245 if(!empty($assoc['c']))
1247 $rows_found = $assoc['c'];
1251 $count = $sugar_config['list_max_entries_per_page'];
1253 $next_offset = $start + $count;
1255 $result = $this->limitQuery($select_query, $start, $count);
1256 $row_count = $this->getRowCount($result);
1258 $sql = "INSERT INTO ".$table;
1259 $custom_sql = "INSERT INTO ".$table."_cstm";
1261 // get field definitions
1262 $fields = $bean->getFieldDefinitions();
1263 $custom_fields = array();
1265 if($bean->hasCustomFields()){
1266 foreach ($fields as $fieldDef){
1267 if($fieldDef['source'] == 'custom_fields'){
1268 $custom_fields[$fieldDef['name']] = $fieldDef['name'];
1271 if(!empty($custom_fields)){
1272 $custom_fields['id_c'] = 'id_c';
1273 $id_field = array('name' => 'id_c', custom_type => 'id',);
1274 $fields[] = $id_field;
1278 // get column names and values
1279 $row_array = array();
1281 $cstm_row_array = array();
1282 $cstm_columns = array();
1283 $built_columns = false;
1284 //configure client helper
1285 $dbHelper = $this->configureHelper($db_type);
1286 while(($row = $this->fetchByAssoc($result)) != null)
1289 $cstm_values = array();
1290 if(!$is_related_query){
1291 foreach ($fields as $fieldDef)
1293 if(isset($fieldDef['source']) && $fieldDef['source'] != 'db' && $fieldDef['source'] != 'custom_fields') continue;
1294 $val = $row[$fieldDef['name']];
1296 //handle auto increment values here only need to do this on insert not create
1297 if ($fieldDef['name'] == 'deleted'){
1298 $values['deleted'] = $val;
1299 if(!$built_columns){
1300 $columns[] = 'deleted';
1305 $type = $fieldDef['type'];
1306 if(!empty($fieldDef['custom_type'])){
1307 $type = $fieldDef['custom_type'];
1309 // need to do some thing about types of values
1310 if($db_type == 'mysql' && $val == '' && ($type == 'datetime' || $type == 'date' || $type == 'int' || $type == 'currency' || $type == 'decimal')){
1311 if(!empty($custom_fields[$fieldDef['name']]))
1312 $cstm_values[$fieldDef['name']] = 'null';
1314 $values[$fieldDef['name']] = 'null';
1316 if(isset($type) && $type=='int') {
1317 if(!empty($custom_fields[$fieldDef['name']]))
1318 $cstm_values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val));
1320 $values[$fieldDef['name']] = $GLOBALS['db']->quote(from_html($val));
1322 if(!empty($custom_fields[$fieldDef['name']]))
1323 $cstm_values[$fieldDef['name']] = "'".$GLOBALS['db']->quote(from_html($val))."'";
1325 $values[$fieldDef['name']] = "'".$GLOBALS['db']->quote(from_html($val))."'";
1328 if(!$built_columns){
1329 if(!empty($custom_fields[$fieldDef['name']]))
1330 $cstm_columns[] = $fieldDef['name'];
1332 $columns[] = $fieldDef['name'];
1338 foreach ($row as $key=>$val)
1340 if($key != 'orc_row'){
1341 $values[$key] = "'$val'";
1342 if(!$built_columns){
1348 $built_columns = true;
1349 if(!empty($values)){
1350 $row_array[] = $values;
1352 if(!empty($cstm_values) && !empty($cstm_values['id_c']) && (strlen($cstm_values['id_c']) > 7)){
1353 $cstm_row_array[] = $cstm_values;
1357 //if (sizeof ($values) == 0) return ""; // no columns set
1359 // get the entire sql
1360 $sql .= "(".implode(",", $columns).") ";
1362 for($i = 0; $i < count($row_array); $i++){
1363 $sql .= " (".implode(",", $row_array[$i]).")";
1364 if($i < (count($row_array) - 1)){
1369 // get the entire sql
1370 $custom_sql .= "(".implode(",", $cstm_columns).") ";
1371 $custom_sql .= "VALUES";
1373 for($i = 0; $i < count($cstm_row_array); $i++){
1374 $custom_sql .= " (".implode(",", $cstm_row_array[$i]).")";
1375 if($i < (count($cstm_row_array) - 1)){
1376 $custom_sql .= ", ";
1379 return array('data' => $sql, 'cstm_sql' => $custom_sql, 'result_count' => $row_count, 'total_count' => $rows_found, 'next_offset' => $next_offset);
1383 * Disconnects all instances
1385 public function disconnectAll()
1387 global $dbinstances;
1389 if (!empty($dbinstances)) {
1390 $cur = current($dbinstances);
1393 $cur = next($dbinstances);
1400 * This function sets the query threshold limit
1402 * @param int $limit value of query threshold limit
1404 public static function setQueryLimit($limit){
1405 //reset the queryCount
1406 self::$queryCount = 0;
1408 self::$queryLimit = $limit;
1412 * Returns the static queryCount value
1414 * @return int value of the queryCount static variable
1416 public static function getQueryCount()
1418 return self::$queryCount;
1423 * Resets the queryCount value to 0
1426 public static function resetQueryCount() {
1427 self::$queryCount = 0;
1431 * This function increments the global $sql_queries variable
1433 * @param $sql The SQL statement being counted
1435 public function countQuery(
1439 if (self::$queryLimit != 0 && ++self::$queryCount > self::$queryLimit
1440 &&(empty($GLOBALS['current_user']) || !is_admin($GLOBALS['current_user']))) {
1441 require_once('include/resource/ResourceManager.php');
1442 $resourceManager = ResourceManager::getInstance();
1443 $resourceManager->notifyObservers('ERR_QUERY_LIMIT');
1448 * Returns a string properly quoted for this database
1450 * @param string $string
1451 * @param bool $isLike
1453 public function quote(
1458 return from_html($string);
1462 * Returns a string properly quoted for this database that is an email
1464 * @param string $string
1465 * @param bool $isLike
1467 abstract public function quoteforEmail(
1473 * Quote the strings of the passed in array
1475 * The array must only contain strings
1477 * @param array $array
1478 * @param bool $isLike
1480 public function arrayQuote(
1485 for($i = 0; $i < count($array); $i++){
1486 $array[$i] = $this->quote($array[$i], $isLike);
1490 * Parses and runs queries
1492 * @param string $sql SQL Statement to execute
1493 * @param bool $dieOnError True if we want to call die if the query returns errors
1494 * @param string $msg Message to log if error occurs
1495 * @param bool $suppress Flag to suppress all error output unless in debug logging mode.
1496 * @return resource result set
1498 abstract public function query(
1500 $dieOnError = false,
1506 * Runs a limit query: one where we specify where to start getting records and how many to get
1508 * @param string $sql
1511 * @param boolean $dieOnError
1512 * @param string $msg
1513 * @return resource query result
1515 abstract function limitQuery(
1519 $dieOnError = false,
1523 * Frees out previous results
1525 * @param resource $result optional, pass if you want to free a single result instead of all results
1527 protected function freeResult(
1531 $free_result = $this->backendFunctions['free_result'];
1532 if(!$result && $this->lastResult){
1533 $result = current($this->lastResult);
1535 $free_result($result);
1536 $result = next($this->lastResult);
1538 $this->lastResult = array();
1541 $free_result($result);
1546 * Runs a query and returns a single row
1548 * @param string $sql SQL Statement to execute
1549 * @param bool $dieOnError True if we want to call die if the query returns errors
1550 * @param string $msg Message to log if error occurs
1551 * @param bool $suppress Message to log if error occurs
1552 * @return array single row from the query
1554 public function getOne(
1556 $dieOnError = false,
1561 $GLOBALS['log']->info("Get One: . |$sql|");
1562 $this->checkConnection();
1563 if(!($this instanceof MysqlManager) || stripos($sql, ' LIMIT ') === false) {
1564 $queryresult = $this->limitQuery($sql, 0, 1, $dieOnError, $msg);
1566 // backward compatibility with queries having LIMIT
1567 $queryresult = $this->query($sql, $dieOnError, $msg);
1572 $row = $this->fetchByAssoc($queryresult);
1576 $this->checkError($msg.' Get One Failed:' . $sql, $dieOnError);
1578 $this->freeResult($queryresult);
1579 return array_shift($row);
1583 * will return the associative array of the row for a query or false if more than one row was returned
1587 * @param string $sql
1588 * @param bool $dieonerror
1589 * @param string $msg
1590 * @param bool $encode
1591 * @return array associative array of the row or false
1593 public function requireSingleRow(
1595 $dieOnError = false,
1600 $GLOBALS['log']->info('call to DBManager::requireSingleRow() is deprecated');
1601 $result = $this->limitQuery($sql,0,2, $dieOnError, $msg);
1604 while ($row = $this->fetchByAssoc($result)){
1605 if(!$firstRow)$firstRow = $row;
1616 * Returns the description of fields based on the result
1618 * @param resource $result
1619 * @param boolean $make_lower_case
1620 * @return array field array
1622 abstract public function getFieldsArray(
1624 $make_lower_case = false);
1627 * Returns the number of rows returned by the result
1629 * @param resource $result
1632 public function getRowCount(
1636 $row_count = $this->backendFunctions['row_count'];
1637 if(isset($result) && !empty($result)){
1638 return $row_count($result);
1644 * Returns the number of rows affected by the last query
1648 public function getAffectedRowCount()
1650 $affected_row_count = $this->backendFunctions['affected_row_count'];
1651 return $affected_row_count($this->getDatabase());
1655 * Fetches the next row in the query result into an associative array
1657 * @param resource $result
1658 * @param int $rowNum optional, specify a certain row to return
1659 * @param bool $encode optional, true if we want html encode the resulting array
1660 * @return array returns false if there are no more rows available to fetch
1662 abstract public function fetchByAssoc(
1668 * Connects to the database backend
1670 * Takes in the database settings and opens a database connection based on those
1671 * will open either a persistent or non-persistent connection.
1672 * If a persistent connection is desired but not available it will defualt to non-persistent
1674 * configOptions must include
1675 * db_host_name - server ip
1676 * db_user_name - database user name
1677 * db_password - database password
1679 * @param array $configOptions
1680 * @param boolean $dieOnError
1682 abstract public function connect(
1683 array $configOptions = null,
1688 * Disconnects from the database
1690 * Also handles any cleanup needed
1692 public function disconnect()
1694 $GLOBALS['log']->debug('Calling DBManager::disconnect()');
1695 $close = $this->backendFunctions['close'];
1696 if(isset($this->database)){
1697 $this->freeResult();
1698 if ( is_resource($this->database) || is_object($this->database) )
1699 $close($this->database);
1700 unset($this->database);
1705 * Returns the field description for a given field in table
1707 * @param string $name
1708 * @param string $tablename
1711 protected function describeField(
1716 global $table_descriptions;
1717 if(isset($table_descriptions[$tablename])
1718 && isset($table_descriptions[$tablename][$name]))
1719 return $table_descriptions[$tablename][$name];
1721 $table_descriptions[$tablename] = array();
1722 $table_descriptions[$tablename][$name] = $this->helper->get_columns($tablename);
1724 if(isset($table_descriptions[$tablename][$name]))
1725 return $table_descriptions[$tablename][$name];
1731 * Returns the index description for a given index in table
1733 * @param string $name
1734 * @param string $tablename
1737 protected function describeIndex(
1742 global $table_descriptions;
1743 if(isset($table_descriptions[$tablename]) && isset($table_descriptions[$tablename]['indexes']) && isset($table_descriptions[$tablename]['indexes'][$name])){
1744 return $table_descriptions[$tablename]['indexes'][$name];
1747 $table_descriptions[$tablename]['indexes'] = array();
1749 $result = $this->helper->get_indices($tablename);
1751 foreach($result as $index_name => $row) {
1752 if(!isset($table_descriptions[$tablename]['indexes'][$index_name])){
1753 $table_descriptions[$tablename]['indexes'][$index_name] = array();
1755 $table_descriptions[$tablename]['indexes'][$index_name]['Column_name'] = $row;
1758 if(isset($table_descriptions[$tablename]['indexes'][$name])){
1759 return $table_descriptions[$tablename]['indexes'][$name];
1766 * Returns an array of table for this database
1768 * @return $tables an array of with table names
1769 * @return false if no tables found
1771 abstract public function getTablesArray();
1774 * Return's the version of the database
1778 abstract public function version();
1781 * Checks if a table with the name $tableName exists
1782 * and returns true if it does or false otherwise
1784 * @param string $tableName
1787 abstract public function tableExists($tableName);
1790 * Truncates a string to a given length
1792 * @param string $string
1793 * @param int $len length to trim to
1796 public function truncate(
1801 if ( is_numeric($len) && $len > 0)
1803 $string = mb_substr($string,0,(int) $len, "UTF-8");
1810 * Given a sql stmt attempt to parse it into the sql and the tokens. Then return the index of this prepared statement
1811 * Tokens can come in the following forms:
1812 * ? - a scalar which will be quoted
1813 * ! - a literal which will not be quoted
1814 * & - binary data to read from a file
1816 * @param string $sql The sql to parse
1817 * @return int index of the prepared statement to be used with execute
1819 public function prepareQuery($sql){
1820 //parse out the tokens
1821 $tokens = preg_split('/((?<!\\\)[&?!])/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE);
1823 //maintain a count of the actual tokens for quick reference in execute
1827 foreach ($tokens as $key => $val) {
1837 //escape any special characters
1838 $tokens[$key] = preg_replace('/\\\([&?!])/', "\\1", $val);
1839 $sqlStr .= $tokens[$key];
1844 $this->preparedTokens[] = array('tokens' => $tokens, 'tokenCount' => $count, 'sqlString' => $sqlStr);
1845 end($this->preparedTokens);
1846 return key($this->preparedTokens);
1850 * Takes a prepared stmt index and the data to replace and creates the query and runs it.
1852 * @param int $stmt The index of the prepared statement from preparedTokens
1853 * @param array $data The array of data to replace the tokens with.
1854 * @return resource result set or false on error
1856 public function executePreparedQuery($stmt, $data = array()){
1857 if(!empty($this->preparedTokens[$stmt])){
1858 if(!is_array($data)){
1859 $data = array($data);
1862 $pTokens = $this->preparedTokens[$stmt];
1864 //ensure that the number of data elements matches the number of replacement tokens
1865 //we found in prepare().
1866 if(count($data) != $pTokens['tokenCount']){
1867 //error the data count did not match the token count
1873 $tokens = $pTokens['tokens'];
1874 foreach ($tokens as $val) {
1877 $query .= $this->quote($data[$dataIndex++]);
1880 $filename = $data[$dataIndex++];
1881 $query .= sugar_file_get_contents($filename);
1884 $query .= $data[$dataIndex++];
1891 return $this->query($query);
1898 * Run both prepare and execute without the client having to run both individually.
1900 * @param string $sql The sql to parse
1901 * @param array $data The array of data to replace the tokens with.
1902 * @return resource result set or false on error
1904 public function pQuery($sql, $data = array()){
1905 $stmt = $this->prepareQuery($sql);
1906 return $this->executePreparedQuery($stmt, $data);
1910 * Use when you need to convert a database string to a different value; this function does it in a
1911 * database-backend aware way
1913 * @param string $string database string to convert
1914 * @param string $type type of conversion to do
1915 * @param array $additional_parameters optional, additional parameters to pass to the db function
1916 * @param array $additional_parameters_oracle_only optional, additional parameters to pass to the db function on oracle only
1919 public function convert(
1922 array $additional_parameters = array(),
1923 array $additional_parameters_oracle_only = array()
1930 * Returns the database string needed for concatinating multiple database strings together
1932 * @param string $table table name of the database fields to concat
1933 * @param array $fields fields in the table to concat together
1936 abstract public function concat(
1942 * Undoes database conversion
1944 * @param string $string database string to convert
1945 * @param string $type type of conversion to do
1948 public function fromConvert(