Mysql.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Define SQL for Mysql database system
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license copyright
  7. */
  8. class Mysql
  9. {
  10. const label = 'MySQL';
  11. const connection = 'mysql:host={{BASE_HOST}};dbname={{BASE_NAME}}';
  12. const description = 'Base robuste authentifiée necessitant un serveur Mysql (Conseillé)';
  13. public static function fields(){
  14. return array(
  15. array('id'=>'host','label'=>'Serveur','default'=>'localhost','comment'=>''),
  16. array('id'=>'login','label'=>'Identifiant','default'=>'','comment'=>''),
  17. array('id'=>'password','label'=>'Mot de passe','default'=>'','comment'=>''),
  18. array('id'=>'name','label'=>'Nom de la base','default'=>'','comment'=>'')
  19. );
  20. }
  21. public static function types(){
  22. $types = array();
  23. $types['string'] = 'VARCHAR(225) CHARACTER SET utf8 COLLATE utf8_general_ci';
  24. $types['longstring'] = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
  25. $types['key'] = 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY';
  26. $types['object'] = $types['timestamp'] = $types['datetime'] = $types['date'] = $types['int'] = 'INT(11)';
  27. $types['boolean'] = 'tinyint(1) NOT NULL DEFAULT \'0\'';
  28. $types['blob'] = ' BLOB';
  29. $types['float'] = 'FLOAT';
  30. $types['decimal'] = 'DECIMAL(20,2)';
  31. $types['default'] = 'TEXT CHARACTER SET utf8 COLLATE utf8_general_ci';
  32. return $types;
  33. }
  34. public static function beforeTransaction($pdo){
  35. $pdo->exec("set names utf8");
  36. }
  37. public static function select(){
  38. $sql = 'SELECT {{:selected}}{{value}}{{;}},{{/;}}{{/:selected}} FROM `{{table}}` {{?joins}}{{:joins}}LEFT JOIN `{{jointable2}}` ON `{{jointable1}}`.{{field1}}= `{{jointable2}}`.{{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}}';
  39. return $sql;
  40. }
  41. public static function delete(){
  42. $sql = 'DELETE FROM `{{table}}` {{?filter}}WHERE {{:filter}}`{{key}}`{{operator}}{{value}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{?limit}}LIMIT {{:limit}}{{value}}{{;}},{{/;}}{{/:limit}}{{/?limit}}';
  43. return $sql;
  44. }
  45. public static function count(){
  46. $sql = 'SELECT COUNT({{selected}}) number FROM `{{table}}` {{?filter}}WHERE {{:filter}}`{{key}}`{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  47. return $sql;
  48. }
  49. public static function update(){
  50. $sql = 'UPDATE `{{table}}` SET {{?fields}} {{:fields}}`{{key}}`={{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}} {{?filters}}WHERE {{:filters}}`{{key}}`{{operator}}{{value}} {{;}} AND {{/;}} {{/:filters}} {{/?filters}}';
  51. return $sql;
  52. }
  53. public static function insert(){
  54. $sql = 'INSERT INTO `{{table}}` ({{?fields}} {{:fields}}`{{key}}` {{;}} , {{/;}} {{/:fields}} {{/?fields}})VALUES({{?fields}} {{:fields}}{{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  55. return $sql;
  56. }
  57. public static function create(){
  58. $sql = 'CREATE TABLE IF NOT EXISTS `{{table}}` ({{?fields}} {{:fields}}`{{key}}` {{value}}{{;}} , {{/;}} {{/:fields}} {{/?fields}}) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
  59. return $sql;
  60. }
  61. public static function drop(){
  62. $sql = 'DROP TABLE IF EXISTS `{{table}}`;';
  63. return $sql;
  64. }
  65. public static function truncate(){
  66. $sql = 'TRUNCATE TABLE `{{table}}`;';
  67. return $sql;
  68. }
  69. }
  70. ?>