Sqlite.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Define SQL for Mysql database system
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license MIT
  7. */
  8. class Sqlite
  9. {
  10. const label = 'SQLite3';
  11. const connection = 'sqlite:{{ROOT}}file/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 operators($key=null){
  27. $operators =
  28. array(
  29. 'in'=> array(
  30. 'slug'=> 'in',
  31. 'label'=>'Contient une des valeurs',
  32. 'sql'=>function($column,$values,&$query,&$data){
  33. $places = array();
  34. foreach ($values as $value) {
  35. $places[] = '?';
  36. $data[] = $value;
  37. }
  38. $query .= $column.' IN ('. implode(',',$places).')';
  39. }),
  40. 'not in'=> array(
  41. 'slug'=> 'not in',
  42. 'label'=>'Ne contient aucune des valeurs',
  43. 'sql'=>function($column,$values,&$query,&$data){
  44. $places = array();
  45. foreach ($values as $value) {
  46. $places[] = '?';
  47. $data[] = $value;
  48. }
  49. $query .= $column.' NOT IN ('. implode(',',$places).')';
  50. }),
  51. 'null'=> array(
  52. 'slug'=> 'null',
  53. 'label'=>'Non renseigné',
  54. 'sql'=>'{{column}} IS NULL'
  55. ),
  56. 'not null'=> array(
  57. 'slug'=> 'not null',
  58. 'label'=>'Renseigné',
  59. 'sql'=>'{{column}} IS NOT NULL'
  60. ),
  61. 'like'=> array(
  62. 'slug'=> 'like',
  63. 'label'=>'Contient',
  64. 'sql'=>function($column,$values,&$query,&$data){
  65. $data[] = '%'.$values[0].'%';
  66. $query .= ' '.$column.' LIKE ?' ;
  67. }
  68. ),
  69. 'not like'=> array(
  70. 'slug'=> 'not like',
  71. 'label'=>'Ne contient pas',
  72. 'sql'=>function($column,$values,&$query,&$data){
  73. $data[] = '%'.$values[0].'%';
  74. $query .= ' '.$column.' NOT LIKE ?' ;
  75. }
  76. ),
  77. '='=> array(
  78. 'slug'=> '=',
  79. 'label'=>'Egal',
  80. 'sql'=>'{{column}} = {{value}}'
  81. ),
  82. '!='=> array(
  83. 'slug'=> '!=',
  84. 'label'=>'Différent',
  85. 'sql'=>'{{column}} != {{value}}'
  86. ),
  87. '<'=> array(
  88. 'slug'=> '<',
  89. 'label'=>'Inférieur',
  90. 'sql'=>'{{column}} < {{value}}'
  91. ),
  92. '>'=> array(
  93. 'slug'=> '>',
  94. 'label'=>'Supérieur',
  95. 'sql'=>'{{column}} > {{value}}'
  96. ),
  97. 'between'=> array(
  98. 'slug'=> 'between',
  99. 'label'=>'Entre',
  100. 'values' => 2,
  101. 'sql'=>function($column,$values,&$query,&$data){
  102. $query .= ' '.$column.' between ? AND ? ' ;
  103. $data[] = $values[0];
  104. $data[] = $values[1];
  105. }
  106. ),
  107. 'inline-or'=> array(
  108. 'slug'=> 'inline-or',
  109. 'label'=>'Contient une des valeurs',
  110. 'sql'=>function($column,$values,&$query,&$data){
  111. $values = explode(',', $values[0]);
  112. $query .= ' ( ';
  113. foreach($values as $i=>$value){
  114. $query .= ' '.($i!=0?' OR ':' ').$column.' LIKE ? ';
  115. $data[] = '%,'.$value.',%';
  116. }
  117. $query .= ' ) ';
  118. }),
  119. 'inline-and'=> array(
  120. 'slug'=> 'inline-and',
  121. 'label'=>'Contient toutes les valeurs',
  122. 'sql'=>function($column,$values,&$query,&$data){
  123. $values = explode(',', $values[0]);
  124. $query .= ' ( ';
  125. foreach($values as $i=>$value){
  126. $query .= ' '.($i!=0?' AND ':' ').$column.' LIKE ? ';
  127. $data[] = '%,'.$value.',%';
  128. }
  129. $query .= ' ) ';
  130. })
  131. );
  132. if(!isset($key)) return $operators;
  133. return isset($operators[$key]) ?$operators[$key] : array('label'=>'Non définit','sql'=>'');
  134. }
  135. public static function processField(&$field,&$value,&$values,&$i){
  136. if($field['operator'] == 'BETWEEN' || $field['operator'] == 'NOT BETWEEN'){
  137. foreach ($value as $v2) {
  138. $tag = ':'.$i;
  139. $field['tag'][]= $tag;
  140. $values[$tag] = $v2;
  141. $i++;
  142. }
  143. $field['tag'] = implode(' AND ',$field['tag']);
  144. }else if($field['operator'] == 'IN' || $field['operator'] == 'NOT IN'){
  145. $field['tag'] = array();
  146. $valueArray = !is_array($value)?explode(',',$value) : $value;
  147. foreach ($valueArray as $v2) {
  148. $tag = ':'.$i;
  149. $field['tag'][]= $tag;
  150. $values[$tag] = $v2;
  151. $i++;
  152. }
  153. $field['tag'] = implode(',',$field['tag']);
  154. $field['operator'] = $field['operator'].'(';
  155. $field['postoperator'] = ')';
  156. }else if(is_null($value)){
  157. $field['operator'] = ' IS'.($field['operator']=='!=' ? ' NOT ':' ').'NULL ';
  158. $field['tag'] = '';
  159. }else{
  160. $tag = ':'.$i;
  161. $field['tag'] = $tag;
  162. $values[$tag] = $value;
  163. $i++;
  164. }
  165. }
  166. public static function types(){
  167. $types = array();
  168. $types['string'] = $types['timestamp'] = $types['datetime'] = $types['date'] = 'VARCHAR(255)';
  169. $types['longstring'] = 'TEXT';
  170. $types['key'] = 'INTEGER NOT NULL PRIMARY KEY';
  171. $types['object'] = $types['integer'] = 'bigint(20)';
  172. $types['int'] = 'INTEGER';
  173. $types['boolean'] = 'INTEGER(1)';
  174. $types['blob'] = ' BLOB';
  175. $types['float'] = 'REAL';
  176. $types['decimal'] = 'DECIMAL(20,2)';
  177. $types['default'] = 'TEXT';
  178. return $types;
  179. }
  180. public static function select(){
  181. $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}}';
  182. return $sql;
  183. }
  184. public static function delete(){
  185. $sql = 'DELETE FROM {{table}} {{?filter}}WHERE {{:filter}}"{{key}}"{{operator}}{{value}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{?limit}}LIMIT {{:limit}}{{value}}{{;}},{{/;}}{{/:limit}}{{/?limit}}';
  186. return $sql;
  187. }
  188. public static function count(){
  189. $sql = 'SELECT COUNT({{selected}}) number FROM {{table}} {{?filter}}WHERE {{:filter}}"{{key}}"{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  190. return $sql;
  191. }
  192. public static function update(){
  193. $sql = 'UPDATE {{table}} SET {{?fields}} {{:fields}}"{{key}}"={{value}} {{;}}, {{/;}}{{/:fields}} {{/?fields}} {{?filter}}WHERE {{:filter}}"{{table}}"."{{key}}"{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  194. return $sql;
  195. }
  196. public static function insert_head(){
  197. $sql = 'INSERT INTO {{table}} ({{?fields}} {{:fields}}"{{key}}" {{;}} , {{/;}} {{/:fields}} {{/?fields}})VALUES';
  198. return $sql;
  199. }
  200. public static function insert_body(){
  201. $sql = '({{?fields}} {{:fields}}{{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  202. return $sql;
  203. }
  204. public static function insert(){
  205. $sql = self::insert_head().self::insert_body();
  206. return $sql;
  207. }
  208. public static function create(){
  209. $sql = 'CREATE TABLE IF NOT EXISTS "{{table}}" ({{?fields}} {{:fields}}"{{key}}" {{value}}{{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  210. return $sql;
  211. }
  212. public static function drop(){
  213. $sql = 'DROP TABLE IF EXISTS "{{table}}";';
  214. return $sql;
  215. }
  216. public static function truncate(){
  217. $sql = 'TRUNCATE TABLE "{{table}}";';
  218. return $sql;
  219. }
  220. public static function create_index(){
  221. $sql = 'CREATE INDEX IF NOT EXISTS "{{index_name}}" ON "{{table}}" ("{{column}}")';
  222. return $sql;
  223. }
  224. public static function drop_index(){
  225. $sql = 'DROP INDEX "{{index_name}}" ON "{{table}}"';
  226. return $sql;
  227. }
  228. public static function count_index(){
  229. //On desactive le check sur sqlite, la notion IF NOT EXISTS existant dans ce sgbd
  230. $sql = "SELECT 0 \"exists\"";
  231. return $sql;
  232. }
  233. public static function show_tables(){
  234. $sql = 'select name from SQLite_master WHERE type="table"';
  235. return $sql;
  236. }
  237. public static function show_columns(){
  238. $sql = "PRAGMA table_info({{table}});";
  239. return $sql;
  240. }
  241. }
  242. ?>