Exemplo n.º 1
0
 /**
  * @param $dsn
  * @param string $fileName
  * @param null $prefix
  * @param bool $isQueryString
  * @param bool $dieOnErrors
  */
 public static function sourceSQLFile($dsn, $fileName, $prefix = NULL, $isQueryString = FALSE, $dieOnErrors = TRUE)
 {
     require_once 'DB.php';
     $db = DB::connect($dsn);
     if (PEAR::isError($db)) {
         die("Cannot open {$dsn}: " . $db->getMessage());
     }
     if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) {
         $db->query('SET SESSION sql_mode = STRICT_TRANS_TABLES');
     }
     if (!$isQueryString) {
         $string = $prefix . file_get_contents($fileName);
     } else {
         // use filename as query string
         $string = $prefix . $fileName;
     }
     // get rid of comments starting with # and --
     $string = preg_replace("/^#[^\n]*\$/m", "\n", $string);
     $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
     $queries = preg_split('/;\\s*$/m', $string);
     foreach ($queries as $query) {
         $query = trim($query);
         if (!empty($query)) {
             CRM_Core_Error::debug_query($query);
             $res =& $db->query($query);
             if (PEAR::isError($res)) {
                 if ($dieOnErrors) {
                     die("Cannot execute {$query}: " . $res->getMessage());
                 } else {
                     echo "Cannot execute {$query}: " . $res->getMessage() . "<p>";
                 }
             }
         }
     }
 }
 /**
  * sends query to database - this is the private one that must work
  *   - internal functions use this rather than $this->query()
  *
  * @param  string  $string
  * @access private
  * @return mixed none or PEAR_Error
  */
 function _query($string)
 {
     global $_DB_DATAOBJECT, $queries, $user;
     $this->_connect();
     CRM_Core_Error::debug_query($string);
     $DB =& $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
     $options =& $_DB_DATAOBJECT['CONFIG'];
     $_DB_driver = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver'];
     if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
         $this->debug($string, $log = "QUERY");
     }
     if (strtoupper($string) == 'BEGIN') {
         if ($_DB_driver == 'DB') {
             $DB->autoCommit(false);
         } else {
             $DB->beginTransaction();
         }
         // db backend adds begin anyway from now on..
         return true;
     }
     if (strtoupper($string) == 'COMMIT') {
         $res = $DB->commit();
         if ($_DB_driver == 'DB') {
             $DB->autoCommit(true);
         }
         return $res;
     }
     if (strtoupper($string) == 'ROLLBACK') {
         $DB->rollback();
         if ($_DB_driver == 'DB') {
             $DB->autoCommit(true);
         }
         return true;
     }
     if (!empty($options['debug_ignore_updates']) && strtolower(substr(trim($string), 0, 6)) != 'select' && strtolower(substr(trim($string), 0, 4)) != 'show' && strtolower(substr(trim($string), 0, 8)) != 'describe') {
         $this->debug('Disabling Update as you are in debug mode');
         return $this->raiseError("Disabling Update as you are in debug mode", null);
     }
     //if (@$_DB_DATAOBJECT['CONFIG']['debug'] > 1) {
     // this will only work when PEAR:DB supports it.
     //$this->debug($DB->getAll('explain ' .$string,DB_DATAOBJECT_FETCHMODE_ASSOC), $log="sql",2);
     //}
     // some sim
     $t = explode(' ', microtime());
     $_DB_DATAOBJECT['QUERYENDTIME'] = $time = $t[0] + $t[1];
     for ($tries = 0; $tries < 3; $tries++) {
         if ($_DB_driver == 'DB') {
             $result = $DB->query($string);
         } else {
             switch (strtolower(substr(trim($string), 0, 6))) {
                 case 'insert':
                 case 'update':
                 case 'delete':
                     $result = $DB->exec($string);
                     break;
                 default:
                     $result = $DB->query($string);
                     break;
             }
         }
         // see if we got a failure.. - try again a few times..
         if (!is_a($result, 'PEAR_Error')) {
             break;
         }
         if ($result->getCode() != -14) {
             // *DB_ERROR_NODBSELECTED
             break;
             // not a connection error..
         }
         sleep(1);
         // wait before retyring..
         $DB->connect($DB->dsn);
     }
     if (is_a($result, 'PEAR_Error')) {
         if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
             $this->debug($result->toString(), "Query Error", 1);
         }
         return $this->raiseError($result);
     }
     /* CRM-3225 */
     if (function_exists('variable_get') && variable_get('dev_query', 0)) {
         // this is for drupal devel module
         // If devel.module query logging is enabled, prepend a comment with the username and calling function
         // to the SQL string.
         $bt = debug_backtrace();
         // t() may not be available yet so we don't wrap 'Anonymous'
         $name = $user->uid ? $user->name : variable_get('anonymous', 'Anonymous');
         $query = $bt[3]['function'] . "\n/* " . $name . ' */ ' . str_replace("\n ", '', $string);
         list($usec, $sec) = explode(' ', microtime());
         $stop = (double) $usec + (double) $sec;
         $diff = $stop - $time;
         $queries[] = array($query, $diff);
     }
     if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
         $t = explode(' ', microtime());
         $_DB_DATAOBJECT['QUERYENDTIME'] = $t[0] + $t[1];
         $this->debug('QUERY DONE IN  ' . ($t[0] + $t[1] - $time) . " seconds", 'query', 1);
     }
     switch (strtolower(substr(trim($string), 0, 6))) {
         case 'insert':
         case 'update':
         case 'delete':
             if ($_DB_driver == 'DB') {
                 // pear DB specific
                 return $DB->affectedRows();
             }
             return $result;
     }
     if (is_object($result)) {
         // lets hope that copying the result object is OK!
         // fix notices in unit test.
         if (!CRM_Utils_Array::value('RESULTSEQ', $GLOBALS['_DB_DATAOBJECT'])) {
             $GLOBALS['_DB_DATAOBJECT']['RESULTSEQ'] = 1;
         }
         $_DB_resultid = $GLOBALS['_DB_DATAOBJECT']['RESULTSEQ']++;
         $_DB_DATAOBJECT['RESULTS'][$_DB_resultid] = $result;
         $this->_DB_resultid = $_DB_resultid;
     }
     $this->N = 0;
     if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) {
         $this->debug(serialize($result), 'RESULT', 5);
     }
     if (method_exists($result, 'numrows')) {
         if ($_DB_driver == 'DB') {
             $DB->expectError(DB_ERROR_UNSUPPORTED);
         } else {
             $DB->expectError(MDB2_ERROR_UNSUPPORTED);
         }
         $this->N = $result->numrows();
         if (is_a($this->N, 'PEAR_Error')) {
             $this->N = true;
         }
         $DB->popExpect();
     }
 }