@@ -2204,14 +2204,19 @@ public function testInsertSimple() {
2204
2204
]);
2205
2205
$ result = $ query ->sql ();
2206
2206
$ this ->assertQuotedQuery (
2207
- 'INSERT INTO <articles> \(<title>, <body>\) ' .
2207
+ 'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )? ' .
2208
2208
'VALUES \(:c0, :c1\) ' ,
2209
2209
$ result ,
2210
2210
true
2211
2211
);
2212
2212
2213
2213
$ result = $ query ->execute ();
2214
- $ this ->assertCount (1 , $ result , '1 row should be inserted ' );
2214
+ $ result ->closeCursor ();
2215
+
2216
+ //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2217
+ if (!$ this ->connection ->driver () instanceof \Cake \Database \Driver \Sqlserver) {
2218
+ $ this ->assertCount (1 , $ result , '1 row should be inserted ' );
2219
+ }
2215
2220
2216
2221
$ expected = [
2217
2222
[
@@ -2240,14 +2245,19 @@ public function testInsertSparseRow() {
2240
2245
]);
2241
2246
$ result = $ query ->sql ();
2242
2247
$ this ->assertQuotedQuery (
2243
- 'INSERT INTO <articles> \(<title>, <body>\) ' .
2248
+ 'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )? ' .
2244
2249
'VALUES \(:c0, :c1\) ' ,
2245
2250
$ result ,
2246
2251
true
2247
2252
);
2248
2253
2249
2254
$ result = $ query ->execute ();
2250
- $ this ->assertCount (1 , $ result , '1 row should be inserted ' );
2255
+ $ result ->closeCursor ();
2256
+
2257
+ //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2258
+ if (!$ this ->connection ->driver () instanceof \Cake \Database \Driver \Sqlserver) {
2259
+ $ this ->assertCount (1 , $ result , '1 row should be inserted ' );
2260
+ }
2251
2261
2252
2262
$ expected = [
2253
2263
[
@@ -2278,7 +2288,12 @@ public function testInsertMultipleRowsSparse() {
2278
2288
]);
2279
2289
2280
2290
$ result = $ query ->execute ();
2281
- $ this ->assertCount (2 , $ result , '2 rows should be inserted ' );
2291
+ $ result ->closeCursor ();
2292
+
2293
+ //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2294
+ if (!$ this ->connection ->driver () instanceof \Cake \Database \Driver \Sqlserver) {
2295
+ $ this ->assertCount (2 , $ result , '2 rows should be inserted ' );
2296
+ }
2282
2297
2283
2298
$ expected = [
2284
2299
[
@@ -2319,15 +2334,20 @@ public function testInsertFromSelect() {
2319
2334
2320
2335
$ result = $ query ->sql ();
2321
2336
$ this ->assertQuotedQuery (
2322
- 'INSERT INTO <articles> \(<title>, <body>, <author_id>\) SELECT ' ,
2337
+ 'INSERT INTO <articles> \(<title>, <body>, <author_id>\) (OUTPUT INSERTED\.\* )? SELECT ' ,
2323
2338
$ result ,
2324
2339
true
2325
2340
);
2326
2341
$ this ->assertQuotedQuery (
2327
2342
'SELECT <name>, \'some text \', 99 FROM <authors> ' , $ result , true );
2328
2343
$ result = $ query ->execute ();
2344
+ $ result ->closeCursor ();
2345
+
2346
+ //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2347
+ if (!$ this ->connection ->driver () instanceof \Cake \Database \Driver \Sqlserver) {
2348
+ $ this ->assertCount (1 , $ result );
2349
+ }
2329
2350
2330
- $ this ->assertCount (1 , $ result );
2331
2351
$ result = (new Query ($ this ->connection ))->select ('* ' )
2332
2352
->from ('articles ' )
2333
2353
->where (['author_id ' => 99 ])
@@ -2376,24 +2396,61 @@ public function testInsertFailureMixingTypesQueryFirst() {
2376
2396
*/
2377
2397
public function testInsertExpressionValues () {
2378
2398
$ query = new Query ($ this ->connection );
2379
- $ query ->insert (['title ' ])
2399
+ $ query ->insert (['title ' , ' author_id ' ])
2380
2400
->into ('articles ' )
2381
- ->values (['title ' => $ query ->newExpr ("SELECT 'jose' " )]);
2401
+ ->values (['title ' => $ query ->newExpr ("SELECT 'jose' " ), ' author_id ' => 99 ]);
2382
2402
2383
2403
$ result = $ query ->execute ();
2404
+ $ result ->closeCursor ();
2405
+
2406
+ //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2407
+ if (!$ this ->connection ->driver () instanceof \Cake \Database \Driver \Sqlserver) {
2408
+ $ this ->assertCount (1 , $ result );
2409
+ }
2410
+
2411
+ $ result = (new Query ($ this ->connection ))->select ('* ' )
2412
+ ->from ('articles ' )
2413
+ ->where (['author_id ' => 99 ])
2414
+ ->execute ();
2384
2415
$ this ->assertCount (1 , $ result );
2416
+ $ expected = [
2417
+ 'id ' => 4 ,
2418
+ 'title ' => 'jose ' ,
2419
+ 'body ' => null ,
2420
+ 'author_id ' => '99 ' ,
2421
+ 'published ' => 'N ' ,
2422
+ ];
2423
+ $ this ->assertEquals ($ expected , $ result ->fetch ('assoc ' ));
2385
2424
2386
2425
$ subquery = new Query ($ this ->connection );
2387
2426
$ subquery ->select (['name ' ])
2388
2427
->from ('authors ' )
2389
2428
->where (['id ' => 1 ]);
2390
2429
2391
2430
$ query = new Query ($ this ->connection );
2392
- $ query ->insert (['title ' ])
2431
+ $ query ->insert (['title ' , ' author_id ' ])
2393
2432
->into ('articles ' )
2394
- ->values (['title ' => $ subquery ]);
2433
+ ->values (['title ' => $ subquery, ' author_id ' => 100 ]);
2395
2434
$ result = $ query ->execute ();
2435
+ $ result ->closeCursor ();
2436
+ //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2437
+ if (!$ this ->connection ->driver () instanceof \Cake \Database \Driver \Sqlserver) {
2438
+ $ this ->assertCount (1 , $ result );
2439
+ }
2440
+
2441
+ $ result = (new Query ($ this ->connection ))->select ('* ' )
2442
+ ->from ('articles ' )
2443
+ ->where (['author_id ' => 100 ])
2444
+ ->execute ();
2396
2445
$ this ->assertCount (1 , $ result );
2446
+ $ expected = [
2447
+ 'id ' => 5 ,
2448
+ 'title ' => 'mariano ' ,
2449
+ 'body ' => null ,
2450
+ 'author_id ' => '100 ' ,
2451
+ 'published ' => 'N ' ,
2452
+ ];
2453
+ $ this ->assertEquals ($ expected , $ result ->fetch ('assoc ' ));
2397
2454
}
2398
2455
2399
2456
/**
@@ -2937,7 +2994,8 @@ public function testSqlCaseStatement() {
2937
2994
'comment ' => 'In limbo ' ,
2938
2995
'published ' => 'L '
2939
2996
])
2940
- ->execute ();
2997
+ ->execute ()
2998
+ ->closeCursor ();
2941
2999
2942
3000
$ query = new Query ($ this ->connection );
2943
3001
$ conditions = [
0 commit comments