Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. class SQL extends PDO {
  2.  
  3.     private static $instance = null;
  4.     private $dsn;
  5.     private $userName;
  6.     private $password;
  7.     private $options;
  8.     private $serverName;
  9.     private $returnType;
  10.     private $tablePrefix;
  11.     private $charSet;
  12.     private static $command;
  13.     private $data;
  14.     private $fetchMode;
  15.  
  16.     private function __clone() {
  17.  
  18.     }
  19.  
  20.  
  21.     public static function getInstance($serverName = 'db', $fetchMode = PDO::FETCH_OBJ) {
  22.         if (!isset(self::$instance)) {
  23.             self::$instance = new SQL($serverName, $fetchMode);
  24.         }
  25.         return self::$instance;
  26.     }
  27.  
  28.     private function loadConfig() {
  29.         $configPath = 'server->' . $this->serverName . '_' . RUN_MODE . '->';
  30.         $this->dsn = Config::read($configPath . 'dsn');
  31.         $this->userName = Config::read($configPath . 'userName');
  32.         $this->password = Config::read($configPath . 'password');
  33.         $this->tablePrefix = Config::read($configPath . 'tablePrefix');
  34.         $this->charSet = Config::read($configPath . 'charSet');
  35.         $this->options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '" . $this->charSet . "'");
  36.     }
  37.  
  38.     public function __construct($serverName, $fetchMode) {
  39.         $this->serverName = $serverName;
  40.         $this->fetchMode = $fetchMode;
  41.  
  42.         $this->loadConfig();
  43.         parent::__construct($this->dsn, $this->userName, $this->password, $this->options);
  44.         parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  45.     }
  46.  
  47.     private function parseCommand() {
  48.         foreach ($this->data as $value) {
  49.             if (!is_numeric($value)) {
  50.                 $this->data[] = parent::quote($value);
  51.             }
  52.         }
  53.         $commandVars = '';
  54.         $commandValues = '';
  55.         if (preg_match_all(' /\{(.*?)}/', self::$command, $variables)) {
  56.  
  57.             $commandVars = $variables[0];
  58.             for ($i = 0; $i < count($variables[1]); $i++) {
  59.                 $commandValues [] = $this->tablePrefix . $variables[1][$i];
  60.             }
  61.         }
  62.         self::$command = vsprintf(str_replace($commandVars, $commandValues, self::$command), $this->data);
  63.         $return = explode(' ', self::$command);
  64.         $this->returnType = $return[0];
  65.     }
  66.  
  67.     public function execute() {
  68.  
  69.         $this->data = func_get_args();
  70.         self::$command = $this->data[0];
  71.         array_shift($this->data);
  72.         $this->parseCommand();
  73.  
  74.         if (strtolower($this->returnType) == 'select') {
  75.             $command = $this->prepare(self::$command);
  76.             $command->setFetchMode($this->fetchMode);
  77.             $command->execute();
  78.             self::$command = '';
  79.             return $command->fetchAll();
  80.         } else {
  81.             $command = false;
  82.             try {
  83.                 $command = $this->exec(self::$command);
  84.             } catch (PDOException $err) {
  85.                 // echo $err->getMessage();
  86.             }
  87.  
  88.             self::$command = '';
  89.             if ($command == false) {
  90.                 return false;
  91.             } else {
  92.                 return true;
  93.             }
  94.         }
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement