Beispiel #1
0
 /**
  * @return bool
  */
 public function getDebug()
 {
     return AnyDatasetContext::getInstance()->getDebug();
 }
 /**
  * The connection string must be defined in the config file 'config/anydataset.php'
  * 
  * @param string $dbname
  * @throws DatabaseException
  * @throws InvalidArgumentException
  */
 public function __construct($dbname)
 {
     $config = ['url' => $dbname];
     if (!preg_match('~^(\\w+)://~', $dbname)) {
         $config = AnyDatasetContext::getInstance()->getConnectionString($dbname);
     }
     $this->setDbConnectionString($config['url']);
     $patDriver = "(?P<driver>[\\w\\.]+)\\:\\/\\/";
     $patCredentials = "(?:((?P<username>\\S+):(?P<password>\\S+)|(?P<username2>\\S+))@)?";
     $patHost = "(?P<host>[\\w\\-\\.,_]+)(?::(?P<port>\\d+))?";
     $patDatabase = "(\\/(?P<database>[\\w\\-\\.]+))?";
     $patExtra = "(?:\\?(?P<extraparam>(?:[\\w\\-\\.]+=[\\w\\-%\\.\\/]+&?)*))?";
     $patFile = "(?P<path>(?:\\w\\:)?\\/(?:[\\w\\-\\.]+\\/?)+)?";
     // Try to parse the connection string.
     $pat = "/{$patDriver}({$patCredentials}{$patHost}{$patDatabase}|{$patFile}){$patExtra}/i";
     $parts = array();
     if (!preg_match($pat, $this->getDbConnectionString(), $parts)) {
         throw new InvalidArgumentException("Connection string " . $this->getDbConnectionString() . " is invalid! Please fix it.");
     }
     // Set the Driver
     $this->setDriver($parts['driver']);
     if (!isset($parts['path']) && !isset($parts['host'])) {
         throw new InvalidArgumentException("Connection string " . $this->getDbConnectionString() . " is invalid! Please fix it.");
     }
     // If a path pattern was found set it; otherwise define the database properties
     if (array_key_exists('path', $parts) && !empty($parts['path'])) {
         $this->setFilePath($parts['path']);
     } else {
         $this->setUsername(empty($parts['username']) ? $parts['username2'] : $parts['username']);
         $this->setPassword(isset($parts['password']) ? $parts['password'] : '');
         $this->setServer($parts['host']);
         $this->setPort(isset($parts['port']) ? $parts['port'] : '');
         $this->setDatabase(isset($parts['database']) ? $parts['database'] : '');
     }
     // If extra param is defined, set it.
     if (array_key_exists('extraparam', $parts) && !empty($parts['extraparam'])) {
         $arrAux = explode('&', $parts['extraparam']);
         foreach ($arrAux as $item) {
             $aux = explode("=", $item);
             $this->addExtraParam($aux[0], $aux[1]);
         }
     }
 }