Database
[ ]
[ ]
[ ]
[ ]
[ ]
Source for file query_select_mssql.php
Documentation is available at query_select_mssql.php
1. <?php
2. /**
3. * File containing the ezcQuerySelectMssql class.
4. *
5. * @package Database
6. * @version 1.0
7. * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
8. * @license http://ez.no/licenses/new_bsd New BSD License
9. */
10.
11. /**
12. * SQL Server specific implementation of ezcQuery.
13. *
14. * This class reimplements the LIMIT method in which the
15. * SQL Server differs from the standard implementation in ezcQuery.
16. *
17. * @see ezcQuery
18. * @package Database
19. * @version 1.4
20. */
21. class ezcQuerySelectMssql extends ezcQuerySelect
22. {
23. /**
24. * If a limit and/or offset has been set for this query.
25. *
26. * @var bool
27. */
28. private $hasLimit = false;
29.
30. /**
31. * The limit set.
32. *
33. * @var int
34. */
35. private $limit = 0;
36.
37. /**
38. * The offset set.
39. *
40. * @var int
41. */
42. private $offset = 0;
43.
44. /**
45. * Same as ezcQuerySelect::$orderString but inverted
46. * for use in the LIMIT functionality.
47. *
48. * @var string
49. */
50. private $invertedOrderString = null;
51.
52. /**
53. * Resets the query object for reuse.
54. *
55. * @return void
56. */
57. public function reset()
58. {
59. $this->hasLimit = false;
60. $this->limit = 0;
61. $this->offset = 0;
62. $this->orderColumns = array();
63. parent::reset();
64. }
65.
66. /**
67. * Returns SQL that limits the result set.
68. *
69. * $limit controls the maximum number of rows that will be returned.
70. * $offset controls which row that will be the first in the result
71. * set from the total amount of matching rows.
72. *
73. * @param int $limit integer expression
74. * @param int $offset integer expression
75. * @return void
76. */
77. public function limit( $limit, $offset = 0 )
78. {
79. $this->hasLimit = true;
80. $this->limit = $limit;
81. $this->offset = $offset;
82. return $this;
83. }
84.
85. /**
86. * Saves the ordered columns in an internal array so we can invert that order
87. * if we need to in the limit() workaround
88. *
89. * @param string $column a column name in the result set
90. * @param string $type if the column should be sorted ascending or descending.
91. * you can specify this using ezcQuerySelect::ASC or ezcQuerySelect::DESC
92. * @return ezcQuery a pointer to $this
93. */
94. public function orderBy( $column, $type = self::ASC )
95. {
96. if ( $this->invertedOrderString )
97. {
98. $this->invertedOrderString .= ', ';
99. }
100. else
101. {
102. $this->invertedOrderString = 'ORDER BY ';
103. }
104. $this->invertedOrderString .= $column . ' ' . ( $type == self::ASC ? self::DESC : self::ASC );
105. return parent::orderBy( $column, $type );
106. }
107.
108. /**
109. * Transforms the $query to make it only select the $rowCount first rows
110. *
111. * @param int $rowCount number of rows to return
112. * @param string $query SQL select query
113. * @return string
114. */
115. static private function top( $rowCount, $query )
116. {
117. return 'SELECT TOP ' . $rowCount . substr( $query, strlen( 'SELECT' ) );
118. }
119.
120. /**
121. * Transforms the query from the parent to provide LIMIT functionality.
122. *
123. * Note: doesn't work exactly like the MySQL equivalent; it will always return
124. * $limit rows even if $offset + $limit exceeds the total number of rows.
125. *
126. * @throws ezcQueryInvalidException if offset is used and orderBy is not.
127. * @return string
128. */
129. public function getQuery()
130. {
131. $query = parent::getQuery();
132. if ( $this->hasLimit )
133. {
134. if ( $this->offset)
135. {
136. if ( !$this->orderString )
137. {
138. // Uh ow. We need some columns to sort in the oposite order to make this work
139. throw new ezcQueryInvalidException( "LIMIT workaround for MS SQL", "orderBy() was not called before getQuery()." );
140. }
141. return 'SELECT * FROM ( SELECT TOP ' . $this->limit . ' * FROM ( ' . self::top( $this->offset + $this->limit, $query ) . ' ) AS ezcDummyTable1 ' . $this->invertedOrderString . ' ) AS ezcDummyTable2 ' . $this->orderString;
142. }
143. return self::top( $this->limit, $query );
144. }
145. return $query;
146. }
147. }
148.
149. ?>
Last updated: Wed, 18 Jun 2008