@@ -15,7 +15,7 @@ $connection = connectToDB()->getConnection();
15
15
Nette \Database \Helpers::loadFromFile ($ connection , __DIR__ . "/files/ {$ driverName }-nette_test1.sql " );
16
16
17
17
18
- test ('' , function () use ($ connection ) {
18
+ test ('fetches key-value pairs using column names in both directions ' , function () use ($ connection ) {
19
19
$ res = $ connection ->query ('SELECT * FROM book ORDER BY title ' );
20
20
Assert::same ([
21
21
1 => '1001 tipu a triku pro PHP ' ,
@@ -33,7 +33,7 @@ test('', function () use ($connection) {
33
33
});
34
34
35
35
36
- test ('' , function () use ($ connection ) {
36
+ test ('fetches pairs using column indexes ' , function () use ($ connection ) {
37
37
$ pairs = $ connection ->query ('SELECT title, id FROM book ORDER BY title ' )->fetchPairs (1 , 0 );
38
38
Assert::same ([
39
39
1 => '1001 tipu a triku pro PHP ' ,
@@ -44,7 +44,7 @@ test('', function () use ($connection) {
44
44
});
45
45
46
46
47
- test ('' , function () use ($ connection ) {
47
+ test ('fetches pairs with same column for key and value ' , function () use ($ connection ) {
48
48
$ pairs = $ connection ->query ('SELECT * FROM book ORDER BY id ' )->fetchPairs ('id ' , 'id ' );
49
49
Assert::same ([
50
50
1 => 1 ,
@@ -55,7 +55,7 @@ test('', function () use ($connection) {
55
55
});
56
56
57
57
58
- test ('' , function () use ($ connection ) {
58
+ test ('fetches pairs using key column only ' , function () use ($ connection ) {
59
59
$ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs ('id ' );
60
60
Assert::equal ([
61
61
1 => Nette \Database \Row::from (['id ' => 1 ]),
@@ -66,7 +66,7 @@ test('', function () use ($connection) {
66
66
});
67
67
68
68
69
- test ('' , function () use ($ connection ) {
69
+ test ('fetches pairs using datetime values as keys ' , function () use ($ connection ) {
70
70
$ pairs = $ connection ->query ('UPDATE author SET born = ? WHERE id = 11 ' , new DateTime ('2002-02-20 ' ));
71
71
$ pairs = $ connection ->query ('UPDATE author SET born = ? WHERE id = 12 ' , new DateTime ('2002-02-02 ' ));
72
72
$ pairs = $ connection ->query ('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born ' )->fetchPairs ('born ' , 'name ' );
@@ -77,64 +77,78 @@ test('', function () use ($connection) {
77
77
});
78
78
79
79
80
- $ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs ('id ' );
81
- Assert::equal ([
82
- 1 => Nette \Database \Row::from (['id ' => 1 ]),
83
- 2 => Nette \Database \Row::from (['id ' => 2 ]),
84
- 3 => Nette \Database \Row::from (['id ' => 3 ]),
85
- 4 => Nette \Database \Row::from (['id ' => 4 ]),
86
- ], $ pairs );
87
-
88
-
89
- $ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs (null , 'id ' );
90
- Assert::same ([
91
- 0 => 1 ,
92
- 1 => 2 ,
93
- 2 => 3 ,
94
- 3 => 4 ,
95
- ], $ pairs );
96
-
97
-
98
- $ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs ();
99
- Assert::same ([
100
- 0 => 1 ,
101
- 1 => 2 ,
102
- 2 => 3 ,
103
- 3 => 4 ,
104
- ], $ pairs );
105
-
106
-
107
- $ pairs = $ connection ->query ('SELECT id, id + 1 AS id1 FROM book ORDER BY id ' )->fetchPairs ();
108
- Assert::same ([
109
- 1 => 2 ,
110
- 2 => 3 ,
111
- 3 => 4 ,
112
- 4 => 5 ,
113
- ], $ pairs );
114
-
115
-
116
- $ pairs = $ connection ->query ('SELECT id, id + 1 AS id1, title FROM book ORDER BY id ' )->fetchPairs ();
117
- Assert::same ([
118
- 1 => 2 ,
119
- 2 => 3 ,
120
- 3 => 4 ,
121
- 4 => 5 ,
122
- ], $ pairs );
123
-
124
-
125
- $ pairs = $ connection ->query ('UPDATE author SET born = ? WHERE id = 11 ' , new DateTime ('2002-02-20 ' ));
126
- $ pairs = $ connection ->query ('UPDATE author SET born = ? WHERE id = 12 ' , new DateTime ('2002-02-02 ' ));
127
- $ pairs = $ connection ->query ('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born ' )->fetchPairs ('born ' , 'name ' );
128
- Assert::same ([
129
- '2002-02-02 00:00:00.000000 ' => 'David Grudl ' ,
130
- '2002-02-20 00:00:00.000000 ' => 'Jakub Vrana ' ,
131
- ], $ pairs );
132
-
133
-
134
- $ pairs = $ connection ->query ('SELECT 1.5 AS k, 1 AS v ' )->fetchPairs ();
135
- Assert::same ([
136
- '1.5 ' => 1 ,
137
- ], $ pairs );
80
+ test ('fetches pairs using id column with no key specified ' , function () use ($ connection ) {
81
+ $ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs ('id ' );
82
+ Assert::equal ([
83
+ 1 => Nette \Database \Row::from (['id ' => 1 ]),
84
+ 2 => Nette \Database \Row::from (['id ' => 2 ]),
85
+ 3 => Nette \Database \Row::from (['id ' => 3 ]),
86
+ 4 => Nette \Database \Row::from (['id ' => 4 ]),
87
+ ], $ pairs );
88
+ });
89
+
90
+
91
+ test ('fetches pairs with numeric keys when no key column specified ' , function () use ($ connection ) {
92
+ $ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs (null , 'id ' );
93
+ Assert::same ([
94
+ 0 => 1 ,
95
+ 1 => 2 ,
96
+ 2 => 3 ,
97
+ 3 => 4 ,
98
+ ], $ pairs );
99
+ });
100
+
101
+
102
+ test ('fetches pairs with default parameters ' , function () use ($ connection ) {
103
+ $ pairs = $ connection ->query ('SELECT id FROM book ORDER BY id ' )->fetchPairs ();
104
+ Assert::same ([
105
+ 0 => 1 ,
106
+ 1 => 2 ,
107
+ 2 => 3 ,
108
+ 3 => 4 ,
109
+ ], $ pairs );
110
+ });
111
+
112
+
113
+ test ('fetches pairs using expression as value ' , function () use ($ connection ) {
114
+ $ pairs = $ connection ->query ('SELECT id, id + 1 AS id1 FROM book ORDER BY id ' )->fetchPairs ();
115
+ Assert::same ([
116
+ 1 => 2 ,
117
+ 2 => 3 ,
118
+ 3 => 4 ,
119
+ 4 => 5 ,
120
+ ], $ pairs );
121
+ });
122
+
123
+
124
+ test ('fetches pairs with additional columns ' , function () use ($ connection ) {
125
+ $ pairs = $ connection ->query ('SELECT id, id + 1 AS id1, title FROM book ORDER BY id ' )->fetchPairs ();
126
+ Assert::same ([
127
+ 1 => 2 ,
128
+ 2 => 3 ,
129
+ 3 => 4 ,
130
+ 4 => 5 ,
131
+ ], $ pairs );
132
+ });
133
+
134
+
135
+ test ('fetches pairs with datetime values ' , function () use ($ connection ) {
136
+ $ pairs = $ connection ->query ('UPDATE author SET born = ? WHERE id = 11 ' , new DateTime ('2002-02-20 ' ));
137
+ $ pairs = $ connection ->query ('UPDATE author SET born = ? WHERE id = 12 ' , new DateTime ('2002-02-02 ' ));
138
+ $ pairs = $ connection ->query ('SELECT * FROM author WHERE born IS NOT NULL ORDER BY born ' )->fetchPairs ('born ' , 'name ' );
139
+ Assert::same ([
140
+ '2002-02-02 00:00:00.000000 ' => 'David Grudl ' ,
141
+ '2002-02-20 00:00:00.000000 ' => 'Jakub Vrana ' ,
142
+ ], $ pairs );
143
+ });
144
+
145
+
146
+ test ('fetches pairs with float key ' , function () use ($ connection ) {
147
+ $ pairs = $ connection ->query ('SELECT 1.5 AS k, 1 AS v ' )->fetchPairs ();
148
+ Assert::same ([
149
+ '1.5 ' => 1 ,
150
+ ], $ pairs );
151
+ });
138
152
139
153
140
154
test ('with callback ' , function () use ($ connection ) {
0 commit comments