mahi /atk4/lib/DB/dsql.php

Language PHP Lines 1903
MD5 Hash fd65fdc2f084d9d791dadb9ae38ad314
Repository https://github.com/mahimarathore/mahi.git View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
<?php // vim:ts=4:sw=4:et:fdm=marker
/**
   This file is part of Agile Toolkit 4 http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
*/
/**
 * Implementation of SQL Query Abstraction Layer for Agile Toolkit
 *
 * @link http://agiletoolkit.org/doc/dsql
 */
class DB_dsql extends AbstractModel implements Iterator {
    /**
     * Data accumulated by calling Definition methods, which is then
     * used when rendering
     */
    public $args=array();

    /** List of PDO parametical arguments for a query. Used only during rendering. */
    public $params=array();

    /** Manually-specified params */
    public $extra_params=array();

    /** PDO Statement, if query is prepared. Used by iterator */
    public $stmt=null;

    /** Expression to use when converting to string */
    public $template=null;

    /**
     * You can switch mode with select(), insert(), update() commands.
     * Mode is initialized to "select" by default
     */
    public $mode=null;

    /** Used to determine main table. */
    public $main_table=null;

    /** If no fields are defined, this field is used */
    public $default_field='*';

    public $default_exception='Exception_DB';

    /** call $q->debug() to turn on debugging. */
    public $debug=false;

    /** prefix for all parameteric variables: a, a_2, a_3, etc */
    public $param_base='a';

    /** When you convert this object to string, the following happens: */
    public $output_mode='getOne';

    /** Backtics are added around all fields. Set this to blank string to avoid */
    public $bt='`';

    /**
     * Templates are used to construct most common queries. Templates may be
     * changed in vendor-specific implementation of dsql (extending this class)
     */
    public $sql_templates=array(
        'select'=>"select [options] [field] [from] [table] [join] [where] [group] [having] [order] [limit]",
        'insert'=>"insert [options_insert] into [table_noalias] ([set_fields]) values ([set_values])",
        'replace'=>"replace [options_replace] into [table_noalias] ([set_fields]) values ([set_value])",
        'update'=>"update [table_noalias] set [set] [where]",
        'delete'=>"delete from  [table_noalias] [where]",
        'truncate'=>'truncate table [table_noalias]'
    );
    /** required for non-id based tables */
    public $id_field;

    // {{{ Generic routines
    function _unique(&$array,$desired=null)
    {
        $desired=preg_replace('/[^a-zA-Z0-9:]/', '_', $desired);
        $desired=parent::_unique($array, $desired);
        return $desired;
    }
    function __clone()
    {
        $this->stmt=null;
    }
    function __toString()
    {
        try {
            if ($this->output_mode==='render') {
                return $this->render();
            } else {
                return (string)$this->getOne();
            }
        } catch (Exception $e) {
            $this->api->caughtException($e);
            //return "Exception: ".$e->getMessage();
        }

        return $this->toString();

        if ($this->expr) {
            return $this->parseTemplate($this->expr);
        }
        return $this->select();
    }

    /** 
     * Explicitly sets template to your query. Remember to change 
     * $this->mode if you switch this
     *
     * @param string $template New template to use by render
     *
     * @return DB_dsql $this
     */
    function template($template)
    {
        $this->template=$template;
        return $this;
    }

    /** 
     * Change prefix for parametric values. Not really useful.
     *
     * @param string $param_base prefix to use for param names
     *
     * @return DB_dsql $this
     */
    function paramBase($param_base)
    {
        $this->param_base=$param_base;
        return $this;
    }

    /**
     * Create new dsql object linked with the same database connection and
     * bearing same data-type. You can use this method to create sub-queries.
     *
     * @return DB_dsql Empty query for same database
     */
    function dsql()
    {
        return $this->owner->dsql(get_class($this));
    }

    /**
     * Converts value into parameter and returns reference. Use only during
     * query rendering. Consider using `consume()` instead.
     *
     * @param string $val String literal containing input data
     *
     * @return string Safe and escapeed string
     */
    function escape($val)
    {
        if ($val===UNDEFINED) {
            return '';
        }
        if (is_array($val)) {
            $out=array();
            foreach ($val as $v) {
                $out[]=$this->escape($v);
            }
            return $out;
        }
        $name=':'.$this->param_base;
        $name=$this->_unique($this->params, $name);
        $this->params[$name]=$val;
        return $name;
    }

    /**
     * Recursively renders sub-query or expression, combining parameters.
     * If the argument is more likely to be a field, use tick=true
     *
     * @param object|string $dsql Expression
     * @param boolean       $tick Preferred quoted style
     *
     * @return string Quoted expression
     */
    function consume($dsql, $tick = true)
    {
        if ($dsql===UNDEFINED) {
            return '';
        }
        if ($dsql===null) {
            return '';
        }
        if (is_object($dsql) && $dsql instanceof Field) {
            $dsql=$dsql->getExpr();
        }
        if (!is_object($dsql) || !$dsql instanceof DB_dsql) {
            return $tick?$this->bt($dsql):$dsql;
        }
        $dsql->params = &$this->params;
        $ret = $dsql->_render();
        if ($dsql->mode==='select') {
            $ret='('.$ret.')';
        }
        unset($dsql->params);
        $dsql->params=array();
        return $ret;
    }

    /** 
     * Defines a custom tag variable. WARNING: always backtick / escaped
     * argument if it's unsafe
     *
     * @param string        $tag   Corresponds to [tag] inside template
     * @param string|object $value Value for the template tag
     *
     * @return DB_dsql $this
     */
    function setCustom($tag, $value = null)
    {
        if (is_array($tag)) {
            foreach ($tag as $key => $val) {
                $this->setCustom($key, $val);
            }
            return $this;
        }
        $this->args['custom'][$tag]=$value;
        return $this;
    }

    /**
     * Removes definition for argument. $q->del('where'), $q->del('fields') etc.
     *
     * @param string $args Could be 'field', 'where', 'order', 'limit', etc
     *
     * @return DB_dsql $this
     */
    function del($args)
    {
        $this->args[$args]=array();
        return $this;
    }

    /**
     * Removes all definitions. Start from scratch
     *
     * @return DB_dsql $this
     */
    function reset()
    {
        $this->args=array();
        return $this;
    }
    // }}}

    // {{{ Dynamic Query Definition methods

    // {{{ Generic methods
    /** 
     * Returns new dynamic query and initializes it to use specific template.
     *
     * @param string $expr SQL Expression. Don't pass unverified input
     * @param array  $tags Array of tags and values. @see setCustom()
     *
     * @return DB_dsql New dynamic query, won't affect $this
     */
    function expr($expr, $tags = array())
    {
        return $this->dsql()->useExpr($expr, $tags);
    }

    /**
     * Change template of existing query instead of creating new one. If unsure
     * use expr()
     *
     * @param string $expr SQL Expression. Don't pass unverified input
     * @param array  $tags Obsolete, use templates / setCustom()
     *
     * @return DB_dsql $this
     */
    function useExpr($expr, $tags = array())
    {
        foreach ($tags as $key => $value) {
            if ($key[0] == ':') {
                $this->extra_params[$key] = $value;
                continue;
            }

            $this->args['custom'][$key]=$value;
        }

        $this->template=$expr;
        if ($tags) {
            $this->setCustom($tags);
        }
        $this->output_mode='render';
        return $this;
    }

    /** 
     * Shortcut to produce expression which concatinates "where" clauses with 
     * "OR" operator
     *
     * @return DB_dsql New dynamic query, won't affect $this
     */
    function orExpr()
    {
        return $this->expr('([orwhere])');
    }

    /**
     * Shortcut to produce expression for series of conditions concatinated
     * with "and". Useful to be passed inside where() or join()
     *
     * @return DB_dsql New dynamic query, won't affect $this
     */
    function andExpr()
    {
        return $this->expr('([andwhere])');
    }

    /**
     * Return expression containing a properly escaped field. Use make 
     * subquery condition reference parent query 
     *
     * @param string $fld Field in SQL table
     *
     * @return DB_dsql Expression pointing to specified field
     */
    function getField($fld)
    {
        if ($this->main_table===false ){
            throw $this->exception(
                'Cannot use getField() when multiple tables are queried'
            );
        }
        return $this->expr(
            $this->bt($this->main_table).
            '.'.
            $this->bt($fld)
        );
    }
    // }}}
    // {{{ table()
    /** 
     * Specifies which table to use in this dynamic query. You may specify
     * array to perform operation on multiple tables.
     *
     * Examples:
     *  $q->table('user');
     *  $q->table('user','u');
     *  $q->table('user')->table('salary')
     *  $q->table(array('user','salary'));
     *  $q->table(array('user','salary'),'user');
     *  $q->table(array('u'=>'user','s'=>'salary'));
     *
     * If you specify multiple tables, you still need to make sure to add 
     * proper "where" conditions. All the above examples return $q (for chaining)
     *
     * You can also call table without arguments, which will return current table:
     *
     *  echo $q->table();
     *
     * If multiple tables are used, "false" is returned. Return is not quoted.
     * Please avoid using table() without arguments as more tables may be
     * dynamically added later.
     *
     * @param string $table Specify table to use
     * @param string $alias Specify alias for the table
     *
     * @return DB_dsql $this
     **/
    function table($table = UNDEFINED, $alias = UNDEFINED)
    {
        if ($table===UNDEFINED) {
            return $this->main_table;
        }

        if (is_array($table)) {
            foreach ($table as $alias => $t) {
                if (is_numeric($alias)) {
                    $alias=UNDEFINED;
                }
                $this->table($t, $alias);
            }
            return $this;
        }

        // main_table tracking allows us to
        if ($this->main_table===null) {
            $this->main_table=$alias===UNDEFINED||!$alias?$table:$alias;
        } elseif ($this->main_table) {
            $this->main_table=false;   // query from multiple tables
        }

        $this->args['table'][]=array($table,$alias);
        return $this;
    }

    /**
     * Renders part of the template: [table]
     * Do not call directly
     *
     * @return string Parsed template chunk
     */
    function render_table()
    {
        $ret=array();
        if (!is_array($this->args['table'])) {
            return;
        }
        foreach ($this->args['table'] as $row) {
            list($table, $alias)=$row;

            $table=$this->bt($table);

            if ($alias!==UNDEFINED && $alias) {
                $table.=' '.$this->bt($alias);
            }

            $ret[]=$table;
        }
        return join(',', $ret);
    }

    /** 
     * Conditionally returns "from", only if table is Specified
     * Do not call directly
     *
     * @return string Parsed template chunk
     */
    function render_from()
    {
        if ($this->args['table']) {
            return 'from';
        }
        return '';
    }

    /** 
     * Returns template component [table_noalias]
     *
     * @return string Parsed template chunk
     */
    function render_table_noalias()
    {
        $ret=array();
        foreach ($this->args['table'] as $row) {
            list($table, $alias)=$row;

            $table=$this->bt($table);


            $ret[]=$table;
        }
        return join(', ', $ret);
    }
    // }}}
    // {{{ field()
    /**
     * Adds new column to resulting select by querying $field. 
     *
     * Examples:
     *  $q->field('name');
     *
     * Second argument specifies table for regular fields
     *  $q->field('name','user');
     *  $q->field('name','user')->field('line1','address');
     *
     * Array as a first argument will specify mulitple fields, same as calling field() multiple times
     *  $q->field(array('name','surname'));
     *
     * Associative array will assume that "key" holds the alias. Value may be object.
     *  $q->field(array('alias'=>'name','alias2'=>surname'));
     *  $q->field(array('alias'=>$q->expr(..), 'alias2'=>$q->dsql()->.. ));
     *
     * You may use array with aliases together with table specifier.
     *  $q->field(array('alias'=>'name','alias2'=>surname'),'user');
     *
     * You can specify $q->expr() for calculated fields. Alias is mandatory.
     *  $q->field( $q->expr('2+2'),'alias');                // must always use alias
     *
     * You can use $q->dsql() for subqueries. Alias is mandatory.
     *  $q->field( $q->dsql()->table('x')... , 'alias');    // must always use alias
     *
     * @param string|array $field Specifies field to select
     * @param string       $table Specify if not using primary table
     * @param string       $alias Specify alias for this field
     *
     * @return DB_dsql $this
     */
    function field($field, $table = null, $alias = null)
    {
        if (is_array($field)) {
            foreach ($field as $alias => $f) {
                if (is_numeric($alias)) {
                    $alias=null;
                }
                $this->field($f, $table, $alias);
            }
            return $this;
        } elseif (is_string($field)) {
            $field=explode(',', $field);
            if (count($field) > 1) {
                foreach ($field as $f) {
                    $this->field($f, $table, $alias);
                }
                return $this;
            }
            $field=$field[0];
        }

        if (is_object($field)) {
            $alias=$table;
            $table=null;
        }
        $this->args['fields'][]=array($field, $table, $alias);
        return $this;
    }

    /**
     * Removes all field definitions and returns only field you specify
     * as parameter to this method. Original query is not affected ($this)
     * Same as for field() syntax
     *
     * @param string|array $field Specifies field to select
     * @param string       $table Specify if not using primary table
     * @param string       $alias Specify alias for this field
     *
     * @return DB_dsql Clone of $this with only one field
     */
    function fieldQuery($field, $table = null, $alias = null)
    {
        $q=clone $this;
        return $q->del('fields')->field($field, $table, $alias);
    }

    /** 
     * Returns template component [field]
     *
     * @return string Parsed template chunk
     */
    function render_field()
    {
        $result=array();
        if (!$this->args['fields']) {
            if ($this->default_field instanceof DB_dsql) {
                return $this->consume($this->default_field);
            }
            return (string)$this->default_field;
        }
        foreach ($this->args['fields'] as $row) {
            list($field,$table,$alias)=$row;
            if ($alias===$field) {
                $alias=UNDEFINED;
            }
            /**/$this->api->pr->start('dsql/render/field/consume');
            $field=$this->consume($field);
            /**/$this->api->pr->stop();
            if (!$field) {
                $field=$table;
                $table=UNDEFINED;
            }
            if ($table && $table!==UNDEFINED) {
                $field=$this->bt($table).'.'.$field;
            }
            if ($alias && $alias!==UNDEFINED) {
                $field.=' '.$this->bt($alias);
            }
            $result[]=$field;
        }
        return join(',', $result);
    }
    // }}}
    // {{{ where() and having()
    /** 
     * Adds condition to your query
     *
     * Examples:
     *  $q->where('id',1);
     *
     * Second argument specifies table for regular fields
     *  $q->where('id>','1');
     *  $q->where('id','>',1);
     *
     * You may use expressions
     *  $q->where($q->expr('a=b'));
     *  $q->where('date>',$q->expr('now()'));
     *  $q->where($q->expr('length(password)'),'>',5);
     *
     * Finally, subqueries can also be used
     *  $q->where('foo',$q->dsql()->table('foo')->field('name'));
     *
     * To specify OR conditions
     *  $q->where($q->orExpr()->where('a',1)->where('b',1));
     *
     * you can also use the shortcut:
     * 
     *  $q->where(array('a is null','b is null'));
     *
     * @param mixed  $field Field, array for OR or Expression
     * @param string $cond  Condition such as '=', '>' or 'is not'
     * @param string $value Value. Will be quoted unless you pass expression
     * @param string $kind  Do not use directly. Use having()
     * 
     * @return DB_dsql $this
     */
    function where($field, $cond = UNDEFINED, $value = UNDEFINED, $kind = 'where')
    {
        if (is_array($field)) {
            // or conditions
            $or=$this->orExpr();
            foreach ($field as $row) {
                if (is_array($row)) {
                    $or->where(
                        $row[0],
                        isset($row[1])?$row[1]:UNDEFINED,
                        isset($row[2])?$row[2]:UNDEFINED
                    );
                } elseif (is_object($row)) {
                    $or->where($row);
                } else {
                    $or->where($or->expr($row));
                }
            }
            $field=$or;
            $this->api->x=1;
        }


        if (is_string($field) && !preg_match('/^[.a-zA-Z0-9_]*$/', $field)) {
            // field contains non-alphanumeric values. Look for condition
            preg_match(
                '/^([^ <>!=]*)([><!=]*|( *(not|is|in|like))*) *$/',
                $field,
                $matches
            );
            $value=$cond;
            $cond=$matches[2];
            if (!$cond) {
                // IF COMPAT
                $matches[1]=$this->expr($field);
                if ($value && $value!==UNDEFINED) {
                    $cond='=';
                } else {
                    $cond=UNDEFINED;
                }
            }
            $field=$matches[1];
        }

        $this->args[$kind][]=array($field,$cond,$value);
        return $this;
    }

    /**
     * Same syntax as where()
     *
     * @param mixed  $field Field, array for OR or Expression
     * @param string $cond  Condition such as '=', '>' or 'is not'
     * @param string $value Value. Will be quoted unless you pass expression
     *
     * @return DB_dsql $this
     */
    function having($field, $cond = UNDEFINED, $value = UNDEFINED)
    {
        return $this->where($field, $cond, $value, 'having');
    }

    /**
     * Subroutine which renders either [where] or [having]
     *
     * @param string $kind 'where' or 'having'
     * 
     * @return string Parsed chunk of query
     */
    function _render_where($kind)
    {
        $ret=array();
        foreach ($this->args[$kind] as $row) {
            list($field,$cond,$value)=$row;

            if (is_object($field)) {
                // if first argument is object, condition must be explicitly
                // specified
                $field=$this->consume($field);
            } else {
                list($table, $field)=explode('.', $field, 2);
                if ($field) {
                    $field=$this->bt($table).'.'.$this->bt($field);
                } else {
                    $field=$this->bt($table);
                }
            }

            if ($value===UNDEFINED && $cond===UNDEFINED) {
                $r=$field;
                $ret[]=$r;
                continue;
            }

            if ($value===UNDEFINED) {
                $value=$cond;
                $cond='=';
                if (is_array($value)) {
                    $cond='in';
                }
                if (is_object($value) && @$value->mode==='select') {
                    $cond='in';
                }
            } else {
                $cond=trim($cond);
            }

            if ($cond==='=' && $value===null) {
                $cond='is';
            }


            if ($cond==='in' && is_string($value)) {
                $value=explode(',', $value);
            }

            if (is_array($value)) {
                $v=array();
                foreach ($value as $vv) {
                    $v[]=$this->escape($vv);
                }
                $value='('.join(',', $v).')';
                $cond='in';
                $r=$this->consume($field).' '.$cond.' '.$value;
                $ret[]=$r;
                continue;
            }

            if (is_object($value)) {
                $value=$this->consume($value);
            } else {
                $value=$this->escape($value);
            }

            $r=$field.' '.$cond.' '.$value;
            $ret[]=$r;
        }
        return $ret;
    }

    /**
     * Renders [where]
     *
     * @return string rendered SQL chunk
     */
    function render_where()
    {
        if (!$this->args['where']) {
            return;
        }
        return 'where '.join(' and ', $this->_render_where('where'));
    }

    /**
     * Renders [orwhere]
     *
     * @return string rendered SQL chunk
     */
    function render_orwhere()
    {
        if (!$this->args['where']) {
            return;
        }
        return join(' or ', $this->_render_where('where'));
    }

    /**
     * Renders [andwhere]
     *
     * @return string rendered SQL chunk
     */
    function render_andwhere()
    {
        if (!$this->args['where']) {
            return;
        }
        return join(' and ', $this->_render_where('where'));
    }

    /**
     * Renders [having]
     *
     * @return string rendered SQL chunk
     */
    function render_having()
    {
        if (!$this->args['having']) {
            return;
        }
        return 'having '.join(' and ', $this->_render_where('having'));
    }
    // }}}
    // {{{ join()
    /** 
     * Joins your query with another table
     *
     * Examples:
     *  $q->join('address');         // on user.address_id=address.id
     *  $q->join('address.user_id'); // on address.user_id=user.id
     *  $q->join('address a');       // With alias
     *  $q->join(array('a'=>'address')); // Also alias
     *
     * Second argument may specify the field of the master table
     *  $q->join('address', 'billing_id');
     *  $q->join('address.code', 'code');
     *  $q->join('address.code', 'user.code');
     *
     * Third argument may specify which kind of join to use.
     *  $q->join('address', null, 'left');
     *  $q->join('address.code', 'user.code', 'inner');
     *
     * Using array syntax you can join multiple tables too
     *  $q->join(array('a'=>'address', 'p'=>'portfolio'));
     *
     * You can use expression for more complex joins
     *  $q->join('address', 
     *      $q->exprOrExpr()
     *          ->where('user.billing_id=address.id')
     *          ->where('user.technical_id=address.id')
     *  )
     *
     * @param string $foreign_table  Table to join with
     * @param string $master_field   Field in master table
     * @param string $join_kind      'left' or 'inner', etc
     * @param string $_foreign_alias Internal, don't use
     *
     * @return DB_dsql $this
     */
    function join(
        $foreign_table,
        $master_field = null,
        $join_kind = null,
        $_foreign_alias = null
    ) {
        // Compatibility mode
        if (isset($this->api->compat)) {
            if (strpos($foreign_table, ' ')) {
                list($foreign_table, $alias)=explode(' ', $foreign_table);
                $foreign_table=array($alias => $foreign_table);
            }
            if (strpos($master_field, '=')) {
                $master_field=$this->expr($master_field);
            }
        }

        // If array - add recursively
        if (is_array($foreign_table)) {
            foreach ($foreign_table as $alias => $foreign) {
                if (is_numeric($alias)) {
                    $alias=null;
                }

                $this->join($foreign, $master_field, $join_kind, $alias);
            }
            return $this;
        }
        $j=array();

        // Split and deduce fields
        list($f1, $f2)=explode('.', $foreign_table, 2);

        if (is_object($master_field)) {
            $j['expr']=$master_field;
        } else {
            // Split and deduce primary table
            if (is_null($master_field)) {
                list($m1, $m2)=array(null, null);
            } else {
                list($m1, $m2)=explode('.', $master_field, 2);
            }
            if (is_null($m2)) {
                $m2=$m1;
                $m1=null;
            }
            if (is_null($m1)) {
                $m1=$this->main_table;
            }

            // Identify fields we use for joins
            if (is_null($f2) && is_null($m2)) {
                $m2=$f1.'_id';
            }
            if (is_null($m2)) {
                $m2='id';
            }
            $j['m1']=$m1;
            $j['m2']=$m2;
        }
        $j['f1']=$f1;
        if (is_null($f2)) {
            $f2='id';
        }
        $j['f2']=$f2;

        $j['t']=$join_kind?:'left';
        $j['fa']=$_foreign_alias;

        $this->args['join'][]=$j;
        return $this;
    }

    /**
     * Renders [join]
     *
     * @return string rendered SQL chunk
     */
    function render_join()
    {
        if (!$this->args['join']) {
            return '';
        }
        $joins=array();
        foreach ($this->args['join'] as $j) {
            $jj='';

            $jj.=$j['t'].' join ';

            $jj.=$this->bt($j['f1']);

            if (!is_null($j['fa'])) {
                $jj.=' as '.$this->bt($j['fa']);
            }

            $jj.=' on ';

            if ($j['expr']) {
                $jj.=$this->consume($j['expr']);
            } else {
                $jj.=
                    $this->bt($j['fa']?:$j['f1']).'.'.
                    $this->bt($j['f2']).' = '.
                    $this->bt($j['m1']).'.'.
                    $this->bt($j['m2']);
            }
            $joins[]=$jj;
        }
        return implode(' ', $joins);
    }
    // }}}
    // {{{ group()
    /**
     * Implemens GROUP BY functionality. Simply pass either string field
     * or expression
     *
     * @param string|object $group Group by this
     *
     * @return DB_dsql $this
     */
    function group($group)
    {
        return $this->_setArray($group, 'group');
    }

    /**
     * Renders [group]
     *
     * @return string rendered SQL chunk
     */
    function render_group()
    {
        if (!$this->args['group']) {
            return'';
        }
        $x=array();
        foreach ($this->args['group'] as $arg) {
            $x[]=$this->consume($arg);
        }
        return 'group by '.implode(', ', $x);
    }
    // }}}
    // {{{ order()
    /** 
     * Orders results by field or Expression. See documentation for full
     * list of possible arguments
     *
     * $q->order('name');
     * $q->order('name desc');
     * $q->order('name desc, id asc')
     * $q->order('name',true);
     *
     * @param string $order Order by
     * @param string $desc  true to sort descending
     *
     * @return DB_dsql $this
     */
    function order($order, $desc = null)
    {
        // Case with comma-separated fields or first argument being an array
        if (is_string($order) && strpos($order, ',')!==false) {
            // Check for multiple
            $order=explode(',', $order);
        }
        if (is_array($order)) {
            if (!is_null($desc)) {
                throw $this->exception(
                    'If first argument is array, second argument must not be used'
                );
            }
            foreach (array_reverse($order) as $o) {
                $this->order($o);
            }
            return $this;
        }

        // First argument may contain space, to divide field and keyword
        if (is_null($desc) && is_string($order) && strpos($order, ' ')!==false) {
            list($order, $desc)=array_map('trim', explode(' ', trim($order), 2));
        }

        if (is_string($order) && strpos($order, '.')!==false) {
            $order=join('.', $this->bt(explode('.', $order)));
        }

        if (is_bool($desc)) {
            $desc=$desc?'desc':'';
        } elseif (strtolower($desc)==='asc') {
            $desc='';
        } elseif ($desc && strtolower($desc)!='desc') {
            throw $this->exception('Incorrect ordering keyword')
                ->addMoreInfo('order by', $desc);
        }

        // TODO:
        /*
        if (isset($this->args['order'][0]) and (
            $this->args['order'][0] === array($order,$desc))) {
        }
         */
        $this->args['order'][]=array($order,$desc);
        return $this;
    }

    /**
     * Renders [order]
     *
     * @return string rendered SQL chunk
     */
    function render_order()
    {
        if (!$this->args['order']) {
            return'';
        }
        $x=array();
        foreach ($this->args['order'] as $tmp) {
            list($arg,$desc)=$tmp;
            $x[]=$this->consume($arg).($desc?(' '.$desc):'');
        }
        return 'order by '.implode(', ', array_reverse($x));
    }
    // }}}
    // {{{ option() and args()
    /** 
     * Defines query option, such as DISTINCT
     *
     * @param string|expresion $option Option to put after SELECT
     *
     * @return DB_dsql $this
     */
    function option($option)
    {
        return $this->_setArray($option, 'options');
    }

    /**
     * Renders [options]
     *
     * @return string rendered SQL chunk
     */
    function render_options()
    {
        return @implode(' ', $this->args['options']);
    }

    /** 
     * Defines insert query option, such as IGNORE
     *
     * @param string|expresion $option Option to put after SELECT
     *
     * @return DB_dsql $this
     */
    function option_insert($option)
    {
        return $this->_setArray($option, 'options_insert');
    }

    /**
     * Renders [options_insert]
     *
     * @return string rendered SQL chunk
     */
    function render_options_insert()
    {
        if (!$this->args['options_insert']) {
            return '';
        }
        return implode(' ', $this->args['options_insert']);
    }
    // }}}
    // {{{  call() and function execution
    /** 
     * Sets a template for a user-defined method call with specified arguments
     *
     * @param string $fx   Name of the user defined method
     * @param array  $args Arguments in mixed form
     *
     * @return DB_dsql $this
     */
    function call($fx, $args = null)
    {
        $this->mode='call';
        $this->args['fx']=$fx;
        if (!is_null($args)) {
            $this->args($args);
        }
        $this->template="call [fx]([args])";
        return $this;
    }

    /**
     * Executes a standard function with arguments, such as IF
     *
     * $q->fx('if', array($condition, $if_true, $if_false));
     *
     * @param string $fx   Name of the built-in method
     * @param array  $args Arguments
     *
     * @return DB_dsql $this
     */
    function fx($fx, $args = null)
    {
        $this->mode='fx';
        $this->args['fx']=$fx;
        if (!is_null($args)) {
            $this->args($args);
        }
        $this->template="[fx]([args])";
        return $this;
    }

    /**
     * set arguments for call(). Used by fx() and call() but you can use This
     * with ->expr("in ([args])")->args($values);
     *
     * @param array $args Array with mixed arguments 
     *
     * @return DB_dsql $this
     */
    function args($args)
    {
        return $this->_setArray($args, 'args', false);
    }

    /**
     * Renders [args]
     *
     * @return string rendered SQL chunk
     */
    function render_args()
    {
        $x=array();
        foreach ($this->args['args'] as $arg) {
            $x[]=is_object($arg)?
                $this->consume($arg):
                $this->escape($arg);
        }
        return implode(', ', $x);
    }

    /**
     * Sets IGNORE option 
     *
     * @return DB_dsql $this
     */
    function ignore()
    {
        $this->args['options_insert'][]='ignore';
        return $this;
    }

    /**
     * Check if specified option was previously added
     *
     * @param string $option Which option to check?
     *
     * @return boolean
     */
    function hasOption($option)
    {
        return @in_array($option, $this->args['options']);
    }

    /**
     * Check if specified insert option was previously added
     *
     * @param string $option Which option to check?
     *
     * @return boolean
     */
    function hasInsertOption($option)
    {
        return @in_array($option, $this->args['options_insert']);
    }
    // }}}
    // {{{ limit()
    /** 
     * Limit how many rows will be returned
     *
     * @param int $cnt   Number of rows to return
     * @param int $shift Offset, how many rows to skip
     *
     * @return DB_dsql $this
     */
    function limit($cnt, $shift = 0)
    {
        $this->args['limit']=array(
            'cnt'=>$cnt,
            'shift'=>$shift
        );
        return $this;
    }

    /**
     * Renders [limit]
     *
     * @return string rendered SQL chunk
     */
    function render_limit()
    {
        if ($this->args['limit']) {
            return 'limit '.
                (int)$this->args['limit']['shift'].
                ', '.
                (int)$this->args['limit']['cnt'];
        }
    }
    // }}}
    // {{{ set()
    /** 
     * Sets field value for INSERT or UPDATE statements
     *
     * @param string $field Name of the field
     * @param mixed  $value Value of the field
     *
     * @return DB_dsql $this
     */
    function set($field, $value = UNDEFINED)
    {
        if ($value===false) {
            throw $this->exception('Value "false" is not supported by SQL');
        }
        if (is_array($field)) {
            foreach ($field as $key => $value) {
                $this->set($key, $value);
            }
            return $this;
        }

        if ($value===UNDEFINED) {
            throw $this->exception('Specify value when calling set()');
        }

        $this->args['set'][$field]=$value;
        return $this;
    }
    /**
     * Renders [set] for UPDATE query
     *
     * @return string rendered SQL chunk
     */
    function render_set()
    {
        $x=array();
        if ($this->args['set']) {
            foreach($this->args['set'] as $field=>$value){
                if (is_object($field)) {
                    $field=$this->consume($field);
                } else {
                    $field=$this->bt($field);
                }
                if (is_object($value)) {
                    $value=$this->consume($value);
                } else {
                    $value=$this->escape($value);
                }

                $x[]=$field.'='.$value;
            }
        }
        return join(', ', $x);
    }
    /**
     * Renders [set_fields] for INSERT
     *
     * @return string rendered SQL chunk
     */
    function render_set_fields()
    {
        $x=array();
        if ($this->args['set']) {
            foreach ($this->args['set'] as $field => $value) {

                if (is_object($field)) {
                    $field=$this->consume($field);
                } else {
                    $field=$this->bt($field);
                }

                $x[]=$field;
            }
        }
        return join(',', $x);
    }
    /**
     * Renders [set_values] for INSERT
     *
     * @return string rendered SQL chunk
     */
    function render_set_values()
    {
        $x=array();
        if ($this->args['set']) {
            foreach ($this->args['set'] as $field => $value) {

                if (is_object($value)) {
                    $value=$this->consume($value);
                } else {
                    $value=$this->escape($value);
                }

                $x[]=$value;
            }
        }
        return join(',', $x);
    }
    // }}}
    // {{{ Miscelanious
    /**
     * Adds backtics around argument. This will allow you to use reserved
     * SQL words as table or field names such as "table"
     *
     * @param string $s any string
     *
     * @return string Quoted string
     */
    function bt($s)
    {
        if (is_array($s)) {
            $out=array();
            foreach ($s as $ss) {
                $out[]=$this->bt($ss);
            }
            return $out;
        }

        if (!$this->bt
            || is_object($s)
            || $s==='*'
            || strpos($s, '.')!==false
            || strpos($s, '(')!==false
            || strpos($s, $this->bt)!==false
        ) {
            return $s;
        }

        return $this->bt.$s.$this->bt;
    }
    /**
     * Internal method which can be used by simple param-giving methods such
     * as option(), group(), etc
     *
     * @param string  $values       hm
     * @param string  $name         hm
     * @param boolean $parse_commas hm
     *
     * @private
     * @return DB_dsql $this
     */
    function _setArray($values, $name, $parse_commas = true)
    {
        if (is_string($values) && $parse_commas && strpos($values, ',')) {
            $values=explode(',', $values);
        }
        if (!is_array($values)) {
            $values=array($values);
        }
        if (!isset($this->args[$name])) {
            $this->args[$name]=array();
        }
        $this->args[$name]=array_merge($this->args[$name], $values);
        return $this;
    }
    // }}}

    // }}}

    // {{{ Statement templates and interfaces

    /**
     * Switch template for this query. Determines what would be done
     * on execute.
     * 
     * By default it is in SELECT mode
     *
     * @param string $mode A key for $this->sql_templates
     * 
     * @return DB_dsql $this
     */
    function SQLTemplate($mode)
    {
        $this->mode=$mode;
        $this->template=$this->sql_templates[$mode];
        return $this;
    }
    /**
     * Return expression for concatinating multiple values
     * Accepts variable number of arguments, all of them would be
     * escaped
     * 
     * @return DB_dsql clone of $this
     */
    function concat()
    {
        $t=clone $this;
        return $t->fx('concat', func_get_args());
    }

    /**
     * Creates a query for listing tables in databse-specific form
     * Agile Toolkit DSQL does not pretend to know anything about model
     * structure, so result parsing is up to you
     *
     * @param string $table Table
     * 
     * @return DB_dsql clone of $this
     */
    function describe($table)
    {
        return $this->expr('desc [desc_table]')
            ->setCustom('desc_table', $this->bt($table));
    }

    /**
     * Renders [fx]
     *
     * @return string rendered SQL chunk
     */
    function render_fx()
    {
        return $this->args['fx'];
    }

    /**
     * Creates expression for SUM()
     *
     * @param string|object $arg Typically an expression of a sub-query
     *
     * @return DB_dsql clone of $this
     */
    function sum($arg = null)
    {
        if (is_null($arg)) {
            $arg='*';
        }
        return $this->expr('sum([sum])')->setCustom('sum', $this->bt($arg));
    }

    /**
     * Creates expression for COUNT()
     *
     * @param string|object $arg Typically an expression of a sub-query
     *
     * @return DB_dsql clone of $this
     */
    function count($arg = null)
    {
        if (is_null($arg)) {
            $arg='*';
        }
        return $this->expr('count([count])')->setCustom('count', $this->bt($arg));
    }
    /**
     * Returns method for generating random numbers. This is used for ordering
     * table in random order
     *
     * @return DB_dsql clone of $this
     */
    function random()
    {
        return $this->expr('rand()');
    }
    // }}}

    // {{{ More complex query generations and specific cases

    /** 
     * Executes current query
     *
     * @return DB_dsql $this
     */
    function execute()
    {
        try {
            /**/$this->api->pr->start('dsql/execute/render');
            $q=$this->render();
            /**/$this->api->pr->next('dsql/execute/query');
            $this->stmt=$this->owner->query($q, $this->params);
            $this->template=$this->mode=null;
            /**/$this->api->pr->stop();
            return $this;
        } catch (PDOException $e) {
            throw $this->exception('Database Query Failed')
                ->addPDOException($e)
                ->addMoreInfo('mode', $this->mode)
                ->addMoreInfo('params', $this->params)
                ->addMoreInfo('query', $q)
                ->addMoreInfo('template', $this->template)
                ;
        }
    }

    /**
     * Executes select query.
     *
     * @return DB_dsql() $this
     */
    function select()
    {
        return $this->SQLTemplate('select')->execute();
    }

    /** 
     * Executes insert query. Returns ID of new record. 
     *
     * @return int new record ID (from last_id)
     */
    function insert()
    {
        $this->SQLTemplate('insert')->execute();
        return
            $this->hasInsertOption('ignore')?null:
            $this->owner->lastID();
    }

    /**
     * Inserts multiple rows of data. Uses ignore option
     * AVOID using this, might not be implemented correctly
     *
     * @param array $array Insert multiple rows into table with one query
     *
     * @return array List of IDs
     */
    function insertAll($array)
    {
        $ids=array();
        foreach ($array as $hash) {
            $ids[]=$this->del('set')->set($hash)->insert();
        }
        return $ids;
    }

    /**
     * Executes update query
     *
     * @return DB_dsql $this
     */
    function update()
    {
        return $this->SQLTemplate('update')->execute();
    }

    /**
     * Executes replace query
     *
     * @return DB_dsql $this
     */
    function replace()
    {
        return $this->SQLTemplate('replace')->execute();
    }

    /**
     * Executes delete query
     *
     * @return DB_dsql $this
     */
    function delete()
    {
        return $this->SQLTemplate('delete')->execute();
    }

    /**
     * Executes truncate query
     *
     * @return DB_dsql $this
     */
    function truncate()
    {
        return $this->SQLTemplate('truncate')->execute();
    }


    /** @obsolete, use select() */
    function do_select(){
        return $this->select();
    }
    /** @obsolete, use insert() */
    function do_insert(){
        return $this->insert();
    }
    /** @obsolete, use update() */
    function do_update(){
        return $this->update();
    }
    /** @obsolete, use replace() */
    function do_replace(){
        return $this->replace();
    }
    // }}}

    // {{{ Data fetching modes
    /** 
     * Will execute DSQL query and return all results inside array of hashes
     *
     * @return array Array of associative arrays
     */
    function get()
    {
        if (!$this->stmt) {
            $this->execute();
        }
        $res=$this->stmt->fetchAll(PDO::FETCH_ASSOC);
        $this->rewind();
        $this->stmt=null;
        return $res;
    }

    /**
     * Will execute DSQL query and return first column of a first row
     *
     * You can also simply cast your DSQL into string to get this value
     *
     * echo $dsql;
     *
     * @return string Value of first column in first row
     */
    function getOne()
    {
        $res=$this->getRow();
        $this->rewind();
        $this->stmt=null;
        return $res[0];
    }
    /**
     * Will execute DSQL query and return first row as array (not hash). If 
     * you call several times will return subsequent rows
     *
     * @return array Next row of your data (not hash)
     */
    function getRow()
    {
        return $this->fetch(PDO::FETCH_NUM);
    }
    /** 
     * Will execute DSQL query and return first row as hash (column=>value) 
     *
     * @return array Hash of next row in data stream
     */
    function getHash()
    {
        return $this->fetch(PDO::FETCH_ASSOC);
    }
    /**
     * Will execute the query (if it's not executed already) and return
     * first row
     *
     * @param int $mode PDO fetch mode
     *
     * @return mixed return result of PDO::fetch
     */
    function fetch($mode = PDO::FETCH_ASSOC)
    {
        if (!$this->stmt) {
            $this->execute();
        }
        return $this->stmt->fetch($mode);
    }
    // {{{ Obsolete functions 
    /** @obsolete. Use get() */
    function fetchAll(){
        return $this->get();
    }
    /** @obsolete. Use getQne() */
    function do_getOne(){
        return $this->getOne(); 
    }
    /** @obsolete. Use get() */
    function do_getAllHash(){ 
        return $this->get(); 
    }
    function do_getAll(){ 
        return $this->get(); 
    }
    /** @obsolete. Use get() */
    function getAll(){
        return $this->get();
    }
    /** @obsolete. Use getRow() */
    function do_getRow(){ 
        return $this->getRow();
    }
    /** @obsolete. Use getHash() */
    function do_getHash(){ 
        return $this->getHash(); 
    }
    // }}}


    /** 
     * Sets flag to hint SQL (if supported) to prepare total number of columns.
     * Use foundRows() to read this afterwards
     *
     * @return DB_dsql $this
     */
    function calcFoundRows(){
        return $this;
    }

    /**
     * Obsolete - naming bug
     */
    function calc_found_rows()
    {
        return $this->calcFoundRows();
    }
    /**
     * After fetching data, call this to find out how many rows there were in
     * total. Call calcFoundRows() for better performance
     *
     * @return string number of results
     */
    function foundRows()
    {
        if ($this->hasOption('SQL_CALC_FOUND_ROWS')) {
            return $this->owner->getOne('select found_rows()');
        }
        /* db-compatibl way: */
        $c=clone $this;
        $c->del('limit');
        $c->fieldQuery('count(*)');
        return $c->getOne();
    }
    // }}}

    // {{{ Iterator support
    public $data=false;
    public $_iterating=false;
    public $preexec=false;
    /**
     * Execute query faster, but don't fetch data until iterating started. This
     * can be done if you need to know foundRows() before fetching data
     *
     * @return DB_dsql $this
     */
    function preexec()
    {
        $this->execute();
        $this->preexec=true;
        return $this;
    }
    function rewind()
    {
        if($this->_iterating){
            $this->stmt=null;
            $this->_iterating=false;
        }
        $this->_iterating=true;
        return $this;
    }
    function next()
    {
        $this->data = $this->fetch();
        return $this;
    }
    function current()
    {
        return $this->data;
    }
    function key()
    {
        return $this->data[$this->id_field];
    }
    function valid()
    {
        if(!$this->stmt || $this->preexec){
            $this->preexec=false;
            $this->data = $this->fetch();
        }
        return (boolean)$this->data;
    }
    // }}}

    // {{{ Rendering
    /**
     * Will set a flag which will output query (echo) as it is being rendered.
     *
     * @return DB_dsql $this
     */
    function debug()
    {
        $this->debug=1;
        return $this;
    }
    /**
     * Return formatted debug output
     *
     * @param string $r Rendered material
     *
     * @return string debug of the query
     */
    function getDebugQuery($r = null)
    {
        if (!$r) {
            $r=$this->_render();
        }
        $d=$r;
        $pp=array();
        $d=preg_replace('/`([^`]*)`/', '`<font color="black">\1</font>`', $d);
        foreach (array_reverse($this->params) as $key => $val) {
            if (is_string($val)) {
                $d=preg_replace('/'.$key.'([^_]|$)/', '"<font color="green">'.
                    htmlspecialchars(addslashes($val)).'</font>"\1', $d);
            } elseif (is_null($val)) {
                $d=preg_replace(
                    '/'.$key.'([^_]|$)/',
                    '<font color="black">NULL</font>\1',
                    $d
                );
            } elseif (is_numeric($val)) {
                $d=preg_replace(
                    '/'.$key.'([^_]|$)/',
                    '<font color="red">'.$val.'</font>\1',
                    $d
                );
            } else {
                $d=preg_replace('/'.$key.'([^_]|$)/', $val.'\1', $d);
            }

            $pp[]=$key;
        }
        return "<font color='blue'>".$d."</font> <font color='gray'>[".
            join(', ', $pp)."]</font><br/>";
    }
    /**
     * Converts query into string format. This will contain parametric
     * references
     *
     * @return string resulting query
     */
    function render()
    {
        $this->params=$this->extra_params;
        $r=$this->_render();
        if ($this->debug) {
            echo $this->getDebugQuery($r);
        }
        return $r;
    }
    /**
     * Helper for render(), which does the actual work
     *
     * @private
     * @return string resulting query
     */
    function _render()
    {
        /**/$this->api->pr->start('dsql/render');
        if (is_null($this->template)) {
            $this->SQLTemplate('select');
        }
        $self=$this;
        $res= preg_replace_callback(
            '/\[([a-z0-9_]*)\]/',
            function ($matches) use ($self) {
                /**/$self->api->pr->next('dsql/render/'.$matches[1], true);
                $fx='render_'.$matches[1];
                if (isset($self->args['custom'][$matches[1]])) {
                    return $self->consume($self->args['custom'][$matches[1]], false);
                } elseif ($self->hasMethod($fx)) {
                    return $self->$fx();
                } else {
                    return $matches[0];
                }
            },
                $this->template
            );
        /**/$this->api->pr->stop(null, true);
        return $res;
    }
    // }}}
}
Back to Top