SqlServer.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * Define SQL for SQL Server database system
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license MIT
  7. */
  8. class SqlServer
  9. {
  10. const label = 'SQLServer';
  11. const connection = 'sqlsrv:Server={{host}};Database={{name}}';
  12. const description = 'Base microsoft SQL Server authentifiée';
  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'=>'host','label'=>'Serveur','default'=>'localhost','comment'=>''),
  24. array('id'=>'login','label'=>'Identifiant','default'=>'','comment'=>''),
  25. array('id'=>'password','label'=>'Mot de passe','default'=>'','comment'=>''),
  26. array('id'=>'name','label'=>'Nom de la base','default'=>'','comment'=>'')
  27. );
  28. }
  29. public static function operators($key=null){
  30. $operators =
  31. array(
  32. 'in'=> array(
  33. 'slug'=> 'in',
  34. 'label'=>'Contient une des valeurs',
  35. 'sql'=>function($column,$values,&$query,&$data){
  36. $places = array();
  37. foreach ($values as $value) {
  38. $places[] = '?';
  39. $data[] = $value;
  40. }
  41. $query .= $column.' IN ('. implode(',',$places).')';
  42. }),
  43. 'not in'=> array(
  44. 'slug'=> 'not in',
  45. 'label'=>'Ne contient aucune des valeurs',
  46. 'sql'=>function($column,$values,&$query,&$data){
  47. $places = array();
  48. foreach ($values as $value) {
  49. $places[] = '?';
  50. $data[] = $value;
  51. }
  52. $query .= $column.' NOT IN ('. implode(',',$places).')';
  53. }),
  54. 'null'=> array(
  55. 'slug'=> 'null',
  56. 'label'=>'Non renseigné',
  57. 'sql'=>'{{column}} IS NULL'
  58. ),
  59. 'not null'=> array(
  60. 'slug'=> 'not null',
  61. 'label'=>'Renseigné',
  62. 'sql'=>'{{column}} IS NOT NULL'
  63. ),
  64. 'like'=> array(
  65. 'slug'=> 'like',
  66. 'label'=>'Contient',
  67. 'sql'=>function($column,$values,&$query,&$data){
  68. $data[] = '%'.$values[0].'%';
  69. $query .= ' '.$column.' LIKE ?' ;
  70. }
  71. ),
  72. 'not like'=> array(
  73. 'slug'=> 'not like',
  74. 'label'=>'Ne contient pas',
  75. 'sql'=>function($column,$values,&$query,&$data){
  76. $data[] = '%'.$values[0].'%';
  77. $query .= ' '.$column.' NOT LIKE ?' ;
  78. }
  79. ),
  80. '='=> array(
  81. 'slug'=> '=',
  82. 'label'=>'Egal',
  83. 'sql'=>'{{column}} = {{value}}'
  84. ),
  85. '!='=> array(
  86. 'slug'=> '!=',
  87. 'label'=>'Différent',
  88. 'sql'=>'{{column}} != {{value}}'
  89. ),
  90. '<'=> array(
  91. 'slug'=> '<',
  92. 'label'=>'Inférieur',
  93. 'sql'=>'{{column}} < {{value}}'
  94. ),
  95. '>'=> array(
  96. 'slug'=> '>',
  97. 'label'=>'Supérieur',
  98. 'sql'=>'{{column}} > {{value}}'
  99. ),
  100. 'between'=> array(
  101. 'slug'=> 'between',
  102. 'label'=>'Entre',
  103. 'values' => 2,
  104. 'sql'=>function($column,$values,&$query,&$data){
  105. $query .= ' '.$column.' between ? AND ? ' ;
  106. $data[] = $values[0];
  107. $data[] = $values[1];
  108. }
  109. ),
  110. 'inline-or'=> array(
  111. 'slug'=> 'inline-or',
  112. 'label'=>'Contient une des valeurs',
  113. 'sql'=>function($column,$values,&$query,&$data){
  114. $values = explode(',', $values[0]);
  115. $query .= ' ( ';
  116. foreach($values as $i=>$value){
  117. $query .= ' '.($i!=0?' OR ':' ').$column.' LIKE ? ';
  118. $data[] = '%,'.$value.',%';
  119. }
  120. $query .= ' ) ';
  121. }),
  122. 'inline-and'=> array(
  123. 'slug'=> 'inline-and',
  124. 'label'=>'Contient toutes les valeurs',
  125. 'sql'=>function($column,$values,&$query,&$data){
  126. $values = explode(',', $values[0]);
  127. $query .= ' ( ';
  128. foreach($values as $i=>$value){
  129. $query .= ' '.($i!=0?' AND ':' ').$column.' LIKE ? ';
  130. $data[] = '%,'.$value.',%';
  131. }
  132. $query .= ' ) ';
  133. })
  134. );
  135. if(!isset($key)) return $operators;
  136. return isset($operators[$key]) ?$operators[$key] : array('label'=>'Non définit','sql'=>'');
  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. }
  185. public static function select(){
  186. $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}}';
  187. return $sql;
  188. }
  189. public static function delete(){
  190. $sql = 'DELETE FROM "{{table}}" {{?filter}}WHERE {{:filter}}"{{key}}"{{operator}}{{value}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{?limit}}LIMIT {{:limit}}{{value}}{{;}},{{/;}}{{/:limit}}{{/?limit}}';
  191. return $sql;
  192. }
  193. public static function count(){
  194. $sql = 'SELECT COUNT({{selected}}) number FROM "{{table}}" {{?filter}}WHERE {{:filter}}"{{key}}"{{operator}}{{value}}{{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}}';
  195. return $sql;
  196. }
  197. public static function update(){
  198. $sql = 'UPDATE "{{table}}" SET {{?fields}} {{:fields}}"{{key}}"={{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}} {{?filter}} WHERE {{:filter}} "{{table}}"."{{key}}" {{operator}} {{value}} {{postoperator}} {{;}} AND {{/;}} {{/:filter}} {{/?filter}} {{/?filters}}';
  199. return $sql;
  200. }
  201. public static function insert_head(){
  202. $sql = 'INSERT INTO "{{table}}" ({{?fields}} {{:fields}}"{{key}}" {{;}} , {{/;}} {{/:fields}} {{/?fields}})VALUES';
  203. return $sql;
  204. }
  205. public static function insert_body(){
  206. $sql = '({{?fields}} {{:fields}}{{value}} {{;}} , {{/;}} {{/:fields}} {{/?fields}})';
  207. return $sql;
  208. }
  209. public static function insert(){
  210. $sql = self::insert_head().self::insert_body();
  211. return $sql;
  212. }
  213. public static function create(){
  214. $sql = 'CREATE TABLE IF NOT EXISTS "{{table}}" ({{?fields}} {{:fields}}"{{key}}" {{value}}{{;}} , {{/;}} {{/:fields}} {{/?fields}}) DEFAULT CHARSET=utf8;';
  215. return $sql;
  216. }
  217. public static function drop(){
  218. $sql = 'DROP TABLE IF EXISTS "{{table}}";';
  219. return $sql;
  220. }
  221. public static function truncate(){
  222. $sql = 'TRUNCATE TABLE "{{table}}";';
  223. return $sql;
  224. }
  225. public static function create_index(){
  226. $sql = 'CREATE INDEX "{{index_name}}" ON "{{table}}" ({{column}})';
  227. return $sql;
  228. }
  229. public static function drop_index(){
  230. $sql = 'DROP INDEX "{{index_name}}" ON "{{table}}"';
  231. return $sql;
  232. }
  233. public static function count_index(){
  234. $sql = "SELECT COUNT(1) \"exists\" FROM INFORMATION_SCHEMA.STATISTICS
  235. WHERE table_schema=DATABASE() AND table_name='{{table}}' AND index_name='{{index_name}}'";
  236. return $sql;
  237. }
  238. public static function show_tables(){
  239. $sql = 'SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = \'U\'';
  240. return $sql;
  241. }
  242. public static function show_columns(){
  243. $sql = "select * from information_schema.columns where table_name = '{{table}}'";
  244. return $sql;
  245. }
  246. }
  247. ?>