Guest User

Untitled

a guest
May 27th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. <?php
  2. class Kram_Sql_Mysql extends Kram_Object implements IKram_Sql_Module
  3. {
  4. private $hostname;
  5. private $username;
  6. private $password;
  7. private $database;
  8. private $charset;
  9. /** @var resource, last query resource */
  10. private $result;
  11.  
  12. /** @var array $connect_resource, all resources */
  13. static private $connect_resource = array();
  14.  
  15. /**
  16. * Set conf
  17. * @param string $hostname
  18. * @param string $username
  19. * @param string $password
  20. * @param string $database
  21. * @param string $charset
  22. * @return Kram_Sql_Mysql
  23. */
  24. function Kram_Sql_Mysql($hostname, $username, $password, $database, $charset)
  25. {
  26. $this->hostname = $hostname;
  27. $this->username = $username;
  28. $this->password = $password;
  29. $this->database = $database;
  30. $this->charset = $charset;
  31. }
  32.  
  33. public function query($pattern, $args = array())
  34. {
  35. $query = $this->executePattern($pattern, $args);
  36. $result = mysql_query($query, $this->getResourceConnection());
  37. if ($result === false)
  38. {
  39. throw new Kram_Exception("Query cannot be execute: $query. Error: " .
  40. mysql_error($this->getResourceConnection()));
  41. }
  42.  
  43. $this->result = $result;
  44. return $this->result;
  45. }
  46.  
  47. public function insert($arr, $table = null)
  48. {
  49. $keys = array();
  50. $values = array();
  51.  
  52. if (is_array($arr) && count($arr) > 0)
  53. {
  54. foreach ($arr as $column => $value)
  55. {
  56. $keys[] = $column;
  57. $values[] = $this->safeData($value);
  58. }
  59. }
  60.  
  61. $pattern = "INSERT INTO $table (" . implode(',', $keys) . ") VALUES (" . implode(',', $values) . ")";
  62. $this->query($pattern);
  63.  
  64. return $this->isError();
  65. }
  66.  
  67. public function delete($table = null, $where = null, $args = array())
  68. {
  69. $pattern = "DELETE FROM $table ";
  70. if ($where !== null)
  71. {
  72. $where = $this->executePattern($where, $args);
  73. $pattern .= "WHERE $where";
  74. }
  75.  
  76. $this->query($pattern);
  77.  
  78. return $this->isError();
  79. }
  80.  
  81. public function update($data, $table = null, $where = null, $args = array())
  82. {
  83. $pattern = null;
  84. $set = null;
  85.  
  86. if (is_array($data) && count($data) > 0)
  87. {
  88. $i = 0;
  89. foreach ($data as $column => $value)
  90. {
  91. $set .= "$column = " . $this->safeData($value);
  92. if ($i < count($data)-1)
  93. {
  94. $set .= ",";
  95. }
  96. $i++;
  97. }
  98. }
  99.  
  100. $where = $this->executePattern($where, $args);
  101. $pattern = "UPDATE $table SET $set WHERE $where";
  102. $this->query($pattern);
  103.  
  104. return $this->isError();
  105. }
  106.  
  107. public function fetchAll($res = null)
  108. {
  109. $arr = false;
  110.  
  111. if ($res === null)
  112. {
  113. $res = $this->result;
  114. }
  115.  
  116. if ($this->numRows($res) > 0)
  117. {
  118. $arr = array();
  119. $i = 0;
  120. while ($row = mysql_fetch_array($res))
  121. {
  122. foreach ($row as $column => $value)
  123. {
  124. if (!is_numeric($column))
  125. {
  126. $arr[$i][$column] = $this->changeType($value);
  127. }
  128. }
  129. $i++;
  130. }
  131. }
  132.  
  133. return $arr;
  134. }
  135.  
  136. public function fetchRow($row = 0, $col = null, $res = null)
  137. {
  138. if ($res === null)
  139. {
  140. $res = $this->result;
  141. }
  142.  
  143. $data = $this->changeType(mysql_result($res, $row, $col));
  144.  
  145. return $data;
  146. }
  147.  
  148. public function numRows($res = null)
  149. {
  150. if ($res === null)
  151. {
  152. $res = $this->result;
  153. }
  154. return mysql_num_rows($res);
  155. }
  156.  
  157. public function affectedNumRows()
  158. {
  159. return mysql_affected_rows($this->getResourceConnection());
  160. }
  161.  
  162. public function insertID()
  163. {
  164. return mysql_insert_id($this->getResourceConnection());
  165. }
  166.  
  167. /**
  168. * is connected to MySQL DB
  169. * @return boolean
  170. */
  171. private function isConnect()
  172. {
  173. if (!isset(self::$connect_resource[$this->getAlias()]))
  174. {
  175. return false;
  176. }
  177.  
  178. return true;
  179. }
  180.  
  181. /**
  182. * Connect to SQL server
  183. * @return void
  184. */
  185. private function connect()
  186. {
  187. $conn = mysql_connect($this->hostname,
  188. $this->username,
  189. $this->password);
  190. if (!is_resource($conn))
  191. {
  192. throw new Kram_Exception("Connection to SQL server not be a success.");
  193. }
  194. else if (!mysql_select_db($this->database, $conn))
  195. {
  196. throw new Kram_Exception("Database does not exist.");
  197. }
  198. else
  199. {
  200. self::$connect_resource[$this->getAlias()] = $conn;
  201. $this->query("SET NAMES $this->charset");
  202. }
  203. }
  204.  
  205. /**
  206. * Get alias by setted hostname and database name
  207. * @return string
  208. */
  209. private function getAlias()
  210. {
  211. return $this->hostname . ':' . $this->database;
  212. }
  213.  
  214. /**
  215. * Get actual resource connection
  216. * @return resource
  217. */
  218. private function getResourceConnection()
  219. {
  220. if (!$this->isConnect())
  221. {
  222. $this->connect();
  223. }
  224.  
  225. return self::$connect_resource[$this->getAlias()];
  226. }
  227.  
  228. /**
  229. * Safe data for MySQL
  230. * @param string|int $mixed
  231. * @return safe string
  232. */
  233. private function safeData($mixed)
  234. {
  235. if (is_numeric($mixed))
  236. {
  237. return intval($mixed);
  238. }
  239.  
  240. return "'" . mysql_real_escape_string($mixed, $this->getResourceConnection()) . "'";
  241. }
  242.  
  243. private function executePattern($pattern, $args = array())
  244. {
  245. $union = key($args);
  246. $newPattern = null;
  247.  
  248. if (strlen($pattern) > 0 && strpos($pattern, '?') !== false && is_array($args) && count($args) > 0)
  249. {
  250. for ($i = 0; $i < strlen($pattern); $i++)
  251. {
  252.  
  253. if ($pattern[$i] == '?')
  254. {
  255. $newPattern .= $this->safeData($args[$union]);
  256. $union++;
  257. }
  258. else
  259. {
  260. $newPattern .= $pattern[$i];
  261. }
  262. }
  263. }
  264. else
  265. {
  266. $newPattern = $pattern;
  267. }
  268. return $newPattern;
  269. }
  270.  
  271. private function isError()
  272. {
  273. if ($this->affectedNumRows() <= 0)
  274. {
  275. return false;
  276. }
  277.  
  278. return true;
  279. }
  280.  
  281. private function changeType($mixed)
  282. {
  283. if (is_numeric($mixed))
  284. {
  285. $mixed = (int) $mixed;
  286. }
  287. return $mixed;
  288. }
  289. }
Add Comment
Please, Sign In to add comment