/database/class.QueryBuilderPlatformMssql.php

https://github.com/palmic/2yaml · PHP · 335 lines · 284 code · 18 blank · 33 comment · 41 complexity · a3752b1967b4cdad0f86bdbcfab8c15a MD5 · raw file

  1. <?php
  2. class QueryBuilderPlatformMssql extends QueryBuilderPlatform
  3. {
  4. public function getInsert(/*string*/ $table, /*array*/ $values = array()) {
  5. if (strlen($table) < 1) {
  6. throw new DbControlException("Ilegal parameter table. Must be NOT-NULL string.");
  7. }
  8. if (count($values) < 1) {
  9. throw new DbControlException("Ilegal parameter values. Must be NOT-NULL string.");
  10. }
  11. foreach( $values as $index => $value) {
  12. $columnsString .= strlen($columnsString) > 0 ? ", " : "";
  13. $columnsString .= reset(self::getQuotesColumnName()) . $index . end(self::getQuotesColumnName());
  14. $valuesString .= strlen($valuesString) > 0 ? ", " : "";
  15. $valuesString .= $this->getValueWrapped($value);
  16. }
  17. $table = reset($this->getQuotesTableName()) . $table . end($this->getQuotesTableName());
  18. $out = "INSERT INTO $table ($columnsString) VALUES ($valuesString)";
  19. return $out;
  20. }
  21. public function getDelete(/*string*/ $table, QueryBuilderWhere $where = NULL) {
  22. if ($where instanceof QueryBuilderWhere) $whereString = $this->getWhereStringByObject($where);
  23. $table = reset($this->getQuotesTableName()) . $table . end($this->getQuotesTableName());
  24. $out = "DELETE FROM $table \n$whereString";
  25. return $out;
  26. }
  27. public function getUpdate(/*string*/ $table, /*array*/ $values, QueryBuilderWhere $where = NULL) {
  28. if (strlen($table) < 1) {
  29. throw new DbControlException("Ilegal parameter table. Must be NOT-NULL string.");
  30. }
  31. if (strlen($values) < 1) {
  32. throw new DbControlException("Ilegal parameter values. Must be NOT-NULL string.");
  33. }
  34. foreach($values as $index => $value) {
  35. $value = $this->getValueWrapped($value);
  36. $updateString .= strlen($updateString) > 0 ? ", " : "";
  37. $updateString .= reset(self::getQuotesColumnName()) ."$index". end(self::getQuotesColumnName()) . "=$value";
  38. }
  39. if ($where instanceof QueryBuilderWhere) $whereString = $this->getWhereStringByObject($where);
  40. $table = reset($this->getQuotesTableName()) . $table . end($this->getQuotesTableName());
  41. $out = "UPDATE $table SET \n$updateString \n$whereString";
  42. return $out;
  43. }
  44. public function getSelectColumns($table, $what = array(), QueryBuilderWhere $where = NULL, $limit = array(), $groupBy = array(), $orderBy = array()) {
  45. try {
  46. if (!is_array($what)) {
  47. throw new DbControlException("Ilegal parameter what. Must be NOT-NULL string.");
  48. }
  49. // whatString
  50. $whatString = "";
  51. if (count($what) < 1) {
  52. $whatString = "*";
  53. }
  54. else {
  55. foreach ($what as $columnName) {
  56. $whatString .= strlen($whatString) > 0 ? ", " : "";
  57. $whatString .= reset(self::getQuotesColumnName()) ."$columnName". end(self::getQuotesColumnName()) ."";
  58. }
  59. }
  60. return $this->getSelect($table, $whatString, $where, $limit, $groupBy, $orderBy);
  61. }
  62. catch (Exception $e) {
  63. throw $e;
  64. }
  65. }
  66. public function getSelectCount($table, QueryBuilderWhere $where = NULL, $limit = array(), $groupBy = array(), $orderBy = array()) {
  67. try {
  68. return $this->getSelect($table, "COUNT(*) AS count", $where, $limit, $groupBy, $orderBy);
  69. }
  70. catch (Exception $e) {
  71. throw $e;
  72. }
  73. }
  74. public function getSelectMaxColumns($table, $what = array(), QueryBuilderWhere $where = NULL, $groupBy = array(), $orderBy = array()) {
  75. try {
  76. if (!is_array($what)) {
  77. throw new DbControlException("Ilegal parameter what. Must be NOT-NULL string.");
  78. }
  79. if (count($what) < 1) {
  80. throw new DbControlException("You have to specify columns for get max values.");
  81. }
  82. // whatString
  83. $whatString = "";
  84. foreach ($what as $columnName) {
  85. $whatString .= strlen($whatString) > 0 ? ", " : "";
  86. $whatString .= "MAX(". reset(self::getQuotesColumnName()) ."$columnName". end(self::getQuotesColumnName()) .") AS $columnName";
  87. }
  88. return $this->getSelect($table, $whatString, $where, $groupBy, $orderBy);
  89. }
  90. catch (Exception $e) {
  91. throw $e;
  92. }
  93. }
  94. public function getDoesTableExists($table, $database = "") {
  95. try {
  96. throw new DbControlException( "getDoesTableExists() is not implemented yet!");
  97. }
  98. catch (Exception $e) {
  99. throw $e;
  100. }
  101. }
  102. public function getCreateTable($table, $columns = array(), $attributes = array()) {
  103. try {
  104. throw new DbControlException( "getCreateTable() is not implemented yet!");
  105. }
  106. catch (Exception $e) {
  107. throw $e;
  108. }
  109. }
  110. public function getAddColumns($table, $columns = array()) {
  111. try {
  112. throw new DbControlException( "getAddColumns() is not implemented yet!");
  113. }
  114. catch (Exception $e) {
  115. throw $e;
  116. }
  117. }
  118. protected function getSelect($table, $what = "*", QueryBuilderWhere $where = NULL, $limit = array(), $groupBy = array(), $orderBy = array()) {
  119. if (strlen($table) < 1) {
  120. throw new DbControlException("Ilegal parameter table. Must be NOT-NULL string.");
  121. }
  122. if (strlen($what) < 1) {
  123. throw new DbControlException("Ilegal parameter what. Must be NOT-NULL string.");
  124. }
  125. if (!is_array($limit)) {
  126. throw new DbControlException("Ilegal parameter limit. Must be array.");
  127. }
  128. if (!is_array($groupBy)) {
  129. throw new DbControlException("Ilegal parameter groupBy. Must be array.");
  130. }
  131. if (!is_array($orderBy)) {
  132. throw new DbControlException("Ilegal parameter groupBy. Must be array.");
  133. }
  134. if ($where instanceof QueryBuilderWhere) $whereString = $this->getWhereStringByObject($where);
  135. // groupByString
  136. $groupByString = "";
  137. if (count($groupBy) < 1) {
  138. $groupByString = "";
  139. }
  140. else {
  141. foreach ($groupBy as $columnName) {
  142. $groupByString .= strlen($groupByString) > 0 ? ", " : "";
  143. $groupByString .= reset(self::getQuotesColumnName()) ."$columnName". end(self::getQuotesColumnName()) ."";
  144. }
  145. $groupByString = "GROUP BY $groupByString";
  146. }
  147. // orderByString
  148. $orderByString = $this->getOrderByStringByArray($orderBy);
  149. $orderByStringReverse = $this->getOrderByStringByArray($orderBy, true);
  150. // limitString
  151. $limitString = "";
  152. switch (count($limit)) {
  153. case 0:
  154. case 2:
  155. NULL;
  156. break;
  157. case 1:
  158. $limit = array(0, current($limit));
  159. break;
  160. default:
  161. throw new DbControlException("Ilegal parameter limit!");
  162. }
  163. $outTmp = "$what FROM ". reset(self::getQuotesTableName()) ."$table". end(self::getQuotesTableName()) .
  164. " $whereString $groupByString $orderByString";
  165. // LIMIT clause emulation for MSSQL
  166. /**
  167. * FOR INSTANCE EMULATION OF:
  168. * - SELECT emp_id,lname,fname FROM employee LIMIT 20,10
  169. * select * from (
  170. select top 10 emp_id,lname,fname from (
  171. select top 30 emp_id,lname,fname
  172. from employee
  173. order by lname asc
  174. ) as newtbl order by lname desc
  175. ) as newtbl2 order by lname asc
  176. */
  177. if (count($limit) > 0) {
  178. $limitOffset = reset($limit);
  179. $limitLimit = end($limit);
  180. /* check table count */
  181. $sqlCountNoLimit = "SELECT COUNT(*) AS count FROM ". reset(self::getQuotesTableName()) ."$table". end(self::getQuotesTableName()) .
  182. " $whereString $groupByString";
  183. $dbControl = new DbControl($this->task);
  184. $countNoLimit = $dbControl->initiateQuery($sqlCountNoLimit)->count;
  185. $limitLimit = $limitOffset + $limitLimit > $countNoLimit
  186. ? $limitOffset > $countNoLimit ? 0 : $countNoLimit - $limitOffset
  187. : $limitLimit;
  188. $out = "SELECT * FROM (
  189. SELECT TOP ". $limitLimit ." $what FROM (".
  190. "SELECT TOP ". ($limitOffset + $limitLimit) ." $outTmp
  191. ) AS order_tmptable1 $orderByStringReverse".
  192. ") AS order_tmptable2 $orderByString";
  193. }
  194. else {
  195. $out = "SELECT $outTmp";
  196. }
  197. return $out;
  198. }
  199. /**
  200. * returns where clause as string generated from QueryBuilderWhere instance
  201. * @param QueryBuilderWhere $where
  202. * @param bool $isSub - whether is it generated for sub where or not (true only for recursive call, so do not care)
  203. * @return string
  204. */
  205. protected function getWhereStringByObject (QueryBuilderWhere $where, $isSub = false) {
  206. // whereString
  207. $whereString = "";
  208. /* FULLTEXT WHERE */
  209. if ($where instanceof QueryBuilderWhereFullText) {
  210. $columnsString = "";
  211. $phrasesString = "";
  212. foreach ($where->getPhrases() as $phrase) {
  213. $phrase = ereg_replace("[[:punct:]]", "", $phrase);
  214. if (strlen(trim($phrase)) < 1) continue;
  215. $phrase = $this->escapeString($phrase);
  216. $phrasesString .= strlen($phrasesString) > 0 ? " " : "";
  217. $phrasesString .= '"'. $phrase .'"';
  218. }
  219. foreach ($where->getColumns() as $columnName) {
  220. $columnString = reset(self::getQuotesColumnName()) . $columnName . end(self::getQuotesColumnName());
  221. $whereString .= strlen($whereString) > 0 ? " OR " : "";
  222. $whereString .= "CONTAINS (". $columnString .", \"$phrasesString\")";
  223. }
  224. }
  225. /* NORMAL WHERE */
  226. else {
  227. if (count($where->getConditions()) < 1) {
  228. NULL;
  229. }
  230. else {
  231. foreach ($where->getConditions() as $condition) {
  232. $columnName = $condition["column"];
  233. $glue = $condition["glue"] > 0 ? "OR" : "AND";
  234. switch ($condition["comparison"]) {
  235. case -3 : $comparisonValue = strtoupper($this->getValueWrapped($condition["value"])) == "NULL"
  236. ? " IS NOT NULL"
  237. : " != ". $this->getValueWrapped($condition["value"]) .""; break;
  238. case -2 : $comparisonValue =" < ". $this->getValueWrapped($condition["value"]) .""; break;
  239. case -1 : $comparisonValue =" <= ". $this->getValueWrapped($condition["value"]) .""; break;
  240. case 0 : $comparisonValue = strtoupper($this->getValueWrapped($condition["value"])) == "NULL"
  241. ? " IS NULL"
  242. : " = ". $this->getValueWrapped($condition["value"]) .""; break;
  243. case 1 : $comparisonValue =" >= ". $this->getValueWrapped($condition["value"]) .""; break;
  244. case 2 : $comparisonValue =" > ". $this->getValueWrapped($condition["value"]) .""; break;
  245. case 3 : $comparisonValue =" LIKE ". $this->getValueWrapped("%". $condition["value"] ."") .""; break;
  246. case 4 : $comparisonValue =" LIKE ". $this->getValueWrapped("%". $condition["value"] ."%") .""; break;
  247. case 5 : $comparisonValue =" LIKE ". $this->getValueWrapped("". $condition["value"] ."%") .""; break;
  248. default : throw new DbControlException("Ilegal comparison '". $condition["comparison"] ."'!");
  249. }
  250. $whereString .= strlen($whereString) > 0 ? " $glue " : "";
  251. $whereString .= reset(self::getQuotesColumnName()) . $columnName . end(self::getQuotesColumnName()) . $comparisonValue;
  252. }
  253. }
  254. }
  255. foreach ($where->getWheres() as $subWhereSet) {
  256. $subWhere = $subWhereSet["where"];
  257. $subWhereGlue = $subWhereSet["glue"] > 0 ? "OR" : "AND";
  258. if (strlen($subWhereString = $this->getWhereStringByObject($subWhere, true)) < 1) {
  259. continue;
  260. }
  261. $whereString = strlen($whereString) > 0 ? "($whereString) $subWhereGlue " : "";
  262. $whereString .= "($subWhereString)";
  263. }
  264. if (!$isSub && strlen($whereString) > 0) {
  265. $whereString = "WHERE $whereString";
  266. }
  267. return $whereString;
  268. }
  269. /**
  270. * returns ORDER BY clause as string generated from given array
  271. * @param array $orderBy
  272. * @param bool $reverse - order by statemants generated in reverse logic if true (only for cross platform emulations of some unavailable queries)
  273. * @return string
  274. */
  275. protected function getOrderByStringByArray ($orderBy = array(), $reverse = false) {
  276. $orderByString = "";
  277. if (count($orderBy) < 1) {
  278. $orderByString = "";
  279. }
  280. else {
  281. foreach ($orderBy as $columnName => $value) {
  282. $orderByString .= strlen($orderByString) > 0 ? ", " : "";
  283. $orderByString .= reset(self::getQuotesColumnName()) ."$columnName". end(self::getQuotesColumnName()) ."";
  284. if ($reverse) { $orderByString .= $value < 1 ? " ASC" : " DESC"; }
  285. else { $orderByString .= $value < 1 ? " DESC" : " ASC"; }
  286. }
  287. $orderByString = "ORDER BY $orderByString";
  288. }
  289. return $orderByString;
  290. }
  291. public function getValueFormatedDateTime($timeStamp = 0) {
  292. if (!is_int($timeStamp)) {
  293. throw new DbControlException("Ilegal parameter timeStamp. Must be NOT-NULL integer.");
  294. }
  295. if ($timeStamp < 1) {
  296. throw new DbControlException("Ilegal parameter timeStamp. Must be NOT-NULL integer.");
  297. }
  298. return date("d.m.Y H:i:s", $timeStamp);
  299. }
  300. public function getQuotesDatabaseName() {
  301. return array("[", "]");
  302. }
  303. public function getQuotesTableName() {
  304. return array("[", "]");
  305. }
  306. public function getQuotesColumnName() {
  307. return array("[", "]");
  308. }
  309. public function getQuotesValue() {
  310. return array("'", "'");
  311. }
  312. protected function escapeString($string = "") {
  313. $string = str_replace("'", "''", $string);
  314. return $string;
  315. }
  316. }
  317. ?>