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
//! PostgreSQL implementation.

use crate::{archiver::YotsubaArchiver, enums::YotsubaEndpoint, sql::*};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use enum_iterator::IntoEnumIterator;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, convert::TryFrom};
use tokio_postgres::{Client, Row, Statement};

/// Unused for now. This is just for the Cargo doc.
#[cold]
#[allow(dead_code)]
pub mod core {
    //! Implementation of the default schema.  
    //!
    //! - [`Client`] The PostgreSQL implementation ([`tokio_postgres`]) to run the SQL queries
    //! - [`Schema`](struct.Schema.html) The schema and list of SQL queries that `ena` uses
    #[allow(unused_imports)]
    use super::*;
    use std::ops::Deref;

    /// Client wrapper for the implementation of the database ([`tokio_postgres`])
    pub struct Client<T>(T);

    impl<T> Client<T> {
        fn new(x: T) -> Client<T> {
            Client(x)
        }
    }

    impl<T> Deref for Client<T> {
        type Target = T;

        fn deref(&self) -> &T {
            &self.0
        }
    }

    /// Core schema and SQL queries to use
    pub struct Schema;

    impl Schema {
        fn new() -> Self {
            Self {}
        }
    }

    /// The schema for each row.  
    ///
    /// This is just for documentation.  
    /// Optimized specifically for Postgres by using [column tetris](https://www.2ndquadrant.com/en/blog/on-rocks-and-sand/) to save space.  
    /// The original schema is used as a base with reasonable changes made.  
    ///
    /// ## Added
    /// - [`subnum`](struct.Post.html#structfield.subnum) For ghost posts
    /// - [`deleted_on`](struct.Post.html#structfield.deleted_on) For context
    /// - [`last_modified`](struct.Post.html#structfield.last_modified) For context and possible use
    ///   of search engines
    /// - [`sha256`](struct.Post.html#structfield.sha256) For file dedup and to prevent [MD5 collisions](https://github.com/4chan/4chan-API/issues/70)
    /// - [`sha256t`](struct.Post.html#structfield.sha256t) For thumbnail dedup and to prevent [MD5 collisions](https://github.com/4chan/4chan-API/issues/70)
    /// - [`extra`](struct.Post.html#structfield.extra) For any future schema changes
    /// ## Removed
    /// - [`now`](struct.Post.html#structfield.now) Redundant with
    ///   [`time`](struct.Post.html#structfield.time)
    /// - [`archived`](struct.Post.html#structfield.archived) Redundant with
    ///   [`archived_on`](struct.Post.html#structfield.archived_on)  
    /// ## Modified  
    /// - [`md5`](struct.Post.html#structfield.md5) From base64 to binary, to save space
    /// - [`country`](struct.Post.html#structfield.country) Use `troll_country` if [`country`](#structfield.country) is `NULL`
    /// - To [`bool`], to save space
    ///     - [`sticky`](struct.Post.html#structfield.sticky)
    ///     - [`closed`](struct.Post.html#structfield.closed)
    ///     - [`filedeleted`](struct.Post.html#structfield.filedeleted)
    ///     - [`spoiler`](struct.Post.html#structfield.spoiler)
    ///     - [`m_img`](struct.Post.html#structfield.m_img)
    ///     - [`bumplimit`](struct.Post.html#structfield.bumplimit)
    ///     - [`imagelimit`](struct.Post.html#structfield.imagelimit)
    /// ## Side notes  
    /// Postgres doesn't have a numeric smaller than [`i16`] or unsigned integers.  
    /// For example: [`custom_spoiler`](struct.Post.html#structfield.custom_spoiler) should ideally
    /// be [`u8`].
    ///
    /// ---
    /// <details><summary><b>SQL table query</b></summary>
    /// <p>
    ///
    /// ```sql
    ///                                           Table "asagi.a"
    ///      Column     |   Type   | Collation | Nullable | Default | Storage  | Stats target | Description
    /// ----------------+----------+-----------+----------+---------+----------+--------------+-------------
    ///  no             | bigint   |           | not null |         | plain    |              |
    ///  subnum         | bigint   |           |          |         | plain    |              |
    ///  tim            | bigint   |           |          |         | plain    |              |
    ///  resto          | bigint   |           | not null | 0       | plain    |              |
    ///  time           | bigint   |           | not null | 0       | plain    |              |
    ///  last_modified  | bigint   |           |          |         | plain    |              |
    ///  archived_on    | bigint   |           |          |         | plain    |              |
    ///  deleted_on     | bigint   |           |          |         | plain    |              |
    ///  fsize          | integer  |           |          |         | plain    |              |
    ///  w              | integer  |           |          |         | plain    |              |
    ///  h              | integer  |           |          |         | plain    |              |
    ///  replies        | integer  |           |          |         | plain    |              |
    ///  images         | integer  |           |          |         | plain    |              |
    ///  unique_ips     | integer  |           |          |         | plain    |              |
    ///  tn_w           | smallint |           |          |         | plain    |              |
    ///  tn_h           | smallint |           |          |         | plain    |              |
    ///  custom_spoiler | smallint |           |          |         | plain    |              |
    ///  since4pass     | smallint |           |          |         | plain    |              |
    ///  sticky         | boolean  |           |          |         | plain    |              |
    ///  closed         | boolean  |           |          |         | plain    |              |
    ///  filedeleted    | boolean  |           |          |         | plain    |              |
    ///  spoiler        | boolean  |           |          |         | plain    |              |
    ///  m_img          | boolean  |           |          |         | plain    |              |
    ///  bumplimit      | boolean  |           |          |         | plain    |              |
    ///  imagelimit     | boolean  |           |          |         | plain    |              |
    ///  name           | text     |           |          |         | extended |              |
    ///  sub            | text     |           |          |         | extended |              |
    ///  com            | text     |           |          |         | extended |              |
    ///  filename       | text     |           |          |         | extended |              |
    ///  ext            | text     |           |          |         | extended |              |
    ///  trip           | text     |           |          |         | extended |              |
    ///  id             | text     |           |          |         | extended |              |
    ///  capcode        | text     |           |          |         | extended |              |
    ///  country        | text     |           |          |         | extended |              |
    ///  country_name   | text     |           |          |         | extended |              |
    ///  semantic_url   | text     |           |          |         | extended |              |
    ///  tag            | text     |           |          |         | extended |              |
    ///  md5            | bytea    |           |          |         | extended |              |
    ///  sha256         | bytea    |           |          |         | extended |              |
    ///  sha256t        | bytea    |           |          |         | extended |              |
    ///  extra          | jsonb    |           |          |         | extended |              |
    /// Indexes:
    ///     "unq_idx_no_subnum_a" UNIQUE, btree (no, COALESCE(subnum, 0::bigint))
    ///     "idx_a_no_resto" btree (no, resto)
    ///     "idx_a_time" btree (((floor(("time" / 86400)::double precision) * '86400'::double precision)::bigint))
    ///     "trgm_idx_a_com" gin (com asagi.gin_trgm_ops)
    /// ```
    ///
    /// </p>
    /// </details>
    /// 
    /// ---  
    /// The following below is taken from the [official docs](https://github.com/4chan/4chan-API) where applicable.  
    #[cold]
    #[allow(dead_code)]
    #[rustfmt::skip]
    #[derive(Deserialize, Serialize, Debug, Clone)]
    #[serde(default)]
    pub struct Post {
        /// Appears: `always`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> The numeric post ID</font>
        pub no: i64,
    
        /// Appears: `always if post is a ghost post`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Used in FF for ghost posting</font>
        pub subnum: Option<i64>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `integer`  
        /// <font style="color:#789922;">> Unix timestamp + microtime that an image was
        /// uploaded</font>
        pub tim: Option<i64>,
    
        /// Appears: `always`  
        /// Possible values: `0` or `any positive integer`  
        /// <font style="color:#789922;">> For replies: this is the ID of the thread being replied
        /// to. For OP: this value is zero</font>
        pub resto: i64,
    
        /// Appears: `always`  
        /// Possible values: `UNIX timestamp`  
        /// <font style="color:#789922;">> UNIX timestamp the post was created</font>
        pub time: i64,
    
        /// Appears: `always`  
        /// Possible values: `UNIX timestamp`  
        /// <font style="color:#789922;">> UNIX timestamp the post had any of its fields
        /// modified</font>
        pub last_modified: i64,
    
        /// Appears: `OP only, if thread has been archived`  
        /// Possible values: `UNIX timestamp`  
        /// <font style="color:#789922;">> UNIX timestamp the post was archived</font>
        pub archived_on: Option<i64>,
    
        /// Appears: `always if post has been deleted`  
        /// Possible values: `UNIX timestamp`  
        /// <font style="color:#789922;">> UNIX timestamp the post was deleted</font>
        pub deleted_on: Option<i64>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Size of uploaded file in bytes</font>
        pub fsize: Option<i32>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Image width dimension</font>
        pub w: Option<i32>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Image height dimension</font>
        pub h: Option<i32>,
    
        /// Appears: `OP only`  
        /// Possible values: `0` or `any positive integer`  
        /// <font style="color:#789922;">> Total number of replies to a thread</font>
        pub replies: Option<i32>,
    
        /// Appears: `OP only`  
        /// Possible values: `0` or `any positive integer`  
        /// <font style="color:#789922;">> Total number of image replies to a thread</font>
        pub images: Option<i32>,
    
        /// Appears: `OP only, only if thread has NOT been archived`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Number of unique posters in a thread</font>
        pub unique_ips: Option<i32>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Thumbnail image width dimension</font>
        pub tn_w: Option<i16>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `any positive integer`  
        /// <font style="color:#789922;">> Thumbnail image height dimension</font>
        pub tn_h: Option<i16>,
    
        /// Appears: `if post has attachment and attachment is spoilered`  
        /// Possible values: `1-10` or not set  
        /// <font style="color:#789922;">> The custom spoiler ID for a spoilered image </font>
        pub custom_spoiler: Option<i16>,
    
        /// Appears: `if poster put 'since4pass' in the options field`  
        /// Possible values: `any 4 digit year`  
        /// <font style="color:#789922;">> Year 4chan pass bought</font>
        pub since4pass: Option<i16>,
    
        /// Appears: `OP only, if thread is currently stickied`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> If the thread is being pinned to the top of the page</font>
        pub sticky: Option<bool>,
    
        /// Appears: `OP only, if thread is currently closed`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> If the thread is closed to replies</font>
        pub closed: Option<bool>,
        
        /// Appears: `if post had attachment and attachment is deleted`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> If the file was deleted from the post</font>
        pub filedeleted: Option<bool>,
        
        /// Appears: `if post has attachment and attachment is spoilered`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> If the image was spoilered or not</font>
        pub spoiler: Option<bool>,
        
        /// Appears: `any post that has a mobile-optimized image`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> Mobile optimized image exists for post</font>
        pub m_img: Option<bool>,
        
        /// Appears: `OP only, only if bump limit has been reached`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> If a thread has reached bumplimit, it will no longer bump</font>
        pub bumplimit: Option<bool>,
        
        /// Appears: `OP only, only if image limit has been reached`  
        /// Possible values: `1` or not set  
        /// <font style="color:#789922;">> If an image has reached image limit, no more image replies can be made</font>
        pub imagelimit: Option<bool>,
    
        /// Appears: `always` <sup><b>Note:</b> Can be empty if user has a tripcode and opted out of a name</sup>  
        /// Possible values: `any string`  
        /// <font style="color:#789922;">> Name user posted with. Defaults to `Anonymous`</font>
        pub name: Option<String>,
    
        /// Appears: `OP only, if subject was included`  
        /// Possible values: `any string`  
        /// <font style="color:#789922;">> OP Subject text</font>
        pub sub: Option<String>,
    
        /// Appears: `if comment was included`  
        /// Possible values: `any HTML escaped string`  
        /// <font style="color:#789922;">> Comment (HTML escaped)</font>
        pub com: Option<String>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `any string`  
        /// <font style="color:#789922;">> Filename as it appeared on the poster's device</font>
        pub filename: Option<String>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values: `.jpg`, `.png`, `.gif`, `.pdf`, `.swf`, `.webm`  
        /// <font style="color:#789922;">> Filetype</font>
        pub ext: Option<String>,
    
        /// Appears: `if post has tripcode`  
        /// Possible values: `any string`  
        /// <font style="color:#789922;">> The user's tripcode, in format: `!tripcode` or `!!securetripcode`</font>
        pub trip: Option<String>,
    
        /// Appears: `if post has ID`  
        /// Possible values: `any 8 characters`  
        /// <font style="color:#789922;">> The poster's ID</font>
        pub id: Option<String>,
    
        /// Appears: `if post has capcode`  
        /// Possible values: Not set, `mod`, `admin`, `admin_highlight`, `manager`, `developer`, `founder`  
        /// <font style="color:#789922;">> The capcode identifier for a post</font>
        pub capcode: Option<String>,
    
        /// Appears: `if country flags are enabled`  
        /// Possible values: `2 character string` or `XX` if unknown or `troll_country` if present 
        /// <font style="color:#789922;">> Poster's [ISO 3166-1 alpha-2 country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)</font>
        pub country: Option<String>,
    
        /// Appears: `Name of any country`  
        /// Possible values: `any string`  
        /// <font style="color:#789922;">> Poster's country name</font>
        pub country_name: Option<String>,
    
        /// Appears: `OP only`  
        /// Possible values: `string`  
        /// <font style="color:#789922;">> SEO URL slug for thread</font>
        pub semantic_url: Option<String>,
    
        /// Appears: `OP only`, `/f/ only`  
        /// Possible values: `Game`, `Loop`, etc..  
        /// <font style="color:#789922;">> The category of `.swf` upload</font>
        pub tag: Option<String>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values:   
        /// <font style="color:#789922;">> 24 character (base64), decoded binary MD5 hash of file</font>
        pub md5: Option<Vec<u8>>,
    
        /// Appears: `always if post has attachment`  
        /// Possible values:   
        /// <font style="color:#789922;">> 65 character (hex), binary SHA256 hash of file</font>
        pub sha256: Option<Vec<u8>>,
    
        /// Appears: `always if post has thumbnail attachment`, excludes `/f/`  
        /// Possible values:  
        /// <font style="color:#789922;">> 65 character (hex), binary SHA256 hash of thumbnail</font>
        pub sha256t: Option<Vec<u8>>,
        
        /// Appears: ` `  
        /// Possible values:  
        /// <font style="color:#789922;">> Optional field for extra column additions</font>
        pub extra: Option<serde_json::Value>
    }

    impl Default for Post {
        fn default() -> Self {
            let t = std::time::SystemTime::now()
                .duration_since(std::time::SystemTime::UNIX_EPOCH)
                .unwrap();
            Self {
                no:             t.as_secs() as i64,
                subnum:         None,
                tim:            None,
                resto:          t.as_secs() as i64,
                time:           t.as_secs() as i64,
                last_modified:  t.as_secs() as i64,
                archived_on:    None,
                deleted_on:     None,
                fsize:          None,
                w:              None,
                h:              None,
                replies:        None,
                images:         None,
                unique_ips:     None,
                tn_w:           None,
                tn_h:           None,
                custom_spoiler: None,
                since4pass:     None,
                sticky:         None,
                closed:         None,
                filedeleted:    None,
                spoiler:        None,
                m_img:          None,
                bumplimit:      None,
                imagelimit:     None,
                name:           None,
                sub:            None,
                com:            None,
                filename:       None,
                ext:            None,
                trip:           None,
                id:             None,
                capcode:        None,
                country:        None,
                country_name:   None,
                semantic_url:   None,
                tag:            None,
                md5:            None,
                sha256:         None,
                sha256t:        None,
                extra:          None
            }
        }
    }
}

pub mod asagi {
    //! Implementation of the Asagi schema.  
    //!
    //! - [`Schema`] The schema and list of SQL queries that `asagi` uses

    /// Nonexistent :)
    pub struct Schema;
}
#[async_trait]
impl Archiver
    for YotsubaArchiver<Statement, tokio_postgres::Row, tokio_postgres::Client, reqwest::Client>
{
    async fn run_inner(&self) -> Result<()> {
        Ok(self.run().await?)
    }
}

impl QueryRaw for Client {
    fn inquiry(&self, statement: YotsubaStatement, id: QueryIdentifier) -> String {
        let endpoint = id.endpoint;
        match statement {
            YotsubaStatement::InitSchema => format!(
                r#"
            CREATE SCHEMA IF NOT EXISTS "{schema}";
            SET search_path TO "{schema}";
            CREATE TABLE IF NOT EXISTS index_counters (
                          id character varying(50) NOT NULL,
                          val integer NOT NULL,
                          PRIMARY KEY (id));
            "#,
                schema = id.schema.unwrap()
            ),
            YotsubaStatement::InitMetadata => r#"
    CREATE TABLE IF NOT EXISTS metadata (
        board text NOT NULL,
        threads jsonb,
        archive jsonb,
        PRIMARY KEY (board),
        CONSTRAINT board_unique UNIQUE (board));
  
    CREATE INDEX IF NOT EXISTS metadata_board_idx on metadata(board);
    "#
            .into(),
            YotsubaStatement::InitBoard => {
                format!(
                    r#"
            CREATE TABLE IF NOT EXISTS "{board}" (
              no bigint NOT NULL,
              subnum bigint,
              tim bigint,
              resto bigint NOT NULL DEFAULT 0,
              time bigint NOT NULL DEFAULT 0,
              last_modified bigint,
              archived_on bigint,
              deleted_on bigint,
              fsize int,
              w int,
              h int,
              replies int,
              images int,
              unique_ips int,
              tn_w smallint,
              tn_h smallint,
              custom_spoiler smallint,
              since4pass smallint,
              sticky boolean,
              closed boolean,
              filedeleted boolean,
              spoiler boolean,
              m_img boolean,
              bumplimit boolean,
              imagelimit boolean,
              name text,
              sub text,
              com text,
              filename text,
              ext text,
              trip text,
              id text,
              capcode text,
              country text,
              country_name text,
              semantic_url text,
              tag text,
              md5 bytea,
              sha256 bytea,
              sha256t bytea,
              extra jsonb);
            
            CREATE UNIQUE INDEX IF NOT EXISTS "unq_idx_no_subnum_{board}" on "{board}"(no, coalesce(subnum,0));
            
            {timescale_extra}
            
            CREATE INDEX IF NOT EXISTS "idx_{board}_no_resto" on "{board}"(no, resto);
            
            -- Needs to be superuser
            CREATE EXTENSION IF NOT EXISTS pg_trgm;
            
            CREATE INDEX IF NOT EXISTS "trgm_idx_{board}_com" ON "{board}" USING gin (com gin_trgm_ops);
            -- SET enable_seqscan TO OFF;"#,
                    board = id.board,
                    timescale_extra = if matches!(id.engine, Database::TimescaleDB) {
                        format!(
                            r#"CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
                            select public.create_hypertable('{board}', 'no', if_not_exists => true, chunk_time_interval => 3900000);
                            "#,
                            board = id.board
                        )
                    } else {
                        "".into()
                    },
                )
                // 1day => sec: 86400
                // 1month => sec: 2592000
            }
            YotsubaStatement::Metadata => {
                // ?? This endpoint will always be threads because archived threads are handled
                // locally because it's just an array
                if matches!(id.endpoint, YotsubaEndpoint::Threads) {
                    format!(
                        r#"
                    SELECT (CASE WHEN (
                        SELECT jsonb_agg(newv->'no') FROM
                            (SELECT jsonb_array_elements(jsonb_array_elements("{endpoint}")->'threads') as newv FROM "metadata" WHERE board = $1) z
                        WHERE newv->'no' IS NOT NULL
                        ) IS NOT NULL AND "{endpoint}" IS NOT NULL
                        THEN TRUE ELSE FALSE END) as "check"
                    FROM "metadata" WHERE board = $1;
                    "#,
                        endpoint = id.endpoint
                    )
                } else {
                    format!(
                        r#"
                 SELECT (CASE WHEN (
                    SELECT jsonb_agg(newv) FROM
                        (SELECT jsonb_array_elements("{endpoint}") as newv FROM "metadata" WHERE board = $1) z
                    WHERE newv IS NOT NULL
                    ) IS NOT NULL AND "{endpoint}" IS NOT NULL
                    THEN TRUE ELSE FALSE END) as "check"
                FROM "metadata" WHERE board = $1;
                 "#,
                        endpoint = id.endpoint
                    )
                }
            }
            YotsubaStatement::UpdateMetadata => format!(
                r#"
            INSERT INTO metadata(board, {endpoint})
              VALUES ($1, $2::jsonb)
              ON CONFLICT (board)
              DO UPDATE
                SET {endpoint} = $2::jsonb;
            "#,
                endpoint = id.endpoint
            ),
            YotsubaStatement::Threads => {
                // This endpoint will always be threads because archived threads
                // are already in array format, we can just return them
                "SELECT jsonb_agg(newv->'no')
                FROM
                (SELECT jsonb_array_elements(jsonb_array_elements($1::jsonb)->'threads') as newv)z
                WHERE newv->'no' is not null;"
                    .into()
            }

            // Combine new and prev threads.json into one. This retains the prev
            // threads (which the new json doesn't contain, meaning they're either
            // pruned or archived).  That's especially useful for boards without
            // archives. Use the WHERE clause to select only modified threads. Now
            // we basically have a list of deleted and modified threads.
            // Return back this list to be processed.
            // Use the new threads.json as the base now.
            // Additionally the fetched thread is checksummed against the thread in db
            // if any changes have been made.
            YotsubaStatement::ThreadsModified => format!(
                r#"
        SELECT (
        CASE WHEN new_hash IS DISTINCT FROM prev_hash THEN
        (
          SELECT jsonb_agg({}) from
          (select jsonb_array_elements({}) as prev from metadata where board = $1)x
          full JOIN
          (select jsonb_array_elements({}) as newv)z
          ON {}
          where newv is null or prev is null {}
        ) END
        ) FROM 
        (SELECT sha256(decode({} #>> '{{}}', 'escape')) as prev_hash from metadata where board=$1) w
        FULL JOIN
        (SELECT sha256(decode($2::jsonb #>> '{{}}', 'escape')) as new_hash) q
        ON TRUE;
        "#,
                if endpoint == YotsubaEndpoint::Threads {
                    r#"COALESCE(newv->'no',prev->'no')"#
                } else {
                    "coalesce(newv,prev)"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"jsonb_array_elements(threads)->'threads'"#
                } else {
                    "archive"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"jsonb_array_elements($2::jsonb)->'threads'"#
                } else {
                    "$2::jsonb"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"prev->'no' = (newv -> 'no')"#
                } else {
                    "prev = newv"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"or not prev->'last_modified' <@ (newv -> 'last_modified')"#
                } else {
                    ""
                },
                if endpoint == YotsubaEndpoint::Threads { "threads" } else { "archive" }
            ),

            // This query is only run ONCE at every startup
            // Running a JOIN to compare against the entire DB on EVERY INSERT/UPDATE
            // would not be that great. That is not done here.
            // This gets all the threads from cache, compares it to the new json to get
            // new + modified threads Then compares that result to the database
            // where a thread is deleted or archived, and takes only the threads
            // where's it's not deleted or archived.
            // This might be unwanted as the size of the database grows.
            YotsubaStatement::ThreadsCombined => format!(
                r#"
        select jsonb_agg(c) from (
          SELECT coalesce {1} as c from
            (select jsonb_array_elements({2}) as prev from metadata where board = $1)x
          full JOIN
            (select jsonb_array_elements({3}) as newv)z
          ON {4}
        )q
        left join
          (select no as nno from "{0}" where resto=0 and (archived_on is not null or deleted_on is not null))w
        ON c = nno
        where nno is null;
        "#,
                id.board,
                if endpoint == YotsubaEndpoint::Threads {
                    r#"(prev->'no', newv->'no')::bigint"#
                } else {
                    "(newv, prev)::bigint"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"jsonb_array_elements(threads)->'threads'"#
                } else {
                    "archive"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"jsonb_array_elements($2::jsonb)->'threads'"#
                } else {
                    "$2::jsonb"
                },
                if endpoint == YotsubaEndpoint::Threads {
                    r#"prev->'no' = (newv -> 'no')"#
                } else {
                    "prev = newv"
                }
            ),
            YotsubaStatement::UpdateThread => format!(
                r#"
            INSERT INTO "{board}" (
              no,sticky,closed,name,sub,com,filedeleted,spoiler,
              custom_spoiler,filename,ext,w,h,tn_w,tn_h,tim,time,md5,
              fsize, m_img,resto,trip,id,capcode,country,country_name,bumplimit,
              archived_on,imagelimit,semantic_url,replies,images,unique_ips,tag,since4pass,last_modified)
              SELECT
              no,sticky::int::bool,closed::int::bool,name,sub,com,filedeleted::int::bool,spoiler::int::bool,
              custom_spoiler,filename,ext,w,h,tn_w,tn_h,tim,time, (CASE WHEN length(q.md5)>20 and q.md5 IS NOT NULL THEN decode(REPLACE (q.md5, E'\\', '')::text, 'base64'::text) ELSE null::bytea END) AS md5,
              fsize, m_img::int::bool, resto,trip,q.id,capcode,COALESCE(country, troll_country) as country,country_name,bumplimit::int::bool,
              archived_on,imagelimit::int::bool,semantic_url,replies,images,unique_ips,
              tag,since4pass, extract(epoch from now())::bigint as last_modified
              FROM jsonb_populate_recordset(null::"schema_4chan", $1::jsonb->'posts') q
              WHERE q.no IS NOT NULL
            ON CONFLICT (no, coalesce(subnum,0)) 
            DO
              UPDATE SET 
              no = excluded.no,
              sticky = excluded.sticky::int::bool,
              closed = excluded.closed::int::bool,
              name = excluded.name,
              sub = excluded.sub,
              com = excluded.com,
              filedeleted = excluded.filedeleted::int::bool,
              spoiler = excluded.spoiler::int::bool,
              custom_spoiler = excluded.custom_spoiler,
              filename = excluded.filename,
              ext = excluded.ext,
              w = excluded.w,
              h = excluded.h,
              tn_w = excluded.tn_w,
              tn_h = excluded.tn_h,
              tim = excluded.tim,
              time = excluded.time,
              md5 = excluded.md5,
              fsize = excluded.fsize,
              m_img = excluded.m_img::int::bool,
              resto = excluded.resto,
              trip = excluded.trip,
              id = excluded.id,
              capcode = excluded.capcode,
              country = excluded.country,
              country_name = excluded.country_name,
              bumplimit = excluded.bumplimit::int::bool,
              archived_on = excluded.archived_on,
              imagelimit = excluded.imagelimit::int::bool,
              semantic_url = excluded.semantic_url,
              replies = excluded.replies,
              images = excluded.images,
              unique_ips = CASE WHEN excluded.unique_ips is not null THEN excluded.unique_ips ELSE "{board}".unique_ips END,
              tag = excluded.tag,
              since4pass = excluded.since4pass,
              last_modified = extract(epoch from now())::bigint
              WHERE excluded.no IS NOT NULL AND EXISTS (
              SELECT 
                "{board}".no,
                "{board}".sticky,
                "{board}".closed,
                "{board}".name,
                "{board}".sub,
                "{board}".com,
                "{board}".filedeleted,
                "{board}".spoiler,
                "{board}".custom_spoiler,
                "{board}".filename,
                "{board}".ext,
                "{board}".w,
                "{board}".h,
                "{board}".tn_w,
                "{board}".tn_h,
                "{board}".tim,
                "{board}".time,
                "{board}".md5,
                "{board}".fsize,
                "{board}".m_img,
                "{board}".resto,
                "{board}".trip,
                "{board}".id,
                "{board}".capcode,
                "{board}".country,
                "{board}".country_name,
                "{board}".bumplimit,
                "{board}".archived_on,
                "{board}".imagelimit,
                "{board}".semantic_url,
                "{board}".replies,
                "{board}".images,
                --"{board}".unique_ips,
                "{board}".tag,
                "{board}".since4pass
                WHERE "{board}".no IS NOT NULL
              EXCEPT
              SELECT 
                excluded.no,
                excluded.sticky,
                excluded.closed,
                excluded.name,
                excluded.sub,
                excluded.com,
                excluded.filedeleted,
                excluded.spoiler,
                excluded.custom_spoiler,
                excluded.filename,
                excluded.ext,
                excluded.w,
                excluded.h,
                excluded.tn_w,
                excluded.tn_h,
                excluded.tim,
                excluded.time,
                excluded.md5,
                excluded.fsize,
                excluded.m_img,
                excluded.resto,
                excluded.trip,
                excluded.id,
                excluded.capcode,
                excluded.country,
                excluded.country_name,
                excluded.bumplimit,
                excluded.archived_on,
                excluded.imagelimit,
                excluded.semantic_url,
                excluded.replies,
                excluded.images,
                --excluded.unique_ips,
                excluded.tag,
                excluded.since4pass
              WHERE excluded.no IS NOT NULL AND excluded.no = "{board}".no )"#,
                board = id.board
            ),
            YotsubaStatement::Delete => format!(
                r#"
          UPDATE "{board}"
          SET deleted_on    = extract(epoch from now())::bigint,
            last_modified = extract(epoch from now())::bigint
          WHERE
          no = $1 AND deleted_on is NULL;
          "#,
                board = id.board
            ),
            YotsubaStatement::UpdateDeleteds => format!(
                r#"
            INSERT INTO "{board}" (no, time, resto)
            SELECT x.* FROM
              (
                SELECT no, time, resto FROM "{board}" where (no=$2 or resto=$2) and no >= (
                    (SELECT min(no) FROM jsonb_populate_recordset(null::"schema_4chan", $1::jsonb->'posts'))
                ) order by no
              ) x
              --(SELECT * FROM "{board}" where no=$2 or resto=$2 order by no) x
            FULL JOIN
              (SELECT no, time, resto FROM jsonb_populate_recordset(null::"schema_4chan", $1::jsonb->'posts')) z
              --(SELECT * FROM jsonb_populate_recordset(null::"schema_4chan", $1::jsonb->'posts')) z
            ON x.no = z.no
            WHERE z.no is null
            ON CONFLICT (no, coalesce(subnum,0)) 
            DO
            UPDATE
            SET deleted_on = extract(epoch from now())::bigint,
              last_modified = extract(epoch from now())::bigint
            WHERE
            "{board}".deleted_on is NULL; "#,
                board = id.board
            ),
            YotsubaStatement::InitType => r#"
        DO $$
        BEGIN
          IF NOT EXISTS (SELECT typname FROM pg_type WHERE typname = 'schema_4chan') THEN
            CREATE TYPE "schema_4chan" AS (
              "no" bigint,
              sticky smallint,
              closed smallint,
              "now" text,
              "name" text,
              sub text,
              com text,
              filedeleted smallint,
              spoiler smallint,
              custom_spoiler smallint,
              filename text,
              ext text,
              w int,
              h int,
              tn_w smallint,
              tn_h smallint,
              tim bigint,
              "time" bigint,
              "md5" text,
              fsize int,
              m_img smallint,
              resto int,
              trip text,
              id text,
              capcode text,
              country text,
              troll_country text,
              country_name text,
              archived smallint,
              bumplimit smallint,
              archived_on bigint,
              imagelimit smallint,
              semantic_url text,
              replies int,
              images int,
              unique_ips int,
              tag text,
              since4pass smallint
            );
          END IF;
        END
        $$;"#
                .into(),
            YotsubaStatement::InitViews => {
                let board = id.board;
                let safe_create_view = |n, stmt| {
                    format!(
                        r#"
        DO $$
        BEGIN
        CREATE VIEW "{}{}" AS
          {}
        EXCEPTION
        WHEN SQLSTATE '42P07' THEN
          NULL;
        END;
        $$;
        "#,
                        board, n, stmt
                    )
                };

                let main_view = |is_main| {
                    safe_create_view(
                        if is_main { "_asagi" } else { "_deleted" },
                        format!(
                            r#"
            SELECT
            no AS doc_id,
            (CASE WHEN md5 IS NOT NULL THEN no ELSE NULL END) AS media_id,
            0::smallint AS poster_ip, -- Unused in Asagi. Used in FF.
            no AS num,
            subnum, -- Unused in Asagi. Used in FF for ghost posts.
            (CASE WHEN resto=0 THEN no ELSE resto END) AS thread_num,
            (CASE WHEN resto=0 THEN true ELSE false END) AS op,
            "time" AS "timestamp",
            (CASE WHEN deleted_on IS NULL THEN 0 ELSE deleted_on END) AS "timestamp_expired",
            (CASE WHEN tim IS NOT NULL THEN (tim::text || 's.jpg') ELSE NULL END) AS preview_orig,
            (CASE WHEN tn_w IS NULL THEN 0 ELSE tn_w END) AS preview_w,
            (CASE WHEN tn_h IS NULL THEN 0 ELSE tn_h END) AS preview_h,
            (CASE WHEN filename IS NOT NULL THEN (filename::text || ext) ELSE NULL END) AS media_filename,
            (CASE WHEN w IS NULL THEN 0 ELSE w END) AS media_w,
            (CASE WHEN h IS NULL THEN 0 ELSE h END) AS media_h,
            (CASE WHEN fsize IS NULL THEN 0 ELSE fsize END) AS media_size,
            encode(md5, 'base64') AS media_hash,
            (CASE WHEN tim IS NOT NULL and ext IS NOT NULL THEN (tim::text || ext) ELSE NULL END) AS media_orig,
            (CASE WHEN spoiler IS NULL THEN false ELSE spoiler END) AS spoiler,
            (CASE WHEN deleted_on IS NULL THEN false ELSE true END) AS deleted,
            (CASE WHEN capcode='manager' OR capcode='Manager' THEN 'G' ELSE coalesce (upper(left(capcode, 1)),'N') end) AS capcode,
            NULL AS email, -- Used by Asagi but no longer in the API. Used by FF.
            name,
            trip,
            sub AS title,
            (select r29 from 
              regexp_replace (
              com, E'&#039;', E'\'', 'g') r0
              , regexp_replace(r0, E'&gt;', '>', 'g') r1
              , regexp_replace(r1, E'&lt;', '<', 'g') r2
              , regexp_replace(r2, E'&quot;', E'\"', 'g') r3
              , regexp_replace(r3, E'&amp;', E'&', 'g') r4
              , regexp_replace(r4, E'\\s*$', '', 'g') r5
              , regexp_replace(r5, E'^\\s*$', '', 'g') r6
              , regexp_replace(r6, E'<span class=\"capcodeReplies\"><span style=\"font-size: smaller;\"><span style=\"font-weight: bold;\">(?:Administrator|Moderator|Developer) Repl(?:y|ies):</span>.*?</span><br></span>', '', 'g') r7
              , regexp_replace(r7, E'\\[(/?(banned|moot|spoiler|code))]', '[\1:lit]', 'g') r8
              , regexp_replace(r8, E'<span class=\"abbr\">.*?</span>', '', 'g') r9
              , regexp_replace(r9, E'<table class=\"exif\"[^>]*>.*?</table>', '', 'g') r10
              , regexp_replace(r10, E'<br><br><small><b>Oekaki Post</b>.*?</small>', '', 'g') r11
              , regexp_replace(r11, E'<(?:b|strong) style=\"color:\\s*red;\">(.*?)</(?:b|strong)>', '[banned]\1[/banned]', 'g') r12
              , regexp_replace(r12, E'<div style=\"padding: 5px;margin-left: \\.5em;border-color: #faa;border: 2px dashed rgba\\(255,0,0,\\.1\\);border-radius: 2px\">(.*?)</div>', '[moot]\1[/moot]', 'g') r13
              , regexp_replace(r13, E'<span class=\"fortune\" style=\"color:(.*?)\"><br><br><b>(.*?)</b></span>', '\n\n[fortune color=\"\1\"]$2[/fortune]', 'g') r14
              , regexp_replace(r14, E'<(?:b|strong)>(.*?)</(?:b|strong)>', '[b]\1[/b]', 'g') r15
              , regexp_replace(r15, E'<pre[^>]*>', '[code]', 'g') r16
              , replace(r16, '</pre>', '[/code]') r17
              , regexp_replace(r17, E'<span class=\"math\">(.*?)</span>', '[math]\1[/math]', 'g') r18
              , regexp_replace(r18, E'<div class=\"math\">(.*?)</div>', '[eqn]\1[/eqn]', 'g') r19
              , regexp_replace(r19, E'<font class=\"unkfunc\">(.*?)</font>', '\1', 'g') r20
              , regexp_replace(r20, E'<span class=\"quote\">(.*?)</span>', '\1', 'g') r21
              , regexp_replace(r21, E'<span class=\"(?:[^\"]*)?deadlink\">(.*?)</span>', '\1', 'g') r22 
              , regexp_replace(r22, E'<a.*?>(.*?)</a>', '\1', 'g') r23 -- Changed for postgres regex
              , regexp_replace(r23, E'<span class=\"spoiler\"[^>]*>(.*?)</span>', '[spoiler]\1[/spoiler]', 'g') r24
              , regexp_replace(r24, E'<span class=\"sjis\">(.*?)</span>', '[shiftjis]\1[/shiftjis]', 'g') r25 
              , regexp_replace(r25, E'<s>', '[spoiler]', 'g') r26
              , regexp_replace(r26, E'</s>', '[/spoiler]', 'g') r27
              , regexp_replace(r27, E'<br\\s*/?>', E'\n', 'g') r28
              , regexp_replace(r28, E'<wbr>', '', 'g') r29
              ) AS comment,
            NULL AS delpass, -- Unused in Asagi. Used in FF.
            (CASE WHEN sticky IS NULL or sticky=false THEN false ELSE sticky END) AS sticky,
            (CASE WHEN (closed IS NOT NULL or closed=true) AND (archived_on is null) THEN closed ELSE FALSE END) AS locked,
            (CASE WHEN id='Developer' THEN 'Dev' ELSE id END) AS poster_hash, --not the same AS media_hash
            country AS poster_country,
            country_name AS poster_country_name,
            NULLIF(
                jsonb_strip_nulls(jsonb_build_object('uniqueIps', unique_ips::text, 'since4pass', since4pass::text, 'trollCountry', 
                    case when 
                    country = ANY ('{{AC,AN,BL,CF,CM,CT,DM,EU,FC,GN,GY,JH,KN,MF,NB,NZ,PC,PR,RE,TM,TR,UN,WP}}'::text[])
                    then
                    country
                    else
                    null
                    end
                ))::text, '{{}}') as exif, -- JSON in text format of uniqueIps, since4pass, and trollCountry. Has some deprecated fields but still used by Asagi and FF.
            (CASE WHEN archived_on IS NULL THEN false ELSE true END) AS archived,
            archived_on
            FROM "{board}"
            {extra};
            "#,
                            board = board,
                            extra = if is_main { "" } else { "WHERE deleted_on is not null" }
                        )
                    )
                };

                let board_threads = safe_create_view(
                    "_threads",
                    format!(
                        r#"
          SELECT
          no as thread_num,
          "time" as time_op,
          last_modified as time_last,
          last_modified as time_bump,
          (CASE WHEN subnum is not null then "time" else NULL END ) as time_ghost,
          (CASE WHEN subnum is not null then last_modified else NULL END )  as time_ghost_bump,
          last_modified as time_last_modified,
          (SELECT COUNT(no) FROM "{board}" re WHERE t.no = resto or t.no = no) as nreplies,
          (SELECT COUNT(md5) FROM "{board}" re WHERE t.no = resto or t.no = no) as nimages,
          (CASE WHEN sticky IS NULL or sticky=false THEN false ELSE sticky END) AS sticky,
          (CASE WHEN (closed IS NOT NULL or closed=true) AND (archived_on is null) THEN closed ELSE FALSE END) AS locked
          from "{board}" t where resto=0;
        "#,
                        board = board
                    )
                );

                let board_users = safe_create_view(
                    "_users",
                    format!(
                        r#"
        SELECT
          ROW_NUMBER() OVER (ORDER by min(t.time)) AS user_id,
          t.n AS name,
          t.tr AS trip,
          MIN(t.time) AS firstseen, COUNT(*) AS postcount
          FROM (SELECT *, COALESCE(name,'') AS n, COALESCE(trip,'') AS tr
        FROM "{board}") t GROUP BY t.n,t.tr;
        "#,
                        board = board
                    )
                );

                let board_images = safe_create_view(
                    "_images",
                    format!(
                        r#"
          SELECT ROW_NUMBER() OVER(ORDER by zz.media) AS media_id, * FROM (
            SELECT
              ENCODE(md5, 'base64') AS media_hash,
              MAX(tim)::text || max(ext) as media,
              (CASE WHEN MAX(resto) = 0 THEN MAX(tim)::text || 's.jpg' ELSE NULL END) AS preview_op,
              (CASE WHEN MAX(resto) != 0 THEN MAX(tim)::text || 's.jpg' ELSE NULL END) AS preview_reply,
              COUNT(md5)::int AS total,
              0::smallint AS banned, --unused in asagi, used in FF
              encode(sha256, 'hex')::text || max(ext) as media_sha256,
              (CASE WHEN MAX(resto) = 0 THEN encode(sha256, 'hex')::text || 's.jpg' ELSE NULL END) AS preview_op_sha256,
              (CASE WHEN MAX(resto) != 0 THEN encode(sha256t, 'hex')::text || 's.jpg' ELSE NULL END) AS preview_reply_sha256
            FROM "{board}" WHERE md5 IS NOT NULL GROUP BY md5, sha256, sha256t)zz;
        "#,
                        board = board
                    )
                );

                let board_daily = safe_create_view(
                    "_daily",
                    format!(
                        r#"
        SELECT
          MIN(t.no) AS firstpost,
          t.day AS day,
          COUNT(*) AS posts, COUNT(md5) AS images, COUNT(CASE WHEN name ~* '.*sage.*' THEN name ELSE  NULL END) AS sage,
          COUNT(CASE WHEN name = 'Anonymous' AND trip IS NULL THEN name ELSE NULL END) AS anons, COUNT(trip) AS trips,
          COUNT(CASE WHEN COALESCE(name <> 'Anonymous' AND trip IS NULL, TRUE) THEN name ELSE NULL END) AS names
        FROM (SELECT *, (FLOOR(time/86400)*86400)::bigint AS day FROM "{board}")t GROUP BY t.day ORDER BY day;
        "#,
                        board = board
                    )
                );

                format!(
                    r#"
        {1}
      
        {2}
      
        {3}
      
        {4}
      
        {5}
      
        {6}
      
        CREATE INDEX IF NOT EXISTS "idx_{0}_time" on "{0}"(((floor((("time" / 86400))::double precision) * '86400'::double precision)::bigint));
      
        "#,
                    board,
                    main_view(true),
                    main_view(false),
                    board_threads,
                    board_users,
                    board_images,
                    board_daily
                )
            }
            YotsubaStatement::Medias => format!(
                r#"
        SELECT * FROM "{board}"
        WHERE (md5 is not null) {media_mode} AND filedeleted IS NULL AND (no=$1 or resto=$1)
        ORDER BY no desc;"#,
                board = id.board,
                media_mode = match id.media_mode {
                    YotsubaStatement::UpdateHashThumbs => "AND (sha256t IS NULL)",
                    YotsubaStatement::UpdateHashMedia => "AND (sha256 IS NULL)",
                    _ => "AND (sha256 IS NULL OR sha256t IS NULL)"
                }
            ),
            YotsubaStatement::UpdateHashMedia | YotsubaStatement::UpdateHashThumbs => format!(
                r#"
        UPDATE "{0}"
        SET last_modified = extract(epoch from now())::bigint,
              "{1}" = $2
        WHERE
        no = $1 AND "{1}" IS NULL;
        "#,
                id.board,
                if statement == YotsubaStatement::UpdateHashThumbs {
                    format!("{}t", id.hash_type)
                } else {
                    format!("{}", id.hash_type)
                }
            )
        }
    }
}

/// For the rest of [`YotsubaStatement`]
#[async_trait]
impl Query<Statement, Row> for Client {
    async fn first(
        &self, statement: YotsubaStatement, id: &QueryIdentifier,
        statements: &StatementStore<Statement>, item: Option<&[u8]>, no: Option<u64>
    ) -> Result<u64>
    {
        log::debug!("|Query| Running: {} /{}/", statement, id.board);
        let id = QueryIdentifier { media_mode: statement, ..id.clone() };
        let board = id.board;
        let received_statement = statements
            .get(&id)
            .ok_or_else(|| anyhow!("|Query::{}| Empty statement from id: {:?}", statement, id));
        let item = item.ok_or_else(|| anyhow!("|Query::{}| Empty `json` item received", statement));
        let no = no.ok_or_else(|| anyhow!("|Query::{}| Empty `no` received", statement));
        match statement {
            YotsubaStatement::InitSchema
            | YotsubaStatement::InitType
            | YotsubaStatement::InitMetadata
            | YotsubaStatement::InitBoard
            | YotsubaStatement::InitViews => {
                self.batch_execute(&self.inquiry(statement, id)).await?;
                Ok(1)
            }
            YotsubaStatement::UpdateMetadata => Ok(self
                .execute(received_statement?, &[
                    &board.to_string(),
                    &serde_json::from_slice::<serde_json::Value>(item?)?
                ])
                .await?),
            YotsubaStatement::UpdateThread => Ok(self
                .execute(received_statement?, &[&serde_json::from_slice::<serde_json::Value>(
                    item?
                )?])
                .await?),
            YotsubaStatement::Delete =>
                Ok(self.execute(received_statement?, &[&i64::try_from(no?)?]).await?),
            YotsubaStatement::UpdateDeleteds => Ok(self
                .execute(received_statement?, &[
                    &serde_json::from_slice::<serde_json::Value>(item?)?,
                    &i64::try_from(no?)?
                ])
                .await?),
            YotsubaStatement::UpdateHashMedia | YotsubaStatement::UpdateHashThumbs => {
                self.execute(received_statement?, &[&(no? as i64), &item?]).await?;
                Ok(1)
            }
            // YotsubaStatement::Medias => {},
            // YotsubaStatement::Threads => {},
            // YotsubaStatement::ThreadsModified => {},
            // YotsubaStatement::ThreadsCombined => {},
            YotsubaStatement::Metadata => Ok(self
                .query(received_statement?, &[&board.to_string()])
                .await
                .ok()
                .filter(|re| !re.is_empty())
                .map(|re| re[0].try_get(0))
                .ok_or_else(|| anyhow!("|Query::{}| Empty entry in metadata", statement))?
                .map(|b: bool| if b { 1u64 } else { 0u64 })?),
            _ => Err(anyhow!("|Query| Unknown statement: {}", statement))
        }
    }

    async fn get_list(
        &self, statement: YotsubaStatement, id: &QueryIdentifier,
        statements: &StatementStore<Statement>, item: Option<&[u8]>, _no: Option<u64>
    ) -> Result<Queue>
    {
        log::debug!("|Query| Running: {} /{}/", statement, id.board);
        // This `get_list` method could be run with any one of the following:
        if !matches!(
            statement,
            YotsubaStatement::Threads
                | YotsubaStatement::ThreadsModified
                | YotsubaStatement::ThreadsCombined
        ) {
            return Err(anyhow!(
                "|Query::{}| Unknown statement: {}",
                YotsubaStatement::Threads,
                statement
            ));
        }
        // Inside `create_statements` we use `media_mode` as the `statement`
        // Se we have to put in the right `statement` to get the right id

        let id = QueryIdentifier { media_mode: statement, ..id.clone() };
        // log::info!("{:?}", &id);
        let endpoint = id.endpoint;
        let board = id.board;

        let item = item.ok_or_else(|| anyhow!("|Query::{}| Empty `json` item received", statement));

        // Return the archive thread as a `Queue` because it's already an array
        if matches!(endpoint, YotsubaEndpoint::Archive)
            && matches!(statement, YotsubaStatement::Threads)
        {
            return Ok(serde_json::from_slice(item?)?);
        }

        let json = serde_json::from_slice::<serde_json::Value>(item?)?;
        // let u: Vec<Threads> = serde_json::from_slice(item.unwrap())?; // artifact from mysql
        let received_statement = statements
            .get(&id)
            .ok_or_else(|| anyhow!("|Query::{}| Empty statement from id: {:?}", statement, &id));
        let res: Queue = if matches!(statement, YotsubaStatement::Threads) {
            self.query_one(received_statement?, &[&json]).await
        } else {
            self.query_one(received_statement?, &[&board.to_string(), &json]).await
        }
        .map(|row| row.try_get(0))?
        .map(|r: Option<serde_json::Value>| r)?
        .map(|res| serde_json::from_value::<HashSet<Option<u64>>>(res))
        .ok_or_else(|| {
            anyhow!("|Query::{}| Empty or null in getting from `{}` endpoint", statement, endpoint)
        })?
        .map(|v| v.into_iter().filter(Option::is_some).map(Option::unwrap).collect())?;
        Ok(res)
    }

    async fn get_rows(
        &self, statement: YotsubaStatement, id: &QueryIdentifier,
        statements: &StatementStore<Statement>, _item: Option<&[u8]>, no: Option<u64>
    ) -> Result<Vec<Row>>
    {
        log::debug!("|Query| Running: {} /{}/", statement, id.board);
        if !matches!(statement, YotsubaStatement::Medias) {
            return Err(anyhow!(
                "|Query::{}| Unknown statement: {}",
                YotsubaStatement::Medias,
                statement
            ));
        }
        let id = QueryIdentifier { media_mode: statement, ..id.clone() };
        let no = no.ok_or_else(|| anyhow!("|Query::{}| Empty `no` received", statement));
        let received_statement = statements
            .get(&id)
            .ok_or_else(|| anyhow!("|Query::{}| Empty statement from id: {:?}", statement, id));
        Ok(self.query(received_statement?, &[&(no? as i64)]).await?)
    }

    async fn create_statements(
        &self, engine: Database, endpoint: YotsubaEndpoint, board: crate::enums::YotsubaBoard
    ) -> StatementStore<Statement> {
        let mut statement_store = std::collections::HashMap::new();
        let statements: Vec<_> = YotsubaStatement::into_enum_iter().collect();
        let hash_type = crate::enums::YotsubaHash::Sha256;
        if matches!(endpoint, YotsubaEndpoint::Media) {
            for statement in statements {
                match statement {
                    YotsubaStatement::Medias
                    | YotsubaStatement::UpdateHashMedia
                    | YotsubaStatement::UpdateHashThumbs => {
                        let id = QueryIdentifier::new(
                            engine, endpoint, board, None, None, hash_type, statement
                        );
                        statement_store.insert(
                            id.clone(),
                            self.prepare(&self.inquiry(statement, id)).await.unwrap()
                        );
                    }
                    _ => {}
                }
            }
            return statement_store;
        }
        for statement in statements {
            match statement {
                YotsubaStatement::Medias
                | YotsubaStatement::UpdateHashMedia
                | YotsubaStatement::UpdateHashThumbs
                | YotsubaStatement::InitSchema
                | YotsubaStatement::InitType
                | YotsubaStatement::InitMetadata
                | YotsubaStatement::InitBoard
                | YotsubaStatement::InitViews => {}
                _ => {
                    let id = QueryIdentifier::new(
                        engine,
                        endpoint,
                        board,
                        None,
                        None,
                        crate::enums::YotsubaHash::Sha256,
                        statement
                    );
                    statement_store.insert(
                        id.clone(),
                        self.prepare(&self.inquiry(statement, id)).await.unwrap()
                    );
                }
            }
        }

        statement_store
    }
}

#[cfg(test)]
mod test {

    use super::*;
    use crate::enums::{YotsubaBoard, YotsubaHash};
    use once_cell::sync::Lazy;
    #[allow(unused_imports)]
    #[cfg(test)]
    use pretty_assertions::{assert_eq, assert_ne};
    use serde_json::json;
    static BOARD: Lazy<YotsubaBoard> = Lazy::new(|| YotsubaBoard::a);
    const DB_URL: &str = "postgresql://postgres:zxc@localhost:5432/fdb2";
    const SCHEMA: &str = "asagi";
    const ENGINE: Database = Database::PostgreSQL;
    // const CHARSET: &str = "utf8";

    enum JsonType {
        Unknown,
        Valid,
        DeprecatedFields,
        AddedFields,
        MixedFields
    }

    macro_rules! send_thread_tests {
        ($($name:ident: $value:expr,)*) => {
            $(
                #[tokio::test]
                async fn $name() -> Result<()> {
                    let ( endpoint, board, mode, json_type) = $value;
                    test_send_single_thread(endpoint, board, mode, json_type).await?;
                    // assert_eq!(expected, fib(input));
                    Ok(())
                }
            )*
        }
    }
    macro_rules! get_threads_tests {
        ($($name:ident: $value:expr,)*) => {
            $(
                #[tokio::test]
                // #[should_panic(expected = "|threads| Empty or null in getting threads")] // for unknowns
                async fn $name() -> Result<()> {
                    let ( endpoint, board, mode, json_type) = $value;
                    test_get_threads(endpoint, board, mode, json_type).await?;
                    // assert_eq!(expected, fib(input));
                    Ok(())
                }
            )*
        }
    }

    macro_rules! get_threads_tests_panic {
        ($($name:ident: $value:expr,)*) => {
            $(
                // #[should_panic(expected = "|threads| Empty or null in getting threads")] // for unknowns
                #[tokio::test]
                #[should_panic]
                async fn $name() {
                    let (endpoint, board, mode, json_type) = $value;
                    test_get_threads(endpoint, board, mode, json_type).await.unwrap();
                    // assert_eq!(expected, fib(input));
                }
            )*
        }
    }
    #[cfg(test)]
    send_thread_tests! {
        // Send single thread
        send_single_thread_valid_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::UpdateThread, JsonType::Valid),
        send_single_thread_deprecated_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::UpdateThread, JsonType::DeprecatedFields),
        send_single_thread_added_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::UpdateThread, JsonType::AddedFields),
        send_single_thread_mixed_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::UpdateThread, JsonType::MixedFields),
    }

    #[cfg(test)]
    get_threads_tests! {
        // threads.json
        get_threads_send_valid_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::Threads, JsonType::Valid),
        get_threads_send_added_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::Threads, JsonType::AddedFields),
        get_threads_send_mixed_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::Threads, JsonType::MixedFields),

        get_threads_modified_send_valid_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsModified, JsonType::Valid),
        get_threads_modified_send_deprecated_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsModified, JsonType::DeprecatedFields),
        get_threads_modified_send_added_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsModified, JsonType::AddedFields),
        get_threads_modified_send_mixed_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsModified, JsonType::MixedFields),

        get_threads_combined_send_valid_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::Valid),
        get_threads_combined_send_deprecated_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::DeprecatedFields),
        get_threads_combined_send_added_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::AddedFields),
        get_threads_combined_send_mixed_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::MixedFields),

        get_archive_send_valid_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::Threads, JsonType::Valid),
        get_archive_modified_send_valid_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsModified, JsonType::Valid),
        get_archive_combined_send_valid_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::Valid),

        get_archive_send_deprecated_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::Threads, JsonType::DeprecatedFields),
        get_archive_modified_send_deprecated_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsModified, JsonType::DeprecatedFields),
        get_archive_combined_send_deprecated_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::DeprecatedFields),

    }

    #[cfg(test)]
    get_threads_tests_panic! {
        // threads.json
        get_threads_send_unknown_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::Threads, JsonType::Unknown),
        get_threads_send_deprecated_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::Threads, JsonType::DeprecatedFields),
        get_threads_modified_send_unknown_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsModified, JsonType::Unknown),
        get_threads_combined_send_unknown_json: (YotsubaEndpoint::Threads, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::Unknown),

        // archive.json
        get_archive_send_added_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::Threads, JsonType::AddedFields),
        get_archive_modified_send_added_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsModified, JsonType::AddedFields),
        get_archive_combined_send_added_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::AddedFields),

        get_archive_send_mixed_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::Threads, JsonType::MixedFields),
        get_archive_modified_send_mixed_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsModified, JsonType::MixedFields),
        get_archive_combined_send_mixed_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::MixedFields),

        get_archive_send_unknown_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::Threads, JsonType::Unknown),
        get_archive_modified_send_unknown_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsModified, JsonType::Unknown),
        get_archive_combined_send_unknown_json: (YotsubaEndpoint::Archive, *BOARD, YotsubaStatement::ThreadsCombined, JsonType::Unknown),
    }

    async fn test_get_threads(
        endpoint: YotsubaEndpoint, board: YotsubaBoard, mode: YotsubaStatement, json_type: JsonType
    ) -> Result<Queue> {
        let _json = match json_type {
            JsonType::Unknown =>
                if endpoint == YotsubaEndpoint::Threads {
                    json!({"test":1, "test2":2, "test3":3})
                } else {
                    json!(["1243234", "5645756", "75686786", "456454325", "test", "test1"])
                },
            JsonType::Valid =>
                if endpoint == YotsubaEndpoint::Threads {
                    json!(
                    [
                        {
                          "page": 1,
                          "threads": [
                            { "no": 196649146, "last_modified": 1576266882, "replies": 349 },
                            { "no": 196656555, "last_modified": 1576266881, "replies": 6 },
                            { "no": 196654076, "last_modified": 1576266880, "replies": 191 },
                            { "no": 196637792, "last_modified": 1576266880, "replies": 233 },
                            { "no": 196647457, "last_modified": 1576266880, "replies": 110 },
                            { "no": 196624742, "last_modified": 1576266873, "replies": 103 },
                            { "no": 196656097, "last_modified": 1576266868, "replies": 7 },
                            { "no": 196645355, "last_modified": 1576266866, "replies": 361 },
                            { "no": 196655995, "last_modified": 1576266867, "replies": 3 },
                            { "no": 196655998, "last_modified": 1576266860, "replies": 5 },
                            { "no": 196652782, "last_modified": 1576266858, "replies": 42 },
                            { "no": 196656536, "last_modified": 1576266853, "replies": 5 },
                            { "no": 196621039, "last_modified": 1576266853, "replies": 189 },
                            { "no": 196640441, "last_modified": 1576266851, "replies": 495 },
                            { "no": 196637247, "last_modified": 1576266850, "replies": 101 }
                          ]
                        },
                        {
                          "page": 2,
                          "threads": [
                            { "no": 196650664, "last_modified": 1576266846, "replies": 29 },
                            { "no": 196646963, "last_modified": 1576266845, "replies": 387 },
                            { "no": 196648390, "last_modified": 1576266844, "replies": 36 },
                            { "no": 196651494, "last_modified": 1576266832, "replies": 10 },
                            { "no": 196656773, "last_modified": 1576266827, "replies": 0 },
                            { "no": 196653207, "last_modified": 1576266827, "replies": 20 },
                            { "no": 196643737, "last_modified": 1576266825, "replies": 82 },
                            { "no": 196626714, "last_modified": 1576266824, "replies": 467 },
                            { "no": 196654299, "last_modified": 1576266821, "replies": 9 },
                            { "no": 196636729, "last_modified": 1576266819, "replies": 216 },
                            { "no": 196655015, "last_modified": 1576266819, "replies": 3 },
                            { "no": 196642084, "last_modified": 1576266818, "replies": 233 },
                            { "no": 196649533, "last_modified": 1576266816, "replies": 122 },
                            { "no": 196640416, "last_modified": 1576266806, "replies": 381 },
                            { "no": 196656724, "last_modified": 1576266794, "replies": 1 }
                          ]
                        }
                    ])
                } else {
                    json!([1243234, 5645756, 75686786, 456454325, 231412, 564576567, 34523234])
                },
            JsonType::DeprecatedFields =>
                if endpoint == YotsubaEndpoint::Threads {
                    json!(
                    [
                        {
                          "page": 1,
                          "threads": [
                            { "no_more": 196649146, "last_modified": 1576266882, "replies": 349 },
                            { "no_more": 196656555, "last_modified": 1576266881, "replies": 7 }
                          ]
                        },
                        {
                          "page": 2,
                          "threads": [
                            { "no_more": 196650664, "last_modified": 1576266846, "replies": 387},
                            { "no_more": 196646963, "last_modified": 1576266845, "replies": 487 }
                          ]
                        }
                    ])
                } else {
                    json!([])
                },
            JsonType::AddedFields =>
                if endpoint == YotsubaEndpoint::Threads {
                    json!(
                    [
                        {
                          "page": 1,
                          "threads": [
                            { "no": 196649146, "last_modified": 1576266882, "replies": 349, "new_field": 349 },
                            { "no": 196656555, "last_modified": 1576266881, "replies": 6,  "new_field": 7 }
                          ]
                        },
                        {
                          "page": 2,
                          "threads": [
                            { "no": 196650664, "last_modified": 1576266846, "replies": 387, "new_field": 387 },
                            { "no": 196646963, "last_modified": 1576266845, "replies": 487, "new_field": 487 },
                            { "no": 196648390, "last_modified": 1576266844, "replies": 36 , "new_field": 36 }
                          ]
                        }
                    ])
                } else {
                    json!([{}, "1243234", "5645756", "75686786", "456454325", "test", "test1"])
                },
            JsonType::MixedFields =>
                if endpoint == YotsubaEndpoint::Threads {
                    json!(
                    [
                        {
                          "page": 1,
                          "threads": [
                            { "no": 196649146, "last_modified": 1576266882, "replies": 349, "new_field": 349 },
                            { "no_more": 196656555, "last_modified": 1576266881, "replies": 6,  "new_field": 7 }
                          ]
                        },
                        {
                          "page": 2,
                          "threads": [
                            { "no": 196650664, "last_modified": 1576266846, "replies": 387, "new_field": 387 },
                            { "no": 196646963, "last_modified": 1576266845, "replies": 487, "new_field": 487 },
                            { "no_more": 196648390, "last_modified": 1576266844, "replies": 36 , "new_field": 36 }
                          ]
                        }
                    ])
                } else {
                    json!(["1243234", 5645756, "75686786", 456454325, "test", "test1"])
                },
        };

        let json = serde_json::to_vec(&_json).unwrap();

        let (db_client, connection) =
            tokio_postgres::connect(DB_URL, tokio_postgres::NoTls).await?;

        tokio::spawn(async move {
            if let Err(e) = connection.await {
                eprintln!("Connection error: {}", e);
            }
        });
        // Set search path
        db_client.query(format!(r#"SET search_path TO "{}";"#, SCHEMA).as_str(), &[]).await?;

        let statements = db_client.create_statements(ENGINE, endpoint, board).await;

        // let gen_id = |statement| {
        //     QueryIdentifier::new(ENGINE, endpoint, board, Some(SCHEMA.into()),
        // Some(CHARSET.into()), YotsubaHash::Sha256, statement) };

        // This is the ID used by `create_statements` for the get`threads_*` variants.
        let id = &QueryIdentifier::new(
            ENGINE,
            endpoint,
            board,
            None,
            None,
            YotsubaHash::Sha256,
            YotsubaStatement::Medias
        );

        match mode {
            YotsubaStatement::Threads
            | YotsubaStatement::ThreadsModified
            | YotsubaStatement::ThreadsCombined =>
                Ok(db_client.get_list(mode, &id, &statements, Some(json.as_slice()), None).await?),
            _ => Err(anyhow!("Error. Entered this test with : `{}` statement", mode))
        }
    }
    async fn test_send_single_thread(
        endpoint: YotsubaEndpoint, board: YotsubaBoard, mode: YotsubaStatement, json_type: JsonType
    ) -> Result<u64> {
        let _json = match json_type {
            JsonType::Unknown => json!({"test":1, "test2":2, "test3":3}),
            JsonType::Valid => json!(
                {
                    "posts": [{
                      "no": 5679879,
                      "sticky": 1,
                      "closed": 1,
                      "now": r#"12\/31\/18(Mon)17:05:48"#,
                      "name": "Anonymous",
                      "sub": r#"Welcome to \/po\/!"#,
                      "com": r#"Welcome to \/po\/! We specialize in origami, papercraft, and everything that\u2019s relevant to paper engineering. This board is also an great library of relevant PDF books and instructions, one of the best resource of its kind on the internet.<br><br>Questions and discussions of papercraft and origami are welcome. Threads for topics covered by paper engineering in general are also welcome, such as kirigami, bookbinding, printing technology, sticker making, gift boxes, greeting cards, and more.<br><br>Requesting is permitted, even encouraged if it\u2019s a good request; fulfilled requests strengthens this board\u2019s role as a repository of books and instructions. However do try to keep requests in relevant threads, if you can.<br><br>\/po\/ is a slow board! Do not needlessly bump threads."#,
                      "filename": "yotsuba_folding",
                      "ext": ".png",
                      "w": 530,
                      "h": 449,
                      "tn_w": 250,
                      "tn_h": 211,
                      "tim": 1546293948883 as i64,
                      "time": 1546293948,
                      "md5": "uZUeZeB14FVR+Mc2ScHvVA==",
                      "fsize": 516657,
                      "resto": 0,
                      "capcode": "mod",
                      "semantic_url": "welcome-to-po",
                      "replies": 2,
                      "images": 2,
                      "unique_ips": 1
                    }]}
            ),
            JsonType::DeprecatedFields => json!(
                {
                    "posts": [{
                      "no": 4588723,
                      "sticky": 1,
                      "closed": 1,
                      "ext": ".png",
                      "resto": 123457,
                      "w": 530,
                      "h": 449,
                      "tn_w": 250,
                      "tn_h": 211,
                      "tim": 1546293948883 as i64,
                      "time": 1546293948,
                      "unique_ips": 1
                    }]}
            ),
            JsonType::AddedFields => json!(
                {
                    "posts": [{
                      "no": 462537,
                      "sticky": 1,
                      "closed": 1,
                      "now": r#"12\/31\/18(Mon)17:05:48"#,
                      "name": "Anonymous",
                      "sub": "Wrg!",
                      "com": "sdf",
                      "filename": "yotsuba_folding",
                      "ext": ".png",
                      "w": 530,
                      "h": 449,
                      "tn_w": 250,
                      "tn_h": 211,
                      "tim": 1546293948883 as i64,
                      "time": 1546293948,
                      "md5": "uZUeZeB14FVR+Mc2ScHvVA==",
                      "fsize": 516657,
                      "resto": 0,
                      "capcode": "mod",
                      "semantic_url": "welcome-to-po",
                      "replies": 2,
                      "images": 2,
                      "unique_ips": 1,
                      "test": 1,
                      "added": 1
                    }]}
            ),
            JsonType::MixedFields => json!(
                {
                    "posts": [{
                      "no": 6745672,
                      "sticky": 1,
                      "closed": 1,
                      "ext": ".png",
                      "w": 530,
                      "h": 449,
                      "tn_w": 250,
                      "resto": 123457,
                      "tn_h": 211,
                      "tim": 1546293948883 as i64,
                      "time": 1546293948,
                      "unique_ips": 1,
                      "test": 1,
                      "test2": 1
                    }]}
            )
        };

        let json = serde_json::to_vec(&_json).unwrap();

        let (db_client, connection) =
            tokio_postgres::connect(DB_URL, tokio_postgres::NoTls).await?;

        tokio::spawn(async move {
            if let Err(e) = connection.await {
                eprintln!("Connection error: {}", e);
            }
        });
        // Set search path
        db_client.query(format!(r#"SET search_path TO "{}";"#, SCHEMA).as_str(), &[]).await?;

        let statements = db_client.create_statements(ENGINE, endpoint, board).await;

        // This is the ID used by `create_statements` for the get`threads_*` variants.
        let id = &QueryIdentifier::new(
            ENGINE,
            endpoint,
            board,
            None,
            None,
            YotsubaHash::Sha256,
            YotsubaStatement::Medias
        );

        match mode {
            YotsubaStatement::UpdateThread =>
                Ok(db_client.first(mode, &id, &statements, Some(json.as_slice()), None).await?),
            _ => Err(anyhow!("Error. Entered this test with : `{}` statement", mode))
        }
    }
}