Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Kram_Sql_Mysql extends Kram_Object implements IKram_Sql_Module
- {
- private $hostname;
- private $username;
- private $password;
- private $database;
- private $charset;
- /** @var resource, last query resource */
- private $result;
- /** @var array $connect_resource, all resources */
- static private $connect_resource = array();
- /**
- * Set conf
- * @param string $hostname
- * @param string $username
- * @param string $password
- * @param string $database
- * @param string $charset
- * @return Kram_Sql_Mysql
- */
- function Kram_Sql_Mysql($hostname, $username, $password, $database, $charset)
- {
- $this->hostname = $hostname;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $this->charset = $charset;
- }
- public function query($pattern, $args = array())
- {
- $query = $this->executePattern($pattern, $args);
- $result = mysql_query($query, $this->getResourceConnection());
- if ($result === false)
- {
- throw new Kram_Exception("Query cannot be execute: $query. Error: " .
- mysql_error($this->getResourceConnection()));
- }
- $this->result = $result;
- return $this->result;
- }
- public function insert($arr, $table = null)
- {
- $keys = array();
- $values = array();
- if (is_array($arr) && count($arr) > 0)
- {
- foreach ($arr as $column => $value)
- {
- $keys[] = $column;
- $values[] = $this->safeData($value);
- }
- }
- $pattern = "INSERT INTO $table (" . implode(',', $keys) . ") VALUES (" . implode(',', $values) . ")";
- $this->query($pattern);
- return $this->isError();
- }
- public function delete($table = null, $where = null, $args = array())
- {
- $pattern = "DELETE FROM $table ";
- if ($where !== null)
- {
- $where = $this->executePattern($where, $args);
- $pattern .= "WHERE $where";
- }
- $this->query($pattern);
- return $this->isError();
- }
- public function update($data, $table = null, $where = null, $args = array())
- {
- $pattern = null;
- $set = null;
- if (is_array($data) && count($data) > 0)
- {
- $i = 0;
- foreach ($data as $column => $value)
- {
- $set .= "$column = " . $this->safeData($value);
- if ($i < count($data)-1)
- {
- $set .= ",";
- }
- $i++;
- }
- }
- $where = $this->executePattern($where, $args);
- $pattern = "UPDATE $table SET $set WHERE $where";
- $this->query($pattern);
- return $this->isError();
- }
- public function fetchAll($res = null)
- {
- $arr = false;
- if ($res === null)
- {
- $res = $this->result;
- }
- if ($this->numRows($res) > 0)
- {
- $arr = array();
- $i = 0;
- while ($row = mysql_fetch_array($res))
- {
- foreach ($row as $column => $value)
- {
- if (!is_numeric($column))
- {
- $arr[$i][$column] = $this->changeType($value);
- }
- }
- $i++;
- }
- }
- return $arr;
- }
- public function fetchRow($row = 0, $col = null, $res = null)
- {
- if ($res === null)
- {
- $res = $this->result;
- }
- $data = $this->changeType(mysql_result($res, $row, $col));
- return $data;
- }
- public function numRows($res = null)
- {
- if ($res === null)
- {
- $res = $this->result;
- }
- return mysql_num_rows($res);
- }
- public function affectedNumRows()
- {
- return mysql_affected_rows($this->getResourceConnection());
- }
- public function insertID()
- {
- return mysql_insert_id($this->getResourceConnection());
- }
- /**
- * is connected to MySQL DB
- * @return boolean
- */
- private function isConnect()
- {
- if (!isset(self::$connect_resource[$this->getAlias()]))
- {
- return false;
- }
- return true;
- }
- /**
- * Connect to SQL server
- * @return void
- */
- private function connect()
- {
- $conn = mysql_connect($this->hostname,
- $this->username,
- $this->password);
- if (!is_resource($conn))
- {
- throw new Kram_Exception("Connection to SQL server not be a success.");
- }
- else if (!mysql_select_db($this->database, $conn))
- {
- throw new Kram_Exception("Database does not exist.");
- }
- else
- {
- self::$connect_resource[$this->getAlias()] = $conn;
- $this->query("SET NAMES $this->charset");
- }
- }
- /**
- * Get alias by setted hostname and database name
- * @return string
- */
- private function getAlias()
- {
- return $this->hostname . ':' . $this->database;
- }
- /**
- * Get actual resource connection
- * @return resource
- */
- private function getResourceConnection()
- {
- if (!$this->isConnect())
- {
- $this->connect();
- }
- return self::$connect_resource[$this->getAlias()];
- }
- /**
- * Safe data for MySQL
- * @param string|int $mixed
- * @return safe string
- */
- private function safeData($mixed)
- {
- if (is_numeric($mixed))
- {
- return intval($mixed);
- }
- return "'" . mysql_real_escape_string($mixed, $this->getResourceConnection()) . "'";
- }
- private function executePattern($pattern, $args = array())
- {
- $union = key($args);
- $newPattern = null;
- if (strlen($pattern) > 0 && strpos($pattern, '?') !== false && is_array($args) && count($args) > 0)
- {
- for ($i = 0; $i < strlen($pattern); $i++)
- {
- if ($pattern[$i] == '?')
- {
- $newPattern .= $this->safeData($args[$union]);
- $union++;
- }
- else
- {
- $newPattern .= $pattern[$i];
- }
- }
- }
- else
- {
- $newPattern = $pattern;
- }
- return $newPattern;
- }
- private function isError()
- {
- if ($this->affectedNumRows() <= 0)
- {
- return false;
- }
- return true;
- }
- private function changeType($mixed)
- {
- if (is_numeric($mixed))
- {
- $mixed = (int) $mixed;
- }
- return $mixed;
- }
- }
Add Comment
Please, Sign In to add comment