当前位置: 首页>>代码示例>>PHP>>正文


PHP OC_DB::connection方法代码示例

本文整理汇总了PHP中OC_DB::connection方法的典型用法代码示例。如果您正苦于以下问题:PHP OC_DB::connection方法的具体用法?PHP OC_DB::connection怎么用?PHP OC_DB::connection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OC_DB的用法示例。


在下文中一共展示了OC_DB::connection方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: connect

 /**
  * connects to the database
  * @return boolean|null true if connection can be established or false on error
  *
  * Connects to the database as specified in config.php
  */
 public static function connect()
 {
     if (self::$connection) {
         return true;
     }
     $type = OC_Config::getValue('dbtype', 'sqlite');
     $factory = new \OC\DB\ConnectionFactory();
     if (!$factory->isValidType($type)) {
         return false;
     }
     $connectionParams = array('user' => OC_Config::getValue('dbuser', ''), 'password' => OC_Config::getValue('dbpassword', ''));
     $name = OC_Config::getValue('dbname', 'owncloud');
     if ($factory->normalizeType($type) === 'sqlite3') {
         $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT . '/data');
         $connectionParams['path'] = $datadir . '/' . $name . '.db';
     } else {
         $host = OC_Config::getValue('dbhost', '');
         if (strpos($host, ':')) {
             // Host variable may carry a port or socket.
             list($host, $portOrSocket) = explode(':', $host, 2);
             if (ctype_digit($portOrSocket)) {
                 $connectionParams['port'] = $portOrSocket;
             } else {
                 $connectionParams['unix_socket'] = $portOrSocket;
             }
         }
         $connectionParams['host'] = $host;
         $connectionParams['dbname'] = $name;
     }
     $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_');
     try {
         self::$connection = $factory->getConnection($type, $connectionParams);
     } catch (\Doctrine\DBAL\DBALException $e) {
         OC_Log::write('core', $e->getMessage(), OC_Log::FATAL);
         OC_User::setUserId(null);
         // send http status 503
         header('HTTP/1.1 503 Service Temporarily Unavailable');
         header('Status: 503 Service Temporarily Unavailable');
         OC_Template::printErrorPage('Failed to connect to database');
         die;
     }
     return true;
 }
开发者ID:Combustible,项目名称:core,代码行数:49,代码来源:db.php

示例2: disconnect

 /**
  * @brief Disconnect
  * @returns true/false
  *
  * This is good bye, good bye, yeah!
  */
 public static function disconnect()
 {
     // Cut connection if required
     if (self::$connection) {
         if (self::$backend == self::BACKEND_MDB2) {
             self::$connection->disconnect();
         }
         self::$connection = false;
         self::$MDB2 = false;
         self::$PDO = false;
     }
     return true;
 }
开发者ID:noci2012,项目名称:owncloud,代码行数:19,代码来源:db.php

示例3: connect

 /**
  * @brief connects to the database
  * @return bool true if connection can be established or false on error
  *
  * Connects to the database as specified in config.php
  */
 public static function connect()
 {
     if (self::$connection) {
         return true;
     }
     // The global data we need
     $name = OC_Config::getValue("dbname", "owncloud");
     $host = OC_Config::getValue("dbhost", "");
     $user = OC_Config::getValue("dbuser", "");
     $pass = OC_Config::getValue("dbpassword", "");
     $type = OC_Config::getValue("dbtype", "sqlite");
     if (strpos($host, ':')) {
         list($host, $port) = explode(':', $host, 2);
     } else {
         $port = false;
     }
     // do nothing if the connection already has been established
     if (!self::$connection) {
         $config = new \Doctrine\DBAL\Configuration();
         $eventManager = new \Doctrine\Common\EventManager();
         switch ($type) {
             case 'sqlite':
             case 'sqlite3':
                 $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT . '/data');
                 $connectionParams = array('user' => $user, 'password' => $pass, 'path' => $datadir . '/' . $name . '.db', 'driver' => 'pdo_sqlite');
                 $connectionParams['adapter'] = '\\OC\\DB\\AdapterSqlite';
                 $connectionParams['wrapperClass'] = 'OC\\DB\\Connection';
                 break;
             case 'mysql':
                 $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'port' => $port, 'dbname' => $name, 'charset' => 'UTF8', 'driver' => 'pdo_mysql');
                 $connectionParams['adapter'] = '\\OC\\DB\\Adapter';
                 $connectionParams['wrapperClass'] = 'OC\\DB\\Connection';
                 // Send "SET NAMES utf8". Only required on PHP 5.3 below 5.3.6.
                 // See http://stackoverflow.com/questions/4361459/php-pdo-charset-set-names#4361485
                 $eventManager->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
                 break;
             case 'pgsql':
                 $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'port' => $port, 'dbname' => $name, 'driver' => 'pdo_pgsql');
                 $connectionParams['adapter'] = '\\OC\\DB\\AdapterPgSql';
                 $connectionParams['wrapperClass'] = 'OC\\DB\\Connection';
                 break;
             case 'oci':
                 $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'dbname' => $name, 'charset' => 'AL32UTF8', 'driver' => 'oci8');
                 if (!empty($port)) {
                     $connectionParams['port'] = $port;
                 }
                 $connectionParams['adapter'] = '\\OC\\DB\\AdapterOCI8';
                 $connectionParams['wrapperClass'] = 'OC\\DB\\OracleConnection';
                 $eventManager->addEventSubscriber(new \Doctrine\DBAL\Event\Listeners\OracleSessionInit());
                 break;
             case 'mssql':
                 $connectionParams = array('user' => $user, 'password' => $pass, 'host' => $host, 'port' => $port, 'dbname' => $name, 'charset' => 'UTF8', 'driver' => 'pdo_sqlsrv');
                 $connectionParams['adapter'] = '\\OC\\DB\\AdapterSQLSrv';
                 $connectionParams['wrapperClass'] = 'OC\\DB\\Connection';
                 break;
             default:
                 return false;
         }
         $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_');
         try {
             self::$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config, $eventManager);
             if ($type === 'sqlite' || $type === 'sqlite3') {
                 // Sqlite doesn't handle query caching and schema changes
                 // TODO: find a better way to handle this
                 self::$connection->disableQueryStatementCaching();
             }
         } catch (\Doctrine\DBAL\DBALException $e) {
             OC_Log::write('core', $e->getMessage(), OC_Log::FATAL);
             OC_User::setUserId(null);
             // send http status 503
             header('HTTP/1.1 503 Service Temporarily Unavailable');
             header('Status: 503 Service Temporarily Unavailable');
             OC_Template::printErrorPage('Failed to connect to database');
             die;
         }
     }
     return true;
 }
开发者ID:omusico,项目名称:isle-web-framework,代码行数:84,代码来源:db.php


注:本文中的OC_DB::connection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。