Exemplo n.º 1
0
 /**
  * 独立预处理
  *
  * @param string $sql
  * @param array $driverOptions
  *
  * @return Statement
  *
  * @throws SqlEmptyException
  */
 public function prepareQuery($sql, array $driverOptions = array())
 {
     //效验sql语句
     if (!$sql) {
         $error = array('code' => 1024, 'sql' => '', 'message' => 'sql语句为空,无法执行query操作!', 'connectionName' => $this->name);
         $this->app->logger()->sql($error);
         throw new SqlEmptyException('数据库查询错误。', 1024);
     }
     //处理表前缀
     $sql = preg_replace('/\\{:(\\S+?):\\}/', $this->config['prefix'] . '$1', $sql);
     //保存预处理sql
     $this->prepareSQL = $sql;
     return parent::prepare($sql, $driverOptions);
 }
Exemplo n.º 2
0
 /**
  * 尝试具体配置的连接
  *
  * @param array $config 连接配置
  * @param string $name 连接名称,留空表示测试连接
  *
  * @throws \PDOException
  *
  * @return \Monkey\Database\Connection|false
  */
 public function tryConnecting($config, $name = 'test')
 {
     //设置连接类名
     $class = ucfirst(strtolower($config['protocol']));
     $class = $class == 'Mysql' ? '' : '\\' . $class;
     //如果是Mysql驱动,直接使用父类,目的是获得更高的效率
     $class = __NAMESPACE__ . $class . '\\Connection';
     try {
         //创建连接对象
         $connect = new $class($this->app, $name, $config);
     } catch (\PDOException $e) {
         //处理连接错误,记录错误日志
         $error = array('error_title' => '连接到PDO时出错。', 'message' => $e->getMessage(), 'code' => $e->getCode());
         $this->app->logger()->sql($error);
         throw $e;
     }
     return $connect;
 }