Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> klasa sql
kayman
post
Post #1





Grupa: Zarejestrowani
Postów: 556
Pomógł: 40
Dołączył: 20.07.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


w zasadzie 2 klasy bo connect jest oddzielnie

nie staram się wynaleźć koła na nowo tylko uprościć sobie robotę (IMG:style_emoticons/default/smile.gif)

ale pewnie jest tu parę błędów więc oceniajcie

btw. PDO do mnie nie przemawia jakoś a może przyzwyczaiłem się do starego?

pozdrawiam

  1. <?php
  2.  
  3. class DBConnect {
  4.  
  5. private static $_instance;
  6. public static $db;
  7. private static $host = 'localhost';
  8. private static $user = 'root';
  9. private static $pass = '';
  10. private static $database = 'ukcrm';
  11.  
  12. private function __construct() {
  13. $this->connect();
  14. }
  15.  
  16. public function setConnect() {
  17. if (!isset(self::$_instance)) {
  18. self::$_instance = new DBConnect();
  19. }
  20. }
  21.  
  22. private static function connect() {
  23. self::$db = mysqli_connect(self::$host, self::$user, self::$pass, self::$database) or die('Connection fail');
  24. }
  25.  
  26. public function changeConnect($host, $user, $pass, $db) {
  27. if (isset(self::$_instance)) {
  28. mysqli_close(self::$db);
  29. self::$host = $host;
  30. self::$user = $user;
  31. self::$pass = $pass;
  32. self::$database = $db;
  33. self::connect();
  34. } else {
  35. self::$_instance = new DBConnect();
  36. self::ChangeConnect($host, $user, $pass, $db);
  37. }
  38. }
  39.  
  40. }
  41.  
  42. class sql {
  43.  
  44. private $table;
  45. private $params;
  46. private $where;
  47. private $columns;
  48. private $result;
  49. private $sql;
  50. private $order;
  51. private $orderMode = 'ASC';
  52. private $limit;
  53.  
  54. function __construct() {
  55. DBConnect::setConnect();
  56. $this->resetSql();
  57. }
  58.  
  59. public function resetSql() {
  60. $this->table = '';
  61. $this->params = array();
  62. $this->where = array();
  63. $this->result = array();
  64. $this->columns = array();
  65. $this->order = array();
  66. $this->sql = '';
  67. $this->limit = '';
  68. }
  69.  
  70. public function setTable($table) {
  71. $this->table = $table;
  72. }
  73.  
  74. public function setSqlString($string) {
  75. $this->sql = $string;
  76. }
  77.  
  78. public function crearteSQL($mode) {
  79. if ($mode == 'insert') {
  80. $this->sql = 'INSERT INTO ' . $this->table . ' SET ' . implode(', ', $this->params);
  81. }
  82. if ($mode == 'update') {
  83. $this->sql = 'UPDATE ' . $this->table . ' SET ' . implode(', ', $this->params) . ' WHERE ' . implode(' ', $this->where);
  84. }
  85. if ($mode == 'delete') {
  86. $this->sql = 'DELETE FROM ' . $this->table . ' WHERE ' . implode(' ', $this->where);
  87. }
  88. if ($mode == 'select') {
  89. $this->sql = 'SELECT ' . implode(', ', $this->columns) . ' FROM ' . $this->table;
  90. if (!empty($this->where))
  91. $this->sql .= ' WHERE ' . implode(' ', $this->where);
  92. if (!empty($this->order)) {
  93. $this->sql .= ' ORDER BY ' . implode(', ', $this->order) . ' ' . $this->orderMode;
  94. }
  95. if ($this->limit) {
  96. $this->sql .= ' LIMIT ' . $this->limit;
  97. }
  98. }
  99. }
  100.  
  101. public function setLimit($value) {
  102. $this->limit = $value;
  103. }
  104.  
  105. public function setOrderMode($value) {
  106. $this->orderMode = $value;
  107. }
  108.  
  109. public function addOrder($column) {
  110. $this->order[] = $column;
  111. }
  112.  
  113. public function addParam($name, $value, $filter) {
  114. if ($value) {
  115. switch ($filter) {
  116. case 'int':
  117. $value = $this->filterInt($value);
  118. break;
  119. case 'double':
  120. $value = $this->filterDouble($value);
  121. break;
  122. case 'text':
  123. $value = $this->filterText($value);
  124. break;
  125. case 'like':
  126. $value = $this->filterLike($value);
  127. break;
  128. }
  129. } else {
  130. $value = "NULL";
  131. if ($filter == 'double')
  132. $value = '0';
  133. }
  134. $this->params[] = $name . '=' . $value;
  135. }
  136.  
  137. public function addWhere($column, $value, $filter, $sign = '=') {
  138. if ($value) {
  139. switch ($filter) {
  140. case 'int':
  141. $value = $this->filterInt($value);
  142. break;
  143. case 'double':
  144. $value = $this->filterDouble($value);
  145. break;
  146. case 'text':
  147. $value = $this->filterText($value);
  148. break;
  149. case 'like':
  150. $value = $this->filterLike($value);
  151. $sign = ' LIKE ';
  152. break;
  153. }
  154. } else {
  155. $value = "NULL";
  156. }
  157. $this->where[] = $column . $sign . $value;
  158. }
  159.  
  160. public function addTextWhere($value) {
  161. $this->where[] = $value;
  162. }
  163.  
  164. public function addColumn($value) {
  165. $this->columns[] = $value;
  166. }
  167.  
  168. public function addColumns($val = array()) {
  169. if (!empty($val)) {
  170. foreach ($val as $value) {
  171. $this->addColumn($value);
  172. }
  173. }
  174. }
  175.  
  176. public function sqlQuery() {
  177. $this->result = mysqli_query(DBConnect::$db, $this->sql) or die(mysqli_error());
  178. }
  179.  
  180. public function getResult() {
  181. return $this->result;
  182. }
  183.  
  184. public function getRow() {
  185. return mysqli_fetch_assoc($this->result);
  186. }
  187.  
  188. public function numRows() {
  189. return mysqli_num_rows($this->result);
  190. }
  191.  
  192. public function testSQL() {
  193. echo $this->sql . '<br />';
  194. exit();
  195. }
  196.  
  197. private function filter($value, $type) {
  198. $value = mysqli_real_escape_string(DBConnect::$db, $value);
  199. switch ($type) {
  200. case "text":
  201. $value = ($value != "") ? "'" . $value . "'" : "NULL";
  202. break;
  203. case "int":
  204. $value = ($value != "") ? intval($value) : "NULL";
  205. break;
  206. case "double":
  207. $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";
  208. break;
  209. }
  210. return $value;
  211. }
  212.  
  213. public function filterInt($value) {
  214. return $this->filter($value, 'int');
  215. }
  216.  
  217. public function filterText($value) {
  218. return $this->filter($value, 'text');
  219. }
  220.  
  221. public function filterDouble($value) {
  222. $value = str_replace(',', '.', $value);
  223. return $this->filter($value, 'double');
  224. }
  225.  
  226. public function filterLike($value) {
  227. return $this->filter('%' . $value . '%', 'text');
  228. }
  229.  
  230. }
  231.  
  232. //przykładowe wywołanie
  233.  
  234. $sql = new sql();
  235. $sql->setTable('kkk k left join iii i on k.kkk_id = i.id');
  236. $cols = array('k.id', 'k.number', 'k.name', 'k.preson');
  237. $sql->addColumns($cols);
  238. $cols = array('i.id as invid', 'i.number as inumber', 'i.name as iname', 'i.preson as iperson');
  239. $sql->addColumns($cols);
  240. $sql->addWhere('k.id', $_GET['id'], 'int');
  241. $sql->crearteSQL('select');
  242. $sql->sqlQuery();
  243. if ($sql->numRows() == 1) {
  244. $row = $sql->getRow();
  245. }
  246.  
  247.  
  248. //inne wywolanie
  249.  
  250. if (isset($_POST['action'])) {
  251. $sql = new sql();
  252. $sql->setTable('anytable');
  253.  
  254. $sql->addParam('flue_sizing', $_POST['flue_sizing'], 'text');
  255. $sql->addParam('flue_route', $_POST['flue_route'], 'text');
  256. $sql->addParam('based_on_client_info', $_POST['based_on_client_info'], 'text');
  257. $sql->addParam('instalation_time1', $_POST['instalation_time1'], 'text');
  258. $sql->addParam('instalation_time1_combo', $_POST['instalation_time1_combo'], 'text');
  259. $sql->addParam('instalation_time2', $_POST['instalation_time2'], 'text');
  260. $sql->addParam('instalation_time2_combo', $_POST['instalation_time2_combo'], 'text');
  261. $sql->addParam('lead_time', $_POST['lead_time'], 'text');
  262. $sql->addParam('lead_time_combo', $_POST['lead_time_combo'], 'text');
  263.  
  264. $sql->addParam('zone', $_POST['zone'], 'text');
  265. $sql->addParam('terrain_category', $_POST['terrain_category'], 'text');
  266. $sql->addParam('basic_wind_speed', $_POST['basic_wind_speed'], 'text');
  267. $sql->addParam('reliability_level', $_POST['reliability_level'], 'text');
  268.  
  269. $sql->addParam('lorry_length', $_POST['lorry_length'], 'text');
  270. $sql->addParam('crane_radius_of_loading', $_POST['crane_radius_of_loading'], 'text');
  271. $sql->addParam('building_height', $_POST['building_height'], 'text');
  272. $sql->addParam('crane_capacity', $_POST['crane_capacity'], 'text');
  273. $sql->addParam('crane_radius_for_lifting', $_POST['crane_radius_for_lifting'], 'text');
  274.  
  275. $sql->addParam('nr_of_appliances', $_POST['nr_of_appliances'], 'text');
  276.  
  277. if ($_POST['action'] == 'edit') {
  278. $sql->addWhere('id', $_POST['id'], 'int');
  279. $sql->addParam('data_mod', date('Y-m-d'), 'text');
  280. $sql->crearteSQL('update');
  281. //$sql->testSQL();
  282. $sql->sqlQuery();
  283. }
  284. }
  285. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
com
post
Post #2





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

Ostrzeżenie: (0%)
-----


Cytat
Uważam, że takie klasy jak ta są bardzo ważne i potrzebne, ponieważ pomagają autorowi i klientom klasy ogarnąć podstawy obiektowości i podstawowe polecenia sqla.

Sam sobie przeczysz.. bo jak ktoś skorzysta z takiej "klasy" nie majac duzego pojecia itak nie pozna np sql bo wszystko ma opakowane i wywołuje to np jak tu
  1. $sql->addWhere('k.id', $_GET['id'], 'int');
  2. $sql->crearteSQL('select');

co mu tak naprawde nic nie mówi... Ja w pierwszej swojej wypowiedzi przeoczyłem to, na co zwrócił mi uwage !*! i miał rację, ponadto wymyślanie koła na nowo tylko dla satysfakcji, że działa jest bez sensu, w tym przypadku ponieważ tak jak powiedziano ta klasa nie ma niczego wiecej niż dostarcza nam PDO, a nawet dało by się wymienić szereg ograniczen, chociażby wspomniane przez mnie czy prez Ciebie inne silniki bazodanowe...

Cytat
To kim wy jesteście, że wiecie wszystko najlepiej?

Nikt nie wie wszystkiego tym bardziej najlepiej, źle zinterpretowałeś te słowa, chodziło o to że przychodzą tu osoby, które nie posiadają wiedzy, bo sie jescze uczą i próbują nam wmawiać, że cos nie działa albo działa zupełnie inaczej niż ma, a sami nawet czesto nie zajrzeli do manuala, tylko mówią bo im sie tak wydaje... i potem potrafia prowadzic dyskusje na jakis temat która itak kończy sie na tym że sie okazuje iż mieliśmy rację... Nie twierdze że nik nie ma prawa do pomyłek, ale jesli jest sie czegoś pewnym to trzeba miec na to niezbite dowody...

Najlepiej zakończmy te dyskusję bo powoli odbiega ona od tematu oceny..

Jedno jest pewne !*! masz napewno rację (IMG:style_emoticons/default/smile.gif)

Cytat
Wiesz jak masz 50000 razy napisać SELECT... no to lepiej raz


Tutaj faktycznie to jest tak jak powiedziałeś, choć można by zaimplementować to troche inaczej z zachowaniem elastyczności i troche w kierunku tego co powiedziałem, ale to już nie w tym temacie, dlatego Twoje argumenty są jak nabardziej słuszne jesli chodzi o klase zaprezetowaną w tym wątku i też sie z Tobą zgadzam (IMG:style_emoticons/default/smile.gif)

Ten post edytował com 12.11.2013, 23:33:02
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 07:12