Mysql.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Define SQL for Mysql database system
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license MIT
  7. */
  8. class Mysql
  9. {
  10. const label = 'MySQL';
  11. const connection = 'mysql:host={{host}};dbname={{name}}';
  12. const description = 'Base robuste authentifiée necessitant un serveur Mysql (Conseillé)';
  13. const table_escape = '`';
  14. const column_escape = '`';
  15. public static function pdo_attributes(){
  16. return array(
  17. PDO::ATTR_PERSISTENT => true,
  18. PDO::ATTR_ERRMODE=> PDO::ERRMODE_EXCEPTION
  19. );
  20. }
  21. public static function operators($key=null){
  22. $operators =
  23. array(
  24. 'in'=> array(
  25. 'slug'=> 'in',
  26. 'label'=>'Contient une des valeurs',
  27. 'sql'=>function($column,$values,&$query,&$data){
  28. $places = array();
  29. foreach ($values as $value) {
  30. $places[] = '?';
  31. $data[] = $value;
  32. }
  33. $query .= $column.' IN ('. implode(',',$places).')';
  34. }),
  35. 'not in'=> array(
  36. 'slug'=> 'not in',
  37. 'label'=>'Ne contient aucune des valeurs',
  38. 'sql'=>function($column,$values,&$query,&$data){
  39. $places = array();
  40. foreach ($values as $value) {
  41. $places[] = '?';
  42. $data[] = $value;
  43. }
  44. $query .= $column.' NOT IN ('. implode(',',$places).')';
  45. }),
  46. 'null'=> array(
  47. 'slug'=> 'null',
  48. 'label'=>'Non renseigné',
  49. 'sql'=>'{{column}} IS NULL'
  50. ),
  51. 'not null'=> array(
  52. 'slug'=> 'not null',
  53. 'label'=>'Renseigné',
  54. 'sql'=>'{{column}} IS NOT NULL'
  55. ),
  56. 'like'=> array(
  57. 'slug'=> 'like',
  58. 'label'=>'Contient',
  59. 'sql'=>function($column,$values,&$query,&$data){
  60. $data[] = '%'.$values[0].'%';
  61. $query .= ' '.$column.' LIKE ?' ;
  62. }
  63. ),
  64. 'not like'=> array(
  65. 'slug'=> 'not like',
  66. 'label'=>'Ne contient pas',
  67. 'sql'=>function($column,$values,&$query,&$data){
  68. $data[] = '%'.$values[0].'%';
  69. $query .= ' '.$column.' NOT LIKE ?' ;
  70. }
  71. ),
  72. '='=> array(
  73. 'slug'=> '=',
  74. 'label'=>'Egal',
  75. 'sql'=>'{{column}} = {{value}}'
  76. ),
  77. '!='=> array(
  78. 'slug'=> '!=',
  79. 'label'=>'Différent',
  80. 'sql'=>'{{column}} != {{value}}'
  81. ),
  82. '<'=> array(
  83. 'slug'=> '<',
  84. 'label'=>'Inférieur',
  85. 'sql'=>'{{column}} < {{value}}'
  86. ),
  87. '>'=> array(
  88. 'slug'=> '>',
  89. 'label'=>'Supérieur',
  90. 'sql'=>'{{column}} > {{value}}'
  91. ),
  92. 'between'=> array(
  93. 'slug'=> 'between',
  94. 'label'=>'Entre',
  95. 'values' => 2,
  96. 'sql'=>function($column,$values,&$query,&$data){
  97. $query .= ' '.$column.' between ? AND ? ' ;
  98. $data[] = $values[0];
  99. $data[] = $values[1];
  100. }
  101. ),
  102. 'inline-or'=> array(
  103. 'slug'=> 'inline-or',
  104. 'label'=>'Contient une des valeurs',
  105. 'sql'=>function($column,$values,&$query,&$data){
  106. $values = explode(',', $values[0]);
  107. $query .= ' ( ';
  108. foreach($values as $i=>$value){
  109. $query .= ' '.($i!=0?' OR ':' ').$column.' LIKE ? ';
  110. $data[] = '%,'.$value.',%';
  111. }
  112. $query .= ' ) ';
  113. }),
  114. 'inline-and'=> array(
  115. 'slug'=> 'inline-and',
  116. 'label'=>'Contient toutes les valeurs',
  117. 'sql'=>function($column,$values,&$query,&$data){
  118. $values = explode(',', $values[0]);
  119. $query .= ' ( ';
  120. foreach($values as $i=>$value){
  121. $query .= ' '.($i!=0?' AND ':' ').$column.' LIKE ? ';
  122. $data[] = '%,'.$value.',%';
  123. }
  124. $query .= ' ) ';
  125. })
  126. );
  127. if(!isset($key)) return $operators;
  128. return isset($operators[$key]) ?$operators[$key] : array('label'=>'Non définit','sql'=>'');
  129. }
  130. public static function fields(){
  131. return array(
  132. array('id'=>'host','label'=>'Serveur','default'=>'localhost','comment'=>''),
  133. array('id'=>'login','label'=>'Identifiant','default'=>'','comment'=>''),
  134. array('id'=>'password','label'=>'Mot de passe','default'=>'','comment'=>''),
  135. array('id'=>'name','label'=>'Nom de la base','default'=>'','comment'=>'')
  136. );
  137. }
  138. public static function processField(&$field,&$value,&$values,&$i){
  139. if($field['operator'] == 'BETWEEN' || $field['operator'] == 'NOT BETWEEN'){
  140. foreach ($value as $v2) {
  141. $tag = ':'.$i;
  142. $field['tag'][]= $tag;
  143. $values[$tag] = $v2;
  144. $i++;
  145. }
  146. $field['tag'] = implode(' AND ',$field['tag']);
  147. }else if($field['operator'] == 'IN' || $field['operator'] == 'NOT IN'){
  148. $field['tag'] = array();
  149. $valueArray = !is_array($value)?explode(',',$value) : $value;
  150. foreach ($valueArray as $v2) {
  151. $tag = ':'.$i;
  152. $field['tag'][]= $tag;
  153. $values[$tag] = $v2;
  154. $i++;
  155. }
  156. $field['tag'] = implode(',',$field['tag']);
  157. $field['operator'] = $field['operator'].'(';
  158. $field['postoperator'] = ')';
  159. }else if(is_null($value)){
  160. $field['operator'] = ' IS'.($field['operator']=='!=' ? ' NOT ':' ').'NULL ';
  161. $field['tag'] = '';
  162. }else{
  163. $tag = ':'.$i;
  164. $field['tag'] = $tag;
  165. $values[$tag] = $value;
  166. $i++;
  167. }
  168. }
  169. public static function types(){
  170. $types = array();
  171. $types['string'] = 'VARCHAR(225) CHARACTER SET utf8 COLLATE utf8_general_ci';
  172. $types['longstring'] = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
  173. $types['key'] = 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY';
  174. $types['object'] = $types['timestamp'] = $types['datetime'] = $types['date'] = $types['int'] = 'INT(11)';
  175. $types['boolean'] = 'tinyint(1) NOT NULL DEFAULT \'0\'';
  176. $types['blob'] = ' BLOB';
  177. $types['float'] = 'FLOAT';
  178. $types['decimal'] = 'DECIMAL(20,2)';
  179. $types['default'] = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
  180. return $types;
  181. }
  182. public static function beforeTransaction($pdo){
  183. $pdo->exec("set names utf8");
  184. $pdo->exec("SET time_zone='".TIME_ZONE."'");
  185. }
  186. public static function select(){
  187. $sql = 'SELECT {{:selected}}{{value}}{{;}},{{/;}}{{/:selected}} FROM `{{table}}` {{?joins}}{{:joins}}LEFT JOIN `{{jointable2}}` {{jointableAlias}} ON `{{jointable1}}`.{{field1}}= `{{jointableAlias}}`.{{field2}} {{/:joins}}{{/?joins}} {{?filter}} WHERE {{:filter}} `{{table}}`.`{{key}}` {{operator}} {{value}} {{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{?orderby}}ORDER BY {{:orderby}}{{value}}{{;}},{{/;}}{{/:orderby}} {{/?orderby}} {{?limit}}LIMIT {{:limit}}{{value}}{{;}},{{/;}}{{/:limit}}{{/?limit}}';
  188. return $sql;
  189. }
  190. public static function delete(){
  191. $sql = 'DELETE FROM `{{table}}` {{?filter}}WHERE {{:filter}}`{{key}}`{{operator}}{{value}} {{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{?limit}}LIMIT {{:limit}}{{value}}{{;}},{{/;}}{{/:limit}}{{/?limit}}';
  192. return $sql;
  193. }
  194. public static function count(){
  195. $sql = 'SELECT COUNT({{selected}}) number FROM `{{table}}` {{?filter}}WHERE {{:filter}}`{{key}}`{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  196. return $sql;
  197. }
  198. public static function update(){
  199. $sql = 'UPDATE `{{table}}` SET {{?fields}} {{:fields}}`{{key}}`={{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}} {{?filter}} WHERE {{:filter}} `{{table}}`.`{{key}}` {{operator}} {{value}} {{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  200. return $sql;
  201. }
  202. public static function insert_head(){
  203. $sql = 'INSERT INTO `{{table}}` ({{?fields}} {{:fields}}`{{key}}` {{;}} , {{/;}} {{/:fields}} {{/?fields}})VALUES';
  204. return $sql;
  205. }
  206. public static function insert_body(){
  207. $sql = '({{?fields}} {{:fields}}{{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  208. return $sql;
  209. }
  210. public static function insert(){
  211. $sql = self::insert_head().self::insert_body();
  212. return $sql;
  213. }
  214. public static function create(){
  215. $sql = 'CREATE TABLE IF NOT EXISTS `{{table}}` ({{?fields}} {{:fields}}`{{key}}` {{value}}{{;}} , {{/;}} {{/:fields}} {{/?fields}}) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
  216. return $sql;
  217. }
  218. public static function drop(){
  219. $sql = 'DROP TABLE IF EXISTS `{{table}}`;';
  220. return $sql;
  221. }
  222. public static function truncate(){
  223. $sql = 'TRUNCATE TABLE `{{table}}`;';
  224. return $sql;
  225. }
  226. public static function create_index(){
  227. $sql = 'CREATE INDEX `{{index_name}}` ON `{{table}}` ({{column}})';
  228. return $sql;
  229. }
  230. public static function drop_index(){
  231. $sql = 'DROP INDEX `{{index_name}}` ON `{{table}}`';
  232. return $sql;
  233. }
  234. public static function count_index(){
  235. $sql = "SELECT COUNT(1) `exists` FROM INFORMATION_SCHEMA.STATISTICS
  236. WHERE table_schema=DATABASE() AND table_name='{{table}}' AND index_name='{{index_name}}'";
  237. return $sql;
  238. }
  239. public static function show_tables(){
  240. $sql = 'SHOW TABLES';
  241. return $sql;
  242. }
  243. public static function show_columns(){
  244. $sql = 'SELECT COLUMN_NAME `column`,DATA_TYPE `type` FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = "{{table}}"';
  245. return $sql;
  246. }
  247. }
  248. ?>