SqlTools.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace KarmaFW\Database\Sql;
  3. // TODO: a transformer en une classe trait de SqlDb
  4. class SqlTools
  5. {
  6. protected $db;
  7. public function __construct($db)
  8. {
  9. $this->db = $db;
  10. }
  11. public function parseDSN($dsn)
  12. {
  13. // PARSE A DSN LIKE mysql://user:password@host:port/database
  14. // AND RETURNS driver,host,port,user,passwd,db
  15. if (empty($dsn)) {
  16. return [
  17. 'driver' => '',
  18. 'host' => '',
  19. 'port' => '',
  20. 'user' => '',
  21. 'passwd' => '',
  22. 'db' => '',
  23. ];
  24. }
  25. $parts0 = explode(':', $dsn);
  26. $driver = $parts0[0];
  27. $parts1 = explode('/', $dsn);
  28. $user_passwd_host_port = $parts1[2];
  29. $db = ! empty($parts1[3]) ? $parts1[3] : null;
  30. $parts2 = explode('@', $user_passwd_host_port);
  31. if (count($parts2) > 1) {
  32. // USER (AND OPTIONNALY PASSWORD) IS DEFINED
  33. // mysql://user@host/database
  34. // mysql://user@host:port/database
  35. // mysql://user:password@host/database
  36. // mysql://user:password@host:port/database
  37. $user_password = $parts2[0];
  38. $host_port = $parts2[1];
  39. } else {
  40. // USER AND PASSWORD ARE NOT DEFINED
  41. // mysql://host/database
  42. // mysql://host:port/database
  43. $user_password = '';
  44. $host_port = $parts2[0];
  45. }
  46. $parts3 = explode(':', $host_port);
  47. $host = $parts3[0];
  48. if (count($parts3) > 1) {
  49. // HOST AND PORT ARE DEFINED
  50. // mysql://user@host:port/database
  51. // mysql://user:password@host:port/database
  52. $port = $parts3[1];
  53. } else {
  54. // HOST IS DEFINED. PORT IS NOT DEFINED
  55. // mysql://user@host/database
  56. // mysql://user:password@host/database
  57. $port = 3306;
  58. }
  59. $parts4 = explode(':', $user_password);
  60. $user = $parts4[0];
  61. if (count($parts4) > 1) {
  62. // USER AND PASSWORD ARE DEFINED
  63. // mysql://user:password@host/database
  64. // mysql://user:password@host:port/database
  65. $passwd = $parts4[1];
  66. } else {
  67. // USER IS DEFINED. PASSWORD IS NOT DEFINED
  68. // mysql://user@host/database
  69. // mysql://user@host:port/database
  70. $passwd = '';
  71. }
  72. return [
  73. 'driver' => $driver,
  74. 'host' => $host,
  75. 'port' => $port,
  76. 'user' => $user,
  77. 'passwd' => $passwd,
  78. 'db' => $db,
  79. ];
  80. }
  81. public function escape($var)
  82. {
  83. if (! $this->db->isConnected()) {
  84. $this->db->connect();
  85. }
  86. if (is_null($var)) {
  87. return 'NULL';
  88. }
  89. if (is_bool($var)) {
  90. return intval($var);
  91. }
  92. if (is_int($var)) {
  93. return intval($var);
  94. }
  95. if (is_float($var)) {
  96. return floatval($var);
  97. }
  98. return "'" . $this->db->getDriver()->getConn()->real_escape_string($var) . "'";
  99. }
  100. public function buildSqlWhere($where)
  101. {
  102. $where_sql = array("1" => "1");
  103. if (! empty($where)) {
  104. foreach ($where as $key => $value) {
  105. if (is_null($value)) {
  106. $where_sql[] = $key . ' is null';
  107. }else if (is_bool($value) || is_int($value)) {
  108. $where_sql[] = $key . ' = ' . intval($value);
  109. }else if (is_float($value)) {
  110. $where_sql[] = $key . ' = ' . floatval($value);
  111. }else if (is_string($value)) {
  112. $where_sql[] = $key . ' = ' . $this->escape($value);
  113. }else if ($value instanceof \KarmaFW\Database\Sql\SqlLike) {
  114. $where_sql[] = $key . ' like ' . (string) $this->escape($value);
  115. }else if ($value instanceof \KarmaFW\Database\Sql\SqlIn) {
  116. $value = (string) $value;
  117. if (empty($value)) {
  118. $where_sql[] = '0';
  119. } else {
  120. $where_sql[] = $key . ' in (' . $value . ')';
  121. }
  122. }else if ($value instanceof \KarmaFW\Database\Sql\SqlExpr) {
  123. $where_sql[] = $key . ' = ' . (string) $value;
  124. }else if ($value instanceof \KarmaFW\Database\Sql\SqlWhere) {
  125. $where_sql[] = (string) $value;
  126. }else{
  127. //pre($where, 1);
  128. $where_sql[] = $key . ' = ' . $this->escape($value);
  129. //$where_sql[] = $key . ' = ' . (string) $value;
  130. }
  131. }
  132. }
  133. //pre($where_sql, 1);
  134. return implode(" and ", $where_sql);
  135. }
  136. public function buildSqlUpdateValues($values)
  137. {
  138. $values_sql = array();
  139. if (is_object($values)) {
  140. $values = get_object_vars($values);
  141. }
  142. foreach ($values as $key => $value) {
  143. if (is_null($value)) {
  144. $values_sql[] = $key . ' = NULL';
  145. }else if (gettype($value) === 'string') {
  146. $values_sql[] = $key . ' = ' . $this->escape($value);
  147. }else if (gettype($value) === 'boolean') {
  148. $values_sql[] = $key . ' = ' . intval($value);
  149. }else{
  150. $values_sql[] = $key . ' = ' . $value;
  151. }
  152. }
  153. return implode(", ", $values_sql);
  154. }
  155. public function buildSqlInsertValues($values)
  156. {
  157. $fields_sql = array();
  158. $values_sql = array();
  159. if (is_object($values)) {
  160. $values = get_object_vars($values);
  161. }
  162. foreach ($values as $key => $value) {
  163. if (is_null($value)) {
  164. $values_sql[] = 'NULL';
  165. }else if (gettype($value) === 'string') {
  166. $values_sql[] = $this->escape($value);
  167. }else if (gettype($value) === 'boolean') {
  168. $values_sql[] = intval($value);
  169. }else{
  170. $values_sql[] = $value;
  171. }
  172. $fields_sql[] = $key;
  173. }
  174. return array(
  175. 'fields' => implode(', ', $fields_sql),
  176. 'values' => implode(', ', $values_sql),
  177. );
  178. }
  179. public function buildSqlWhereSearch($q='', $search_fields=[], $min_str_length=1, $max_words=10, $all_words_required=false)
  180. {
  181. $db = $this->db;
  182. $select_sum = "(0";
  183. if ($all_words_required) {
  184. $search_where = "(1";
  185. } else {
  186. $search_where = "(0";
  187. }
  188. $q = trim($q);
  189. $pow_pos_word = 0.5; // correspond à l'index du mot parmi tous les mots de la recherche
  190. $pow_pos_field = 0.5; // correspond à l'index du champ (SQL) de recherche parmi tous les champs de recherche
  191. $pow_pos_match = 0.5; // correspond à la position strpos de la chaine recherchée trouvée dans un des champs de recherche
  192. $pow_length_field_value = 0.3; // correspond à la taille (du texte) de la valeur du champ dans lequel on a trouvé le match
  193. $coef_pos_word = 1;
  194. $coef_pos_field = 1;
  195. $coef_pos_match = 1;
  196. $coef_length_field_value = 1;
  197. if ($search_fields && strlen($q) >= $min_str_length) {
  198. $words = explode(" ", $q);
  199. foreach ($words as $word_idx => $word) {
  200. $word_idx_score = pow(max(1, 10 - $word_idx) * $coef_pos_word, $pow_pos_word); // au dela de 10 mots, on compte comme le 10e mot
  201. $w = $db->escape($word);
  202. $w2 = $db->escape("%" . $word . "%");
  203. $conditions_or = [];
  204. $select_sums = [0];
  205. foreach ($search_fields as $field_idx => $field) {
  206. $conditions_or[] = $field . " like " . $w2;
  207. $field_idx_score = pow(max(1, 10 - $field_idx) * $coef_pos_field, $pow_pos_field); // au dela de 10 fields, on compte comme le 10e field
  208. $select_sums[] = "( if( locate(" . $w . ", ifnull(" . $field . ",'') ) > 0, 1, 0 ) * pow(1 / length(" . $field . ") * " . $coef_length_field_value . ", " . floatval($pow_length_field_value) . ") * " . $word_idx_score . " * " . $field_idx_score . " * pow(greatest( 100 - locate(" . $w . ", ifnull(" . $field . ", '')), 1) * " . floatval($coef_pos_match) . ", " . floatval($pow_pos_match) . ") )";
  209. //$select_sums[] = "( if( locate(" . $w . ", ifnull(" . $field . ",'') ) > 0, 1, 0 ) * (1 / length(" . $field . ")) * " . $word_idx_score . " * " . $field_idx_score . " )";
  210. }
  211. $word_condition = "(" . implode(" or ", $conditions_or) . ")";
  212. if ($all_words_required) {
  213. // TODO
  214. $search_where .= " and (" . $word_condition . ")";
  215. } else {
  216. $search_where .= " or " . $word_condition;
  217. }
  218. //$select_sum .= " + if(" . $word_condition . ", " . $word_idx_score . ", 0)";
  219. $select_sum .= " + (" . implode(" + ", $select_sums) . ")";
  220. if (! empty($max_words) && $word_idx >= $max_words) {
  221. break; // one ne prend plus en compte les mots au dela de $max_words
  222. }
  223. }
  224. }
  225. $search_where .= ")";
  226. $select_sum .= ")";
  227. //pre($select_sum, 1);
  228. return [
  229. 'select' => $select_sum,
  230. 'where' => $search_where,
  231. ];
  232. }
  233. }