/** * @see Idb::excute() * @param string $sql * @return \PDOStatement * @throws DBException */ public function excute($sql) { if ($this->link == null) { $this->connect(); } if (DB_ESCAPE) { $sql = addslashes($sql); } try { $result = $this->link->query($sql); } catch (PDOException $e) { $exception = new DBException("SQL错误:" . $e->getMessage()); $exception->setCode($e->getCode()); $exception->setQuery($sql); if (APP_DEBUG) { __print($sql); } throw $exception; } Debug::appendMessage($sql, 'sql'); //添加调试信息 return $result; }
/** * 执行一条SQL语句,不同类型的SQL语句发送到不同的服务器去执行。 * 1. 读的SQL语句发送到读服务器 * 2. 写入SQL语句发送到写服务器 * 3. 此方法将抛出异常 * @see \herosphp\db\interfaces\ICusterDB::query() * @throws DBException */ public function query($_query) { if ($this->isReadSQL($_query)) { /* 读取数据操作 */ $_db = $this->selectReadServer(); } else { /* 写入数据操作 */ $_db = $this->selectWriteServer(); } try { $_result = $_db->query($_query); } catch (PDOException $e) { $_exception = new DBException("SQL错误!" . $e->getMessage()); $_exception->setCode($e->getCode()); $_exception->setQuery($_query); __print($_exception); die; } if (APP_DEBUG) { Debug::appendMessage($_query, 'sql'); //添加调试信息 } return $_result; }
/** * @see \herosphp\db\interfaces\Idb::query() * @throws DBException */ public function query($_query) { if ($this->link == null) { $this->connect(); } try { $_result = $this->link->query($_query); } catch (PDOException $e) { $_exception = new DBException("SQL错误:" . $e->getMessage()); $_exception->setCode($e->getCode()); $_exception->setQuery($_query); if (APP_DEBUG) { __print($_exception); die; } } if (APP_DEBUG) { Debug::appendMessage($_query, 'sql'); //添加调试信息 } return $_result; }