123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- namespace RTP\Module;
- class DatabaseModule
- {
- private static $instance;
- private static $db_con;
- private $db_history;
-
- private $last_result;
- private $last_sql;
-
- public static function getInstance()
- {
-
- if (!is_null(self::$instance))
- {
- return self::$instance;
- }
- else
- {
-
- return self::getNewInstance();
- }
- }
-
- public static function getNewInstance()
- {
- self::$instance = null;
- self::$instance = new self;
- return self::$instance;
- }
-
- protected function __construct()
- {
- self::connect();
- }
-
- function __destruct()
- {
- self::close();
- }
-
- private function connect()
- {
- $conInfo = DB_TYPE . ':host=' . DB_URL . ';port=' . DB_PORT . ';dbname=' . DB_NAME . ';charset=utf8';
-
- if (DB_PERSISTENT_CONNECTION)
- {
- $option = array(
- \PDO::MYSQL_ATTR_INIT_COMMAND => "set names 'utf8'",
- \PDO::ATTR_PERSISTENT => TRUE,
- \PDO::ATTR_EMULATE_PREPARES => FALSE,
- \PDO::ATTR_STRINGIFY_FETCHES => FALSE
- );
- }
- else
- {
- $option = array(
- \PDO::MYSQL_ATTR_INIT_COMMAND => "set names 'utf8'",
- \PDO::ATTR_EMULATE_PREPARES => FALSE,
- \PDO::ATTR_STRINGIFY_FETCHES => FALSE
- );
- }
-
- try
- {
- self::$db_con = new \PDO($conInfo, DB_USER, DB_PASSWORD, $option);
- }
- catch(\PDOException $e)
- {
- if (DEBUG)
- print_r($e -> getMessage());
- exit ;
- }
- }
-
- public function close()
- {
- self::$db_con = NULL;
- self::$instance = NULL;
- }
-
- public function execute($sql)
- {
- $this -> last_sql = $sql;
- $result = self::$db_con -> exec($sql);
- $this -> getError();
- return $result;
- }
-
- public function query($sql)
- {
- $this -> last_sql = $sql;
- $this -> db_history = self::$db_con -> query($sql);
- $this -> getError();
- $this -> last_result = $this -> db_history -> fetch(\PDO::FETCH_ASSOC);
- return $this -> last_result;
- }
-
- public function queryAll($sql)
- {
- $this -> last_sql = $sql;
- $this -> db_history = self::$db_con -> query($sql);
- $this -> getError();
- $this -> last_result = $this -> db_history -> fetchAll(\PDO::FETCH_ASSOC);
- return $this -> last_result;
- }
-
- public function prepareExecute($sql, $params = NULL)
- {
- $this -> last_sql = $sql;
- $this -> db_history = self::$db_con -> prepare($sql);
- $this -> getError();
- if (is_null($params))
- {
- $this -> db_history -> execute();
- }
- else
- {
- $this -> db_history -> execute($params);
- }
- $this -> getError();
- $this -> last_result = $this -> db_history -> fetch(\PDO::FETCH_ASSOC);
- return $this -> last_result;
- }
-
- public function prepareExecuteAll($sql, $params = NULL)
- {
- $this -> last_sql = $sql;
- $this -> db_history = self::$db_con -> prepare($sql);
- $this -> getError();
- if (is_null($params))
- {
- $this -> db_history -> execute();
- }
- else
- {
- $this -> db_history -> execute($params);
- }
- $this -> getError();
- $this -> last_result = $this -> db_history -> fetchAll(\PDO::FETCH_ASSOC);
- return $this -> last_result;
- }
-
- public function prepareRexecute($params)
- {
- $this -> db_history -> execute($params);
- $this -> getError();
- $this -> last_result = $this -> db_history -> fetch(\PDO::FETCH_ASSOC);
- return $this -> last_result;
- }
-
- public function prepareRexecuteAll($params)
- {
- $this -> db_history -> execute($params);
- $this -> getError();
- $this -> last_result = $this -> db_history -> fetchAll(\PDO::FETCH_ASSOC);
- return $this -> last_result;
- }
-
- public function getAffectRow()
- {
- if (is_null($this -> db_history))
- {
- return 0;
- }
- else
- {
- return $this -> db_history -> rowCount();
- }
- }
-
- public function getLastSQL()
- {
- return $this -> last_sql;
- }
-
- public function getLastInsertID()
- {
- return self::$db_con -> lastInsertId();
- }
-
- public function getError()
- {
- $result = self::$db_con -> errorInfo();
- if (DEBUG)
- {
- if ($result[0] != 00000)
- {
- $error = json_encode(self::$db_con -> errorInfo());
- throw new ExceptionModule(12000, "database error in:$error");
- }
- }
- else
- {
- if ($result[0] != 00000)
- {
- return FALSE;
- }
- else
- return TRUE;
- }
- }
-
- public function beginTransaction()
- {
- self::$db_con -> beginTransaction();
- }
-
- public function rollback()
- {
- self::$db_con -> rollback();
- }
-
- public function commit()
- {
- self::$db_con -> commit();
- }
- }
- ?>
|