| ... | ... |
@@ -1180,5 +1180,504 @@ $result->end_filter(key1 => undef); |
| 1180 | 1180 |
$row = $result->one; |
| 1181 | 1181 |
is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
|
| 1182 | 1182 |
|
| 1183 |
+test 'remove_end_filter and remove_filter'; |
|
| 1184 |
+$dbi = DBIx::Custom->connect; |
|
| 1185 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1186 |
+$dbi->execute($create_table1); |
|
| 1187 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1188 |
+$result = $dbi->select(table => 'table1'); |
|
| 1189 |
+$row = $result |
|
| 1190 |
+ ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
|
|
| 1191 |
+ ->remove_filter |
|
| 1192 |
+ ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
|
|
| 1193 |
+ ->remove_end_filter |
|
| 1194 |
+ ->fetch_first; |
|
| 1195 |
+is_deeply($row, [1, 2]); |
|
| 1196 |
+ |
|
| 1197 |
+test 'empty where select'; |
|
| 1198 |
+$dbi = DBIx::Custom->connect; |
|
| 1199 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1200 |
+$dbi->execute($create_table1); |
|
| 1201 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1202 |
+$result = $dbi->select(table => 'table1', where => {});
|
|
| 1203 |
+$row = $result->one; |
|
| 1204 |
+is_deeply($row, {key1 => 1, key2 => 2});
|
|
| 1205 |
+ |
|
| 1206 |
+test 'select query option'; |
|
| 1207 |
+$dbi = DBIx::Custom->connect; |
|
| 1208 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1209 |
+$dbi->execute($create_table1); |
|
| 1210 |
+$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
|
|
| 1211 |
+is(ref $query, 'DBIx::Custom::Query'); |
|
| 1212 |
+$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
|
|
| 1213 |
+is(ref $query, 'DBIx::Custom::Query'); |
|
| 1214 |
+$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
|
|
| 1215 |
+is(ref $query, 'DBIx::Custom::Query'); |
|
| 1216 |
+$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
|
|
| 1217 |
+is(ref $query, 'DBIx::Custom::Query'); |
|
| 1218 |
+ |
|
| 1219 |
+test 'where'; |
|
| 1220 |
+$dbi = DBIx::Custom->connect; |
|
| 1221 |
+eval { $dbi->execute('drop table table1') };
|
|
| 1222 |
+$dbi->execute($create_table1); |
|
| 1223 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1224 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 1225 |
+$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
| 1226 |
+is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
|
|
| 1227 |
+ |
|
| 1228 |
+$where = $dbi->where |
|
| 1229 |
+ ->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
| 1230 |
+ ->param({key1 => 1});
|
|
| 1231 |
+ |
|
| 1232 |
+$result = $dbi->select( |
|
| 1233 |
+ table => 'table1', |
|
| 1234 |
+ where => $where |
|
| 1235 |
+); |
|
| 1236 |
+$row = $result->all; |
|
| 1237 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1238 |
+ |
|
| 1239 |
+$result = $dbi->select( |
|
| 1240 |
+ table => 'table1', |
|
| 1241 |
+ where => [ |
|
| 1242 |
+ ['and', 'key1 = :key1', 'key2 = :key2'], |
|
| 1243 |
+ {key1 => 1}
|
|
| 1244 |
+ ] |
|
| 1245 |
+); |
|
| 1246 |
+$row = $result->all; |
|
| 1247 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1248 |
+ |
|
| 1249 |
+$where = $dbi->where |
|
| 1250 |
+ ->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
| 1251 |
+ ->param({key1 => 1, key2 => 2});
|
|
| 1252 |
+$result = $dbi->select( |
|
| 1253 |
+ table => 'table1', |
|
| 1254 |
+ where => $where |
|
| 1255 |
+); |
|
| 1256 |
+$row = $result->all; |
|
| 1257 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1258 |
+ |
|
| 1259 |
+$where = $dbi->where |
|
| 1260 |
+ ->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
| 1261 |
+ ->param({});
|
|
| 1262 |
+$result = $dbi->select( |
|
| 1263 |
+ table => 'table1', |
|
| 1264 |
+ where => $where, |
|
| 1265 |
+); |
|
| 1266 |
+$row = $result->all; |
|
| 1267 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1268 |
+ |
|
| 1269 |
+$where = $dbi->where |
|
| 1270 |
+ ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2']) |
|
| 1271 |
+ ->param({key1 => [0, 3], key2 => 2});
|
|
| 1272 |
+$result = $dbi->select( |
|
| 1273 |
+ table => 'table1', |
|
| 1274 |
+ where => $where, |
|
| 1275 |
+); |
|
| 1276 |
+$row = $result->all; |
|
| 1277 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1278 |
+ |
|
| 1279 |
+$where = $dbi->where; |
|
| 1280 |
+$result = $dbi->select( |
|
| 1281 |
+ table => 'table1', |
|
| 1282 |
+ where => $where |
|
| 1283 |
+); |
|
| 1284 |
+$row = $result->all; |
|
| 1285 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1286 |
+ |
|
| 1287 |
+eval {
|
|
| 1288 |
+$where = $dbi->where |
|
| 1289 |
+ ->clause(['uuu']); |
|
| 1290 |
+$result = $dbi->select( |
|
| 1291 |
+ table => 'table1', |
|
| 1292 |
+ where => $where |
|
| 1293 |
+); |
|
| 1294 |
+}; |
|
| 1295 |
+ok($@); |
|
| 1296 |
+ |
|
| 1297 |
+$where = $dbi->where; |
|
| 1298 |
+is("$where", '');
|
|
| 1299 |
+ |
|
| 1300 |
+$where = $dbi->where |
|
| 1301 |
+ ->clause(['or', ('key1 = :key1') x 2])
|
|
| 1302 |
+ ->param({key1 => [1, 3]});
|
|
| 1303 |
+$result = $dbi->select( |
|
| 1304 |
+ table => 'table1', |
|
| 1305 |
+ where => $where, |
|
| 1306 |
+); |
|
| 1307 |
+$row = $result->all; |
|
| 1308 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1309 |
+ |
|
| 1310 |
+$where = $dbi->where |
|
| 1311 |
+ ->clause(['or', ('key1 = :key1') x 2])
|
|
| 1312 |
+ ->param({key1 => [1]});
|
|
| 1313 |
+$result = $dbi->select( |
|
| 1314 |
+ table => 'table1', |
|
| 1315 |
+ where => $where, |
|
| 1316 |
+); |
|
| 1317 |
+$row = $result->all; |
|
| 1318 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1319 |
+ |
|
| 1320 |
+$where = $dbi->where |
|
| 1321 |
+ ->clause(['or', ('key1 = :key1') x 2])
|
|
| 1322 |
+ ->param({key1 => 1});
|
|
| 1323 |
+$result = $dbi->select( |
|
| 1324 |
+ table => 'table1', |
|
| 1325 |
+ where => $where, |
|
| 1326 |
+); |
|
| 1327 |
+$row = $result->all; |
|
| 1328 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1329 |
+ |
|
| 1330 |
+$where = $dbi->where |
|
| 1331 |
+ ->clause('key1 = :key1')
|
|
| 1332 |
+ ->param({key1 => 1});
|
|
| 1333 |
+$result = $dbi->select( |
|
| 1334 |
+ table => 'table1', |
|
| 1335 |
+ where => $where, |
|
| 1336 |
+); |
|
| 1337 |
+$row = $result->all; |
|
| 1338 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1339 |
+ |
|
| 1340 |
+$where = $dbi->where |
|
| 1341 |
+ ->clause('key1 = :key1 key2 = :key2')
|
|
| 1342 |
+ ->param({key1 => 1});
|
|
| 1343 |
+eval{$where->to_string};
|
|
| 1344 |
+like($@, qr/one column/); |
|
| 1345 |
+ |
|
| 1346 |
+$where = $dbi->where |
|
| 1347 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1348 |
+ ->param({key1 => [$dbi->not_exists, 1, 3]});
|
|
| 1349 |
+$result = $dbi->select( |
|
| 1350 |
+ table => 'table1', |
|
| 1351 |
+ where => $where, |
|
| 1352 |
+); |
|
| 1353 |
+$row = $result->all; |
|
| 1354 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1355 |
+ |
|
| 1356 |
+$where = $dbi->where |
|
| 1357 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1358 |
+ ->param({key1 => [1, $dbi->not_exists, 3]});
|
|
| 1359 |
+$result = $dbi->select( |
|
| 1360 |
+ table => 'table1', |
|
| 1361 |
+ where => $where, |
|
| 1362 |
+); |
|
| 1363 |
+$row = $result->all; |
|
| 1364 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1365 |
+ |
|
| 1366 |
+$where = $dbi->where |
|
| 1367 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1368 |
+ ->param({key1 => [1, 3, $dbi->not_exists]});
|
|
| 1369 |
+$result = $dbi->select( |
|
| 1370 |
+ table => 'table1', |
|
| 1371 |
+ where => $where, |
|
| 1372 |
+); |
|
| 1373 |
+$row = $result->all; |
|
| 1374 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1375 |
+ |
|
| 1376 |
+$where = $dbi->where |
|
| 1377 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1378 |
+ ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
|
|
| 1379 |
+$result = $dbi->select( |
|
| 1380 |
+ table => 'table1', |
|
| 1381 |
+ where => $where, |
|
| 1382 |
+); |
|
| 1383 |
+$row = $result->all; |
|
| 1384 |
+is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 1385 |
+ |
|
| 1386 |
+$where = $dbi->where |
|
| 1387 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1388 |
+ ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
|
|
| 1389 |
+$result = $dbi->select( |
|
| 1390 |
+ table => 'table1', |
|
| 1391 |
+ where => $where, |
|
| 1392 |
+); |
|
| 1393 |
+$row = $result->all; |
|
| 1394 |
+is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 1395 |
+ |
|
| 1396 |
+$where = $dbi->where |
|
| 1397 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1398 |
+ ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
|
|
| 1399 |
+$result = $dbi->select( |
|
| 1400 |
+ table => 'table1', |
|
| 1401 |
+ where => $where, |
|
| 1402 |
+); |
|
| 1403 |
+$row = $result->all; |
|
| 1404 |
+is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 1405 |
+ |
|
| 1406 |
+$where = $dbi->where |
|
| 1407 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1408 |
+ ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
|
|
| 1409 |
+$result = $dbi->select( |
|
| 1410 |
+ table => 'table1', |
|
| 1411 |
+ where => $where, |
|
| 1412 |
+); |
|
| 1413 |
+$row = $result->all; |
|
| 1414 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1415 |
+ |
|
| 1416 |
+$where = $dbi->where |
|
| 1417 |
+ ->clause(['or', ('key1 = :key1') x 3])
|
|
| 1418 |
+ ->param({key1 => []});
|
|
| 1419 |
+$result = $dbi->select( |
|
| 1420 |
+ table => 'table1', |
|
| 1421 |
+ where => $where, |
|
| 1422 |
+); |
|
| 1423 |
+$row = $result->all; |
|
| 1424 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1425 |
+ |
|
| 1426 |
+$where = $dbi->where |
|
| 1427 |
+ ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 1428 |
+ ->param({key1 => [2, $dbi->not_exists]});
|
|
| 1429 |
+$result = $dbi->select( |
|
| 1430 |
+ table => 'table1', |
|
| 1431 |
+ where => $where, |
|
| 1432 |
+); |
|
| 1433 |
+$row = $result->all; |
|
| 1434 |
+is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1435 |
+ |
|
| 1436 |
+$where = $dbi->where |
|
| 1437 |
+ ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 1438 |
+ ->param({key1 => [$dbi->not_exists, 2]});
|
|
| 1439 |
+$result = $dbi->select( |
|
| 1440 |
+ table => 'table1', |
|
| 1441 |
+ where => $where, |
|
| 1442 |
+); |
|
| 1443 |
+$row = $result->all; |
|
| 1444 |
+is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 1445 |
+ |
|
| 1446 |
+$where = $dbi->where |
|
| 1447 |
+ ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 1448 |
+ ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
|
|
| 1449 |
+$result = $dbi->select( |
|
| 1450 |
+ table => 'table1', |
|
| 1451 |
+ where => $where, |
|
| 1452 |
+); |
|
| 1453 |
+$row = $result->all; |
|
| 1454 |
+is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1455 |
+ |
|
| 1456 |
+$where = $dbi->where |
|
| 1457 |
+ ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 1458 |
+ ->param({key1 => [0, 2]});
|
|
| 1459 |
+$result = $dbi->select( |
|
| 1460 |
+ table => 'table1', |
|
| 1461 |
+ where => $where, |
|
| 1462 |
+); |
|
| 1463 |
+$row = $result->all; |
|
| 1464 |
+is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 1465 |
+ |
|
| 1466 |
+$where = $dbi->where |
|
| 1467 |
+ ->clause(['and', 'key1 is not null', 'key2 is not null' ]); |
|
| 1468 |
+$result = $dbi->select( |
|
| 1469 |
+ table => 'table1', |
|
| 1470 |
+ where => $where, |
|
| 1471 |
+); |
|
| 1472 |
+$row = $result->all; |
|
| 1473 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 1474 |
+ |
|
| 1475 |
+eval {$dbi->where(ppp => 1) };
|
|
| 1476 |
+like($@, qr/invalid/); |
|
| 1477 |
+ |
|
| 1478 |
+$where = $dbi->where( |
|
| 1479 |
+ clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']], |
|
| 1480 |
+ param => {key1 => 1, key2 => 2}
|
|
| 1481 |
+); |
|
| 1482 |
+$result = $dbi->select( |
|
| 1483 |
+ table => 'table1', |
|
| 1484 |
+ where => $where, |
|
| 1485 |
+); |
|
| 1486 |
+$row = $result->all; |
|
| 1487 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1488 |
+ |
|
| 1489 |
+ |
|
| 1490 |
+$where = $dbi->where( |
|
| 1491 |
+ clause => ['and', ['or'], ['or', ':key1', ':key2']], |
|
| 1492 |
+ param => {}
|
|
| 1493 |
+); |
|
| 1494 |
+$result = $dbi->select( |
|
| 1495 |
+ table => 'table1', |
|
| 1496 |
+ where => $where, |
|
| 1497 |
+); |
|
| 1498 |
+$row = $result->all; |
|
| 1499 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1500 |
+ |
|
| 1501 |
+$where = $dbi->where; |
|
| 1502 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1503 |
+$where->param({key1 => undef});
|
|
| 1504 |
+$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 1505 |
+$row = $result->all; |
|
| 1506 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1507 |
+ |
|
| 1508 |
+$where = $dbi->where; |
|
| 1509 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1510 |
+$where->param({key1 => undef});
|
|
| 1511 |
+$where->if('defined');
|
|
| 1512 |
+$where->map; |
|
| 1513 |
+$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 1514 |
+$row = $result->all; |
|
| 1515 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1516 |
+ |
|
| 1517 |
+$where = $dbi->where; |
|
| 1518 |
+$where->clause(['or', ':key1{=}', ':key1{=}']);
|
|
| 1519 |
+$where->param({key1 => [undef, undef]});
|
|
| 1520 |
+$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
|
|
| 1521 |
+$row = $result->all; |
|
| 1522 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1523 |
+$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
|
|
| 1524 |
+$row = $result->all; |
|
| 1525 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1526 |
+ |
|
| 1527 |
+$where = $dbi->where; |
|
| 1528 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1529 |
+$where->param({key1 => [undef, undef]});
|
|
| 1530 |
+$where->if('defined');
|
|
| 1531 |
+$where->map; |
|
| 1532 |
+$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
|
|
| 1533 |
+$row = $result->all; |
|
| 1534 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1535 |
+$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
|
|
| 1536 |
+$row = $result->all; |
|
| 1537 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1538 |
+ |
|
| 1539 |
+$where = $dbi->where; |
|
| 1540 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1541 |
+$where->param({key1 => 0});
|
|
| 1542 |
+$where->if('length');
|
|
| 1543 |
+$where->map; |
|
| 1544 |
+$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 1545 |
+$row = $result->all; |
|
| 1546 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1547 |
+ |
|
| 1548 |
+$where = $dbi->where; |
|
| 1549 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1550 |
+$where->param({key1 => ''});
|
|
| 1551 |
+$where->if('length');
|
|
| 1552 |
+$where->map; |
|
| 1553 |
+$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 1554 |
+$row = $result->all; |
|
| 1555 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1556 |
+ |
|
| 1557 |
+$where = $dbi->where; |
|
| 1558 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1559 |
+$where->param({key1 => 5});
|
|
| 1560 |
+$where->if(sub { ($_[0] || '') eq 5 });
|
|
| 1561 |
+$where->map; |
|
| 1562 |
+$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 1563 |
+$row = $result->all; |
|
| 1564 |
+is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 1565 |
+ |
|
| 1566 |
+$where = $dbi->where; |
|
| 1567 |
+$where->clause(['and', ':key1{=}']);
|
|
| 1568 |
+$where->param({key1 => 7});
|
|
| 1569 |
+$where->if(sub { ($_[0] || '') eq 5 });
|
|
| 1570 |
+$where->map; |
|
| 1571 |
+$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 1572 |
+$row = $result->all; |
|
| 1573 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 1574 |
+ |
|
| 1575 |
+$where = $dbi->where; |
|
| 1576 |
+$where->param({id => 1, author => 'Ken', price => 1900});
|
|
| 1577 |
+$where->map(id => 'book.id', |
|
| 1578 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 1579 |
+ price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 1580 |
+); |
|
| 1581 |
+is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
|
|
| 1582 |
+ 'book.price' => 1900}); |
|
| 1583 |
+ |
|
| 1584 |
+$where = $dbi->where; |
|
| 1585 |
+$where->param({id => 0, author => 0, price => 0});
|
|
| 1586 |
+$where->map( |
|
| 1587 |
+ id => 'book.id', |
|
| 1588 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 1589 |
+ price => ['book.price', sub { '%' . $_[0] . '%' },
|
|
| 1590 |
+ {if => sub { $_[0] eq 0 }}]
|
|
| 1591 |
+); |
|
| 1592 |
+is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
|
|
| 1593 |
+ |
|
| 1594 |
+$where = $dbi->where; |
|
| 1595 |
+$where->param({id => '', author => '', price => ''});
|
|
| 1596 |
+$where->if('length');
|
|
| 1597 |
+$where->map( |
|
| 1598 |
+ id => 'book.id', |
|
| 1599 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 1600 |
+ price => ['book.price', sub { '%' . $_[0] . '%' },
|
|
| 1601 |
+ {if => sub { $_[0] eq 1 }}]
|
|
| 1602 |
+); |
|
| 1603 |
+is_deeply($where->param, {});
|
|
| 1604 |
+ |
|
| 1605 |
+$where = $dbi->where; |
|
| 1606 |
+$where->param({id => undef, author => undef, price => undef});
|
|
| 1607 |
+$where->if('length');
|
|
| 1608 |
+$where->map( |
|
| 1609 |
+ id => 'book.id', |
|
| 1610 |
+ price => ['book.price', {if => 'exists'}]
|
|
| 1611 |
+); |
|
| 1612 |
+is_deeply($where->param, {'book.price' => undef});
|
|
| 1613 |
+ |
|
| 1614 |
+$where = $dbi->where; |
|
| 1615 |
+$where->param({price => 'a'});
|
|
| 1616 |
+$where->if('length');
|
|
| 1617 |
+$where->map( |
|
| 1618 |
+ id => ['book.id', {if => 'exists'}],
|
|
| 1619 |
+ price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
|
|
| 1620 |
+); |
|
| 1621 |
+is_deeply($where->param, {'book.price' => '%a'});
|
|
| 1622 |
+ |
|
| 1623 |
+$where = $dbi->where; |
|
| 1624 |
+$where->param({id => [1, 2], author => 'Ken', price => 1900});
|
|
| 1625 |
+$where->map( |
|
| 1626 |
+ id => 'book.id', |
|
| 1627 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 1628 |
+ price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 1629 |
+); |
|
| 1630 |
+is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
|
|
| 1631 |
+ 'book.price' => 1900}); |
|
| 1632 |
+ |
|
| 1633 |
+$where = $dbi->where; |
|
| 1634 |
+$where->if('length');
|
|
| 1635 |
+$where->param({id => ['', ''], author => 'Ken', price => 1900});
|
|
| 1636 |
+$where->map( |
|
| 1637 |
+ id => 'book.id', |
|
| 1638 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 1639 |
+ price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 1640 |
+); |
|
| 1641 |
+is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
|
|
| 1642 |
+ 'book.price' => 1900}); |
|
| 1643 |
+ |
|
| 1644 |
+$where = $dbi->where; |
|
| 1645 |
+$where->param({id => ['', ''], author => 'Ken', price => 1900});
|
|
| 1646 |
+$where->map( |
|
| 1647 |
+ id => ['book.id', {if => 'length'}],
|
|
| 1648 |
+ author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
|
|
| 1649 |
+ price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 1650 |
+); |
|
| 1651 |
+is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
|
|
| 1652 |
+ 'book.price' => 1900}); |
|
| 1653 |
+ |
|
| 1654 |
+test 'dbi_option default'; |
|
| 1655 |
+$dbi = DBIx::Custom->new; |
|
| 1656 |
+is_deeply($dbi->dbi_option, {});
|
|
| 1657 |
+ |
|
| 1658 |
+test 'register_tag_processor'; |
|
| 1659 |
+$dbi = DBIx::Custom->connect; |
|
| 1660 |
+$dbi->register_tag_processor( |
|
| 1661 |
+ a => sub { 1 }
|
|
| 1662 |
+); |
|
| 1663 |
+is($dbi->query_builder->tag_processors->{a}->(), 1);
|
|
| 1664 |
+ |
|
| 1665 |
+test 'register_tag'; |
|
| 1666 |
+$dbi = DBIx::Custom->connect; |
|
| 1667 |
+$dbi->register_tag( |
|
| 1668 |
+ b => sub { 2 }
|
|
| 1669 |
+); |
|
| 1670 |
+is($dbi->query_builder->tags->{b}->(), 2);
|
|
| 1671 |
+ |
|
| 1672 |
+test 'table not specify exception'; |
|
| 1673 |
+$dbi = DBIx::Custom->connect; |
|
| 1674 |
+eval {$dbi->insert};
|
|
| 1675 |
+like($@, qr/table/); |
|
| 1676 |
+eval {$dbi->update};
|
|
| 1677 |
+like($@, qr/table/); |
|
| 1678 |
+eval {$dbi->delete};
|
|
| 1679 |
+like($@, qr/table/); |
|
| 1680 |
+eval {$dbi->select};
|
|
| 1681 |
+like($@, qr/table/); |
|
| 1183 | 1682 |
|
| 1184 | 1683 |
1; |
| ... | ... |
@@ -68,942 +68,7 @@ my $binary; |
| 68 | 68 |
# Prepare table |
| 69 | 69 |
$dbi = DBIx::Custom->connect; |
| 70 | 70 |
|
| 71 |
-test 'cache'; |
|
| 72 |
-eval { $dbi->execute('drop table table1') };
|
|
| 73 |
-$dbi->cache(1); |
|
| 74 |
-$dbi->execute($create_table1); |
|
| 75 |
-$source = 'select * from table1 where key1 = :key1 and key2 = :key2;'; |
|
| 76 |
-$dbi->execute($source, {}, query => 1);
|
|
| 77 |
-is_deeply($dbi->{_cached}->{$source},
|
|
| 78 |
- {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
|
|
| 79 |
- |
|
| 80 |
-eval { $dbi->execute('drop table table1') };
|
|
| 81 |
-$dbi->execute($create_table1); |
|
| 82 |
-$dbi->{_cached} = {};
|
|
| 83 |
-$dbi->cache(0); |
|
| 84 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 85 |
-is(scalar keys %{$dbi->{_cached}}, 0, 'not cache');
|
|
| 86 |
- |
|
| 87 |
-test 'execute'; |
|
| 88 |
-eval { $dbi->execute('drop table table1') };
|
|
| 89 |
-$dbi->execute($create_table1); |
|
| 90 |
-{
|
|
| 91 |
- local $Carp::Verbose = 0; |
|
| 92 |
- eval{$dbi->execute('select * frm table1')};
|
|
| 93 |
- like($@, qr/\Qselect * frm table1;/, "fail prepare"); |
|
| 94 |
- like($@, qr/\.t /, "fail : not verbose"); |
|
| 95 |
-} |
|
| 96 |
-{
|
|
| 97 |
- local $Carp::Verbose = 1; |
|
| 98 |
- eval{$dbi->execute('select * frm table1')};
|
|
| 99 |
- like($@, qr/Custom.*\.t /s, "fail : verbose"); |
|
| 100 |
-} |
|
| 101 |
- |
|
| 102 |
-eval{$dbi->execute('select * from table1', no_exists => 1)};
|
|
| 103 |
-like($@, qr/wrong/, "invald SQL"); |
|
| 104 |
- |
|
| 105 |
-$query = $dbi->execute('select * from table1 where key1 = :key1', {}, query => 1);
|
|
| 106 |
-$dbi->dbh->disconnect; |
|
| 107 |
-eval{$dbi->execute($query, param => {key1 => {a => 1}})};
|
|
| 108 |
-ok($@, "execute fail"); |
|
| 109 |
- |
|
| 110 |
-{
|
|
| 111 |
- local $Carp::Verbose = 0; |
|
| 112 |
- eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
|
|
| 113 |
- like($@, qr/\Q.t /, "caller spec : not vebose"); |
|
| 114 |
-} |
|
| 115 |
-{
|
|
| 116 |
- local $Carp::Verbose = 1; |
|
| 117 |
- eval{$dbi->execute('select * from table1 where {0 key1}', {}, query => 1)};
|
|
| 118 |
- like($@, qr/QueryBuilder.*\.t /s, "caller spec : not vebose"); |
|
| 119 |
-} |
|
| 120 |
- |
|
| 121 |
- |
|
| 122 |
-test 'transaction'; |
|
| 123 |
-$dbi = DBIx::Custom->connect; |
|
| 124 |
-eval { $dbi->execute('drop table table1') };
|
|
| 125 |
-$dbi->execute($create_table1); |
|
| 126 |
- |
|
| 127 |
-$dbi->begin_work; |
|
| 128 |
- |
|
| 129 |
-eval {
|
|
| 130 |
- $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 131 |
- die "Error"; |
|
| 132 |
- $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 133 |
-}; |
|
| 134 |
- |
|
| 135 |
-$dbi->rollback if $@; |
|
| 136 |
- |
|
| 137 |
-$result = $dbi->select(table => 'table1'); |
|
| 138 |
-$rows = $result->all; |
|
| 139 |
-is_deeply($rows, [], "rollback"); |
|
| 140 |
- |
|
| 141 |
-$dbi->begin_work; |
|
| 142 |
- |
|
| 143 |
-eval {
|
|
| 144 |
- $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 145 |
- $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 146 |
-}; |
|
| 147 |
- |
|
| 148 |
-$dbi->commit unless $@; |
|
| 149 |
- |
|
| 150 |
-$result = $dbi->select(table => 'table1'); |
|
| 151 |
-$rows = $result->all; |
|
| 152 |
-is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "commit");
|
|
| 153 |
- |
|
| 154 |
-$dbi->dbh->{AutoCommit} = 0;
|
|
| 155 |
-eval{ $dbi->begin_work };
|
|
| 156 |
-ok($@, "exception"); |
|
| 157 |
-$dbi->dbh->{AutoCommit} = 1;
|
|
| 158 |
- |
|
| 159 |
- |
|
| 160 |
-test 'method'; |
|
| 161 |
-$dbi->method( |
|
| 162 |
- one => sub { 1 }
|
|
| 163 |
-); |
|
| 164 |
-$dbi->method( |
|
| 165 |
- two => sub { 2 }
|
|
| 166 |
-); |
|
| 167 |
-$dbi->method({
|
|
| 168 |
- twice => sub {
|
|
| 169 |
- my $self = shift; |
|
| 170 |
- return $_[0] * 2; |
|
| 171 |
- } |
|
| 172 |
-}); |
|
| 173 |
- |
|
| 174 |
-is($dbi->one, 1, "first"); |
|
| 175 |
-is($dbi->two, 2, "second"); |
|
| 176 |
-is($dbi->twice(5), 10 , "second"); |
|
| 177 |
- |
|
| 178 |
-eval {$dbi->XXXXXX};
|
|
| 179 |
-ok($@, "not exists"); |
|
| 180 |
- |
|
| 181 |
-test 'out filter'; |
|
| 182 |
-$dbi = DBIx::Custom->connect; |
|
| 183 |
-eval { $dbi->execute('drop table table1') };
|
|
| 184 |
-$dbi->execute($create_table1); |
|
| 185 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 186 |
-$dbi->register_filter(three_times => sub { $_[0] * 3});
|
|
| 187 |
-$dbi->apply_filter( |
|
| 188 |
- 'table1', 'key1' => {out => 'twice', in => 'three_times'},
|
|
| 189 |
- 'key2' => {out => 'three_times', in => 'twice'});
|
|
| 190 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 191 |
-$result = $dbi->execute('select * from table1;');
|
|
| 192 |
-$row = $result->fetch_hash_first; |
|
| 193 |
-is_deeply($row, {key1 => 2, key2 => 6}, "insert");
|
|
| 194 |
-$result = $dbi->select(table => 'table1'); |
|
| 195 |
-$row = $result->one; |
|
| 196 |
-is_deeply($row, {key1 => 6, key2 => 12}, "insert");
|
|
| 197 |
- |
|
| 198 |
-$dbi = DBIx::Custom->connect; |
|
| 199 |
-eval { $dbi->execute('drop table table1') };
|
|
| 200 |
-$dbi->execute($create_table1); |
|
| 201 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 202 |
-$dbi->register_filter(three_times => sub { $_[0] * 3});
|
|
| 203 |
-$dbi->apply_filter( |
|
| 204 |
- 'table1', 'key1' => {out => 'twice', in => 'three_times'},
|
|
| 205 |
- 'key2' => {out => 'three_times', in => 'twice'});
|
|
| 206 |
-$dbi->apply_filter( |
|
| 207 |
- 'table1', 'key1' => {out => undef}
|
|
| 208 |
-); |
|
| 209 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 210 |
-$result = $dbi->execute('select * from table1;');
|
|
| 211 |
-$row = $result->one; |
|
| 212 |
-is_deeply($row, {key1 => 1, key2 => 6}, "insert");
|
|
| 213 |
- |
|
| 214 |
-$dbi = DBIx::Custom->connect; |
|
| 215 |
-eval { $dbi->execute('drop table table1') };
|
|
| 216 |
-$dbi->execute($create_table1); |
|
| 217 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 218 |
-$dbi->apply_filter( |
|
| 219 |
- 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 220 |
-); |
|
| 221 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
|
|
| 222 |
-$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
|
|
| 223 |
-$result = $dbi->execute('select * from table1;');
|
|
| 224 |
-$row = $result->one; |
|
| 225 |
-is_deeply($row, {key1 => 4, key2 => 2}, "update");
|
|
| 226 |
- |
|
| 227 |
-$dbi = DBIx::Custom->connect; |
|
| 228 |
-eval { $dbi->execute('drop table table1') };
|
|
| 229 |
-$dbi->execute($create_table1); |
|
| 230 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 231 |
-$dbi->apply_filter( |
|
| 232 |
- 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 233 |
-); |
|
| 234 |
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
|
|
| 235 |
-$dbi->delete(table => 'table1', where => {key1 => 1});
|
|
| 236 |
-$result = $dbi->execute('select * from table1;');
|
|
| 237 |
-$rows = $result->all; |
|
| 238 |
-is_deeply($rows, [], "delete"); |
|
| 239 |
- |
|
| 240 |
-$dbi = DBIx::Custom->connect; |
|
| 241 |
-eval { $dbi->execute('drop table table1') };
|
|
| 242 |
-$dbi->execute($create_table1); |
|
| 243 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 244 |
-$dbi->apply_filter( |
|
| 245 |
- 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 246 |
-); |
|
| 247 |
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
|
|
| 248 |
-$result = $dbi->select(table => 'table1', where => {key1 => 1});
|
|
| 249 |
-$result->filter({'key2' => 'twice'});
|
|
| 250 |
-$rows = $result->all; |
|
| 251 |
-is_deeply($rows, [{key1 => 4, key2 => 4}], "select");
|
|
| 252 |
- |
|
| 253 |
-$dbi = DBIx::Custom->connect; |
|
| 254 |
-eval { $dbi->execute('drop table table1') };
|
|
| 255 |
-$dbi->execute($create_table1); |
|
| 256 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 257 |
-$dbi->apply_filter( |
|
| 258 |
- 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 259 |
-); |
|
| 260 |
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
|
|
| 261 |
-$result = $dbi->execute("select * from table1 where key1 = :key1 and key2 = :key2;",
|
|
| 262 |
- param => {key1 => 1, key2 => 2},
|
|
| 263 |
- table => ['table1']); |
|
| 264 |
-$rows = $result->all; |
|
| 265 |
-is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
|
|
| 266 |
- |
|
| 267 |
-$dbi = DBIx::Custom->connect; |
|
| 268 |
-eval { $dbi->execute('drop table table1') };
|
|
| 269 |
-$dbi->execute($create_table1); |
|
| 270 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 271 |
-$dbi->apply_filter( |
|
| 272 |
- 'table1', 'key1' => {out => 'twice', in => 'twice'}
|
|
| 273 |
-); |
|
| 274 |
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
|
|
| 275 |
-$result = $dbi->execute("select * from {table table1} where key1 = :key1 and key2 = :key2;",
|
|
| 276 |
- param => {key1 => 1, key2 => 2});
|
|
| 277 |
-$rows = $result->all; |
|
| 278 |
-is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
|
|
| 279 |
- |
|
| 280 |
-$dbi = DBIx::Custom->connect; |
|
| 281 |
-eval { $dbi->execute('drop table table1') };
|
|
| 282 |
-$dbi->execute($create_table1); |
|
| 283 |
-$dbi->execute($create_table2); |
|
| 284 |
-$dbi->register_filter(twice => sub { $_[0] * 2 });
|
|
| 285 |
-$dbi->register_filter(three_times => sub { $_[0] * 3 });
|
|
| 286 |
-$dbi->apply_filter( |
|
| 287 |
- 'table1', 'key2' => {out => 'twice', in => 'twice'}
|
|
| 288 |
-); |
|
| 289 |
-$dbi->apply_filter( |
|
| 290 |
- 'table2', 'key3' => {out => 'three_times', in => 'three_times'}
|
|
| 291 |
-); |
|
| 292 |
-$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
|
|
| 293 |
-$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
|
|
| 294 |
-$result = $dbi->select( |
|
| 295 |
- table => ['table1', 'table2'], |
|
| 296 |
- column => ['key2', 'key3'], |
|
| 297 |
- where => {'table1.key2' => 1, 'table2.key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
|
|
| 298 |
- |
|
| 299 |
-$result->filter({'key2' => 'twice'});
|
|
| 300 |
-$rows = $result->all; |
|
| 301 |
-is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join");
|
|
| 302 |
- |
|
| 303 |
-$result = $dbi->select( |
|
| 304 |
- table => ['table1', 'table2'], |
|
| 305 |
- column => ['key2', 'key3'], |
|
| 306 |
- where => {'key2' => 1, 'key3' => 2}, relation => {'table1.key1' => 'table2.key1'});
|
|
| 307 |
- |
|
| 308 |
-$result->filter({'key2' => 'twice'});
|
|
| 309 |
-$rows = $result->all; |
|
| 310 |
-is_deeply($rows, [{key2 => 4, key3 => 18}], "select : join : omit");
|
|
| 311 |
- |
|
| 312 |
-test 'each_column'; |
|
| 313 |
-$dbi = DBIx::Custom->connect; |
|
| 314 |
-eval { $dbi->execute('drop table table1') };
|
|
| 315 |
-eval { $dbi->execute('drop table table2') };
|
|
| 316 |
-$dbi->execute($create_table2); |
|
| 317 |
-$dbi->execute('create table table1 (key1 Date, key2 datetime);');
|
|
| 318 |
- |
|
| 319 |
-$infos = []; |
|
| 320 |
-$dbi->each_column(sub {
|
|
| 321 |
- my ($self, $table, $column, $cinfo) = @_; |
|
| 322 |
- |
|
| 323 |
- if ($table =~ /^table/) {
|
|
| 324 |
- my $info = [$table, $column, $cinfo->{COLUMN_NAME}];
|
|
| 325 |
- push @$infos, $info; |
|
| 326 |
- } |
|
| 327 |
-}); |
|
| 328 |
-$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
|
|
| 329 |
-is_deeply($infos, |
|
| 330 |
- [ |
|
| 331 |
- ['table1', 'key1', 'key1'], |
|
| 332 |
- ['table1', 'key2', 'key2'], |
|
| 333 |
- ['table2', 'key1', 'key1'], |
|
| 334 |
- ['table2', 'key3', 'key3'] |
|
| 335 |
- ] |
|
| 336 |
- |
|
| 337 |
-); |
|
| 338 |
-test 'each_table'; |
|
| 339 |
-$dbi = DBIx::Custom->connect; |
|
| 340 |
-eval { $dbi->execute('drop table table1') };
|
|
| 341 |
-eval { $dbi->execute('drop table table2') };
|
|
| 342 |
-$dbi->execute($create_table2); |
|
| 343 |
-$dbi->execute('create table table1 (key1 Date, key2 datetime);');
|
|
| 344 |
- |
|
| 345 |
-$infos = []; |
|
| 346 |
-$dbi->each_table(sub {
|
|
| 347 |
- my ($self, $table, $table_info) = @_; |
|
| 348 |
- |
|
| 349 |
- if ($table =~ /^table/) {
|
|
| 350 |
- my $info = [$table, $table_info->{TABLE_NAME}];
|
|
| 351 |
- push @$infos, $info; |
|
| 352 |
- } |
|
| 353 |
-}); |
|
| 354 |
-$infos = [sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$infos];
|
|
| 355 |
-is_deeply($infos, |
|
| 356 |
- [ |
|
| 357 |
- ['table1', 'table1'], |
|
| 358 |
- ['table2', 'table2'], |
|
| 359 |
- ] |
|
| 360 |
-); |
|
| 361 |
- |
|
| 362 |
-test 'limit'; |
|
| 363 |
-$dbi = DBIx::Custom->connect; |
|
| 364 |
-eval { $dbi->execute('drop table table1') };
|
|
| 365 |
-$dbi->execute($create_table1); |
|
| 366 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 367 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
|
|
| 368 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
|
|
| 369 |
-$dbi->register_tag( |
|
| 370 |
- limit => sub {
|
|
| 371 |
- my ($count, $offset) = @_; |
|
| 372 |
- |
|
| 373 |
- my $s = ''; |
|
| 374 |
- $s .= "limit $count"; |
|
| 375 |
- $s .= " offset $offset" if defined $offset; |
|
| 376 |
- |
|
| 377 |
- return [$s, []]; |
|
| 378 |
- } |
|
| 379 |
-); |
|
| 380 |
-$rows = $dbi->select( |
|
| 381 |
- table => 'table1', |
|
| 382 |
- where => {key1 => 1},
|
|
| 383 |
- append => "order by key2 {limit 1 0}"
|
|
| 384 |
-)->all; |
|
| 385 |
-is_deeply($rows, [{key1 => 1, key2 => 2}]);
|
|
| 386 |
-$rows = $dbi->select( |
|
| 387 |
- table => 'table1', |
|
| 388 |
- where => {key1 => 1},
|
|
| 389 |
- append => "order by key2 {limit 2 1}"
|
|
| 390 |
-)->all; |
|
| 391 |
-is_deeply($rows, [{key1 => 1, key2 => 4},{key1 => 1, key2 => 6}]);
|
|
| 392 |
-$rows = $dbi->select( |
|
| 393 |
- table => 'table1', |
|
| 394 |
- where => {key1 => 1},
|
|
| 395 |
- append => "order by key2 {limit 1}"
|
|
| 396 |
-)->all; |
|
| 397 |
-is_deeply($rows, [{key1 => 1, key2 => 2}]);
|
|
| 398 | 71 |
|
| 399 |
-test 'connect super'; |
|
| 400 |
-{
|
|
| 401 |
- package MyDBI; |
|
| 402 |
- |
|
| 403 |
- use base 'DBIx::Custom'; |
|
| 404 |
- sub connect {
|
|
| 405 |
- my $self = shift->SUPER::connect(@_); |
|
| 406 |
- |
|
| 407 |
- return $self; |
|
| 408 |
- } |
|
| 409 |
- |
|
| 410 |
- sub new {
|
|
| 411 |
- my $self = shift->SUPER::new(@_); |
|
| 412 |
- |
|
| 413 |
- return $self; |
|
| 414 |
- } |
|
| 415 |
-} |
|
| 416 |
- |
|
| 417 |
-$dbi = MyDBI->connect; |
|
| 418 |
-eval { $dbi->execute('drop table table1') };
|
|
| 419 |
-$dbi->execute($create_table1); |
|
| 420 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 421 |
-is($dbi->select(table => 'table1')->one->{key1}, 1);
|
|
| 422 |
- |
|
| 423 |
-$dbi = MyDBI->new; |
|
| 424 |
-$dbi->connect; |
|
| 425 |
-eval { $dbi->execute('drop table table1') };
|
|
| 426 |
-$dbi->execute($create_table1); |
|
| 427 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 428 |
-is($dbi->select(table => 'table1')->one->{key1}, 1);
|
|
| 429 |
- |
|
| 430 |
-{
|
|
| 431 |
- package MyDBI2; |
|
| 432 |
- |
|
| 433 |
- use base 'DBIx::Custom'; |
|
| 434 |
- sub connect {
|
|
| 435 |
- my $self = shift->SUPER::new(@_); |
|
| 436 |
- $self->connect; |
|
| 437 |
- |
|
| 438 |
- return $self; |
|
| 439 |
- } |
|
| 440 |
-} |
|
| 441 |
- |
|
| 442 |
-$dbi = MyDBI->connect; |
|
| 443 |
-eval { $dbi->execute('drop table table1') };
|
|
| 444 |
-$dbi->execute($create_table1); |
|
| 445 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 446 |
-is($dbi->select(table => 'table1')->one->{key1}, 1);
|
|
| 447 |
- |
|
| 448 |
-test 'end_filter'; |
|
| 449 |
-$dbi = DBIx::Custom->connect; |
|
| 450 |
-eval { $dbi->execute('drop table table1') };
|
|
| 451 |
-$dbi->execute($create_table1); |
|
| 452 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 453 |
-$result = $dbi->select(table => 'table1'); |
|
| 454 |
-$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 455 |
-$result->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 });
|
|
| 456 |
-$row = $result->fetch_first; |
|
| 457 |
-is_deeply($row, [6, 40]); |
|
| 458 |
- |
|
| 459 |
-$dbi = DBIx::Custom->connect; |
|
| 460 |
-eval { $dbi->execute('drop table table1') };
|
|
| 461 |
-$dbi->execute($create_table1); |
|
| 462 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 463 |
-$result = $dbi->select(table => 'table1'); |
|
| 464 |
-$result->filter([qw/key1 key2/] => sub { $_[0] * 2 });
|
|
| 465 |
-$result->end_filter([[qw/key1 key2/] => sub { $_[0] * 3 }]);
|
|
| 466 |
-$row = $result->fetch_first; |
|
| 467 |
-is_deeply($row, [6, 12]); |
|
| 468 |
- |
|
| 469 |
-$dbi = DBIx::Custom->connect; |
|
| 470 |
-eval { $dbi->execute('drop table table1') };
|
|
| 471 |
-$dbi->execute($create_table1); |
|
| 472 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 473 |
-$result = $dbi->select(table => 'table1'); |
|
| 474 |
-$result->filter([[qw/key1 key2/] => sub { $_[0] * 2 }]);
|
|
| 475 |
-$result->end_filter([qw/key1 key2/] => sub { $_[0] * 3 });
|
|
| 476 |
-$row = $result->fetch_first; |
|
| 477 |
-is_deeply($row, [6, 12]); |
|
| 478 |
- |
|
| 479 |
-$dbi->register_filter(five_times => sub { $_[0] * 5 });
|
|
| 480 |
-$result = $dbi->select(table => 'table1'); |
|
| 481 |
-$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 482 |
-$result->end_filter({key1 => sub { $_[0] * 3 }, key2 => 'five_times' });
|
|
| 483 |
-$row = $result->one; |
|
| 484 |
-is_deeply($row, {key1 => 6, key2 => 40});
|
|
| 485 |
- |
|
| 486 |
-$dbi->register_filter(five_times => sub { $_[0] * 5 });
|
|
| 487 |
-$dbi->apply_filter('table1',
|
|
| 488 |
- key1 => {end => sub { $_[0] * 3 } },
|
|
| 489 |
- key2 => {end => 'five_times'}
|
|
| 490 |
-); |
|
| 491 |
-$result = $dbi->select(table => 'table1'); |
|
| 492 |
-$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 493 |
-$row = $result->one; |
|
| 494 |
-is_deeply($row, {key1 => 6, key2 => 40}, 'apply_filter');
|
|
| 495 |
- |
|
| 496 |
-$dbi->register_filter(five_times => sub { $_[0] * 5 });
|
|
| 497 |
-$dbi->apply_filter('table1',
|
|
| 498 |
- key1 => {end => sub { $_[0] * 3 } },
|
|
| 499 |
- key2 => {end => 'five_times'}
|
|
| 500 |
-); |
|
| 501 |
-$result = $dbi->select(table => 'table1'); |
|
| 502 |
-$result->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 });
|
|
| 503 |
-$result->filter(key1 => undef); |
|
| 504 |
-$result->end_filter(key1 => undef); |
|
| 505 |
-$row = $result->one; |
|
| 506 |
-is_deeply($row, {key1 => 1, key2 => 40}, 'apply_filter overwrite');
|
|
| 507 |
- |
|
| 508 |
-test 'remove_end_filter and remove_filter'; |
|
| 509 |
-$dbi = DBIx::Custom->connect; |
|
| 510 |
-eval { $dbi->execute('drop table table1') };
|
|
| 511 |
-$dbi->execute($create_table1); |
|
| 512 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 513 |
-$result = $dbi->select(table => 'table1'); |
|
| 514 |
-$row = $result |
|
| 515 |
- ->filter(key1 => sub { $_[0] * 2 }, key2 => sub { $_[0] * 4 })
|
|
| 516 |
- ->remove_filter |
|
| 517 |
- ->end_filter(key1 => sub { $_[0] * 3 }, key2 => sub { $_[0] * 5 })
|
|
| 518 |
- ->remove_end_filter |
|
| 519 |
- ->fetch_first; |
|
| 520 |
-is_deeply($row, [1, 2]); |
|
| 521 |
- |
|
| 522 |
-test 'empty where select'; |
|
| 523 |
-$dbi = DBIx::Custom->connect; |
|
| 524 |
-eval { $dbi->execute('drop table table1') };
|
|
| 525 |
-$dbi->execute($create_table1); |
|
| 526 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 527 |
-$result = $dbi->select(table => 'table1', where => {});
|
|
| 528 |
-$row = $result->one; |
|
| 529 |
-is_deeply($row, {key1 => 1, key2 => 2});
|
|
| 530 |
- |
|
| 531 |
-test 'select query option'; |
|
| 532 |
-$dbi = DBIx::Custom->connect; |
|
| 533 |
-eval { $dbi->execute('drop table table1') };
|
|
| 534 |
-$dbi->execute($create_table1); |
|
| 535 |
-$query = $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, query => 1);
|
|
| 536 |
-is(ref $query, 'DBIx::Custom::Query'); |
|
| 537 |
-$query = $dbi->update(table => 'table1', where => {key1 => 1}, param => {key2 => 2}, query => 1);
|
|
| 538 |
-is(ref $query, 'DBIx::Custom::Query'); |
|
| 539 |
-$query = $dbi->delete(table => 'table1', where => {key1 => 1}, query => 1);
|
|
| 540 |
-is(ref $query, 'DBIx::Custom::Query'); |
|
| 541 |
-$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1);
|
|
| 542 |
-is(ref $query, 'DBIx::Custom::Query'); |
|
| 543 |
- |
|
| 544 |
-test 'where'; |
|
| 545 |
-$dbi = DBIx::Custom->connect; |
|
| 546 |
-eval { $dbi->execute('drop table table1') };
|
|
| 547 |
-$dbi->execute($create_table1); |
|
| 548 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 549 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 550 |
-$where = $dbi->where->clause(['and', 'key1 = :key1', 'key2 = :key2']); |
|
| 551 |
-is("$where", "where ( key1 = :key1 and key2 = :key2 )", 'no param');
|
|
| 552 |
- |
|
| 553 |
-$where = $dbi->where |
|
| 554 |
- ->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
| 555 |
- ->param({key1 => 1});
|
|
| 556 |
- |
|
| 557 |
-$result = $dbi->select( |
|
| 558 |
- table => 'table1', |
|
| 559 |
- where => $where |
|
| 560 |
-); |
|
| 561 |
-$row = $result->all; |
|
| 562 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 563 |
- |
|
| 564 |
-$result = $dbi->select( |
|
| 565 |
- table => 'table1', |
|
| 566 |
- where => [ |
|
| 567 |
- ['and', 'key1 = :key1', 'key2 = :key2'], |
|
| 568 |
- {key1 => 1}
|
|
| 569 |
- ] |
|
| 570 |
-); |
|
| 571 |
-$row = $result->all; |
|
| 572 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 573 |
- |
|
| 574 |
-$where = $dbi->where |
|
| 575 |
- ->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
| 576 |
- ->param({key1 => 1, key2 => 2});
|
|
| 577 |
-$result = $dbi->select( |
|
| 578 |
- table => 'table1', |
|
| 579 |
- where => $where |
|
| 580 |
-); |
|
| 581 |
-$row = $result->all; |
|
| 582 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 583 |
- |
|
| 584 |
-$where = $dbi->where |
|
| 585 |
- ->clause(['and', 'key1 = :key1', 'key2 = :key2']) |
|
| 586 |
- ->param({});
|
|
| 587 |
-$result = $dbi->select( |
|
| 588 |
- table => 'table1', |
|
| 589 |
- where => $where, |
|
| 590 |
-); |
|
| 591 |
-$row = $result->all; |
|
| 592 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 593 |
- |
|
| 594 |
-$where = $dbi->where |
|
| 595 |
- ->clause(['and', ['or', 'key1 > :key1', 'key1 < :key1'], 'key2 = :key2']) |
|
| 596 |
- ->param({key1 => [0, 3], key2 => 2});
|
|
| 597 |
-$result = $dbi->select( |
|
| 598 |
- table => 'table1', |
|
| 599 |
- where => $where, |
|
| 600 |
-); |
|
| 601 |
-$row = $result->all; |
|
| 602 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 603 |
- |
|
| 604 |
-$where = $dbi->where; |
|
| 605 |
-$result = $dbi->select( |
|
| 606 |
- table => 'table1', |
|
| 607 |
- where => $where |
|
| 608 |
-); |
|
| 609 |
-$row = $result->all; |
|
| 610 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 611 |
- |
|
| 612 |
-eval {
|
|
| 613 |
-$where = $dbi->where |
|
| 614 |
- ->clause(['uuu']); |
|
| 615 |
-$result = $dbi->select( |
|
| 616 |
- table => 'table1', |
|
| 617 |
- where => $where |
|
| 618 |
-); |
|
| 619 |
-}; |
|
| 620 |
-ok($@); |
|
| 621 |
- |
|
| 622 |
-$where = $dbi->where; |
|
| 623 |
-is("$where", '');
|
|
| 624 |
- |
|
| 625 |
-$where = $dbi->where |
|
| 626 |
- ->clause(['or', ('key1 = :key1') x 2])
|
|
| 627 |
- ->param({key1 => [1, 3]});
|
|
| 628 |
-$result = $dbi->select( |
|
| 629 |
- table => 'table1', |
|
| 630 |
- where => $where, |
|
| 631 |
-); |
|
| 632 |
-$row = $result->all; |
|
| 633 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 634 |
- |
|
| 635 |
-$where = $dbi->where |
|
| 636 |
- ->clause(['or', ('key1 = :key1') x 2])
|
|
| 637 |
- ->param({key1 => [1]});
|
|
| 638 |
-$result = $dbi->select( |
|
| 639 |
- table => 'table1', |
|
| 640 |
- where => $where, |
|
| 641 |
-); |
|
| 642 |
-$row = $result->all; |
|
| 643 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 644 |
- |
|
| 645 |
-$where = $dbi->where |
|
| 646 |
- ->clause(['or', ('key1 = :key1') x 2])
|
|
| 647 |
- ->param({key1 => 1});
|
|
| 648 |
-$result = $dbi->select( |
|
| 649 |
- table => 'table1', |
|
| 650 |
- where => $where, |
|
| 651 |
-); |
|
| 652 |
-$row = $result->all; |
|
| 653 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 654 |
- |
|
| 655 |
-$where = $dbi->where |
|
| 656 |
- ->clause('key1 = :key1')
|
|
| 657 |
- ->param({key1 => 1});
|
|
| 658 |
-$result = $dbi->select( |
|
| 659 |
- table => 'table1', |
|
| 660 |
- where => $where, |
|
| 661 |
-); |
|
| 662 |
-$row = $result->all; |
|
| 663 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 664 |
- |
|
| 665 |
-$where = $dbi->where |
|
| 666 |
- ->clause('key1 = :key1 key2 = :key2')
|
|
| 667 |
- ->param({key1 => 1});
|
|
| 668 |
-eval{$where->to_string};
|
|
| 669 |
-like($@, qr/one column/); |
|
| 670 |
- |
|
| 671 |
-$where = $dbi->where |
|
| 672 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 673 |
- ->param({key1 => [$dbi->not_exists, 1, 3]});
|
|
| 674 |
-$result = $dbi->select( |
|
| 675 |
- table => 'table1', |
|
| 676 |
- where => $where, |
|
| 677 |
-); |
|
| 678 |
-$row = $result->all; |
|
| 679 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 680 |
- |
|
| 681 |
-$where = $dbi->where |
|
| 682 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 683 |
- ->param({key1 => [1, $dbi->not_exists, 3]});
|
|
| 684 |
-$result = $dbi->select( |
|
| 685 |
- table => 'table1', |
|
| 686 |
- where => $where, |
|
| 687 |
-); |
|
| 688 |
-$row = $result->all; |
|
| 689 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 690 |
- |
|
| 691 |
-$where = $dbi->where |
|
| 692 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 693 |
- ->param({key1 => [1, 3, $dbi->not_exists]});
|
|
| 694 |
-$result = $dbi->select( |
|
| 695 |
- table => 'table1', |
|
| 696 |
- where => $where, |
|
| 697 |
-); |
|
| 698 |
-$row = $result->all; |
|
| 699 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 700 |
- |
|
| 701 |
-$where = $dbi->where |
|
| 702 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 703 |
- ->param({key1 => [1, $dbi->not_exists, $dbi->not_exists]});
|
|
| 704 |
-$result = $dbi->select( |
|
| 705 |
- table => 'table1', |
|
| 706 |
- where => $where, |
|
| 707 |
-); |
|
| 708 |
-$row = $result->all; |
|
| 709 |
-is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 710 |
- |
|
| 711 |
-$where = $dbi->where |
|
| 712 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 713 |
- ->param({key1 => [$dbi->not_exists, 1, $dbi->not_exists]});
|
|
| 714 |
-$result = $dbi->select( |
|
| 715 |
- table => 'table1', |
|
| 716 |
- where => $where, |
|
| 717 |
-); |
|
| 718 |
-$row = $result->all; |
|
| 719 |
-is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 720 |
- |
|
| 721 |
-$where = $dbi->where |
|
| 722 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 723 |
- ->param({key1 => [$dbi->not_exists, $dbi->not_exists, 1]});
|
|
| 724 |
-$result = $dbi->select( |
|
| 725 |
- table => 'table1', |
|
| 726 |
- where => $where, |
|
| 727 |
-); |
|
| 728 |
-$row = $result->all; |
|
| 729 |
-is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 730 |
- |
|
| 731 |
-$where = $dbi->where |
|
| 732 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 733 |
- ->param({key1 => [$dbi->not_exists, $dbi->not_exists, $dbi->not_exists]});
|
|
| 734 |
-$result = $dbi->select( |
|
| 735 |
- table => 'table1', |
|
| 736 |
- where => $where, |
|
| 737 |
-); |
|
| 738 |
-$row = $result->all; |
|
| 739 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 740 |
- |
|
| 741 |
-$where = $dbi->where |
|
| 742 |
- ->clause(['or', ('key1 = :key1') x 3])
|
|
| 743 |
- ->param({key1 => []});
|
|
| 744 |
-$result = $dbi->select( |
|
| 745 |
- table => 'table1', |
|
| 746 |
- where => $where, |
|
| 747 |
-); |
|
| 748 |
-$row = $result->all; |
|
| 749 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 750 |
- |
|
| 751 |
-$where = $dbi->where |
|
| 752 |
- ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 753 |
- ->param({key1 => [2, $dbi->not_exists]});
|
|
| 754 |
-$result = $dbi->select( |
|
| 755 |
- table => 'table1', |
|
| 756 |
- where => $where, |
|
| 757 |
-); |
|
| 758 |
-$row = $result->all; |
|
| 759 |
-is_deeply($row, [{key1 => 3, key2 => 4}], 'not_exists');
|
|
| 760 |
- |
|
| 761 |
-$where = $dbi->where |
|
| 762 |
- ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 763 |
- ->param({key1 => [$dbi->not_exists, 2]});
|
|
| 764 |
-$result = $dbi->select( |
|
| 765 |
- table => 'table1', |
|
| 766 |
- where => $where, |
|
| 767 |
-); |
|
| 768 |
-$row = $result->all; |
|
| 769 |
-is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 770 |
- |
|
| 771 |
-$where = $dbi->where |
|
| 772 |
- ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 773 |
- ->param({key1 => [$dbi->not_exists, $dbi->not_exists]});
|
|
| 774 |
-$result = $dbi->select( |
|
| 775 |
- table => 'table1', |
|
| 776 |
- where => $where, |
|
| 777 |
-); |
|
| 778 |
-$row = $result->all; |
|
| 779 |
-is_deeply($row, [{key1 => 1, key2 => 2},{key1 => 3, key2 => 4}], 'not_exists');
|
|
| 780 |
- |
|
| 781 |
-$where = $dbi->where |
|
| 782 |
- ->clause(['and', '{> key1}', '{< key1}' ])
|
|
| 783 |
- ->param({key1 => [0, 2]});
|
|
| 784 |
-$result = $dbi->select( |
|
| 785 |
- table => 'table1', |
|
| 786 |
- where => $where, |
|
| 787 |
-); |
|
| 788 |
-$row = $result->all; |
|
| 789 |
-is_deeply($row, [{key1 => 1, key2 => 2}], 'not_exists');
|
|
| 790 |
- |
|
| 791 |
-$where = $dbi->where |
|
| 792 |
- ->clause(['and', 'key1 is not null', 'key2 is not null' ]); |
|
| 793 |
-$result = $dbi->select( |
|
| 794 |
- table => 'table1', |
|
| 795 |
- where => $where, |
|
| 796 |
-); |
|
| 797 |
-$row = $result->all; |
|
| 798 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], 'not_exists');
|
|
| 799 |
- |
|
| 800 |
-eval {$dbi->where(ppp => 1) };
|
|
| 801 |
-like($@, qr/invalid/); |
|
| 802 |
- |
|
| 803 |
-$where = $dbi->where( |
|
| 804 |
- clause => ['and', ['or'], ['and', 'key1 = :key1', 'key2 = :key2']], |
|
| 805 |
- param => {key1 => 1, key2 => 2}
|
|
| 806 |
-); |
|
| 807 |
-$result = $dbi->select( |
|
| 808 |
- table => 'table1', |
|
| 809 |
- where => $where, |
|
| 810 |
-); |
|
| 811 |
-$row = $result->all; |
|
| 812 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 813 |
- |
|
| 814 |
- |
|
| 815 |
-$where = $dbi->where( |
|
| 816 |
- clause => ['and', ['or'], ['or', ':key1', ':key2']], |
|
| 817 |
- param => {}
|
|
| 818 |
-); |
|
| 819 |
-$result = $dbi->select( |
|
| 820 |
- table => 'table1', |
|
| 821 |
- where => $where, |
|
| 822 |
-); |
|
| 823 |
-$row = $result->all; |
|
| 824 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 825 |
- |
|
| 826 |
-$where = $dbi->where; |
|
| 827 |
-$where->clause(['and', ':key1{=}']);
|
|
| 828 |
-$where->param({key1 => undef});
|
|
| 829 |
-$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 830 |
-$row = $result->all; |
|
| 831 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 832 |
- |
|
| 833 |
-$where = $dbi->where; |
|
| 834 |
-$where->clause(['and', ':key1{=}']);
|
|
| 835 |
-$where->param({key1 => undef});
|
|
| 836 |
-$where->if('defined');
|
|
| 837 |
-$where->map; |
|
| 838 |
-$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 839 |
-$row = $result->all; |
|
| 840 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 841 |
- |
|
| 842 |
-$where = $dbi->where; |
|
| 843 |
-$where->clause(['or', ':key1{=}', ':key1{=}']);
|
|
| 844 |
-$where->param({key1 => [undef, undef]});
|
|
| 845 |
-$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
|
|
| 846 |
-$row = $result->all; |
|
| 847 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 848 |
-$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
|
|
| 849 |
-$row = $result->all; |
|
| 850 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 851 |
- |
|
| 852 |
-$where = $dbi->where; |
|
| 853 |
-$where->clause(['and', ':key1{=}']);
|
|
| 854 |
-$where->param({key1 => [undef, undef]});
|
|
| 855 |
-$where->if('defined');
|
|
| 856 |
-$where->map; |
|
| 857 |
-$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]});
|
|
| 858 |
-$row = $result->all; |
|
| 859 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 860 |
-$result = $dbi->execute("select * from table1 $where", {key1 => [0, 1]});
|
|
| 861 |
-$row = $result->all; |
|
| 862 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 863 |
- |
|
| 864 |
-$where = $dbi->where; |
|
| 865 |
-$where->clause(['and', ':key1{=}']);
|
|
| 866 |
-$where->param({key1 => 0});
|
|
| 867 |
-$where->if('length');
|
|
| 868 |
-$where->map; |
|
| 869 |
-$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 870 |
-$row = $result->all; |
|
| 871 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 872 |
- |
|
| 873 |
-$where = $dbi->where; |
|
| 874 |
-$where->clause(['and', ':key1{=}']);
|
|
| 875 |
-$where->param({key1 => ''});
|
|
| 876 |
-$where->if('length');
|
|
| 877 |
-$where->map; |
|
| 878 |
-$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 879 |
-$row = $result->all; |
|
| 880 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 881 |
- |
|
| 882 |
-$where = $dbi->where; |
|
| 883 |
-$where->clause(['and', ':key1{=}']);
|
|
| 884 |
-$where->param({key1 => 5});
|
|
| 885 |
-$where->if(sub { ($_[0] || '') eq 5 });
|
|
| 886 |
-$where->map; |
|
| 887 |
-$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 888 |
-$row = $result->all; |
|
| 889 |
-is_deeply($row, [{key1 => 1, key2 => 2}]);
|
|
| 890 |
- |
|
| 891 |
-$where = $dbi->where; |
|
| 892 |
-$where->clause(['and', ':key1{=}']);
|
|
| 893 |
-$where->param({key1 => 7});
|
|
| 894 |
-$where->if(sub { ($_[0] || '') eq 5 });
|
|
| 895 |
-$where->map; |
|
| 896 |
-$result = $dbi->execute("select * from table1 $where", {key1 => 1});
|
|
| 897 |
-$row = $result->all; |
|
| 898 |
-is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 899 |
- |
|
| 900 |
-$where = $dbi->where; |
|
| 901 |
-$where->param({id => 1, author => 'Ken', price => 1900});
|
|
| 902 |
-$where->map(id => 'book.id', |
|
| 903 |
- author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 904 |
- price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 905 |
-); |
|
| 906 |
-is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
|
|
| 907 |
- 'book.price' => 1900}); |
|
| 908 |
- |
|
| 909 |
-$where = $dbi->where; |
|
| 910 |
-$where->param({id => 0, author => 0, price => 0});
|
|
| 911 |
-$where->map( |
|
| 912 |
- id => 'book.id', |
|
| 913 |
- author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 914 |
- price => ['book.price', sub { '%' . $_[0] . '%' },
|
|
| 915 |
- {if => sub { $_[0] eq 0 }}]
|
|
| 916 |
-); |
|
| 917 |
-is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
|
|
| 918 |
- |
|
| 919 |
-$where = $dbi->where; |
|
| 920 |
-$where->param({id => '', author => '', price => ''});
|
|
| 921 |
-$where->if('length');
|
|
| 922 |
-$where->map( |
|
| 923 |
- id => 'book.id', |
|
| 924 |
- author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 925 |
- price => ['book.price', sub { '%' . $_[0] . '%' },
|
|
| 926 |
- {if => sub { $_[0] eq 1 }}]
|
|
| 927 |
-); |
|
| 928 |
-is_deeply($where->param, {});
|
|
| 929 |
- |
|
| 930 |
-$where = $dbi->where; |
|
| 931 |
-$where->param({id => undef, author => undef, price => undef});
|
|
| 932 |
-$where->if('length');
|
|
| 933 |
-$where->map( |
|
| 934 |
- id => 'book.id', |
|
| 935 |
- price => ['book.price', {if => 'exists'}]
|
|
| 936 |
-); |
|
| 937 |
-is_deeply($where->param, {'book.price' => undef});
|
|
| 938 |
- |
|
| 939 |
-$where = $dbi->where; |
|
| 940 |
-$where->param({price => 'a'});
|
|
| 941 |
-$where->if('length');
|
|
| 942 |
-$where->map( |
|
| 943 |
- id => ['book.id', {if => 'exists'}],
|
|
| 944 |
- price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
|
|
| 945 |
-); |
|
| 946 |
-is_deeply($where->param, {'book.price' => '%a'});
|
|
| 947 |
- |
|
| 948 |
-$where = $dbi->where; |
|
| 949 |
-$where->param({id => [1, 2], author => 'Ken', price => 1900});
|
|
| 950 |
-$where->map( |
|
| 951 |
- id => 'book.id', |
|
| 952 |
- author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 953 |
- price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 954 |
-); |
|
| 955 |
-is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
|
|
| 956 |
- 'book.price' => 1900}); |
|
| 957 |
- |
|
| 958 |
-$where = $dbi->where; |
|
| 959 |
-$where->if('length');
|
|
| 960 |
-$where->param({id => ['', ''], author => 'Ken', price => 1900});
|
|
| 961 |
-$where->map( |
|
| 962 |
- id => 'book.id', |
|
| 963 |
- author => ['book.author', sub { '%' . $_[0] . '%' }],
|
|
| 964 |
- price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 965 |
-); |
|
| 966 |
-is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
|
|
| 967 |
- 'book.price' => 1900}); |
|
| 968 |
- |
|
| 969 |
-$where = $dbi->where; |
|
| 970 |
-$where->param({id => ['', ''], author => 'Ken', price => 1900});
|
|
| 971 |
-$where->map( |
|
| 972 |
- id => ['book.id', {if => 'length'}],
|
|
| 973 |
- author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
|
|
| 974 |
- price => ['book.price', {if => sub { $_[0] eq 1900 }}]
|
|
| 975 |
-); |
|
| 976 |
-is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
|
|
| 977 |
- 'book.price' => 1900}); |
|
| 978 |
- |
|
| 979 |
-test 'dbi_option default'; |
|
| 980 |
-$dbi = DBIx::Custom->new; |
|
| 981 |
-is_deeply($dbi->dbi_option, {});
|
|
| 982 |
- |
|
| 983 |
-test 'register_tag_processor'; |
|
| 984 |
-$dbi = DBIx::Custom->connect; |
|
| 985 |
-$dbi->register_tag_processor( |
|
| 986 |
- a => sub { 1 }
|
|
| 987 |
-); |
|
| 988 |
-is($dbi->query_builder->tag_processors->{a}->(), 1);
|
|
| 989 |
- |
|
| 990 |
-test 'register_tag'; |
|
| 991 |
-$dbi = DBIx::Custom->connect; |
|
| 992 |
-$dbi->register_tag( |
|
| 993 |
- b => sub { 2 }
|
|
| 994 |
-); |
|
| 995 |
-is($dbi->query_builder->tags->{b}->(), 2);
|
|
| 996 |
- |
|
| 997 |
-test 'table not specify exception'; |
|
| 998 |
-$dbi = DBIx::Custom->connect; |
|
| 999 |
-eval {$dbi->insert};
|
|
| 1000 |
-like($@, qr/table/); |
|
| 1001 |
-eval {$dbi->update};
|
|
| 1002 |
-like($@, qr/table/); |
|
| 1003 |
-eval {$dbi->delete};
|
|
| 1004 |
-like($@, qr/table/); |
|
| 1005 |
-eval {$dbi->select};
|
|
| 1006 |
-like($@, qr/table/); |
|
| 1007 | 72 |
|
| 1008 | 73 |
|
| 1009 | 74 |
test 'more tests'; |