Complex classes like DBManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DBManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 98 | abstract class DBManager |
||
| 99 | { |
||
| 100 | /** |
||
| 101 | * Name of database |
||
| 102 | * @var resource |
||
| 103 | */ |
||
| 104 | public $database = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Indicates whether we should die when we get an error from the DB |
||
| 108 | */ |
||
| 109 | protected $dieOnError = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Indicates whether we should html encode the results from a query by default |
||
| 113 | */ |
||
| 114 | protected $encode = true; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Records the execution time of the last query |
||
| 118 | */ |
||
| 119 | protected $query_time = 0; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Last error message from the DB backend |
||
| 123 | */ |
||
| 124 | protected $last_error = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Registry of available result sets |
||
| 128 | */ |
||
| 129 | protected $lastResult; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Current query count |
||
| 133 | */ |
||
| 134 | private static $queryCount = 0; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Query threshold limit |
||
| 138 | */ |
||
| 139 | private static $queryLimit = 0; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Array of prepared statements and their correspoding parsed tokens |
||
| 143 | */ |
||
| 144 | protected $preparedTokens = array(); |
||
| 145 | |||
| 146 | /** |
||
| 147 | * TimeDate instance |
||
| 148 | * @var TimeDate |
||
| 149 | */ |
||
| 150 | protected $timedate; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * PHP Logger |
||
| 154 | * @var Logger |
||
| 155 | */ |
||
| 156 | protected $log; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Table descriptions |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | protected static $table_descriptions = array(); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Index descriptions |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected static $index_descriptions = array(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Maximum length of identifiers |
||
| 172 | * @abstract |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | protected $maxNameLengths = array( |
||
| 176 | 'table' => 64, |
||
| 177 | 'column' => 64, |
||
| 178 | 'index' => 64, |
||
| 179 | 'alias' => 64 |
||
| 180 | ); |
||
| 181 | |||
| 182 | /** |
||
| 183 | * DB driver priority |
||
| 184 | * Higher priority drivers override lower priority ones |
||
| 185 | * @var int |
||
| 186 | */ |
||
| 187 | public $priority = 0; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Driver name label, for install |
||
| 191 | * @absrtact |
||
| 192 | * @var string |
||
| 193 | */ |
||
| 194 | public $label = ''; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Type names map |
||
| 198 | * @abstract |
||
| 199 | * @var array |
||
| 200 | */ |
||
| 201 | protected $type_map = array(); |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Type classification into: |
||
| 205 | * - int |
||
| 206 | * - bool |
||
| 207 | * - float |
||
| 208 | * - date |
||
| 209 | * @abstract |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | protected $type_class = array( |
||
| 213 | 'int' => 'int', |
||
| 214 | 'double' => 'float', |
||
| 215 | 'float' => 'float', |
||
| 216 | 'uint' => 'int', |
||
| 217 | 'ulong' => 'bigint', |
||
| 218 | 'long' => 'bigint', |
||
| 219 | 'short' => 'int', |
||
| 220 | 'date' => 'date', |
||
| 221 | 'datetime' => 'date', |
||
| 222 | 'datetimecombo' => 'date', |
||
| 223 | 'time' => 'time', |
||
| 224 | 'bool' => 'bool', |
||
| 225 | 'tinyint' => 'int', |
||
| 226 | 'currency' => 'float', |
||
| 227 | 'decimal' => 'float', |
||
| 228 | 'decimal2' => 'float', |
||
| 229 | ); |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Capabilities this DB supports. Supported list: |
||
| 233 | * affected_rows Can report query affected rows for UPDATE/DELETE |
||
| 234 | * implement getAffectedRowCount() |
||
| 235 | * select_rows Can report row count for SELECT |
||
| 236 | * implement getRowCount() |
||
| 237 | * case_sensitive Supports case-sensitive text columns |
||
| 238 | * fulltext Supports fulltext search indexes |
||
| 239 | * inline_keys Supports defining keys together with the table |
||
| 240 | * auto_increment_sequence Autoincrement support implemented as sequence |
||
| 241 | * limit_subquery Supports LIMIT clauses in subqueries |
||
| 242 | * create_user Can create users for Sugar |
||
| 243 | * create_db Can create databases |
||
| 244 | * collation Supports setting collations |
||
| 245 | * disable_keys Supports temporarily disabling keys (for upgrades, etc.) |
||
| 246 | * |
||
| 247 | * @abstract |
||
| 248 | * Special cases: |
||
| 249 | * fix:expandDatabase - needs expandDatabase fix, see expandDatabase.php |
||
| 250 | * TODO: verify if we need these cases |
||
| 251 | */ |
||
| 252 | protected $capabilities = array(); |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Database options |
||
| 256 | * @var array |
||
| 257 | */ |
||
| 258 | protected $options = array(); |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Create DB Driver |
||
| 262 | */ |
||
| 263 | public function __construct() |
||
| 264 | { |
||
| 265 | $this->timedate = TimeDate::getInstance(); |
||
| 266 | $this->log = isset($GLOBALS['log']) ? $GLOBALS['log'] : null; |
||
| 267 | $this->helper = $this; // compatibility |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Wrapper for those trying to access the private and protected class members directly |
||
| 272 | * @param string $p var name |
||
| 273 | * @return mixed |
||
| 274 | */ |
||
| 275 | public function __get($p) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the current database handle |
||
| 283 | * @return resource |
||
| 284 | */ |
||
| 285 | public function getDatabase() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns this instance's DBHelper |
||
| 293 | * Actually now returns $this |
||
| 294 | * @deprecated |
||
| 295 | * @return DBManager |
||
| 296 | */ |
||
| 297 | public function getHelper() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Checks for error happening in the database |
||
| 304 | * |
||
| 305 | * @param string $msg message to prepend to the error message |
||
| 306 | * @param bool $dieOnError true if we want to die immediately on error |
||
| 307 | * @return bool True if there was an error |
||
| 308 | */ |
||
| 309 | public function checkError($msg = '', $dieOnError = false) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Register database error |
||
| 327 | * If die-on-error flag is set, logs the message and dies, |
||
| 328 | * otherwise sets last_error to the message |
||
| 329 | * @param string $userMessage Message from function user |
||
| 330 | * @param string $message Message from SQL driver |
||
| 331 | * @param bool $dieOnError |
||
| 332 | */ |
||
| 333 | protected function registerError($userMessage, $message, $dieOnError = false) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Return DB error message for the last query executed |
||
| 357 | * @return string Last error message |
||
| 358 | */ |
||
| 359 | public function lastError() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * This method is called by every method that runs a query. |
||
| 366 | * If slow query dumping is turned on and the query time is beyond |
||
| 367 | * the time limit, we will log the query. This function may do |
||
| 368 | * additional reporting or log in a different area in the future. |
||
| 369 | * |
||
| 370 | * @param string $query query to log |
||
| 371 | * @return boolean true if the query was logged, false otherwise |
||
| 372 | */ |
||
| 373 | protected function dump_slow_queries($query) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Scans order by to ensure that any field being ordered by is. |
||
| 394 | * |
||
| 395 | * It will throw a warning error to the log file - fatal if slow query logging is enabled |
||
| 396 | * |
||
| 397 | * @param string $sql query to be run |
||
| 398 | * @param bool $object_name optional, object to look up indices in |
||
| 399 | * @return bool true if an index is found false otherwise |
||
| 400 | */ |
||
| 401 | protected function checkQuery($sql, $object_name = false) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the time the last query took to execute |
||
| 456 | * |
||
| 457 | * @return int |
||
| 458 | */ |
||
| 459 | public function getQueryTime() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Checks the current connection; if it is not connected then reconnect |
||
| 466 | */ |
||
| 467 | public function checkConnection() |
||
| 468 | { |
||
| 469 | $this->last_error = ''; |
||
|
|
|||
| 470 | if (!isset($this->database)) |
||
| 471 | $this->connect(); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Sets the dieOnError value |
||
| 476 | * |
||
| 477 | * @param bool $value |
||
| 478 | */ |
||
| 479 | public function setDieOnError($value) |
||
| 480 | { |
||
| 481 | $this->dieOnError = $value; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Implements a generic insert for any bean. |
||
| 486 | * |
||
| 487 | * @param SugarBean $bean SugarBean instance |
||
| 488 | * @return bool query result |
||
| 489 | * |
||
| 490 | */ |
||
| 491 | public function insert(SugarBean $bean) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Insert data into table by parameter definition |
||
| 501 | * @param string $table Table name |
||
| 502 | * @param array $field_defs Definitions in vardef-like format |
||
| 503 | * @param array $data Key/value to insert |
||
| 504 | * @param array $field_map Fields map from SugarBean |
||
| 505 | * @param bool $execute Execute or return query? |
||
| 506 | * @return bool query result |
||
| 507 | */ |
||
| 508 | public function insertParams($table, $field_defs, $data, $field_map = null, $execute = true) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Implements a generic update for any bean |
||
| 555 | * |
||
| 556 | * @param SugarBean $bean Sugarbean instance |
||
| 557 | * @param array $where values with the keys as names of fields. |
||
| 558 | * If we want to pass multiple values for a name, pass it as an array |
||
| 559 | * If where is not passed, it defaults to id of table |
||
| 560 | * @return bool query result |
||
| 561 | * |
||
| 562 | */ |
||
| 563 | public function update(SugarBean $bean, array $where = array()) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Implements a generic delete for any bean identified by id |
||
| 573 | * |
||
| 574 | * @param SugarBean $bean Sugarbean instance |
||
| 575 | * @param array $where values with the keys as names of fields. |
||
| 576 | * If we want to pass multiple values for a name, pass it as an array |
||
| 577 | * If where is not passed, it defaults to id of table |
||
| 578 | * @return bool query result |
||
| 579 | */ |
||
| 580 | public function delete(SugarBean $bean, array $where = array()) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Implements a generic retrieve for any bean identified by id |
||
| 590 | * |
||
| 591 | * If we want to pass multiple values for a name, pass it as an array |
||
| 592 | * If where is not passed, it defaults to id of table |
||
| 593 | * |
||
| 594 | * @param SugarBean $bean Sugarbean instance |
||
| 595 | * @param array $where values with the keys as names of fields. |
||
| 596 | * @return resource result from the query |
||
| 597 | */ |
||
| 598 | public function retrieve(SugarBean $bean, array $where = array()) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Implements a generic retrieve for a collection of beans. |
||
| 608 | * |
||
| 609 | * These beans will be joined in the sql by the key attribute of field defs. |
||
| 610 | * Currently, this function does support outer joins. |
||
| 611 | * |
||
| 612 | * @param array $beans Sugarbean instance(s) |
||
| 613 | * @param array $cols columns to be returned with the keys as names of bean as identified by |
||
| 614 | * get_class of bean. Values of this array is the array of fieldDefs to be returned for a bean. |
||
| 615 | * If an empty array is passed, all columns are selected. |
||
| 616 | * @param array $where values with the keys as names of bean as identified by get_class of bean |
||
| 617 | * Each value at the first level is an array of values for that bean identified by name of fields. |
||
| 618 | * If we want to pass multiple values for a name, pass it as an array |
||
| 619 | * If where is not passed, all the rows will be returned. |
||
| 620 | * @return resource |
||
| 621 | */ |
||
| 622 | public function retrieveView(array $beans, array $cols = array(), array $where = array()) |
||
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * Implements creation of a db table for a bean. |
||
| 632 | * |
||
| 633 | * NOTE: does not handle out-of-table constraints, use createConstraintSQL for that |
||
| 634 | * @param SugarBean $bean Sugarbean instance |
||
| 635 | */ |
||
| 636 | public function createTable(SugarBean $bean) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * returns SQL to create constraints or indices |
||
| 653 | * |
||
| 654 | * @param SugarBean $bean SugarBean instance |
||
| 655 | * @return array list of SQL statements |
||
| 656 | */ |
||
| 657 | protected function createConstraintSql(SugarBean $bean) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Implements creation of a db table |
||
| 664 | * |
||
| 665 | * @param string $tablename |
||
| 666 | * @param array $fieldDefs Field definitions, in vardef format |
||
| 667 | * @param array $indices Index definitions, in vardef format |
||
| 668 | * @param string $engine Engine parameter, used for MySQL engine so far |
||
| 669 | * @todo: refactor engine param to be more generic |
||
| 670 | * @return bool success value |
||
| 671 | */ |
||
| 672 | public function createTableParams($tablename, $fieldDefs, $indices, $engine = null) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Implements repair of a db table for a bean. |
||
| 695 | * |
||
| 696 | * @param SugarBean $bean SugarBean instance |
||
| 697 | * @param bool $execute true if we want the action to take place, false if we just want the sql returned |
||
| 698 | * @return string SQL statement or empty string, depending upon $execute |
||
| 699 | */ |
||
| 700 | public function repairTable(SugarBean $bean, $execute = true) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Can this field be null? |
||
| 726 | * Auto-increment and ID fields can not be null |
||
| 727 | * @param array $vardef |
||
| 728 | * @return bool |
||
| 729 | */ |
||
| 730 | protected function isNullable($vardef) |
||
| 746 | |||
| 747 | |||
| 748 | /** |
||
| 749 | * Builds the SQL commands that repair a table structure |
||
| 750 | * |
||
| 751 | * @param string $tablename |
||
| 752 | * @param array $fielddefs Field definitions, in vardef format |
||
| 753 | * @param array $indices Index definitions, in vardef format |
||
| 754 | * @param bool $execute optional, true if we want the queries executed instead of returned |
||
| 755 | * @param string $engine optional, MySQL engine |
||
| 756 | * @todo: refactor engine param to be more generic |
||
| 757 | * @return string |
||
| 758 | */ |
||
| 759 | public function repairTableParams($tablename, $fielddefs, $indices, $execute = true, $engine = null) |
||
| 962 | |||
| 963 | /** |
||
| 964 | * Compares two vardefs |
||
| 965 | * |
||
| 966 | * @param array $fielddef1 This is from the database |
||
| 967 | * @param array $fielddef2 This is from the vardef |
||
| 968 | * @param bool $ignoreName Ignore name-only differences? |
||
| 969 | * @return bool true if they match, false if they don't |
||
| 970 | */ |
||
| 971 | public function compareVarDefs($fielddef1, $fielddef2, $ignoreName = false) |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Compare a field in two tables |
||
| 1008 | * @deprecated |
||
| 1009 | * @param string $name field name |
||
| 1010 | * @param string $table1 |
||
| 1011 | * @param string $table2 |
||
| 1012 | * @return array array with keys 'msg','table1','table2' |
||
| 1013 | */ |
||
| 1014 | public function compareFieldInTables($name, $table1, $table2) |
||
| 1051 | // |
||
| 1052 | // /** |
||
| 1053 | // * Compare an index in two different tables |
||
| 1054 | // * @deprecated |
||
| 1055 | // * @param string $name index name |
||
| 1056 | // * @param string $table1 |
||
| 1057 | // * @param string $table2 |
||
| 1058 | // * @return array array with keys 'msg','table1','table2' |
||
| 1059 | // */ |
||
| 1060 | // public function compareIndexInTables($name, $table1, $table2) |
||
| 1061 | // { |
||
| 1062 | // $row1 = $this->describeIndex($name, $table1); |
||
| 1063 | // $row2 = $this->describeIndex($name, $table2); |
||
| 1064 | // $returnArray = array( |
||
| 1065 | // 'table1' => $row1, |
||
| 1066 | // 'table2' => $row2, |
||
| 1067 | // 'msg' => 'error', |
||
| 1068 | // ); |
||
| 1069 | // $ignore_filter = array('Table'=>1, 'Seq_in_index'=>1,'Cardinality'=>1, 'Sub_part'=>1, 'Packed'=>1, 'Comment'=>1); |
||
| 1070 | // |
||
| 1071 | // if ($row1) { |
||
| 1072 | // if (!$row2) { |
||
| 1073 | // //Exists on table1 but not table2 |
||
| 1074 | // $returnArray['msg'] = 'not_exists_table2'; |
||
| 1075 | // } |
||
| 1076 | // else { |
||
| 1077 | // if (sizeof($row1) != sizeof($row2)) { |
||
| 1078 | // $returnArray['msg'] = 'no_match'; |
||
| 1079 | // } |
||
| 1080 | // else { |
||
| 1081 | // $returnArray['msg'] = 'match'; |
||
| 1082 | // foreach ($row1 as $fname => $fvalue) { |
||
| 1083 | // if (!isset($row2[$fname])) { |
||
| 1084 | // $returnArray['msg'] = 'no_match'; |
||
| 1085 | // } |
||
| 1086 | // if(!isset($ignore_filter[$fname]) && $row1[$fname] != $row2[$fname]){ |
||
| 1087 | // $returnArray['msg'] = 'no_match'; |
||
| 1088 | // } |
||
| 1089 | // } |
||
| 1090 | // } |
||
| 1091 | // } |
||
| 1092 | // } else { |
||
| 1093 | // $returnArray['msg'] = 'not_exists_table1'; |
||
| 1094 | // } |
||
| 1095 | // |
||
| 1096 | // return $returnArray; |
||
| 1097 | // } |
||
| 1098 | |||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Creates an index identified by name on the given fields. |
||
| 1102 | * |
||
| 1103 | * @param SugarBean $bean SugarBean instance |
||
| 1104 | * @param array $fieldDefs Field definitions, in vardef format |
||
| 1105 | * @param string $name index name |
||
| 1106 | * @param bool $unique optional, true if we want to create an unique index |
||
| 1107 | * @return bool query result |
||
| 1108 | */ |
||
| 1109 | public function createIndex(SugarBean $bean, $fieldDefs, $name, $unique = true) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * returns a SQL query that creates the indices as defined in metadata |
||
| 1119 | * @param array $indices Assoc array with index definitions from vardefs |
||
| 1120 | * @param string $table Focus table |
||
| 1121 | * @return array Array of SQL queries to generate indices |
||
| 1122 | */ |
||
| 1123 | public function getConstraintSql($indices, $table) |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Adds a new indexes |
||
| 1148 | * |
||
| 1149 | * @param string $tablename |
||
| 1150 | * @param array $indexes indexes to add |
||
| 1151 | * @param bool $execute true if we want to execute the returned sql statement |
||
| 1152 | * @return string SQL statement |
||
| 1153 | */ |
||
| 1154 | public function addIndexes($tablename, $indexes, $execute = true) |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Drops indexes |
||
| 1172 | * |
||
| 1173 | * @param string $tablename |
||
| 1174 | * @param array $indexes indexes to drop |
||
| 1175 | * @param bool $execute true if we want to execute the returned sql statement |
||
| 1176 | * @return string SQL statement |
||
| 1177 | */ |
||
| 1178 | public function dropIndexes($tablename, $indexes, $execute = true) |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Modifies indexes |
||
| 1200 | * |
||
| 1201 | * @param string $tablename |
||
| 1202 | * @param array $indexes indexes to modify |
||
| 1203 | * @param bool $execute true if we want to execute the returned sql statement |
||
| 1204 | * @return string SQL statement |
||
| 1205 | */ |
||
| 1206 | public function modifyIndexes($tablename, $indexes, $execute = true) |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Adds a column to table identified by field def. |
||
| 1214 | * |
||
| 1215 | * @param string $tablename |
||
| 1216 | * @param array $fieldDefs |
||
| 1217 | * @return bool query result |
||
| 1218 | */ |
||
| 1219 | public function addColumn($tablename, $fieldDefs) |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Alters old column identified by oldFieldDef to new fieldDef. |
||
| 1237 | * |
||
| 1238 | * @param string $tablename |
||
| 1239 | * @param array $newFieldDef |
||
| 1240 | * @param bool $ignoreRequired optional, true if we are ignoring this being a required field |
||
| 1241 | * @return bool query result |
||
| 1242 | */ |
||
| 1243 | public function alterColumn($tablename, $newFieldDef, $ignoreRequired = false) |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Drops the table associated with a bean |
||
| 1267 | * |
||
| 1268 | * @param SugarBean $bean SugarBean instance |
||
| 1269 | * @return bool query result |
||
| 1270 | */ |
||
| 1271 | public function dropTable(SugarBean $bean) |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Drops the table by name |
||
| 1278 | * |
||
| 1279 | * @param string $name Table name |
||
| 1280 | * @return bool query result |
||
| 1281 | */ |
||
| 1282 | public function dropTableName($name) |
||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Deletes a column identified by fieldDef. |
||
| 1290 | * |
||
| 1291 | * @param SugarBean $bean SugarBean containing the field |
||
| 1292 | * @param array $fieldDefs Vardef definition of the field |
||
| 1293 | * @return bool query result |
||
| 1294 | */ |
||
| 1295 | public function deleteColumn(SugarBean $bean, $fieldDefs) |
||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Generate a set of Insert statements based on the bean given |
||
| 1305 | * |
||
| 1306 | * @deprecated |
||
| 1307 | * |
||
| 1308 | * @param SugarBean $bean the bean from which table we will generate insert stmts |
||
| 1309 | * @param string $select_query the query which will give us the set of objects we want to place into our insert statement |
||
| 1310 | * @param int $start the first row to query |
||
| 1311 | * @param int $count the number of rows to query |
||
| 1312 | * @param string $table the table to query from |
||
| 1313 | * @param bool $is_related_query |
||
| 1314 | * @return string SQL insert statement |
||
| 1315 | */ |
||
| 1316 | public function generateInsertSQL(SugarBean $bean, $select_query, $start, $count = -1, $table, $is_related_query = false) |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * @deprecated |
||
| 1464 | * Disconnects all instances |
||
| 1465 | */ |
||
| 1466 | public function disconnectAll() |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * This function sets the query threshold limit |
||
| 1473 | * |
||
| 1474 | * @param int $limit value of query threshold limit |
||
| 1475 | */ |
||
| 1476 | public static function setQueryLimit($limit) |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * Returns the static queryCount value |
||
| 1485 | * |
||
| 1486 | * @return int value of the queryCount static variable |
||
| 1487 | */ |
||
| 1488 | public static function getQueryCount() |
||
| 1492 | |||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Resets the queryCount value to 0 |
||
| 1496 | * |
||
| 1497 | */ |
||
| 1498 | public static function resetQueryCount() |
||
| 1502 | |||
| 1503 | /** |
||
| 1504 | * This function increments the global $sql_queries variable |
||
| 1505 | */ |
||
| 1506 | public function countQuery() |
||
| 1515 | |||
| 1516 | /** |
||
| 1517 | * Pre-process string for quoting |
||
| 1518 | * @internal |
||
| 1519 | * @param string $string |
||
| 1520 | * @return string |
||
| 1521 | */ |
||
| 1522 | protected function quoteInternal($string) |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * Return string properly quoted with '' |
||
| 1529 | * @param string $string |
||
| 1530 | * @return string |
||
| 1531 | */ |
||
| 1532 | public function quoted($string) |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * Quote value according to type |
||
| 1539 | * Numerics aren't quoted |
||
| 1540 | * Dates are converted and quoted |
||
| 1541 | * Rest is just quoted |
||
| 1542 | * @param string $type |
||
| 1543 | * @param string $value |
||
| 1544 | * @return string Quoted value |
||
| 1545 | */ |
||
| 1546 | public function quoteType($type, $value) |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Quote the strings of the passed in array |
||
| 1566 | * |
||
| 1567 | * The array must only contain strings |
||
| 1568 | * |
||
| 1569 | * @param array $array |
||
| 1570 | * @return array Quoted strings |
||
| 1571 | */ |
||
| 1572 | public function arrayQuote(array &$array) |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Frees out previous results |
||
| 1582 | * |
||
| 1583 | * @param resource|bool $result optional, pass if you want to free a single result instead of all results |
||
| 1584 | */ |
||
| 1585 | protected function freeResult($result = false) |
||
| 1595 | |||
| 1596 | /** |
||
| 1597 | * @abstract |
||
| 1598 | * Check if query has LIMIT clause |
||
| 1599 | * Relevant for now only for Mysql |
||
| 1600 | * @param string $sql |
||
| 1601 | * @return bool |
||
| 1602 | */ |
||
| 1603 | protected function hasLimit($sql) |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Runs a query and returns a single row containing single value |
||
| 1610 | * |
||
| 1611 | * @param string $sql SQL Statement to execute |
||
| 1612 | * @param bool $dieOnError True if we want to call die if the query returns errors |
||
| 1613 | * @param string $msg Message to log if error occurs |
||
| 1614 | * @return array single value from the query |
||
| 1615 | */ |
||
| 1616 | public function getOne($sql, $dieOnError = false, $msg = '') |
||
| 1634 | |||
| 1635 | /** |
||
| 1636 | * Runs a query and returns a single row |
||
| 1637 | * |
||
| 1638 | * @param string $sql SQL Statement to execute |
||
| 1639 | * @param bool $dieOnError True if we want to call die if the query returns errors |
||
| 1640 | * @param string $msg Message to log if error occurs |
||
| 1641 | * @param bool $suppress Message to log if error occurs |
||
| 1642 | * @return array single row from the query |
||
| 1643 | */ |
||
| 1644 | public function fetchOne($sql, $dieOnError = false, $msg = '', $suppress = false) |
||
| 1659 | |||
| 1660 | /** |
||
| 1661 | * Returns the number of rows affected by the last query |
||
| 1662 | * @abstract |
||
| 1663 | * See also affected_rows capability, will return 0 unless the DB supports it |
||
| 1664 | * @param resource $result query result resource |
||
| 1665 | * @return int |
||
| 1666 | */ |
||
| 1667 | public function getAffectedRowCount($result) |
||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * Returns the number of rows returned by the result |
||
| 1674 | * |
||
| 1675 | * This function can't be reliably implemented on most DB, do not use it. |
||
| 1676 | * @abstract |
||
| 1677 | * @deprecated |
||
| 1678 | * @param resource $result |
||
| 1679 | * @return int |
||
| 1680 | */ |
||
| 1681 | public function getRowCount($result) |
||
| 1685 | |||
| 1686 | /** |
||
| 1687 | * Get table description |
||
| 1688 | * @param string $tablename |
||
| 1689 | * @param bool $reload true means load from DB, false allows using cache |
||
| 1690 | * @return array Vardef-format table description |
||
| 1691 | * |
||
| 1692 | */ |
||
| 1693 | public function getTableDescription($tablename, $reload = false) |
||
| 1700 | |||
| 1701 | /** |
||
| 1702 | * Returns the field description for a given field in table |
||
| 1703 | * |
||
| 1704 | * @param string $name |
||
| 1705 | * @param string $tablename |
||
| 1706 | * @return array |
||
| 1707 | */ |
||
| 1708 | protected function describeField($name, $tablename) |
||
| 1721 | |||
| 1722 | /** |
||
| 1723 | * Returns the index description for a given index in table |
||
| 1724 | * |
||
| 1725 | * @param string $name |
||
| 1726 | * @param string $tablename |
||
| 1727 | * @return array |
||
| 1728 | */ |
||
| 1729 | protected function describeIndex($name, $tablename) |
||
| 1743 | |||
| 1744 | /** |
||
| 1745 | * Truncates a string to a given length |
||
| 1746 | * |
||
| 1747 | * @param string $string |
||
| 1748 | * @param int $len length to trim to |
||
| 1749 | * @return string |
||
| 1750 | * |
||
| 1751 | */ |
||
| 1752 | public function truncate($string, $len) |
||
| 1760 | |||
| 1761 | /** |
||
| 1762 | * Returns the database string needed for concatinating multiple database strings together |
||
| 1763 | * |
||
| 1764 | * @param string $table table name of the database fields to concat |
||
| 1765 | * @param array $fields fields in the table to concat together |
||
| 1766 | * @param string $space Separator between strings, default is single space |
||
| 1767 | * @return string |
||
| 1768 | */ |
||
| 1769 | public function concat($table, array $fields, $space = ' ') |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Given a sql stmt attempt to parse it into the sql and the tokens. Then return the index of this prepared statement |
||
| 1784 | * Tokens can come in the following forms: |
||
| 1785 | * ? - a scalar which will be quoted |
||
| 1786 | * ! - a literal which will not be quoted |
||
| 1787 | * & - binary data to read from a file |
||
| 1788 | * |
||
| 1789 | * @param string $sql The sql to parse |
||
| 1790 | * @return int index of the prepared statement to be used with execute |
||
| 1791 | */ |
||
| 1792 | public function prepareQuery($sql) |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Takes a prepared stmt index and the data to replace and creates the query and runs it. |
||
| 1825 | * |
||
| 1826 | * @param int $stmt The index of the prepared statement from preparedTokens |
||
| 1827 | * @param array $data The array of data to replace the tokens with. |
||
| 1828 | * @return resource result set or false on error |
||
| 1829 | */ |
||
| 1830 | public function executePreparedQuery($stmt, $data = array()) |
||
| 1871 | |||
| 1872 | /** |
||
| 1873 | * Run both prepare and execute without the client having to run both individually. |
||
| 1874 | * |
||
| 1875 | * @param string $sql The sql to parse |
||
| 1876 | * @param array $data The array of data to replace the tokens with. |
||
| 1877 | * @return resource result set or false on error |
||
| 1878 | */ |
||
| 1879 | public function pQuery($sql, $data = array()) |
||
| 1884 | |||
| 1885 | /********************** SQL FUNCTIONS ****************************/ |
||
| 1886 | /** |
||
| 1887 | * Generates sql for create table statement for a bean. |
||
| 1888 | * |
||
| 1889 | * NOTE: does not handle out-of-table constraints, use createConstraintSQL for that |
||
| 1890 | * @param SugarBean $bean SugarBean instance |
||
| 1891 | * @return string SQL Create Table statement |
||
| 1892 | */ |
||
| 1893 | public function createTableSQL(SugarBean $bean) |
||
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Generates SQL for insert statement. |
||
| 1903 | * |
||
| 1904 | * @param SugarBean $bean SugarBean instance |
||
| 1905 | * @return string SQL Create Table statement |
||
| 1906 | */ |
||
| 1907 | public function insertSQL(SugarBean $bean) |
||
| 1914 | |||
| 1915 | /** |
||
| 1916 | * Generates SQL for update statement. |
||
| 1917 | * |
||
| 1918 | * @param SugarBean $bean SugarBean instance |
||
| 1919 | * @param array $where Optional, where conditions in an array |
||
| 1920 | * @return string SQL Create Table statement |
||
| 1921 | */ |
||
| 1922 | public function updateSQL(SugarBean $bean, array $where = array()) |
||
| 1988 | |||
| 1989 | /** |
||
| 1990 | * This method returns a where array so that it has id entry if |
||
| 1991 | * where is not an array or is empty |
||
| 1992 | * |
||
| 1993 | * @param SugarBean $bean SugarBean instance |
||
| 1994 | * @param array $where Optional, where conditions in an array |
||
| 1995 | * @return array |
||
| 1996 | */ |
||
| 1997 | protected function updateWhereArray(SugarBean $bean, array $where = array()) |
||
| 2011 | |||
| 2012 | /** |
||
| 2013 | * Returns a where clause without the 'where' key word |
||
| 2014 | * |
||
| 2015 | * The clause returned does not have an 'and' at the beginning and the columns |
||
| 2016 | * are joined by 'and'. |
||
| 2017 | * |
||
| 2018 | * @param string $table table name |
||
| 2019 | * @param array $whereArray Optional, where conditions in an array |
||
| 2020 | * @return string |
||
| 2021 | */ |
||
| 2022 | protected function getColumnWhereClause($table, array $whereArray = array()) |
||
| 2047 | |||
| 2048 | /** |
||
| 2049 | * This method returns a complete where clause built from the |
||
| 2050 | * where values specified. |
||
| 2051 | * |
||
| 2052 | * @param SugarBean $bean SugarBean that describes the table |
||
| 2053 | * @param array $whereArray Optional, where conditions in an array |
||
| 2054 | * @return string |
||
| 2055 | */ |
||
| 2056 | protected function getWhereClause(SugarBean $bean, array $whereArray=array()) |
||
| 2060 | |||
| 2061 | /** |
||
| 2062 | * Outputs a correct string for the sql statement according to value |
||
| 2063 | * |
||
| 2064 | * @param mixed $val |
||
| 2065 | * @param array $fieldDef field definition |
||
| 2066 | * @return mixed |
||
| 2067 | */ |
||
| 2068 | public function massageValue($val, $fieldDef) |
||
| 2136 | |||
| 2137 | /** |
||
| 2138 | * Massages the field defintions to fill in anything else the DB backend may add |
||
| 2139 | * |
||
| 2140 | * @param array $fieldDef |
||
| 2141 | * @param string $tablename |
||
| 2142 | * @return array |
||
| 2143 | */ |
||
| 2144 | public function massageFieldDef(&$fieldDef, $tablename) |
||
| 2166 | |||
| 2167 | /** |
||
| 2168 | * Take an SQL statement and produce a list of fields used in that select |
||
| 2169 | * @param string $selectStatement |
||
| 2170 | * @return array |
||
| 2171 | */ |
||
| 2172 | public function getSelectFieldsFromQuery($selectStatement) |
||
| 2210 | |||
| 2211 | /** |
||
| 2212 | * returns the field name used in a select |
||
| 2213 | * @param string $string SELECT query |
||
| 2214 | * @return string |
||
| 2215 | */ |
||
| 2216 | protected function getFieldNameFromSelect($string) |
||
| 2234 | |||
| 2235 | /** |
||
| 2236 | * Generates SQL for delete statement identified by id. |
||
| 2237 | * |
||
| 2238 | * @param SugarBean $bean SugarBean instance |
||
| 2239 | * @param array $where where conditions in an array |
||
| 2240 | * @return string SQL Update Statement |
||
| 2241 | */ |
||
| 2242 | public function deleteSQL(SugarBean $bean, array $where) |
||
| 2247 | |||
| 2248 | /** |
||
| 2249 | * Generates SQL for select statement for any bean identified by id. |
||
| 2250 | * |
||
| 2251 | * @param SugarBean $bean SugarBean instance |
||
| 2252 | * @param array $where where conditions in an array |
||
| 2253 | * @return string SQL Select Statement |
||
| 2254 | */ |
||
| 2255 | public function retrieveSQL(SugarBean $bean, array $where) |
||
| 2260 | |||
| 2261 | /** |
||
| 2262 | * This method implements a generic sql for a collection of beans. |
||
| 2263 | * |
||
| 2264 | * Currently, this function does not support outer joins. |
||
| 2265 | * |
||
| 2266 | * @param array $beans Array of values returned by get_class method as the keys and a bean as |
||
| 2267 | * the value for that key. These beans will be joined in the sql by the key |
||
| 2268 | * attribute of field defs. |
||
| 2269 | * @param array $cols Optional, columns to be returned with the keys as names of bean |
||
| 2270 | * as identified by get_class of bean. Values of this array is the array of fieldDefs |
||
| 2271 | * to be returned for a bean. If an empty array is passed, all columns are selected. |
||
| 2272 | * @param array $whereClause Optional, values with the keys as names of bean as identified |
||
| 2273 | * by get_class of bean. Each value at the first level is an array of values for that |
||
| 2274 | * bean identified by name of fields. If we want to pass multiple values for a name, |
||
| 2275 | * pass it as an array. If where is not passed, all the rows will be returned. |
||
| 2276 | * |
||
| 2277 | * @return string SQL Select Statement |
||
| 2278 | */ |
||
| 2279 | public function retrieveViewSQL(array $beans, array $cols = array(), array $whereClause = array()) |
||
| 2349 | |||
| 2350 | /** |
||
| 2351 | * Generates SQL for create index statement for a bean. |
||
| 2352 | * |
||
| 2353 | * @param SugarBean $bean SugarBean instance |
||
| 2354 | * @param array $fields fields used in the index |
||
| 2355 | * @param string $name index name |
||
| 2356 | * @param bool $unique Optional, set to true if this is an unique index |
||
| 2357 | * @return string SQL Select Statement |
||
| 2358 | */ |
||
| 2359 | public function createIndexSQL(SugarBean $bean, array $fields, $name, $unique = true) |
||
| 2375 | |||
| 2376 | /** |
||
| 2377 | * Returns the type of the variable in the field |
||
| 2378 | * |
||
| 2379 | * @param array $fieldDef Vardef-format field def |
||
| 2380 | * @return string |
||
| 2381 | */ |
||
| 2382 | public function getFieldType($fieldDef) |
||
| 2402 | |||
| 2403 | /** |
||
| 2404 | * retrieves the different components from the passed column type as it is used in the type mapping and vardefs |
||
| 2405 | * type format: <baseType>[(<len>[,<scale>])] |
||
| 2406 | * @param string $type Column type |
||
| 2407 | * @return array|bool array containing the different components of the passed in type or false in case the type contains illegal characters |
||
| 2408 | */ |
||
| 2409 | public function getTypeParts($type) |
||
| 2429 | |||
| 2430 | /** |
||
| 2431 | * Returns the defintion for a single column |
||
| 2432 | * |
||
| 2433 | * @param array $fieldDef Vardef-format field def |
||
| 2434 | * @param bool $ignoreRequired Optional, true if we should ignore this being a required field |
||
| 2435 | * @param string $table Optional, table name |
||
| 2436 | * @param bool $return_as_array Optional, true if we should return the result as an array instead of sql |
||
| 2437 | * @return string or array if $return_as_array is true |
||
| 2438 | */ |
||
| 2439 | protected function oneColumnSQLRep($fieldDef, $ignoreRequired = false, $table = '', $return_as_array = false) |
||
| 2520 | |||
| 2521 | /** |
||
| 2522 | * Returns SQL defintions for all columns in a table |
||
| 2523 | * |
||
| 2524 | * @param array $fieldDefs Vardef-format field def |
||
| 2525 | * @param bool $ignoreRequired Optional, true if we should ignor this being a required field |
||
| 2526 | * @param string $tablename Optional, table name |
||
| 2527 | * @return string SQL column definitions |
||
| 2528 | */ |
||
| 2529 | protected function columnSQLRep($fieldDefs, $ignoreRequired = false, $tablename) |
||
| 2547 | |||
| 2548 | /** |
||
| 2549 | * Returns the next value for an auto increment |
||
| 2550 | * @abstract |
||
| 2551 | * @param string $table Table name |
||
| 2552 | * @param string $field_name Field name |
||
| 2553 | * @return string |
||
| 2554 | */ |
||
| 2555 | public function getAutoIncrement($table, $field_name) |
||
| 2559 | |||
| 2560 | /** |
||
| 2561 | * Returns the sql for the next value in a sequence |
||
| 2562 | * @abstract |
||
| 2563 | * @param string $table Table name |
||
| 2564 | * @param string $field_name Field name |
||
| 2565 | * @return string |
||
| 2566 | */ |
||
| 2567 | public function getAutoIncrementSQL($table, $field_name) |
||
| 2571 | |||
| 2572 | /** |
||
| 2573 | * Either creates an auto increment through queries or returns sql for auto increment |
||
| 2574 | * that can be appended to the end of column defination (mysql) |
||
| 2575 | * @abstract |
||
| 2576 | * @param string $table Table name |
||
| 2577 | * @param string $field_name Field name |
||
| 2578 | * @return string |
||
| 2579 | */ |
||
| 2580 | protected function setAutoIncrement($table, $field_name) |
||
| 2585 | |||
| 2586 | /** |
||
| 2587 | * Sets the next auto-increment value of a column to a specific value. |
||
| 2588 | * @abstract |
||
| 2589 | * @param string $table Table name |
||
| 2590 | * @param string $field_name Field name |
||
| 2591 | * @param int $start_value Starting autoincrement value |
||
| 2592 | * @return string |
||
| 2593 | * |
||
| 2594 | */ |
||
| 2595 | public function setAutoIncrementStart($table, $field_name, $start_value) |
||
| 2599 | |||
| 2600 | /** |
||
| 2601 | * Deletes an auto increment |
||
| 2602 | * @abstract |
||
| 2603 | * @param string $table tablename |
||
| 2604 | * @param string $field_name |
||
| 2605 | */ |
||
| 2606 | public function deleteAutoIncrement($table, $field_name) |
||
| 2610 | |||
| 2611 | /** |
||
| 2612 | * This method generates sql for adding a column to table identified by field def. |
||
| 2613 | * |
||
| 2614 | * @param string $tablename |
||
| 2615 | * @param array $fieldDefs |
||
| 2616 | * @return string SQL statement |
||
| 2617 | */ |
||
| 2618 | public function addColumnSQL($tablename, $fieldDefs) |
||
| 2622 | |||
| 2623 | /** |
||
| 2624 | * This method genrates sql for altering old column identified by oldFieldDef to new fieldDef. |
||
| 2625 | * |
||
| 2626 | * @param string $tablename |
||
| 2627 | * @param array $newFieldDefs |
||
| 2628 | * @param bool $ignorerequired Optional, true if we should ignor this being a required field |
||
| 2629 | * @return string|array SQL statement(s) |
||
| 2630 | */ |
||
| 2631 | public function alterColumnSQL($tablename, $newFieldDefs, $ignorerequired = false) |
||
| 2635 | |||
| 2636 | /** |
||
| 2637 | * Generates SQL for dropping a table. |
||
| 2638 | * |
||
| 2639 | * @param SugarBean $bean Sugarbean instance |
||
| 2640 | * @return string SQL statement |
||
| 2641 | */ |
||
| 2642 | public function dropTableSQL(SugarBean $bean) |
||
| 2646 | |||
| 2647 | /** |
||
| 2648 | * Generates SQL for dropping a table. |
||
| 2649 | * |
||
| 2650 | * @param string $name table name |
||
| 2651 | * @return string SQL statement |
||
| 2652 | */ |
||
| 2653 | public function dropTableNameSQL($name) |
||
| 2657 | |||
| 2658 | /** |
||
| 2659 | * Generates SQL for truncating a table. |
||
| 2660 | * @param string $name table name |
||
| 2661 | * @return string |
||
| 2662 | */ |
||
| 2663 | public function truncateTableSQL($name) |
||
| 2667 | |||
| 2668 | /** |
||
| 2669 | * This method generates sql that deletes a column identified by fieldDef. |
||
| 2670 | * |
||
| 2671 | * @param SugarBean $bean Sugarbean instance |
||
| 2672 | * @param array $fieldDefs |
||
| 2673 | * @return string SQL statement |
||
| 2674 | */ |
||
| 2675 | public function deleteColumnSQL(SugarBean $bean, $fieldDefs) |
||
| 2679 | |||
| 2680 | /** |
||
| 2681 | * This method generates sql that drops a column identified by fieldDef. |
||
| 2682 | * Designed to work like the other addColumnSQL() and alterColumnSQL() functions |
||
| 2683 | * |
||
| 2684 | * @param string $tablename |
||
| 2685 | * @param array $fieldDefs |
||
| 2686 | * @return string SQL statement |
||
| 2687 | */ |
||
| 2688 | public function dropColumnSQL($tablename, $fieldDefs) |
||
| 2692 | |||
| 2693 | /** |
||
| 2694 | * Return a version of $proposed that can be used as a column name in any of our supported databases |
||
| 2695 | * Practically this means no longer than 25 characters as the smallest identifier length for our supported DBs is 30 chars for Oracle plus we add on at least four characters in some places (for indicies for example) |
||
| 2696 | * @param string|array $name Proposed name for the column |
||
| 2697 | * @param bool|string $ensureUnique Ensure the name is unique |
||
| 2698 | * @param string $type Name type (table, column) |
||
| 2699 | * @param bool $force Force new name |
||
| 2700 | * @return string|array Valid column name trimmed to right length and with invalid characters removed |
||
| 2701 | */ |
||
| 2702 | public function getValidDBName($name, $ensureUnique = false, $type = 'column', $force = false) |
||
| 2740 | |||
| 2741 | /** |
||
| 2742 | * Returns the valid type for a column given the type in fieldDef |
||
| 2743 | * |
||
| 2744 | * @param string $type field type |
||
| 2745 | * @return string valid type for the given field |
||
| 2746 | */ |
||
| 2747 | public function getColumnType($type) |
||
| 2751 | |||
| 2752 | /** |
||
| 2753 | * Checks to see if passed array is truely an array of defitions |
||
| 2754 | * |
||
| 2755 | * Such an array may have type as a key but it will point to an array |
||
| 2756 | * for a true array of definitions an to a col type for a definition only |
||
| 2757 | * |
||
| 2758 | * @param mixed $defArray |
||
| 2759 | * @return bool |
||
| 2760 | */ |
||
| 2761 | public function isFieldArray($defArray) |
||
| 2774 | |||
| 2775 | /** |
||
| 2776 | * returns true if the type can be mapped to a valid column type |
||
| 2777 | * |
||
| 2778 | * @param string $type |
||
| 2779 | * @return bool |
||
| 2780 | */ |
||
| 2781 | protected function validColumnType($type) |
||
| 2786 | |||
| 2787 | /** |
||
| 2788 | * Generate query for audit table |
||
| 2789 | * @param SugarBean $bean SugarBean that was changed |
||
| 2790 | * @param array $changes List of changes, contains 'before' and 'after' |
||
| 2791 | * @return string Audit table INSERT query |
||
| 2792 | */ |
||
| 2793 | protected function auditSQL(SugarBean $bean, $changes) |
||
| 2820 | |||
| 2821 | /** |
||
| 2822 | * Saves changes to module's audit table |
||
| 2823 | * |
||
| 2824 | * @param SugarBean $bean Sugarbean instance that was changed |
||
| 2825 | * @param array $changes List of changes, contains 'before' and 'after' |
||
| 2826 | * @return bool query result |
||
| 2827 | * |
||
| 2828 | */ |
||
| 2829 | public function save_audit_records(SugarBean $bean, $changes) |
||
| 2833 | |||
| 2834 | /** |
||
| 2835 | * Finds fields whose value has changed. |
||
| 2836 | * The before and after values are stored in the bean. |
||
| 2837 | * Uses $bean->fetched_row && $bean->fetched_rel_row to compare |
||
| 2838 | * |
||
| 2839 | * @param SugarBean $bean Sugarbean instance that was changed |
||
| 2840 | * @param array|null $field_filter Array of filter names to be inspected (NULL means all fields) |
||
| 2841 | * @return array |
||
| 2842 | */ |
||
| 2843 | public function getDataChanges(SugarBean &$bean, array $field_filter = null) |
||
| 2925 | |||
| 2926 | /** |
||
| 2927 | * Uses the audit enabled fields array to find fields whose value has changed. |
||
| 2928 | * The before and after values are stored in the bean. |
||
| 2929 | * Uses $bean->fetched_row && $bean->fetched_rel_row to compare |
||
| 2930 | * |
||
| 2931 | * @param SugarBean $bean Sugarbean instance that was changed |
||
| 2932 | * @return array |
||
| 2933 | */ |
||
| 2934 | public function getAuditDataChanges(SugarBean $bean) |
||
| 2939 | |||
| 2940 | /** |
||
| 2941 | * Setup FT indexing |
||
| 2942 | * @abstract |
||
| 2943 | */ |
||
| 2944 | public function full_text_indexing_setup() |
||
| 2948 | |||
| 2949 | /** |
||
| 2950 | * Quotes a string for storing in the database |
||
| 2951 | * @deprecated |
||
| 2952 | * Return value will be not surrounded by quotes |
||
| 2953 | * |
||
| 2954 | * @param string $string |
||
| 2955 | * @return string |
||
| 2956 | */ |
||
| 2957 | public function escape_quote($string) |
||
| 2961 | |||
| 2962 | /** |
||
| 2963 | * Quotes a string for storing in the database |
||
| 2964 | * @deprecated |
||
| 2965 | * Return value will be not surrounded by quotes |
||
| 2966 | * |
||
| 2967 | * @param string $string |
||
| 2968 | * @return string |
||
| 2969 | */ |
||
| 2970 | public function quoteFormEmail($string) |
||
| 2974 | |||
| 2975 | /** |
||
| 2976 | * Renames an index using fields definition |
||
| 2977 | * |
||
| 2978 | * @param array $old_definition |
||
| 2979 | * @param array $new_definition |
||
| 2980 | * @param string $table_name |
||
| 2981 | * @return string SQL statement |
||
| 2982 | */ |
||
| 2983 | public function renameIndexDefs($old_definition, $new_definition, $table_name) |
||
| 2988 | |||
| 2989 | /** |
||
| 2990 | * Check if type is boolean |
||
| 2991 | * @param string $type |
||
| 2992 | * @return bool |
||
| 2993 | */ |
||
| 2994 | public function isBooleanType($type) |
||
| 2998 | |||
| 2999 | /** |
||
| 3000 | * Get truth value for boolean type |
||
| 3001 | * Allows 'off' to mean false, along with all 'empty' values |
||
| 3002 | * @param mixed $val |
||
| 3003 | * @return bool |
||
| 3004 | */ |
||
| 3005 | protected function _getBooleanValue($val) |
||
| 3013 | |||
| 3014 | /** |
||
| 3015 | * Check if type is a number |
||
| 3016 | * @param string $type |
||
| 3017 | * @return bool |
||
| 3018 | */ |
||
| 3019 | public function isNumericType($type) |
||
| 3026 | |||
| 3027 | /** |
||
| 3028 | * Check if the value is empty value for this type |
||
| 3029 | * @param mixed $val Value |
||
| 3030 | * @param string $type Type (one of vardef types) |
||
| 3031 | * @return bool true if the value if empty |
||
| 3032 | */ |
||
| 3033 | protected function _emptyValue($val, $type) |
||
| 3062 | |||
| 3063 | /** |
||
| 3064 | * @abstract |
||
| 3065 | * Does this type represent text (i.e., non-varchar) value? |
||
| 3066 | * @param string $type |
||
| 3067 | * @return bool |
||
| 3068 | */ |
||
| 3069 | public function isTextType($type) |
||
| 3073 | |||
| 3074 | /** |
||
| 3075 | * Check if this DB supports certain capability |
||
| 3076 | * See $this->capabilities for the list |
||
| 3077 | * @param string $cap |
||
| 3078 | * @return bool |
||
| 3079 | */ |
||
| 3080 | public function supports($cap) |
||
| 3084 | |||
| 3085 | /** |
||
| 3086 | * Create ORDER BY clause for ENUM type field |
||
| 3087 | * @param string $order_by Field name |
||
| 3088 | * @param array $values Possible enum value |
||
| 3089 | * @param string $order_dir Order direction, ASC or DESC |
||
| 3090 | * @return string |
||
| 3091 | */ |
||
| 3092 | public function orderByEnum($order_by, $values, $order_dir) |
||
| 3106 | |||
| 3107 | /** |
||
| 3108 | * Return representation of an empty value depending on type |
||
| 3109 | * The value is fully quoted, converted, etc. |
||
| 3110 | * @param string $type |
||
| 3111 | * @return mixed Empty value |
||
| 3112 | */ |
||
| 3113 | public function emptyValue($type) |
||
| 3121 | |||
| 3122 | /** |
||
| 3123 | * List of available collation settings |
||
| 3124 | * @abstract |
||
| 3125 | * @return string |
||
| 3126 | */ |
||
| 3127 | public function getDefaultCollation() |
||
| 3131 | |||
| 3132 | /** |
||
| 3133 | * List of available collation settings |
||
| 3134 | * @abstract |
||
| 3135 | * @return array |
||
| 3136 | */ |
||
| 3137 | public function getCollationList() |
||
| 3141 | |||
| 3142 | /** |
||
| 3143 | * Returns the number of columns in a table |
||
| 3144 | * |
||
| 3145 | * @param string $table_name |
||
| 3146 | * @return int |
||
| 3147 | */ |
||
| 3148 | public function number_of_columns($table_name) |
||
| 3153 | |||
| 3154 | /** |
||
| 3155 | * Return limit query based on given query |
||
| 3156 | * @param string $sql |
||
| 3157 | * @param int $start |
||
| 3158 | * @param int $count |
||
| 3159 | * @param bool $dieOnError |
||
| 3160 | * @param string $msg |
||
| 3161 | * @return resource|bool query result |
||
| 3162 | * @see DBManager::limitQuery() |
||
| 3163 | */ |
||
| 3164 | public function limitQuerySql($sql, $start, $count, $dieOnError=false, $msg='') |
||
| 3168 | |||
| 3169 | /** |
||
| 3170 | * Return current time in format fit for insertion into DB (with quotes) |
||
| 3171 | * @return string |
||
| 3172 | */ |
||
| 3173 | public function now() |
||
| 3177 | |||
| 3178 | /** |
||
| 3179 | * Check if connecting user has certain privilege |
||
| 3180 | * @param string $privilege |
||
| 3181 | * @return bool Privilege allowed? |
||
| 3182 | */ |
||
| 3183 | public function checkPrivilege($privilege) |
||
| 3227 | |||
| 3228 | /** |
||
| 3229 | * Check if the query is a select query |
||
| 3230 | * @param string $query |
||
| 3231 | * @return bool Is query SELECT? |
||
| 3232 | */ |
||
| 3233 | protected function isSelect($query) |
||
| 3245 | |||
| 3246 | /** |
||
| 3247 | * Parse fulltext search query with mysql syntax: |
||
| 3248 | * terms quoted by "" |
||
| 3249 | * + means the term must be included |
||
| 3250 | * - means the term must be excluded |
||
| 3251 | * * or % at the end means wildcard |
||
| 3252 | * @param string $query |
||
| 3253 | * @return array of 3 elements - query terms, mandatory terms and excluded terms |
||
| 3254 | */ |
||
| 3255 | public function parseFulltextQuery($query) |
||
| 3289 | |||
| 3290 | // Methods to check respective queries |
||
| 3291 | protected $standardQueries = array( |
||
| 3292 | 'ALTER TABLE' => 'verifyAlterTable', |
||
| 3293 | 'DROP TABLE' => 'verifyDropTable', |
||
| 3294 | 'CREATE TABLE' => 'verifyCreateTable', |
||
| 3295 | 'INSERT INTO' => 'verifyInsertInto', |
||
| 3296 | 'UPDATE' => 'verifyUpdate', |
||
| 3297 | 'DELETE FROM' => 'verifyDeleteFrom', |
||
| 3298 | ); |
||
| 3299 | |||
| 3300 | |||
| 3301 | /** |
||
| 3302 | * Extract table name from a query |
||
| 3303 | * @param string $query SQL query |
||
| 3304 | * @return string |
||
| 3305 | */ |
||
| 3306 | protected function extractTableName($query) |
||
| 3317 | |||
| 3318 | /** |
||
| 3319 | * Verify SQl statement using per-DB verification function |
||
| 3320 | * provided the function exists |
||
| 3321 | * @param string $query Query to verify |
||
| 3322 | * @param array $skipTables List of blacklisted tables that aren't checked |
||
| 3323 | * @return string |
||
| 3324 | */ |
||
| 3325 | public function verifySQLStatement($query, $skipTables) |
||
| 3345 | |||
| 3346 | /** |
||
| 3347 | * Tests an CREATE TABLE query |
||
| 3348 | * @param string $table The table name to get DDL |
||
| 3349 | * @param string $query The query to test. |
||
| 3350 | * @return string Non-empty if error found |
||
| 3351 | */ |
||
| 3352 | protected function verifyCreateTable($table, $query) |
||
| 3381 | |||
| 3382 | /** |
||
| 3383 | * Execute multiple queries one after another |
||
| 3384 | * @param array $sqls Queries |
||
| 3385 | * @param bool $dieOnError Die on error, passed to query() |
||
| 3386 | * @param string $msg Error message, passed to query() |
||
| 3387 | * @param bool $suppress Supress errors, passed to query() |
||
| 3388 | * @return resource|bool result set or success/failure bool |
||
| 3389 | */ |
||
| 3390 | public function queryArray(array $sqls, $dieOnError = false, $msg = '', $suppress = false) |
||
| 3400 | |||
| 3401 | /** |
||
| 3402 | * Fetches the next row in the query result into an associative array |
||
| 3403 | * |
||
| 3404 | * @param resource $result |
||
| 3405 | * @param bool $encode Need to HTML-encode the result? |
||
| 3406 | * @return array returns false if there are no more rows available to fetch |
||
| 3407 | */ |
||
| 3408 | public function fetchByAssoc($result, $encode = true) |
||
| 3424 | |||
| 3425 | /** |
||
| 3426 | * Get DB driver name used for install/upgrade scripts |
||
| 3427 | * @return string |
||
| 3428 | */ |
||
| 3429 | public function getScriptName() |
||
| 3434 | |||
| 3435 | /** |
||
| 3436 | * Set database options |
||
| 3437 | * Options are usually db-dependant and derive from $config['dbconfigoption'] |
||
| 3438 | * @param array $options |
||
| 3439 | * @return DBManager |
||
| 3440 | */ |
||
| 3441 | public function setOptions($options) |
||
| 3446 | |||
| 3447 | /** |
||
| 3448 | * Get DB options |
||
| 3449 | * @return array |
||
| 3450 | */ |
||
| 3451 | public function getOptions() |
||
| 3455 | |||
| 3456 | /** |
||
| 3457 | * Get DB option by name |
||
| 3458 | * @param string $option Option name |
||
| 3459 | * @return mixed Option value or null if doesn't exist |
||
| 3460 | */ |
||
| 3461 | public function getOption($option) |
||
| 3468 | |||
| 3469 | /** |
||
| 3470 | * Commits pending changes to the database when the driver is setup to support transactions. |
||
| 3471 | * Note that the default implementation is applicable for transaction-less or auto commit scenarios. |
||
| 3472 | * @abstract |
||
| 3473 | * @return bool true if commit succeeded, false if it failed |
||
| 3474 | */ |
||
| 3475 | public function commit() |
||
| 3480 | |||
| 3481 | /** |
||
| 3482 | * Rollsback pending changes to the database when the driver is setup to support transactions. |
||
| 3483 | * Note that the default implementation is applicable for transaction-less or auto commit scenarios. |
||
| 3484 | * Since rollbacks cannot be done, this implementation always returns false. |
||
| 3485 | * @abstract |
||
| 3486 | * @return bool true if rollback succeeded, false if it failed |
||
| 3487 | */ |
||
| 3488 | public function rollback() |
||
| 3493 | |||
| 3494 | /** |
||
| 3495 | * Check if this DB name is valid |
||
| 3496 | * |
||
| 3497 | * @param string $name |
||
| 3498 | * @return bool |
||
| 3499 | */ |
||
| 3500 | public function isDatabaseNameValid($name) |
||
| 3505 | |||
| 3506 | /** |
||
| 3507 | * Check special requirements for DB installation. |
||
| 3508 | * @abstract |
||
| 3509 | * If everything is OK, return true. |
||
| 3510 | * If something's wrong, return array of error code and parameters |
||
| 3511 | * @return mixed |
||
| 3512 | */ |
||
| 3513 | public function canInstall() |
||
| 3517 | |||
| 3518 | /** |
||
| 3519 | * @abstract |
||
| 3520 | * Code run on new database before installing |
||
| 3521 | */ |
||
| 3522 | public function preInstall() |
||
| 3525 | |||
| 3526 | /** |
||
| 3527 | * @abstract |
||
| 3528 | * Code run on new database after installing |
||
| 3529 | */ |
||
| 3530 | public function postInstall() |
||
| 3533 | |||
| 3534 | /** |
||
| 3535 | * Disable keys on the table |
||
| 3536 | * @abstract |
||
| 3537 | * @param string $tableName |
||
| 3538 | */ |
||
| 3539 | public function disableKeys($tableName) |
||
| 3542 | |||
| 3543 | /** |
||
| 3544 | * Re-enable keys on the table |
||
| 3545 | * @abstract |
||
| 3546 | * @param string $tableName |
||
| 3547 | */ |
||
| 3548 | public function enableKeys($tableName) |
||
| 3551 | |||
| 3552 | /** |
||
| 3553 | * Quote string in DB-specific manner |
||
| 3554 | * @param string $string |
||
| 3555 | * @return string |
||
| 3556 | */ |
||
| 3557 | abstract public function quote($string); |
||
| 3558 | |||
| 3559 | abstract public function quoteIdentifier($string); |
||
| 3560 | |||
| 3561 | /** |
||
| 3562 | * Use when you need to convert a database string to a different value; this function does it in a |
||
| 3563 | * database-backend aware way |
||
| 3564 | * Supported conversions: |
||
| 3565 | * today return current date |
||
| 3566 | * left Take substring from the left |
||
| 3567 | * date_format Format date as string, supports %Y-%m-%d, %Y-%m, %Y |
||
| 3568 | * time_format Format time as string |
||
| 3569 | * date Convert date string to datetime value |
||
| 3570 | * time Convert time string to datetime value |
||
| 3571 | * datetime Convert datetime string to datetime value |
||
| 3572 | * ifnull If var is null, use default value |
||
| 3573 | * concat Concatenate strings |
||
| 3574 | * quarter Quarter number of the date |
||
| 3575 | * length Length of string |
||
| 3576 | * month Month number of the date |
||
| 3577 | * add_date Add specified interval to a date |
||
| 3578 | * add_time Add time interval to a date |
||
| 3579 | * text2char Convert text field to varchar |
||
| 3580 | * |
||
| 3581 | * @param string $string database string to convert |
||
| 3582 | * @param string $type type of conversion to do |
||
| 3583 | * @param array $additional_parameters optional, additional parameters to pass to the db function |
||
| 3584 | * @return string |
||
| 3585 | */ |
||
| 3586 | abstract public function convert($string, $type, array $additional_parameters = array()); |
||
| 3587 | |||
| 3588 | /** |
||
| 3589 | * Converts from Database data to app data |
||
| 3590 | * |
||
| 3591 | * Supported types |
||
| 3592 | * - date |
||
| 3593 | * - time |
||
| 3594 | * - datetime |
||
| 3595 | * - datetimecombo |
||
| 3596 | * - timestamp |
||
| 3597 | * |
||
| 3598 | * @param string $string database string to convert |
||
| 3599 | * @param string $type type of conversion to do |
||
| 3600 | * @return string |
||
| 3601 | */ |
||
| 3602 | abstract public function fromConvert($string, $type); |
||
| 3603 | |||
| 3604 | /** |
||
| 3605 | * Parses and runs queries |
||
| 3606 | * |
||
| 3607 | * @param string $sql SQL Statement to execute |
||
| 3608 | * @param bool $dieOnError True if we want to call die if the query returns errors |
||
| 3609 | * @param string $msg Message to log if error occurs |
||
| 3610 | * @param bool $suppress Flag to suppress all error output unless in debug logging mode. |
||
| 3611 | * @param bool $keepResult Keep query result in the object? |
||
| 3612 | * @return resource|bool result set or success/failure bool |
||
| 3613 | */ |
||
| 3614 | abstract public function query($sql, $dieOnError = false, $msg = '', $suppress = false, $keepResult = false); |
||
| 3615 | |||
| 3616 | /** |
||
| 3617 | * Runs a limit query: one where we specify where to start getting records and how many to get |
||
| 3618 | * |
||
| 3619 | * @param string $sql SELECT query |
||
| 3620 | * @param int $start Starting row |
||
| 3621 | * @param int $count How many rows |
||
| 3622 | * @param boolean $dieOnError True if we want to call die if the query returns errors |
||
| 3623 | * @param string $msg Message to log if error occurs |
||
| 3624 | * @param bool $execute Execute or return SQL? |
||
| 3625 | * @return resource query result |
||
| 3626 | */ |
||
| 3627 | abstract function limitQuery($sql, $start, $count, $dieOnError = false, $msg = '', $execute = true); |
||
| 3628 | |||
| 3629 | |||
| 3630 | /** |
||
| 3631 | * Free Database result |
||
| 3632 | * @param resource $dbResult |
||
| 3633 | */ |
||
| 3634 | abstract protected function freeDbResult($dbResult); |
||
| 3635 | |||
| 3636 | /** |
||
| 3637 | * Rename column in the DB |
||
| 3638 | * @param string $tablename |
||
| 3639 | * @param string $column |
||
| 3640 | * @param string $newname |
||
| 3641 | */ |
||
| 3642 | abstract function renameColumnSQL($tablename, $column, $newname); |
||
| 3643 | |||
| 3644 | /** |
||
| 3645 | * Returns definitions of all indies for passed table. |
||
| 3646 | * |
||
| 3647 | * return will is a multi-dimensional array that |
||
| 3648 | * categorizes the index definition by types, unique, primary and index. |
||
| 3649 | * <code> |
||
| 3650 | * <?php |
||
| 3651 | * array( O |
||
| 3652 | * 'index1'=> array ( |
||
| 3653 | * 'name' => 'index1', |
||
| 3654 | * 'type' => 'primary', |
||
| 3655 | * 'fields' => array('field1','field2') |
||
| 3656 | * ) |
||
| 3657 | * ) |
||
| 3658 | * ?> |
||
| 3659 | * </code> |
||
| 3660 | * This format is similar to how indicies are defined in vardef file. |
||
| 3661 | * |
||
| 3662 | * @param string $tablename |
||
| 3663 | * @return array |
||
| 3664 | */ |
||
| 3665 | abstract public function get_indices($tablename); |
||
| 3666 | |||
| 3667 | /** |
||
| 3668 | * Returns definitions of all indies for passed table. |
||
| 3669 | * |
||
| 3670 | * return will is a multi-dimensional array that |
||
| 3671 | * categorizes the index definition by types, unique, primary and index. |
||
| 3672 | * <code> |
||
| 3673 | * <?php |
||
| 3674 | * array( |
||
| 3675 | * 'field1'=> array ( |
||
| 3676 | * 'name' => 'field1', |
||
| 3677 | * 'type' => 'varchar', |
||
| 3678 | * 'len' => '200' |
||
| 3679 | * ) |
||
| 3680 | * ) |
||
| 3681 | * ?> |
||
| 3682 | * </code> |
||
| 3683 | * This format is similar to how indicies are defined in vardef file. |
||
| 3684 | * |
||
| 3685 | * @param string $tablename |
||
| 3686 | * @return array |
||
| 3687 | */ |
||
| 3688 | abstract public function get_columns($tablename); |
||
| 3689 | |||
| 3690 | /** |
||
| 3691 | * Generates alter constraint statement given a table name and vardef definition. |
||
| 3692 | * |
||
| 3693 | * Supports both adding and droping a constraint. |
||
| 3694 | * |
||
| 3695 | * @param string $table tablename |
||
| 3696 | * @param array $definition field definition |
||
| 3697 | * @param bool $drop true if we are dropping the constraint, false if we are adding it |
||
| 3698 | * @return string SQL statement |
||
| 3699 | */ |
||
| 3700 | abstract public function add_drop_constraint($table, $definition, $drop = false); |
||
| 3701 | |||
| 3702 | /** |
||
| 3703 | * Returns the description of fields based on the result |
||
| 3704 | * |
||
| 3705 | * @param resource $result |
||
| 3706 | * @param boolean $make_lower_case |
||
| 3707 | * @return array field array |
||
| 3708 | */ |
||
| 3709 | abstract public function getFieldsArray($result, $make_lower_case = false); |
||
| 3710 | |||
| 3711 | /** |
||
| 3712 | * Returns an array of tables for this database |
||
| 3713 | * |
||
| 3714 | * @return array|false an array of with table names, false if no tables found |
||
| 3715 | */ |
||
| 3716 | abstract public function getTablesArray(); |
||
| 3717 | |||
| 3718 | /** |
||
| 3719 | * Return's the version of the database |
||
| 3720 | * |
||
| 3721 | * @return string |
||
| 3722 | */ |
||
| 3723 | abstract public function version(); |
||
| 3724 | |||
| 3725 | /** |
||
| 3726 | * Checks if a table with the name $tableName exists |
||
| 3727 | * and returns true if it does or false otherwise |
||
| 3728 | * |
||
| 3729 | * @param string $tableName |
||
| 3730 | * @return bool |
||
| 3731 | */ |
||
| 3732 | abstract public function tableExists($tableName); |
||
| 3733 | |||
| 3734 | /** |
||
| 3735 | * Fetches the next row in the query result into an associative array |
||
| 3736 | * |
||
| 3737 | * @param resource $result |
||
| 3738 | * @return array returns false if there are no more rows available to fetch |
||
| 3739 | */ |
||
| 3740 | abstract public function fetchRow($result); |
||
| 3741 | |||
| 3742 | /** |
||
| 3743 | * Connects to the database backend |
||
| 3744 | * |
||
| 3745 | * Takes in the database settings and opens a database connection based on those |
||
| 3746 | * will open either a persistent or non-persistent connection. |
||
| 3747 | * If a persistent connection is desired but not available it will defualt to non-persistent |
||
| 3748 | * |
||
| 3749 | * configOptions must include |
||
| 3750 | * db_host_name - server ip |
||
| 3751 | * db_user_name - database user name |
||
| 3752 | * db_password - database password |
||
| 3753 | * |
||
| 3754 | * @param array $configOptions |
||
| 3755 | * @param boolean $dieOnError |
||
| 3756 | */ |
||
| 3757 | abstract public function connect(array $configOptions = null, $dieOnError = false); |
||
| 3758 | |||
| 3759 | /** |
||
| 3760 | * Generates sql for create table statement for a bean. |
||
| 3761 | * |
||
| 3762 | * @param string $tablename |
||
| 3763 | * @param array $fieldDefs |
||
| 3764 | * @param array $indices |
||
| 3765 | * @return string SQL Create Table statement |
||
| 3766 | */ |
||
| 3767 | abstract public function createTableSQLParams($tablename, $fieldDefs, $indices); |
||
| 3768 | |||
| 3769 | /** |
||
| 3770 | * Generates the SQL for changing columns |
||
| 3771 | * |
||
| 3772 | * @param string $tablename |
||
| 3773 | * @param array $fieldDefs |
||
| 3774 | * @param string $action |
||
| 3775 | * @param bool $ignoreRequired Optional, true if we should ignor this being a required field |
||
| 3776 | * @return string|array |
||
| 3777 | */ |
||
| 3778 | abstract protected function changeColumnSQL($tablename, $fieldDefs, $action, $ignoreRequired = false); |
||
| 3779 | |||
| 3780 | /** |
||
| 3781 | * Disconnects from the database |
||
| 3782 | * |
||
| 3783 | * Also handles any cleanup needed |
||
| 3784 | */ |
||
| 3785 | abstract public function disconnect(); |
||
| 3786 | |||
| 3787 | /** |
||
| 3788 | * Get last database error |
||
| 3789 | * This function should return last error as reported by DB driver |
||
| 3790 | * and should return false if no error condition happened |
||
| 3791 | * @return string|false Error message or false if no error happened |
||
| 3792 | */ |
||
| 3793 | abstract public function lastDbError(); |
||
| 3794 | |||
| 3795 | /** |
||
| 3796 | * Check if this query is valid |
||
| 3797 | * Validates only SELECT queries |
||
| 3798 | * @param string $query |
||
| 3799 | * @return bool |
||
| 3800 | */ |
||
| 3801 | abstract public function validateQuery($query); |
||
| 3802 | |||
| 3803 | /** |
||
| 3804 | * Check if this driver can be used |
||
| 3805 | * @return bool |
||
| 3806 | */ |
||
| 3807 | abstract public function valid(); |
||
| 3808 | |||
| 3809 | /** |
||
| 3810 | * Check if certain database exists |
||
| 3811 | * @param string $dbname |
||
| 3812 | */ |
||
| 3813 | abstract public function dbExists($dbname); |
||
| 3814 | |||
| 3815 | /** |
||
| 3816 | * Get tables like expression |
||
| 3817 | * @param string $like Expression describing tables |
||
| 3818 | * @return array |
||
| 3819 | */ |
||
| 3820 | abstract public function tablesLike($like); |
||
| 3821 | |||
| 3822 | /** |
||
| 3823 | * Create a database |
||
| 3824 | * @param string $dbname |
||
| 3825 | */ |
||
| 3826 | abstract public function createDatabase($dbname); |
||
| 3827 | |||
| 3828 | /** |
||
| 3829 | * Drop a database |
||
| 3830 | * @param string $dbname |
||
| 3831 | */ |
||
| 3832 | abstract public function dropDatabase($dbname); |
||
| 3833 | |||
| 3834 | /** |
||
| 3835 | * Get database configuration information (DB-dependent) |
||
| 3836 | * @return array|null |
||
| 3837 | */ |
||
| 3838 | abstract public function getDbInfo(); |
||
| 3839 | |||
| 3840 | /** |
||
| 3841 | * Check if certain DB user exists |
||
| 3842 | * @param string $username |
||
| 3843 | */ |
||
| 3844 | abstract public function userExists($username); |
||
| 3845 | |||
| 3846 | /** |
||
| 3847 | * Create DB user |
||
| 3848 | * @param string $database_name |
||
| 3849 | * @param string $host_name |
||
| 3850 | * @param string $user |
||
| 3851 | * @param string $password |
||
| 3852 | */ |
||
| 3853 | abstract public function createDbUser($database_name, $host_name, $user, $password); |
||
| 3854 | |||
| 3855 | /** |
||
| 3856 | * Check if the database supports fulltext indexing |
||
| 3857 | * Note that database driver can be capable of supporting FT (see supports('fulltext)) |
||
| 3858 | * but particular instance can still have it disabled |
||
| 3859 | * @return bool |
||
| 3860 | */ |
||
| 3861 | abstract public function full_text_indexing_installed(); |
||
| 3862 | |||
| 3863 | /** |
||
| 3864 | * Generate fulltext query from set of terms |
||
| 3865 | * @param string $field Field to search against |
||
| 3866 | * @param array $terms Search terms that may be or not be in the result |
||
| 3867 | * @param array $must_terms Search terms that have to be in the result |
||
| 3868 | * @param array $exclude_terms Search terms that have to be not in the result |
||
| 3869 | */ |
||
| 3870 | abstract public function getFulltextQuery($field, $terms, $must_terms = array(), $exclude_terms = array()); |
||
| 3871 | |||
| 3872 | /** |
||
| 3873 | * Get install configuration for this DB |
||
| 3874 | * @return array |
||
| 3875 | */ |
||
| 3876 | abstract public function installConfig(); |
||
| 3877 | |||
| 3878 | /** |
||
| 3879 | * Returns a DB specific FROM clause which can be used to select against functions. |
||
| 3880 | * Note that depending on the database that this may also be an empty string. |
||
| 3881 | * @abstract |
||
| 3882 | * @return string |
||
| 3883 | */ |
||
| 3884 | abstract public function getFromDummyTable(); |
||
| 3885 | |||
| 3886 | /** |
||
| 3887 | * Returns a DB specific piece of SQL which will generate GUID (UUID) |
||
| 3888 | * This string can be used in dynamic SQL to do multiple inserts with a single query. |
||
| 3889 | * I.e. generate a unique Sugar id in a sub select of an insert statement. |
||
| 3890 | * @abstract |
||
| 3891 | * @return string |
||
| 3892 | */ |
||
| 3893 | abstract public function getGuidSQL(); |
||
| 3894 | } |
||
| 3895 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.