Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
DatabaseCompatibilityUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
43  {
48  public static function charLength($columnName)
49  {
50  assert('is_string($columnName)');
51  return 'char_length(' . $columnName . ')';
52  }
53 
58  public static function concat(array $strings)
59  {
60  assert('AssertUtil::all($strings, "is_string")');
61  if (in_array(RedBeanDatabase::getDatabaseType(), array('sqlite', 'pgsql')))
62  {
63  return implode(' || ', $strings);
64  }
65  else
66  {
67  return 'concat(' . implode(', ', $strings) . ')';
68  }
69  }
70 
71  public static function escape($value)
72  {
73  return ZurmoRedBean::$adapter->escape($value);
74  }
75 
79  public static function getAllTableNames()
80  {
81  assert('RedBeanDatabase::isSetup()');
82  return ZurmoRedBean::$writer->getTables();
83  }
84 
90  public static function getDateFormat()
91  {
92  return 'yyyy-MM-dd';
93  }
94 
100  public static function getDateTimeFormat()
101  {
102  return 'yyyy-MM-dd HH:mm:ss';
103  }
104 
108  public static function getMaxVarCharLength()
109  {
110  return 255;
111  }
112 
118  public static function lower($columnName)
119  {
120  assert('is_string($columnName)');
121  return 'lower(' . $columnName . ')';
122  }
123 
129  public static function getQuote()
130  {
131  assert('RedBeanDatabase::isSetup()');
132  if (RedBeanDatabase::getDatabaseType() == 'pgsql')
133  {
134  return '"';
135  }
136  else
137  {
138  return '`';
139  }
140  }
141 
146  public static function getTrue()
147  {
148  assert('RedBeanDatabase::isSetup()');
149  if (RedBeanDatabase::getDatabaseType() == 'sqlite')
150  {
151  return '1';
152  }
153  else
154  {
155  return 'true';
156  }
157  }
158 
163  public static function length($columnName)
164  {
165  assert('is_string($columnName)');
166  return 'length(' . $columnName . ')';
167  }
168 
175  public static function quoteString($string)
176  {
177  assert('is_string($string)');
178  $quote = self::getQuote();
179  return "$quote$string$quote";
180  }
181 
186  public static function getDatabaseMaxColumnNameLength()
187  {
188  $maxColumnNameLength = 12;
189  return $maxColumnNameLength;
190  }
191 
197  public static function getDatabaseReserverWords()
198  {
199  if (RedBeanDatabase::getDatabaseType() == 'mysql')
200  {
201  $reservedWords = array(
202  'ACCESSIBLE', 'ALTER', 'AS', 'BEFORE', 'BINARY', 'BY', 'CASE', 'CHARACTER', 'COLUMN', 'CONTINUE', 'CROSS',
203  'CURRENT_TIMESTAMP', 'DATABASE', 'DAY_MICROSECOND', 'DEC', 'DEFAULT', 'DESC', 'DISTINCT', 'DOUBLE', 'EACH',
204  'ENCLOSED', 'EXIT', 'FETCH', 'FLOAT8', 'FOREIGN', 'GRANT', 'HIGH_PRIORITY', 'HOUR_SECOND', 'IN', 'INNER',
205  'INSERT', 'INT2', 'INT8', 'INTO', 'JOIN', 'KILL', 'LEFT', 'LINEAR', 'LOCALTIME', 'LONG', 'LOOP', 'MATCH',
206  'MEDIUMTEXT', 'MINUTE_SECOND', 'NATURAL', 'NULL', 'OPTIMIZE', 'OR', 'OUTER', 'PRIMARY', 'RANGE', 'READ_WRITE', // Not Coding Standard
207  'REGEXP', 'REPEAT', 'RESTRICT', 'RIGHT', 'SCHEMAS', 'SENSITIVE', 'SHOW', 'SPECIFIC', 'SQLSTATE',
208  'SQL_CALC_FOUND_ROWS', 'STARTING', 'TERMINATED', 'TINYINT', 'TRAILING', 'UNDO', 'UNLOCK', 'USAGE',
209  'UTC_DATE', 'VALUES', 'VARCHARACTER', 'WHERE', 'WRITE', 'ZEROFILL', 'ALL', 'AND', 'ASENSITIVE', 'BIGINT',
210  'BOTH', 'CASCADE', 'CHAR', 'COLLATE', 'CONSTRAINT', 'COUNT', 'CREATE', 'CURRENT_TIME', 'CURSOR', 'DAY_HOUR',
211  'DAY_SECOND', 'DECLARE', 'DELETE', 'DETERMINISTIC', 'DIV', 'DUAL', 'ELSEIF', 'EXISTS', 'FALSE', 'FLOAT4', // Not Coding Standard
212  'FORCE', 'FULLTEXT', 'HAVING', 'HOUR_MINUTE', 'IGNORE', 'INFILE', 'INSENSITIVE', 'INT1', 'INT4', 'INTERVAL',
213  'ITERATE', 'KEYS', 'LEAVE', 'LIMIT', 'LOAD', 'LOCK', 'LONGTEXT', 'MASTER_SSL_VERIFY_SERVER_CERT', 'MEDIUMINT',
214  'MINUTE_MICROSECOND', 'MODIFIES', 'NO_WRITE_TO_BINLOG', 'ON', 'OPTIONALLY', 'OUT', 'PRECISION', 'PURGE',
215  'READS', 'REFERENCES', 'RENAME', 'REQUIRE', 'REVOKE', 'SCHEMA', 'SELECT', 'SET', 'SPATIAL', 'SQLEXCEPTION',
216  'SQL_BIG_RESULT', 'SSL', 'TABLE', 'TINYBLOB', 'TO', 'TRUE', 'UNIQUE', 'UPDATE', 'USING', 'UTC_TIMESTAMP', // Not Coding Standard
217  'VARCHAR', 'WHEN', 'WITH', 'YEAR_MONTH', 'ADD', 'ANALYZE', 'ASC', 'BETWEEN', 'BLOB', 'CALL', 'CHANGE', 'CHECK',
218  'CONDITION', 'CONVERT', 'CURRENT_DATE', 'CURRENT_USER', 'DATABASES', 'DAY_MINUTE', 'DECIMAL', 'DELAYED',
219  'DESCRIBE', 'DISTINCTROW', 'DROP', 'ELSE', 'ESCAPED', 'EXPLAIN', 'FLOAT', 'FOR', 'FROM', 'GROUP',
220  'HOUR_MICROSECOND', 'IF', 'INDEX', 'INOUT', 'INT', 'INT3', 'INTEGER', 'IS', 'KEY', 'LEADING', 'LIKE', 'LINES',
221  'LOCALTIMESTAMP', 'LONGBLOB', 'LOW_PRIORITY', 'MEDIUMBLOB', 'MIDDLEINT', 'MOD', 'NOT', 'NUMERIC', 'OPTION',
222  'ORDER', 'OUTFILE', 'PROCEDURE', 'READ', 'REAL', 'RELEASE', 'REPLACE', 'RETURN', 'RLIKE', 'SECOND_MICROSECOND',
223  'SEPARATOR', 'SMALLINT', 'SQL', 'SQLWARNING', 'SQL_SMALL_RESULT', 'STRAIGHT_JOIN', 'THEN', 'TINYTEXT',
224  'TRIGGER', 'UNION', 'UNSIGNED', 'USE', 'UTC_TIME', 'VARBINARY', 'VARYING', 'WHILE', 'XOR',
225  'GENERAL', 'IGNORE_SERVER_ID', 'MASTER_HEARTBEAT_PERIOD', 'MAXVALUE', 'RESIGNAL', 'SIGNAL', 'SLOW'
226  );
227  }
228  else
229  {
230  throw new NotSupportedException();
231  }
232  return $reservedWords;
233  }
234 
240  public static function getOperatorAndValueWherePart($operatorType, $value)
241  {
242  assert('is_string($operatorType)');
243  if (!SQLOperatorUtil::isValidOperatorTypeByValue($operatorType, $value))
244  {
245  throw new NotSupportedException('value: ' . $value . ' operator type: ' . $operatorType);
246  }
247  if (is_string($value))
248  {
249  return self::resolveToLowerForStringComparison($operatorType, self::escape($value));
250  }
251  elseif (is_array($value) && count($value) > 0)
252  {
253  return SQLOperatorUtil::resolveOperatorAndValueForOneOf($operatorType, $value);
254  }
255  elseif ($value !== null)
256  {
257  return SQLOperatorUtil::getOperatorByType($operatorType) . " " . self::escape($value);
258  }
259  elseif ($value === null)
260  {
261  return SQLOperatorUtil::resolveOperatorAndValueForNullOrEmpty($operatorType);
262  }
263  }
264 
265  public static function resolveToLowerForStringComparison($operatorType, $value)
266  {
267  assert('is_string($operatorType)');
268  assert('is_string($value)');
269  if (RedBeanDatabase::getDatabaseType() != 'mysql')
270  {
271  //todo: for pgsql, need to use lower or ILIKE to make sure evaluation is not case sensitive
272  throw new NotSupportedException();
273  }
274  return SQLOperatorUtil::getOperatorByType($operatorType) .
276  $value .
278  }
279 
292  public static function bulkInsert($tableName, & $rowsOfColumnValues, & $columnNames, $bulkQuantity, $shouldTrim = false)
293  {
294  assert('is_string($tableName)');
295  assert('is_array($rowsOfColumnValues)');
296  assert('is_array($columnNames)');
297  assert('is_int($bulkQuantity)');
298 
299  if (RedBeanDatabase::getDatabaseType() != 'mysql')
300  {
301  throw new NotSupportedException();
302  }
303  $counter = 0;
304  foreach ($rowsOfColumnValues as $row)
305  {
306  if (count($row) == count($columnNames))
307  {
308  if ($counter == 0)
309  {
310  $sql = "INSERT INTO " . self::quoteString($tableName) . "(" . implode(',', $columnNames) . ") VALUES "; // Not Coding Standard
311  }
312  if ($counter == $bulkQuantity)
313  {
314  $quotedRow = array_map(array('DatabaseCompatibilityUtil', 'escape'), $row);
315  if ($shouldTrim)
316  {
317  $quotedRow = array_map('trim', $quotedRow);
318  }
319  $sql .= "('" . implode("','", $quotedRow). "')"; // Not Coding Standard
320  ZurmoRedBean::exec($sql);
321  $counter = 0;
322  }
323  else
324  {
325  $quotedRow = array_map(array('DatabaseCompatibilityUtil', 'escape'), $row);
326  if ($shouldTrim)
327  {
328  $quotedRow = array_map('trim', $quotedRow);
329  }
330  $sql .= "('" . implode("','", $quotedRow). "'),"; // Not Coding Standard
331  $counter++;
332  }
333  }
334  else
335  {
336  throw new BulkInsertFailedException(
337  Zurmo::t('Core', 'Bulk insert failed. There was a row with an incorrect column quantity.') .
338  ' ' .
339  Zurmo::t('Core', 'Row Counter: {rowNumber} Serialized row data: {serializedRowData}',
340  array('{rowNumber}' => $counter, '{serializedRowData}' => serialize($row))));
341  }
342  }
343  if ($counter > 0)
344  {
345  $sql = trim($sql, ','); // Not Coding Standard
346  ZurmoRedBean::exec($sql);
347  }
348  }
349 
359  public static function getDatabaseVersion($databaseType,
360  $databaseHostname,
361  $databaseUsername,
362  $databasePassword,
363  $databasePort)
364  {
365  if ($databaseType != 'mysql')
366  {
367  throw new NotSupportedException();
368  }
369  switch ($databaseType)
370  {
371  case 'mysql':
372  $PhpDriverVersion = phpversion('mysqli');
373  if ($PhpDriverVersion !== null)
374  {
375  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
376  $result = @mysqli_query($connection, "SELECT VERSION()");
377  $row = @mysqli_fetch_row($result);
378  if (is_resource($connection))
379  {
380  mysqli_close($connection);
381  }
382  if (isset($row[0]))
383  {
384  return $row[0];
385  }
386  }
387  }
388  return false;
389  }
390 
395  public static function getDatabaseMaxAllowedPacketsSizeRb()
396  {
397  if (RedBeanDatabase::getDatabaseType() != 'mysql')
398  {
399  throw new NotSupportedException();
400  }
401 
402  $row = ZurmoRedBean::getRow("SHOW VARIABLES LIKE 'max_allowed_packet'");
403 
404  if (isset($row['Value']))
405  {
406  return $row['Value'];
407  }
408  else
409  {
410  return null;
411  }
412  }
413 
424  public static function getDatabaseMaxAllowedPacketsSize($databaseType,
425  $databaseHostname,
426  $databaseUsername,
427  $databasePassword,
428  $databasePort)
429  {
430  if ($databaseType != 'mysql')
431  {
432  throw new NotSupportedException();
433  }
434 
435  switch ($databaseType)
436  {
437  case 'mysql':
438  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
439  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'max_allowed_packet'");
440  $row = @mysqli_fetch_row($result);
441  if (is_resource($connection))
442  {
443  mysqli_close($connection);
444  }
445  if (isset($row[1]))
446  {
447  return $row[1];
448  }
449  }
450  return false;
451  }
452 
462  public static function getDatabaseMaxSpRecursionDepth($databaseType,
463  $databaseHostname,
464  $databaseUsername,
465  $databasePassword,
466  $databasePort)
467  {
468  if ($databaseType != 'mysql')
469  {
470  throw new NotSupportedException();
471  }
472  switch ($databaseType)
473  {
474  case 'mysql':
475  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
476  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'max_sp_recursion_depth'");
477  $row = @mysqli_fetch_row($result);
478  if (is_resource($connection))
479  {
480  mysqli_close($connection);
481  }
482  if (isset($row[1]))
483  {
484  return $row[1];
485  }
486  }
487  return false;
488  }
489 
499  public static function getDatabaseThreadStackValue($databaseType,
500  $databaseHostname,
501  $databaseUsername,
502  $databasePassword,
503  $databasePort)
504  {
505  if ($databaseType != 'mysql')
506  {
507  throw new NotSupportedException();
508  }
509  switch ($databaseType)
510  {
511  case 'mysql':
512  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
513  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'thread_stack'");
514  $row = @mysqli_fetch_row($result);
515  if (is_resource($connection))
516  {
517  mysqli_close($connection);
518  }
519  if (isset($row[1]))
520  {
521  return $row[1];
522  }
523  }
524  return false;
525  }
526 
536  public static function getDatabaseOptimizerSearchDepthValue($databaseType,
537  $databaseHostname,
538  $databaseUsername,
539  $databasePassword,
540  $databasePort)
541  {
542  if ($databaseType != 'mysql')
543  {
544  throw new NotSupportedException();
545  }
546  switch ($databaseType)
547  {
548  case 'mysql':
549  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
550  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'optimizer_search_depth'");
551  $row = @mysqli_fetch_row($result);
552  if (is_resource($connection))
553  {
554  mysqli_close($connection);
555  }
556  if (isset($row[1]))
557  {
558  return $row[1];
559  }
560  }
561  return false;
562  }
563 
573  public static function getDatabaseLogBinValue($databaseType,
574  $databaseHostname,
575  $databaseUsername,
576  $databasePassword,
577  $databasePort)
578  {
579  if ($databaseType != 'mysql')
580  {
581  throw new NotSupportedException();
582  }
583  switch ($databaseType)
584  {
585  case 'mysql':
586  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
587  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'log_bin'");
588  $row = @mysqli_fetch_row($result);
589  if (is_resource($connection))
590  {
591  mysqli_close($connection);
592  }
593  if (isset($row[1]))
594  {
595  return $row[1];
596  }
597  }
598  return false;
599  }
600 
610  public static function getDatabaseLogBinTrustFunctionCreatorsValue($databaseType,
611  $databaseHostname,
612  $databaseUsername,
613  $databasePassword,
614  $databasePort)
615  {
616  if ($databaseType != 'mysql')
617  {
618  throw new NotSupportedException();
619  }
620  switch ($databaseType)
621  {
622  case 'mysql':
623  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
624  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'log_bin_trust_function_creators'");
625  $row = @mysqli_fetch_row($result);
626  if (is_resource($connection))
627  {
628  mysqli_close($connection);
629  }
630  if (isset($row[1]))
631  {
632  return $row[1];
633  }
634  }
635  return false;
636  }
637 
649  public static function getDatabaseDefaultCollation($databaseType,
650  $databaseHostname,
651  $databaseName,
652  $databaseUsername,
653  $databasePassword,
654  $databasePort)
655  {
656  if ($databaseType != 'mysql')
657  {
658  throw new NotSupportedException();
659  }
660 
661  switch ($databaseType)
662  {
663  case 'mysql':
664  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
665  @mysqli_select_db($connection, $databaseName);
666  $result = @mysqli_query($connection, "SHOW VARIABLES LIKE 'collation_database'");
667  $row = @mysqli_fetch_row($result);
668  if (is_resource($connection))
669  {
670  mysqli_close($connection);
671  }
672  if (isset($row[1]))
673  {
674  return $row[1];
675  }
676  }
677  return false;
678  }
679 
690  public static function isDatabaseStrictMode($databaseType,
691  $databaseHostname,
692  $databaseUsername,
693  $databasePassword,
694  $databasePort)
695  {
696  if ($databaseType != 'mysql')
697  {
698  throw new NotSupportedException();
699  }
700  switch ($databaseType)
701  {
702  case 'mysql':
703  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
704  $result = @mysqli_query($connection, "SELECT @@sql_mode;");
705  $row = @mysqli_fetch_row($result);
706  if (is_resource($connection))
707  {
708  mysqli_close($connection);
709  }
710  if (isset($row[0]))
711  {
712  if (strstr($row[0], 'STRICT_TRANS_TABLES') !== false)
713  {
714  $isStrict = true;
715  }
716  else
717  {
718  $isStrict = false;
719  }
720  return $isStrict;
721  }
722  }
723  }
724 
735  public static function checkDatabaseConnection($databaseType, $host, $rootUsername, $rootPassword, $port)
736  {
737  if ($databaseType != 'mysql')
738  {
739  throw new NotSupportedException();
740  }
741 
742  assert('is_string($host) && $host != ""');
743  assert('is_string($rootUsername) && $rootUsername != ""');
744  assert('is_string($rootPassword) && $rootPassword != ""');
745  assert('is_int($port) && $port != ""');
746  switch ($databaseType)
747  {
748  case 'mysql':
749  $result = true;
750  if (($connection = @mysqli_connect($host, $rootUsername, $rootPassword, '', $port)) === false)
751  {
752  $result = array(mysqli_connect_errno(), mysqli_connect_error());
753  }
754  if (is_resource($connection))
755  {
756  mysqli_close($connection);
757  }
758  return $result;
759  }
760  }
761 
773  public static function checkDatabaseExists($databaseType, $host, $rootUsername, $rootPassword, $port,
774  $databaseName)
775  {
776  if ($databaseType != 'mysql')
777  {
778  throw new NotSupportedException();
779  }
780  assert('is_string($host) && $host != ""');
781  assert('is_string($rootUsername) && $rootUsername != ""');
782  assert('is_string($rootPassword) && $rootPassword != ""');
783  assert('is_int($port) && $port != ""');
784  assert('is_string($databaseName) && $databaseName != ""');
785  switch ($databaseType)
786  {
787  case 'mysql':
788  $result = true;
789  if (($connection = @mysqli_connect($host, $rootUsername, $rootPassword, '', $port)) === false ||
790  @mysqli_select_db($connection, $databaseName) === false)
791  {
792  if ($connection === false)
793  {
794  $result = array(mysqli_connect_errno(), mysqli_connect_error());
795  }
796  else
797  {
798  $result = array(mysqli_errno($connection), mysqli_error($connection));
799  }
800  }
801  if (is_resource($connection))
802  {
803  mysqli_close($connection);
804  }
805  return $result;
806  }
807  }
808 
820  public static function checkDatabaseUserExists($databaseType, $host, $rootUsername, $rootPassword, $port, $username)
821  {
822  if ($databaseType != 'mysql')
823  {
824  throw new NotSupportedException();
825  }
826  assert('is_string($host) && $host != ""');
827  assert('is_string($rootUsername) && $rootUsername != ""');
828  assert('is_string($rootPassword) && $rootPassword != ""');
829  assert('is_int($port) && $port != ""');
830  assert('is_string($username) && $username != ""');
831  switch ($databaseType)
832  {
833  case 'mysql':
834  $result = true;
835  $query = "select count(*) from user where Host in ('%', '$host') and User ='$username'";
836  $connection = @mysqli_connect($host, $rootUsername, $rootPassword, '', $port);
837  $databaseConnection = @mysqli_select_db($connection, 'mysql');
838  $queryResult = @mysqli_query($connection, $query);
839  $row = @mysqli_fetch_row($queryResult);
840  if ($connection === false || $databaseConnection === false || $queryResult === false ||
841  $row === false)
842  {
843  if ($connection === false)
844  {
845  $result = array(mysqli_connect_errno(), mysqli_connect_error());
846  }
847  else
848  {
849  $result = array(mysqli_errno($connection), mysqli_error($connection));
850  }
851  }
852  else
853  {
854  if ($row == null)
855  {
856  $result = array(mysqli_errno($connection), mysqli_error($connection));
857  }
858  elseif (is_array($row) && count($row) == 1 && $row[0] == 0)
859  {
860  return false;
861  }
862  else
863  {
864  assert('is_array($row) && count($row) == 1 && $row[0] >= 1');
865  $result = $row[0] == 1;
866  }
867  }
868  if (is_resource($connection))
869  {
870  mysqli_close($connection);
871  }
872  return $result;
873  }
874  }
875 
876  public static function getDatabaseSupportsLoadLocalInFile($databaseType,
877  $databaseHostname,
878  $databaseUsername,
879  $databasePassword,
880  $databasePort)
881  {
882  if ($databaseType != 'mysql')
883  {
884  throw new NotSupportedException();
885  }
886  // TODO: @Shoaibi: Critical: @see: https://www.pivotaltracker.com/story/show/59409504
887  return '0';
888  switch ($databaseType)
889  {
890  case 'mysql':
891  $query = "SELECT * FROM `GLOBAL_VARIABLES` WHERE VARIABLE_NAME='LOCAL_INFILE';";
892  $connection = @mysqli_connect($databaseHostname, $databaseUsername, $databasePassword, '', $databasePort);
893  @mysqli_select_db($connection, 'information_schema');
894  $result = @mysqli_query($connection, $query);
895  $row = @mysqli_fetch_row($result);
896  if (is_resource($connection))
897  {
898  mysqli_close($connection);
899  }
900  if (isset($row[1]))
901  {
902  return $row[1];
903  }
904  }
905  }
906 
908  // Methods that modify things.
909  // The aim is that when all of the checks above pass
910  // these should be expected to succeed.
912 
923  public static function createDatabase($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName)
924  {
925  if ($databaseType != 'mysql')
926  {
927  throw new NotSupportedException();
928  }
929  assert('is_string($host) && $host != ""');
930  assert('is_string($rootUsername) && $rootUsername != ""');
931  assert('is_string($rootPassword) && $rootPassword != ""');
932  assert('is_int($port) && $port != ""');
933  assert('is_string($databaseName) && $databaseName != ""');
934  switch ($databaseType)
935  {
936  case 'mysql':
937  $result = true;
938  if (($connection = @mysqli_connect($host, $rootUsername, $rootPassword, '', $port)) === false ||
939  @mysqli_query($connection, "drop database if exists `$databaseName`") === false ||
940  @mysqli_query($connection, "create database `$databaseName` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;") === false)
941  {
942  if ($connection === false)
943  {
944  $result = array(mysqli_connect_errno(), mysqli_connect_error());
945  }
946  else
947  {
948  $result = array(mysqli_errno($connection), mysqli_error($connection));
949  }
950  }
951  if (is_resource($connection))
952  {
953  mysqli_close($connection);
954  }
955  return $result;
956  }
957  }
958 
973  public static function createDatabaseUser($databaseType, $host, $rootUsername, $rootPassword, $port,
974  $databaseName, $username, $password)
975  {
976  if ($databaseType != 'mysql')
977  {
978  throw new NotSupportedException();
979  }
980  assert('is_string($host) && $host != ""');
981  assert('is_string($rootUsername) && $rootUsername != ""');
982  assert('is_string($rootPassword) && $rootPassword != ""');
983  assert('is_int($port) && $port != ""');
984  assert('is_string($databaseName) && $databaseName != ""');
985  assert('is_string($username) && $username != ""');
986  assert('is_string($password)');
987  switch ($databaseType)
988  {
989  case 'mysql':
990  $result = true;
991  if (($connection = @mysqli_connect($host, $rootUsername, $rootPassword, '', $port)) === false ||
992  // The === 666 is to execute this command ignoring whether it fails.
993  ($userDropped = @mysqli_query($connection, "drop user `$username`")) === 666 ||
994  ($userCreated = @mysqli_query($connection, "grant all on `$databaseName`.* to `$username`@'$host' IDENTIFIED BY '$password'")) === false)
995  {
996  if ($connection === false)
997  {
998  $result = array(mysqli_connect_errno(), mysqli_connect_error());
999  }
1000  else
1001  {
1002  $result = array(mysqli_errno($connection), mysqli_error($connection));
1003  }
1004  }
1005  if (is_resource($connection))
1006  {
1007  mysqli_close($connection);
1008  }
1009  return $result;
1010  }
1011  }
1012 
1013  public static function getTableRowsCountTotal()
1014  {
1015  if (RedBeanDatabase::getDatabaseType() != 'mysql')
1016  {
1017  throw new NotSupportedException();
1018  }
1019  $totalCount = 0;
1020  foreach (static::getAllTableNames() as $tableName)
1021  {
1022  $totalCount += ZurmoRedBean::$writer->count($tableName);
1023  }
1024  return $totalCount;
1025  }
1026 
1031  public static function getDatabaseDefaultPort($databaseType = 'mysql')
1032  {
1033  if ($databaseType == 'mysql')
1034  {
1035  return 3306;
1036  }
1037  else
1038  {
1039  throw new NotSupportedException();
1040  }
1041  }
1042 
1055  public static function backupDatabase($databaseType = 'mysql',
1056  $host,
1057  $username,
1058  $password,
1059  $port,
1060  $databaseName,
1061  $backupFilePath)
1062  {
1063  assert('is_string($host) && $host != ""');
1064  assert('is_string($username) && $username != ""');
1065  assert('is_string($password)');
1066  assert('is_string($databaseName) && $databaseName != ""');
1067  assert('is_string($backupFilePath) && $backupFilePath != ""');
1068 
1069  if ($databaseType == 'mysql')
1070  {
1071  $result = exec("mysqldump --host=$host --user=$username --password=$password --port=$port --routines --add-drop-database $databaseName > $backupFilePath", $output, $returnVal); // Not Coding Standard
1072 
1073  if ($returnVal !== 0)
1074  {
1075  return false;
1076  }
1077  else
1078  {
1079  return true;
1080  }
1081  }
1082  else
1083  {
1084  throw new NotSupportedException();
1085  }
1086  }
1087 
1088  public static function restoreDatabase($databaseType = 'mysql',
1089  $host,
1090  $username,
1091  $password,
1092  $port,
1093  $databaseName,
1094  $restoreFilePath)
1095  {
1096  assert('is_string($host) && $host != ""');
1097  assert('is_string($username) && $username != ""');
1098  assert('is_string($password)');
1099  assert('is_string($databaseName) && $databaseName != ""');
1100  assert('is_string($restoreFilePath) && $restoreFilePath != ""');
1101 
1102  if ($databaseType == 'mysql')
1103  {
1104  $result = exec("mysql --host=$host --user=$username --password=$password --port=$port $databaseName < $restoreFilePath", $output, $returnVal); // Not Coding Standard
1105  if ($returnVal !== 0)
1106  {
1107  return false;
1108  }
1109  else
1110  {
1111  return true;
1112  }
1113  return $result;
1114  }
1115  else
1116  {
1117  throw new NotSupportedException();
1118  }
1119  }
1120 
1127  public static function mapHintTypeIntoDatabaseColumnType($hintType, $length = null)
1128  {
1129  $databaseColumnType = '';
1130  if (RedBeanDatabase::getDatabaseType() == 'mysql')
1131  {
1132  if (in_array($hintType, array('tinyint', 'smallint', 'mediumint', 'bigint')))
1133  {
1134  $databaseColumnType = strtoupper($hintType) . '(11)';
1135  }
1136  else
1137  {
1138  switch ($hintType)
1139  {
1140  case 'blob':
1141  $databaseColumnType = "BLOB";
1142  break;
1143  case 'longblob':
1144  $databaseColumnType = "LONGBLOB";
1145  break;
1146  case 'boolean':
1147  $databaseColumnType = "TINYINT(1) UNSIGNED";
1148  break;
1149  case 'date':
1150  $databaseColumnType = "DATE";
1151  break;
1152  case 'datetime':
1153  $databaseColumnType = "DATETIME";
1154  break;
1155  case 'string':
1156  if (!isset($length))
1157  {
1158  $length = 255;
1159  }
1160  if ($length < 0 || $length > 255)
1161  {
1162  break;
1163  }
1164  $databaseColumnType = "VARCHAR($length)";
1165  break;
1166  case 'text':
1167  $databaseColumnType = "TEXT";
1168  break;
1169  case 'longtext':
1170  $databaseColumnType = "LONGTEXT";
1171  break;
1172  case 'id':
1173  $databaseColumnType = "INT(11) UNSIGNED";
1174  break;
1175  case 'integer':
1176  $databaseColumnType = "INT(11)";
1177  break;
1178  case 'float':
1179  $databaseColumnType = "DOUBLE";
1180  break;
1181  case 'time':
1182  $databaseColumnType = "TIME";
1183  break;
1184  }
1185  }
1186  }
1187  if ($databaseColumnType == '')
1188  {
1189  throw new NotSupportedException();
1190  }
1191  return $databaseColumnType;
1192  }
1193 
1194  public function getCharLimits()
1195  {
1196  }
1197 
1198  public static function makeTimeZoneAdjustmentContent()
1199  {
1200  //todo: move into something that is a wrapper since we can't always know which user we should adjust timezone for.
1201  $timeZoneObject = new DateTimeZone(Yii::app()->user->userModel->timeZone);
1202  $offsetInSeconds = $timeZoneObject->getOffset(new DateTime());
1203  if ($offsetInSeconds > 0)
1204  {
1205  $content = ' + ';
1206  }
1207  elseif ($offsetInSeconds < 0)
1208  {
1209  $content = ' - ';
1210  }
1211  else
1212  {
1213  return;
1214  }
1215  return $content . 'INTERVAL ' . abs($offsetInSeconds) . ' SECOND';
1216  }
1217 
1218  public static function resolveIntegerMaxAllowedValuesByType($signed = false)
1219  {
1220  if (RedBeanDatabase::getDatabaseType() == 'mysql')
1221  {
1222  $intMaxValuesAllows = array('tinyint' => pow(2, 8),
1223  'smallint' => pow(2, 16),
1224  'mediumint' => pow(2, 24),
1225  'integer' => pow(2, 32),
1226  'bigint' => pow(2, 64));
1227  if ($signed)
1228  {
1229  $intMaxValuesAllows = array('tinyint' => pow(2, 7),
1230  'smallint' => pow(2, 15),
1231  'mediumint' => pow(2, 23),
1232  'integer' => pow(2, 31),
1233  'bigint' => pow(2, 63));
1234  }
1235  return $intMaxValuesAllows;
1236  }
1237  throw new NotSupportedException();
1238  }
1239 
1240  public static function resolveCollationByHintType($hint)
1241  {
1242  if (RedBeanDatabase::getDatabaseType() == 'mysql')
1243  {
1244  if (in_array($hint, array('string', 'text', 'longtext', 'email', 'url')))
1245  {
1246  return 'COLLATE utf8_unicode_ci';
1247  }
1248  return null;
1249  }
1250  throw new NotSupportedException();
1251  }
1252 
1253  public static function resolveUnsignedByHintType($hint, $assumeSigned = false, $hintName = null)
1254  {
1255  if (RedBeanDatabase::getDatabaseType() == 'mysql')
1256  {
1257  $integerHintTypes = array_keys(static::resolveIntegerMaxAllowedValuesByType());
1258  if (in_array($hint, $integerHintTypes) && (!$assumeSigned || StringUtil::endsWith($hintName, '_id')))
1259  {
1260  return "UNSIGNED";
1261  }
1262  return null;
1263  }
1264  throw new NotSupportedException();
1265  }
1266  }
1267 ?>
static bulkInsert($tableName, &$rowsOfColumnValues, &$columnNames, $bulkQuantity, $shouldTrim=false)
static getDatabaseMaxAllowedPacketsSize($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static backupDatabase($databaseType= 'mysql', $host, $username, $password, $port, $databaseName, $backupFilePath)
static mapHintTypeIntoDatabaseColumnType($hintType, $length=null)
static endsWith($haystack, $needle)
Definition: StringUtil.php:187
static getOperatorByType($operatorType)
static getDatabaseLogBinTrustFunctionCreatorsValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseLogBinValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static createDatabase($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName)
static isValidOperatorTypeByValue($operatorType, $value)
static resolveValueLeftSideLikePartByOperatorType($operatorType)
static getDatabaseThreadStackValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseOptimizerSearchDepthValue($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static isDatabaseStrictMode($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseMaxSpRecursionDepth($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getDatabaseDefaultPort($databaseType= 'mysql')
static checkDatabaseConnection($databaseType, $host, $rootUsername, $rootPassword, $port)
static checkDatabaseUserExists($databaseType, $host, $rootUsername, $rootPassword, $port, $username)
static getDatabaseDefaultCollation($databaseType, $databaseHostname, $databaseName, $databaseUsername, $databasePassword, $databasePort)
static createDatabaseUser($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName, $username, $password)
static getDatabaseVersion($databaseType, $databaseHostname, $databaseUsername, $databasePassword, $databasePort)
static getOperatorAndValueWherePart($operatorType, $value)
static checkDatabaseExists($databaseType, $host, $rootUsername, $rootPassword, $port, $databaseName)
static resolveValueRightSideLikePartByOperatorType($operatorType)
Generated on Mon Jul 13 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.