Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SQL extends PDO {
- private static $instance = null;
- private $dsn;
- private $userName;
- private $password;
- private $options;
- private $serverName;
- private $returnType;
- private $tablePrefix;
- private $charSet;
- private static $command;
- private $data;
- private $fetchMode;
- private function __clone() {
- }
- public static function getInstance($serverName = 'db', $fetchMode = PDO::FETCH_OBJ) {
- if (!isset(self::$instance)) {
- self::$instance = new SQL($serverName, $fetchMode);
- }
- return self::$instance;
- }
- private function loadConfig() {
- $configPath = 'server->' . $this->serverName . '_' . RUN_MODE . '->';
- $this->dsn = Config::read($configPath . 'dsn');
- $this->userName = Config::read($configPath . 'userName');
- $this->password = Config::read($configPath . 'password');
- $this->tablePrefix = Config::read($configPath . 'tablePrefix');
- $this->charSet = Config::read($configPath . 'charSet');
- $this->options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '" . $this->charSet . "'");
- }
- public function __construct($serverName, $fetchMode) {
- $this->serverName = $serverName;
- $this->fetchMode = $fetchMode;
- $this->loadConfig();
- parent::__construct($this->dsn, $this->userName, $this->password, $this->options);
- parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- private function parseCommand() {
- foreach ($this->data as $value) {
- if (!is_numeric($value)) {
- $this->data[] = parent::quote($value);
- }
- }
- $commandVars = '';
- $commandValues = '';
- if (preg_match_all(' /\{(.*?)}/', self::$command, $variables)) {
- $commandVars = $variables[0];
- for ($i = 0; $i < count($variables[1]); $i++) {
- $commandValues [] = $this->tablePrefix . $variables[1][$i];
- }
- }
- self::$command = vsprintf(str_replace($commandVars, $commandValues, self::$command), $this->data);
- $return = explode(' ', self::$command);
- $this->returnType = $return[0];
- }
- public function execute() {
- $this->data = func_get_args();
- self::$command = $this->data[0];
- array_shift($this->data);
- $this->parseCommand();
- if (strtolower($this->returnType) == 'select') {
- $command = $this->prepare(self::$command);
- $command->setFetchMode($this->fetchMode);
- $command->execute();
- self::$command = '';
- return $command->fetchAll();
- } else {
- $command = false;
- try {
- $command = $this->exec(self::$command);
- } catch (PDOException $err) {
- // echo $err->getMessage();
- }
- self::$command = '';
- if ($command == false) {
- return false;
- } else {
- return true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement