Sqlite.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Define SQL for Mysql database system
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license copyright
  7. */
  8. class Sqlite
  9. {
  10. const label = 'SQLite3';
  11. const connection = 'sqlite:{{ROOT}}db/{{name}}.db';
  12. const description = 'Base légere monofichier sans authentification, simple d\'utilisation/installation mais limitée en performances';
  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 fields(){
  22. return array(
  23. array('id'=>'name','label'=>'Nom de la base','default'=>'.database','comment'=>'')
  24. );
  25. }
  26. public static function processField(&$field,&$value,&$values,&$i){
  27. if($field['operator'] == 'IN'){
  28. $field['tag'] = array();
  29. foreach (explode(',',$value) as $v2) {
  30. $tag = ':'.$i;
  31. $field['tag'][]= $tag;
  32. $values[$tag] = $v2;
  33. $i++;
  34. }
  35. $field['tag'] = implode(',',$field['tag']);
  36. $field['operator'] = 'IN(';
  37. $field['postoperator'] = ')';
  38. }else{
  39. $tag = ':'.$i;
  40. $field['tag'] = $tag;
  41. $values[$tag] = $value;
  42. $i++;
  43. }
  44. }
  45. public static function types(){
  46. $types = array();
  47. $types['string'] = $types['timestamp'] = $types['datetime'] = $types['date'] = 'VARCHAR(255)';
  48. $types['longstring'] = 'TEXT';
  49. $types['key'] = 'INTEGER NOT NULL PRIMARY KEY';
  50. $types['object'] = $types['integer'] = 'bigint(20)';
  51. $types['int'] = 'INTEGER';
  52. $types['boolean'] = 'INTEGER(1)';
  53. $types['blob'] = ' BLOB';
  54. $types['float'] = 'REAL';
  55. $types['decimal'] = 'DECIMAL(20,2)';
  56. $types['default'] = 'TEXT';
  57. return $types;
  58. }
  59. public static function select(){
  60. $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}}';
  61. return $sql;
  62. }
  63. public static function delete(){
  64. $sql = 'DELETE FROM {{table}} {{?filter}}WHERE {{:filter}}"{{key}}"{{operator}}{{value}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{?limit}}LIMIT {{:limit}}{{value}}{{;}},{{/;}}{{/:limit}}{{/?limit}}';
  65. return $sql;
  66. }
  67. public static function count(){
  68. $sql = 'SELECT COUNT({{selected}}) number FROM {{table}} {{?filter}}WHERE {{:filter}}"{{key}}"{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  69. return $sql;
  70. }
  71. public static function update(){
  72. $sql = 'UPDATE {{table}} SET {{?fields}} {{:fields}}"{{key}}"={{value}} {{;}}, {{/;}}{{/:fields}} {{/?fields}} {{?filters}}WHERE {{:filters}}{{key}}{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filters}} {{/?filters}}';
  73. return $sql;
  74. }
  75. public static function insert(){
  76. $sql = 'INSERT INTO {{table}} ({{?fields}} {{:fields}}"{{key}}" {{;}} , {{/;}} {{/:fields}} {{/?fields}})VALUES({{?fields}} {{:fields}}{{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  77. return $sql;
  78. }
  79. public static function create(){
  80. $sql = 'CREATE TABLE IF NOT EXISTS "{{table}}" ({{?fields}} {{:fields}}"{{key}}" {{value}}{{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  81. return $sql;
  82. }
  83. public static function drop(){
  84. $sql = 'DROP TABLE IF EXISTS "{{table}}";';
  85. return $sql;
  86. }
  87. public static function truncate(){
  88. $sql = 'TRUNCATE TABLE "{{table}}";';
  89. return $sql;
  90. }
  91. public static function create_index(){
  92. $sql = 'CREATE INDEX IF NOT EXISTS "{{index_name}}" ON "{{table}}" ("{{column}}")';
  93. return $sql;
  94. }
  95. public static function drop_index(){
  96. $sql = 'DROP INDEX "{{index_name}}" ON "{{table}}"';
  97. return $sql;
  98. }
  99. public static function count_index(){
  100. //On desactive le check sur sqlite, la notion IF NOT EXISTS existant dans ce sgbd
  101. $sql = "SELECT 0 \"exists\"";
  102. return $sql;
  103. }
  104. public static function show_tables(){
  105. $sql = 'select name from SQLite_master WHERE type="table"';
  106. return $sql;
  107. }
  108. public static function show_columns(){
  109. $sql = "PRAGMA table_info({{table}});";
  110. return $sql;
  111. }
  112. }
  113. ?>