SqlTools.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace KarmaFW\Database\Sql;
  3. class SqlTools
  4. {
  5. protected $db;
  6. public function __construct($db)
  7. {
  8. $this->db = $db;
  9. }
  10. public function parseDSN($dsn)
  11. {
  12. // PARSE A DSN LIKE mysql://user:password@host:port/database
  13. // AND RETURNS driver,host,port,user,passwd,db
  14. if (empty($dsn)) {
  15. return [
  16. 'driver' => '',
  17. 'host' => '',
  18. 'port' => '',
  19. 'user' => '',
  20. 'passwd' => '',
  21. 'db' => '',
  22. ];
  23. }
  24. $parts0 = explode(':', $dsn);
  25. $driver = $parts0[0];
  26. $parts1 = explode('/', $dsn);
  27. $user_passwd_host_port = $parts1[2];
  28. $db = ! empty($parts1[3]) ? $parts1[3] : null;
  29. $parts2 = explode('@', $user_passwd_host_port);
  30. if (count($parts2) > 1) {
  31. // USER (AND OPTIONNALY PASSWORD) IS DEFINED
  32. // mysql://user@host/database
  33. // mysql://user@host:port/database
  34. // mysql://user:password@host/database
  35. // mysql://user:password@host:port/database
  36. $user_password = $parts2[0];
  37. $host_port = $parts2[1];
  38. } else {
  39. // USER AND PASSWORD ARE NOT DEFINED
  40. // mysql://host/database
  41. // mysql://host:port/database
  42. $user_password = '';
  43. $host_port = $parts2[0];
  44. }
  45. $parts3 = explode(':', $host_port);
  46. $host = $parts3[0];
  47. if (count($parts3) > 1) {
  48. // HOST AND PORT ARE DEFINED
  49. // mysql://user@host:port/database
  50. // mysql://user:password@host:port/database
  51. $port = $parts3[1];
  52. } else {
  53. // HOST IS DEFINED. PORT IS NOT DEFINED
  54. // mysql://user@host/database
  55. // mysql://user:password@host/database
  56. $port = 3306;
  57. }
  58. $parts4 = explode(':', $user_password);
  59. $user = $parts4[0];
  60. if (count($parts4) > 1) {
  61. // USER AND PASSWORD ARE DEFINED
  62. // mysql://user:password@host/database
  63. // mysql://user:password@host:port/database
  64. $passwd = $parts4[1];
  65. } else {
  66. // USER IS DEFINED. PASSWORD IS NOT DEFINED
  67. // mysql://user@host/database
  68. // mysql://user@host:port/database
  69. $passwd = '';
  70. }
  71. return [
  72. 'driver' => $driver,
  73. 'host' => $host,
  74. 'port' => $port,
  75. 'user' => $user,
  76. 'passwd' => $passwd,
  77. 'db' => $db,
  78. ];
  79. }
  80. public function escape($var)
  81. {
  82. if (is_null($var)) {
  83. return 'NULL';
  84. }
  85. if (is_bool($var)) {
  86. return intval($var);
  87. }
  88. if (is_int($var)) {
  89. return intval($var);
  90. }
  91. if (is_float($var)) {
  92. return floatval($var);
  93. }
  94. return "'" . $this->db->getDriver()->getConn()->real_escape_string($var) . "'";
  95. }
  96. public function buildSqlWhere($where)
  97. {
  98. $where_sql = array("1" => "1");
  99. if (! empty($where)) {
  100. foreach ($where as $key => $value) {
  101. if (is_null($value)) {
  102. $where_sql[] = $key . ' is null';
  103. }else if (is_bool($value) || is_int($value)) {
  104. $where_sql[] = $key . ' = ' . intval($value);
  105. }else if (is_float($value)) {
  106. $where_sql[] = $key . ' = ' . floatval($value);
  107. }else if (is_string($value)) {
  108. $where_sql[] = $key . ' = ' . $this->escape($value);
  109. }else if ($value instanceof \KarmaFW\Database\Sql\SqlLike) {
  110. $where_sql[] = $key . ' like ' . (string) $this->escape($value);
  111. }else if ($value instanceof \KarmaFW\Database\Sql\SqlIn) {
  112. $value = (string) $value;
  113. if (empty($value)) {
  114. $where_sql[] = '0';
  115. } else {
  116. $where_sql[] = $key . ' in (' . $value . ')';
  117. }
  118. }else if ($value instanceof \KarmaFW\Database\Sql\SqlExpr) {
  119. $where_sql[] = $key . ' = ' . (string) $value;
  120. }else if ($value instanceof \KarmaFW\Database\Sql\SqlWhere) {
  121. $where_sql[] = (string) $value;
  122. }else{
  123. $where_sql[] = $key . ' = ' . $this->escape($value);
  124. //$where_sql[] = $key . ' = ' . (string) $value;
  125. }
  126. }
  127. }
  128. //pre($where_sql, 1);
  129. return implode(" and ", $where_sql);
  130. }
  131. public function buildSqlUpdateValues($values)
  132. {
  133. $values_sql = array();
  134. if (is_object($values)) {
  135. $values = get_object_vars($values);
  136. }
  137. foreach ($values as $key => $value) {
  138. if (is_null($value)) {
  139. $values_sql[] = $key . ' = NULL';
  140. }else if (gettype($value) === 'string') {
  141. $values_sql[] = $key . ' = ' . $this->escape($value);
  142. }else if (gettype($value) === 'boolean') {
  143. $values_sql[] = $key . ' = ' . intval($value);
  144. }else{
  145. $values_sql[] = $key . ' = ' . $value;
  146. }
  147. }
  148. return implode(", ", $values_sql);
  149. }
  150. public function buildSqlInsertValues($values)
  151. {
  152. $fields_sql = array();
  153. $values_sql = array();
  154. if (is_object($values)) {
  155. $values = get_object_vars($values);
  156. }
  157. foreach ($values as $key => $value) {
  158. if (is_null($value)) {
  159. $values_sql[] = 'NULL';
  160. }else if (gettype($value) === 'string') {
  161. $values_sql[] = $this->escape($value);
  162. }else if (gettype($value) === 'boolean') {
  163. $values_sql[] = intval($value);
  164. }else{
  165. $values_sql[] = $value;
  166. }
  167. $fields_sql[] = $key;
  168. }
  169. return array(
  170. 'fields' => implode(', ', $fields_sql),
  171. 'values' => implode(', ', $values_sql),
  172. );
  173. }
  174. }