File tree Expand file tree Collapse file tree 5 files changed +189
-1
lines changed
src/Mouf/Database/QueryWriter Expand file tree Collapse file tree 5 files changed +189
-1
lines changed Original file line number Diff line number Diff line change 16
16
"php" : " >=5.3.0" ,
17
17
"mouf/database.dbconnection" : " ~2.0" ,
18
18
"mouf/utils.common.conditioninterface" : " ~2.0" ,
19
- "mouf/utils.value.value-interface" : " ~1.0"
19
+ "mouf/utils.value.value-interface" : " ~1.0" ,
20
+ "mouf/utils.common.paginable-interface" : " ~1.0"
20
21
},
21
22
"autoload" : {
22
23
"psr-0" : {
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Mouf \Database \QueryWriter ;
3
+
4
+ use Mouf \Utils \Value \IntValueInterface ;
5
+
6
+ use Mouf \Database \DBConnection \ConnectionInterface ;
7
+
8
+ /**
9
+ * A utility class that can compute the number of results returned by a query.
10
+ * It does so by embedding the query into a SELECT count(*) query and computing the results.
11
+ *
12
+ * @author David Negrier
13
+ */
14
+ class CountNbResult implements IntValueInterface {
15
+
16
+ /**
17
+ * The Select statement.
18
+ *
19
+ * @var Select
20
+ */
21
+ private $ select ;
22
+
23
+ /**
24
+ * The connection to the database.
25
+ *
26
+ * @var ConnectionInterface
27
+ */
28
+ private $ connection ;
29
+
30
+ /**
31
+ *
32
+ * @param Select $select
33
+ * @param ConnectionInterface $connection
34
+ */
35
+ public function __construct (Select $ select , ConnectionInterface $ connection ) {
36
+ $ this ->select = $ select ;
37
+ $ this ->connection = $ connection ;
38
+ }
39
+
40
+ /**
41
+ * (non-PHPdoc)
42
+ * @see \Mouf\Utils\Value\ArrayValueInterface::val()
43
+ */
44
+ public function val () {
45
+ $ sql = "SELECT count(*) as cnt FROM ( " .$ this ->select ->toSql ().") tmp " ;
46
+ // FIXME: add support for params!
47
+ return $ this ->connection ->getOne ($ sql );
48
+ }
49
+
50
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Mouf \Database \QueryWriter ;
3
+
4
+ use Mouf \Utils \Common \PaginableInterface ;
5
+
6
+ use Mouf \Utils \Value \ArrayValueInterface ;
7
+
8
+ use Mouf \Database \DBConnection \ConnectionInterface ;
9
+
10
+ /**
11
+ * A class that can execute a query and expose the results.
12
+ *
13
+ * @author David Negrier
14
+ */
15
+ class QueryResult implements ArrayValueInterface, PaginableInterface {
16
+
17
+ /**
18
+ * The Select statement.
19
+ *
20
+ * @var Select
21
+ */
22
+ private $ select ;
23
+
24
+ /**
25
+ * The connection to the database.
26
+ *
27
+ * @var ConnectionInterface
28
+ */
29
+ private $ connection ;
30
+
31
+ private $ limit ;
32
+ private $ offset ;
33
+
34
+ /**
35
+ *
36
+ * @param Select $select
37
+ * @param ConnectionInterface $connection
38
+ */
39
+ public function __construct (Select $ select , ConnectionInterface $ connection ) {
40
+ $ this ->select = $ select ;
41
+ $ this ->connection = $ connection ;
42
+ }
43
+
44
+ /**
45
+ * (non-PHPdoc)
46
+ * @see \Mouf\Utils\Value\ArrayValueInterface::val()
47
+ */
48
+ public function val () {
49
+ // FIXME: add support for params!
50
+ $ pdoStatement = $ this ->connection ->query ($ this ->select ->toSql (), $ this ->offset , $ this ->limit );
51
+ return new ResultSet ($ pdoStatement );
52
+ }
53
+
54
+ /**
55
+ * Paginates the result set.
56
+ *
57
+ * @param int $limit
58
+ * @param int $offset
59
+ */
60
+ public function paginate ($ limit , $ offset = 0 ) {
61
+ $ this ->limit = $ limit ;
62
+ $ this ->offset = $ offset ;
63
+ }
64
+
65
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Mouf \Database \QueryWriter ;
3
+
4
+ use Mouf \Utils \Value \ArrayValueInterface ;
5
+
6
+ use Mouf \Database \DBConnection \ConnectionInterface ;
7
+
8
+ /**
9
+ * Wraps the results of a PDOStatement.
10
+ *
11
+ * @author David Negrier
12
+ */
13
+ class ResultSet implements \Iterator {
14
+
15
+ /**
16
+ *
17
+ * @var \PDOStatement
18
+ */
19
+ private $ statement ;
20
+ private $ castToClass ;
21
+ private $ key = 0 ;
22
+ private $ result ;
23
+ private $ fetched = false ;
24
+ private $ rewindCalls = 0 ;
25
+
26
+ public function __construct (\PDOStatement $ statement , $ castToClass = "" ) {
27
+ $ this ->statement = $ statement ;
28
+ $ this ->castToClass = $ castToClass ;
29
+ }
30
+
31
+
32
+ function rewind () {
33
+ $ this ->rewindCalls ++;
34
+ if ($ this ->rewindCalls == 2 ) {
35
+ throw new \Exception ("Error: rewind is not possible in a database rowset. You can call 'foreach' on the rowset only once. Use CachedResultSet to be able to call the result several times. TODO: develop CachedResultSet " );
36
+ }
37
+ }
38
+
39
+ function current () {
40
+
41
+ if (!$ this ->fetched ) {
42
+ $ this ->fetch ();
43
+ }
44
+
45
+ return $ this ->result ;
46
+ }
47
+
48
+ function key () {
49
+ return $ this ->key ;
50
+ }
51
+
52
+ function next () {
53
+ ++$ this ->key ;
54
+ $ this ->fetched = false ;
55
+ $ this ->fetch ();
56
+ }
57
+
58
+ private function fetch () {
59
+ $ this ->result = $ this ->statement ->fetch (\PDO ::FETCH_ASSOC );
60
+ $ this ->fetched = true ;
61
+ }
62
+
63
+ function valid () {
64
+
65
+ if (!$ this ->fetched ) {
66
+ $ this ->fetch ();
67
+ }
68
+
69
+ return $ this ->result !== false ;
70
+ }
71
+ }
Original file line number Diff line number Diff line change @@ -123,4 +123,5 @@ public function toSql() {
123
123
124
124
return $ sql ;
125
125
}
126
+
126
127
}
You can’t perform that action at this time.
0 commit comments