@@ -13,25 +13,41 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverN
13
13
14
14
15
15
test (function () use ($ context ) { // Testing Selection caching
16
- $ bookSelection = $ context ->table ('book ' )->wherePrimary (2 );
17
- Assert::same (reformat ('SELECT * FROM [book] WHERE ([book].[id] = ?) ' ), $ bookSelection ->getSql ());
18
-
16
+ $ sql = array ();
17
+ for ($ i = 0 ; $ i < 4 ; $ i += 1 ) {
18
+ if ($ i !== 2 ) {
19
+ $ bookSelection = $ context ->table ('book ' )->wherePrimary (2 );
20
+ }
19
21
20
- $ book = $ bookSelection ->fetch ();
21
- $ book ->title ;
22
- $ book ->translator ;
23
- $ bookSelection ->__destruct ();
24
- $ bookSelection = $ context ->table ('book ' )->wherePrimary (2 );
25
- Assert::same (reformat ('SELECT [id], [title], [translator_id] FROM [book] WHERE ([book].[id] = ?) ' ), $ bookSelection ->getSql ());
22
+ $ sql [] = $ bookSelection ->getSql ();
26
23
24
+ if ($ i !== 2 ) {
25
+ $ book = $ bookSelection ->fetch ();
26
+ $ book ->title ;
27
+ $ book ->translator ;
28
+ if ($ i === 1 ) {
29
+ $ book ->author ;
30
+ } else {
31
+ $ bookSelection ->__destruct ();
32
+ }
33
+ } else {
34
+ $ bookSelection ->__destruct ();
35
+ }
36
+ }
27
37
28
- $ book = $ bookSelection ->fetch ();
29
- $ book ->author_id ;
30
- Assert::same (reformat ('SELECT * FROM [book] WHERE ([book].[id] = ?) ' ), $ bookSelection ->getSql ());
38
+ /*
39
+ * schedule:
40
+ * - fetch all columns / cycle 1
41
+ * - fetch used columns, require another and fetch all again / cycle 2, 3
42
+ * - fetch used column with new used column / cycle 4
43
+ */
31
44
32
- $ bookSelection ->__destruct ();
33
- $ bookSelection = $ context ->table ('book ' )->wherePrimary (2 );
34
- Assert::same (reformat ('SELECT [id], [title], [translator_id], [author_id] FROM [book] WHERE ([book].[id] = ?) ' ), $ bookSelection ->getSql ());
45
+ Assert::same (array (
46
+ reformat ('SELECT * FROM [book] WHERE ([book].[id] = ?) ' ),
47
+ reformat ('SELECT [id], [title], [translator_id] FROM [book] WHERE ([book].[id] = ?) ' ),
48
+ reformat ('SELECT * FROM [book] WHERE ([book].[id] = ?) ' ),
49
+ reformat ('SELECT [id], [title], [translator_id], [author_id] FROM [book] WHERE ([book].[id] = ?) ' )
50
+ ), $ sql );
35
51
});
36
52
37
53
@@ -107,61 +123,65 @@ test(function() use ($context) {
107
123
});
108
124
109
125
110
- test (function () use ($ context ) {
111
- $ author = $ context ->table ('author ' )->get (11 );
112
- $ books = $ author ->related ('book ' )->where ('translator_id ' , 99 ); // 0 rows
113
- foreach ($ books as $ book ) {}
114
- $ books ->__destruct ();
115
- unset($ author );
116
-
117
- $ author = $ context ->table ('author ' )->get (11 );
118
- $ books = $ author ->related ('book ' )->where ('translator_id ' , 11 );
119
- Assert::same (array ('id ' , 'author_id ' ), $ books ->getPreviousAccessedColumns ());
126
+ test (function () use ($ context ) { // Test saving joining keys even with 0 rows
127
+ $ cols = array ();
128
+ for ($ i = 0 ; $ i < 2 ; $ i += 1 ) {
129
+ $ author = $ context ->table ('author ' )->get (11 );
130
+ $ books = $ author ->related ('book ' )->where ('translator_id ' , 99 ); // 0 rows
131
+ $ cols [] = $ books ->getPreviousAccessedColumns ();
132
+ foreach ($ books as $ book ) {}
133
+ $ books ->__destruct ();
134
+ }
135
+
136
+ Assert::same (array (
137
+ array (),
138
+ array ('id ' , 'author_id ' ),
139
+ ), $ cols );
120
140
});
121
141
122
142
123
143
test (function () use ($ context ) { // Test saving the union of needed cols, the second call is subset
124
- $ author = $ context -> table ( ' author ' )-> get ( 11 );
125
- $ books = $ author -> related ( ' book ' );
126
- foreach ( $ books as $ book ) {
127
- $ book -> translator_id ;
128
- $ book -> title ;
129
- }
130
- $ books -> __destruct ();
131
-
132
- $ author = $ context -> table ( ' author ' )-> get ( 11 );
133
- $ books = $ author -> related ( ' book ' ) ;
134
- foreach ( $ books as $ book ) {
135
- $ book -> title ;
144
+ $ cols = array ( );
145
+ for ( $ i = 0 ; $ i < 3 ; $ i += 1 ) {
146
+ $ author = $ context -> table ( ' author ' )-> get ( 11 );
147
+ $ books = $ author -> related ( ' book ' ) ;
148
+ $ cols [] = $ books -> getPreviousAccessedColumns () ;
149
+ foreach ( $ books as $ book ) {
150
+ if ( $ i === 0 ) {
151
+ $ book -> translator_id ;
152
+ }
153
+ $ book -> title ;
154
+ }
155
+ $ books -> __destruct () ;
136
156
}
137
- $ books ->__destruct ();
138
157
139
- $ author = $ context -> table ( ' author ' )-> get ( 11 );
140
- Assert:: same (
141
- reformat ( ' SELECT [id], [ author_id], [ translator_id], [ title] FROM [book] WHERE ([book].[author_id] IN (?)) ' ),
142
- $ author -> related ( ' book ' )-> getSql ()
143
- );
158
+ Assert:: same ( array (
159
+ array (),
160
+ array ( ' id ' , ' author_id ' , ' translator_id ' , ' title ' ),
161
+ array ( ' id ' , ' author_id ' , ' translator_id ' , ' title ' ),
162
+ ), $ cols ) ;
144
163
});
145
164
146
165
147
166
test (function () use ($ context ) { // Test saving the union of needed cols, the second call is not subset
148
- $ author = $ context ->table ('author ' )->get (11 );
149
- $ books = $ author ->related ('book ' );
150
- foreach ($ books as $ book ) {
151
- $ book ->translator_id ;
152
- }
153
- $ books ->__destruct ();
154
-
155
- $ author = $ context ->table ('author ' )->get (11 );
156
- $ books = $ author ->related ('book ' );
157
- foreach ($ books as $ book ) {
158
- $ book ->title ;
167
+ $ cols = array ();
168
+ for ($ i = 0 ; $ i < 3 ; $ i += 1 ) {
169
+ $ author = $ context ->table ('author ' )->get (11 );
170
+ $ books = $ author ->related ('book ' );
171
+ $ cols [] = $ books ->getPreviousAccessedColumns ();
172
+ foreach ($ books as $ book ) {
173
+ if ($ i === 0 ) {
174
+ $ book ->translator_id ;
175
+ } else {
176
+ $ book ->title ;
177
+ }
178
+ }
179
+ $ books ->__destruct ();
159
180
}
160
- $ books ->__destruct ();
161
181
162
- $ author = $ context -> table ( ' author ' )-> get ( 11 );
163
- Assert:: same (
164
- reformat ( ' SELECT [id], [ author_id], [ translator_id], [title] FROM [book] WHERE ([book].[author_id] IN (?)) ' ),
165
- $ author -> related ( ' book ' )-> getSql ()
166
- );
182
+ Assert:: same ( array (
183
+ array (),
184
+ array ( ' id ' , ' author_id ' , ' translator_id ' ),
185
+ array ( ' id ' , ' author_id ' , ' translator_id ' , ' title ' ),
186
+ ), $ cols ) ;
167
187
});
0 commit comments