forked from titov-vv/jal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledger.sql
More file actions
1665 lines (1495 loc) · 52.2 KB
/
ledger.sql
File metadata and controls
1665 lines (1495 loc) · 52.2 KB
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
--
-- File generated with SQLiteStudio v3.2.1 on Tue Jan 28 22:42:15 2020
--
-- Text encoding used: UTF-8
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
-- Table: account_types
DROP TABLE IF EXISTS account_types;
CREATE TABLE account_types (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
name TEXT (32) NOT NULL
);
-- Table: accounts
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
type_id INTEGER REFERENCES account_types (id) ON DELETE RESTRICT
ON UPDATE CASCADE
NOT NULL,
name TEXT (64) NOT NULL
UNIQUE,
currency_id INTEGER REFERENCES assets (id) ON DELETE RESTRICT
ON UPDATE CASCADE
NOT NULL,
active INTEGER,
number TEXT (32),
reconciled_on INTEGER DEFAULT (0),
organization_id INTEGER REFERENCES agents (id) ON DELETE SET NULL
ON UPDATE CASCADE
);
-- Table: action_details
DROP TABLE IF EXISTS action_details;
CREATE TABLE action_details (
id INTEGER PRIMARY KEY
NOT NULL
UNIQUE,
pid INTEGER REFERENCES actions (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL,
category_id INTEGER REFERENCES categories (id) ON DELETE SET DEFAULT
ON UPDATE CASCADE
NOT NULL
DEFAULT (0),
tag_id INTEGER REFERENCES tags (id) ON DELETE SET NULL
ON UPDATE CASCADE,
sum REAL NOT NULL,
alt_sum REAL DEFAULT (0),
note TEXT (256)
);
-- Table: actions
DROP TABLE IF EXISTS actions;
CREATE TABLE actions (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
timestamp INTEGER NOT NULL,
account_id INTEGER REFERENCES accounts (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL,
peer_id INTEGER REFERENCES agents (id) ON DELETE RESTRICT
ON UPDATE CASCADE
NOT NULL,
alt_currency_id INTEGER REFERENCES assets (id) ON DELETE RESTRICT
ON UPDATE CASCADE
);
-- Table: asset_types
DROP TABLE IF EXISTS asset_types;
CREATE TABLE asset_types (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
name TEXT (32) NOT NULL
);
-- Table: assets
DROP TABLE IF EXISTS assets;
CREATE TABLE assets (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
name TEXT (32) UNIQUE
NOT NULL,
type_id INTEGER REFERENCES asset_types (id) ON DELETE RESTRICT
ON UPDATE CASCADE
NOT NULL,
full_name TEXT (128) NOT NULL,
isin TEXT (12),
web_id TEXT (32),
src_id INTEGER REFERENCES data_sources (id) ON DELETE SET NULL
ON UPDATE CASCADE
NOT NULL
DEFAULT ( -1)
);
-- Table: agents
DROP TABLE IF EXISTS agents;
CREATE TABLE agents (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
pid INTEGER NOT NULL
DEFAULT (0),
name TEXT (64) UNIQUE
NOT NULL,
location TEXT (128)
);
-- Table: balances
DROP TABLE IF EXISTS balances;
CREATE TABLE balances (
level1 INTEGER,
level2 INTEGER,
account_name TEXT NOT NULL,
balance REAL,
currency_name TEXT,
balance_adj REAL,
days_unreconciled INTEGER,
active INTEGER
);
-- Table: balances_aux
DROP TABLE IF EXISTS balances_aux;
CREATE TABLE balances_aux (
account_type INTEGER NOT NULL,
account INTEGER NOT NULL,
currency INTEGER NOT NULL,
balance REAL,
balance_adj REAL,
unreconciled_days INTEGER,
active INTEGER
);
-- Table: books
DROP TABLE IF EXISTS books;
CREATE TABLE books (
id INTEGER PRIMARY KEY
NOT NULL
UNIQUE,
name TEXT (32) NOT NULL
);
-- Table: categories
DROP TABLE IF EXISTS categories;
CREATE TABLE categories (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
pid INTEGER NOT NULL
DEFAULT (0),
name TEXT (64) UNIQUE
NOT NULL,
often INTEGER,
special INTEGER
);
-- Table: data_sources
DROP TABLE IF EXISTS data_sources;
CREATE TABLE data_sources (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
name TEXT (32) NOT NULL
);
-- Table: dividends
DROP TABLE IF EXISTS dividends;
CREATE TABLE dividends (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
timestamp INTEGER NOT NULL,
number TEXT (32) DEFAULT (''),
account_id INTEGER REFERENCES accounts (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL,
asset_id INTEGER REFERENCES assets (id) ON DELETE RESTRICT
ON UPDATE CASCADE
NOT NULL,
sum REAL NOT NULL
DEFAULT (0),
sum_tax REAL DEFAULT (0),
note TEXT (1014),
note_tax TEXT (64)
);
-- Table: holdings
DROP TABLE IF EXISTS holdings;
CREATE TABLE holdings (
level1 INTEGER,
level2 INTEGER,
currency TEXT,
account TEXT,
asset TEXT,
asset_name TEXT,
qty REAL,
open REAL,
quote REAL,
share REAL,
profit_rel REAL,
profit REAL,
value REAL,
value_adj REAL
);
-- Table: holdings_aux
DROP TABLE IF EXISTS holdings_aux;
CREATE TABLE holdings_aux (
currency INTEGER,
account INTEGER,
asset INTEGER,
qty REAL,
value REAL,
quote REAL,
quote_adj REAL,
total REAL,
total_adj REAL
);
-- Table: languages
DROP TABLE IF EXISTS languages;
CREATE TABLE languages (
id INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE
NOT NULL,
language CHAR (2) UNIQUE
NOT NULL
);
-- Table: ledger
DROP TABLE IF EXISTS ledger;
CREATE TABLE ledger (
id INTEGER PRIMARY KEY
NOT NULL
UNIQUE,
timestamp INTEGER NOT NULL,
sid INTEGER NOT NULL,
book_account INTEGER NOT NULL
REFERENCES books (id) ON DELETE NO ACTION
ON UPDATE NO ACTION,
asset_id INTEGER REFERENCES assets (id) ON DELETE SET NULL
ON UPDATE SET NULL,
account_id INTEGER NOT NULL
REFERENCES accounts (id) ON DELETE NO ACTION
ON UPDATE NO ACTION,
amount REAL,
value REAL,
peer_id INTEGER REFERENCES agents (id) ON DELETE NO ACTION
ON UPDATE NO ACTION,
category_id INTEGER REFERENCES categories (id) ON DELETE NO ACTION
ON UPDATE NO ACTION,
tag_id INTEGER REFERENCES tags (id) ON DELETE NO ACTION
ON UPDATE NO ACTION
);
-- Table: ledger_sums
DROP TABLE IF EXISTS ledger_sums;
CREATE TABLE ledger_sums (
sid INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
book_account INTEGER NOT NULL
REFERENCES books (id) ON DELETE NO ACTION
ON UPDATE NO ACTION,
asset_id INTEGER REFERENCES assets (id) ON DELETE SET NULL
ON UPDATE SET NULL,
account_id INTEGER NOT NULL
REFERENCES accounts (id) ON DELETE NO ACTION
ON UPDATE NO ACTION,
sum_amount REAL,
sum_value REAL
);
-- Table: map_category
DROP TABLE IF EXISTS map_category;
CREATE TABLE map_category (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
value VARCHAR (1024) NOT NULL,
mapped_to INTEGER NOT NULL
REFERENCES categories (id) ON DELETE SET DEFAULT
ON UPDATE CASCADE
DEFAULT (0)
);
-- Table: map_peer
DROP TABLE IF EXISTS map_peer;
CREATE TABLE map_peer (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
value VARCHAR (1024) NOT NULL,
mapped_to INTEGER REFERENCES agents (id) ON DELETE SET DEFAULT
ON UPDATE CASCADE
NOT NULL
DEFAULT (0)
);
-- Table: quotes
DROP TABLE IF EXISTS quotes;
CREATE TABLE quotes (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
timestamp INTEGER NOT NULL,
asset_id INTEGER REFERENCES assets (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL,
quote REAL
);
-- Table: sequence
DROP TABLE IF EXISTS sequence;
CREATE TABLE sequence (
id INTEGER PRIMARY KEY
NOT NULL
UNIQUE,
timestamp INTEGER NOT NULL,
type INTEGER NOT NULL,
operation_id INTEGER NOT NULL
);
-- Table: settings
DROP TABLE IF EXISTS settings;
CREATE TABLE settings (
id INTEGER PRIMARY KEY
NOT NULL
UNIQUE,
name TEXT (32) NOT NULL
UNIQUE,
value INTEGER
);
-- Table: t_last_assets
DROP TABLE IF EXISTS t_last_assets;
CREATE TABLE t_last_assets (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
name TEXT (64) UNIQUE
NOT NULL,
total_value REAL
);
-- Table: t_last_dates
DROP TABLE IF EXISTS t_last_dates;
CREATE TABLE t_last_dates (
ref_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL
);
-- Table: t_last_quotes
DROP TABLE IF EXISTS t_last_quotes;
CREATE TABLE t_last_quotes (
timestamp INTEGER NOT NULL,
asset_id INTEGER NOT NULL,
quote REAL
);
-- Table: t_months
DROP TABLE IF EXISTS t_months;
CREATE TABLE t_months (
month INTEGER,
asset_id INTEGER,
last_timestamp INTEGER
);
-- Table: t_pivot
DROP TABLE IF EXISTS t_pivot;
CREATE TABLE t_pivot (
row_key INTEGER NOT NULL,
col_key INTEGER NOT NULL,
value REAL
);
-- Table: tags
DROP TABLE IF EXISTS tags;
CREATE TABLE tags (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
tag TEXT (64) NOT NULL
);
-- Table: corp_actions
DROP TABLE IF EXISTS corp_actions;
CREATE TABLE corp_actions (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
type INTEGER,
note TEXT (1024)
);
-- Table: trades
DROP TABLE IF EXISTS trades;
CREATE TABLE trades (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
timestamp INTEGER NOT NULL,
settlement INTEGER DEFAULT (0),
corp_action_id INTEGER DEFAULT (0),
number TEXT (32) DEFAULT (''),
account_id INTEGER REFERENCES accounts (id) ON DELETE CASCADE
ON UPDATE CASCADE
NOT NULL,
asset_id INTEGER REFERENCES assets (id) ON DELETE RESTRICT
ON UPDATE CASCADE
NOT NULL,
qty REAL NOT NULL
DEFAULT (0),
price REAL NOT NULL
DEFAULT (0),
coupon REAL DEFAULT (0),
fee REAL DEFAULT (0)
);
-- Table: deals
DROP TABLE IF EXISTS deals;
CREATE TABLE deals (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
account_id INTEGER NOT NULL,
asset_id INTEGER NOT NULL,
open_sid INTEGER NOT NULL,
close_sid INTEGER NOT NULL,
qty REAL NOT NULL
);
-- Table: transfer_notes
DROP TABLE IF EXISTS transfer_notes;
CREATE TABLE transfer_notes (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
tid INTEGER NOT NULL
UNIQUE,
note TEXT (1024) NOT NULL
);
-- Table: transfers
DROP TABLE IF EXISTS transfers;
CREATE TABLE transfers (
id INTEGER PRIMARY KEY
UNIQUE
NOT NULL,
tid INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
type INTEGER NOT NULL,
account_id INTEGER NOT NULL
REFERENCES accounts (id) ON DELETE CASCADE
ON UPDATE CASCADE,
amount REAL,
rate REAL
);
-- Index: agents_by_name_idx
DROP INDEX IF EXISTS agents_by_name_idx;
CREATE INDEX agents_by_name_idx ON agents (name);
-- Index: by_sid
DROP INDEX IF EXISTS by_sid;
CREATE INDEX by_sid ON ledger_sums (sid);
-- Index: tid_type_unique
DROP INDEX IF EXISTS tid_type_unique;
CREATE UNIQUE INDEX tid_type_unique ON transfers (tid, type);
-- View: agents_ext
DROP VIEW IF EXISTS agents_ext;
CREATE VIEW agents_ext AS
SELECT a1.*,
count(a2.id) AS children_count,
count(a3.id) AS actions_count
FROM agents AS a1
LEFT JOIN
agents AS a2 ON a1.id = a2.pid
LEFT JOIN
actions AS a3 ON a1.id = a3.peer_id
GROUP BY a1.id;
-- View: all_operations
DROP VIEW IF EXISTS all_operations;
CREATE VIEW all_operations AS
SELECT m.type,
m.id,
m.timestamp,
m.account_id,
a.name AS account,
m.num_peer,
m.asset_id,
s.name AS asset,
s.full_name AS asset_name,
m.note,
m.note2,
m.amount,
m.qty_trid,
m.price,
m.fee_tax,
l.sum_amount AS t_amount,
m.t_qty,
c.name AS currency,
CASE WHEN m.timestamp <= a.reconciled_on THEN 1 ELSE 0 END AS reconciled
FROM (
SELECT 1 AS type,
o.id,
timestamp,
p.name AS num_peer,
account_id,
sum(d.sum) AS amount,
o.alt_currency_id AS asset_id,
NULL AS qty_trid,
sum(d.alt_sum) AS price,
NULL AS fee_tax,
NULL AS t_qty,
NULL AS note,
NULL AS note2,
o.id AS operation_id
FROM actions AS o
LEFT JOIN
agents AS p ON o.peer_id = p.id
LEFT JOIN
action_details AS d ON o.id = d.pid
GROUP BY o.id
UNION ALL
SELECT 2 AS type,
d.id,
d.timestamp,
d.number AS num_peer,
d.account_id,
d.sum AS amount,
d.asset_id,
SUM(coalesce(l.amount, 0) ) AS qty_trid,
NULL AS price,
d.sum_tax AS fee_tax,
NULL AS t_qty,
d.note AS note,
d.note_tax AS note2,
d.id AS operation_id
FROM dividends AS d
LEFT JOIN
ledger AS l ON d.asset_id = l.asset_id AND
d.account_id = l.account_id AND
l.book_account = 4 AND
l.timestamp <= d.timestamp
GROUP BY d.id
UNION ALL
SELECT 3 AS type,
t.id,
t.timestamp,
t.number AS num_peer,
t.account_id,
-(t.price*t.qty) AS amount,
t.asset_id,
t.qty AS qty_trid,
t.price AS price,
t.fee AS fee_tax,
l.sum_amount AS t_qty,
ca.note AS note,
NULL AS note2,
t.id AS operation_id
FROM trades AS t
LEFT JOIN
sequence AS q ON q.type = 3 AND
t.id = q.operation_id
LEFT JOIN
ledger_sums AS l ON l.sid = q.id AND
l.book_account = 4
LEFT JOIN corp_actions AS ca ON t.corp_action_id=ca.id
UNION ALL
SELECT 4 AS type,
r.tid,
r.timestamp,
c.name AS num_peer,
r.account_id,
r.amount,
NULL AS asset_id,
r.type AS qty_trid,
r.rate AS price,
NULL AS fee_tax,
NULL AS t_qty,
n.note,
a.name AS note2,
r.id AS operation_id
FROM transfers AS r
LEFT JOIN
transfer_notes AS n ON r.tid = n.tid
LEFT JOIN
transfers AS tr ON r.tid = tr.tid AND
r.type = -tr.type
LEFT JOIN
accounts AS a ON a.id = tr.account_id
LEFT JOIN
assets AS c ON c.id = a.currency_id
ORDER BY timestamp
)
AS m
LEFT JOIN
accounts AS a ON m.account_id = a.id
LEFT JOIN
assets AS s ON m.asset_id = s.id
LEFT JOIN
assets AS c ON a.currency_id = c.id
LEFT JOIN
sequence AS q ON m.type = q.type AND
m.operation_id = q.operation_id
LEFT JOIN
ledger_sums AS l ON l.sid = q.id AND
(l.book_account = 3 OR
l.book_account = 5);
-- View: all_transactions
DROP VIEW IF EXISTS all_transactions;
CREATE VIEW all_transactions AS
SELECT at.*,
a.currency_id AS currency
FROM (
SELECT 1 AS type,
a.id,
a.timestamp,
CASE WHEN SUM(d.sum) < 0 THEN COUNT(d.sum) ELSE -COUNT(d.sum) END AS subtype,
a.account_id AS account,
NULL AS asset,
SUM(d.sum) AS amount,
d.category_id AS price_category,
a.peer_id AS coupon_peer,
d.tag_id AS fee_tax_tag
FROM actions AS a
LEFT JOIN
action_details AS d ON a.id = d.pid
GROUP BY a.id
UNION ALL
SELECT 2 AS type,
id,
timestamp,
0 AS subtype,
account_id AS account,
asset_id AS asset,
sum AS amount,
NULL AS price_category,
NULL AS coupon_peer,
sum_tax AS fee_tax_tag
FROM dividends
UNION ALL
SELECT 4 AS type,
id,
timestamp,
type AS subtype,
account_id AS account,
NULL AS asset,
amount,
NULL AS price_category,
NULL AS coupon_peer,
NULL AS fee_tax_tag
FROM transfers
UNION ALL
SELECT 3 AS type,
t.id,
t.timestamp,
coalesce(ca.type, 0) AS subtype,
t.account_id AS account,
t.asset_id AS asset,
t.qty AS amount,
t.price AS price_category,
t.coupon AS coupon_peer,
t.fee AS fee_tax_tag
FROM trades AS t
LEFT JOIN
corp_actions AS ca ON t.corp_action_id = ca.id
ORDER BY timestamp,
type,
subtype
)
AS at
LEFT JOIN
accounts AS a ON at.account = a.id;
-- View: categories_ext
DROP VIEW IF EXISTS categories_ext;
CREATE VIEW categories_ext AS
SELECT c1.*,
count(c2.id) AS children_count
FROM categories AS c1
LEFT JOIN
categories c2 ON c1.id = c2.pid
GROUP BY c1.id;
-- View: categories_tree
DROP VIEW IF EXISTS categories_tree;
CREATE VIEW categories_tree AS
WITH RECURSIVE tree (
id,
level,
path
)
AS (
SELECT id,
0,
name
FROM categories
WHERE pid = 0
UNION
SELECT categories.id,
tree.level + 1 AS level,
tree.path || CHAR(127) || categories.name AS path
FROM categories
JOIN
tree
WHERE categories.pid = tree.id
)
SELECT id,
level,
path
FROM tree
ORDER BY path;
-- View: currencies
DROP VIEW IF EXISTS currencies;
CREATE VIEW currencies AS
SELECT id,
name
FROM assets
WHERE type_id = 1;
-- View: frontier
DROP VIEW IF EXISTS frontier;
CREATE VIEW frontier AS
SELECT MAX(sequence.timestamp) AS ledger_frontier
FROM sequence;
-- View: transfers_combined
DROP VIEW IF EXISTS transfers_combined;
CREATE VIEW transfers_combined AS
SELECT f.tid AS id,
f.id AS from_id,
f.timestamp AS from_timestamp,
f.account_id AS from_acc_id,
t.id AS to_id,
t.timestamp AS to_timestamp,
t.account_id AS to_acc_id,
fee.id AS fee_id,
fee.timestamp AS fee_timestamp,
fee.account_id AS fee_acc_id,
f.amount AS from_amount,
t.amount AS to_amount,
fee.amount AS fee_amount,
n.note
FROM transfers AS f
INNER JOIN
transfers AS t ON f.tid = t.tid AND
t.type = 1
LEFT JOIN
transfers AS fee ON f.tid = fee.tid AND
fee.type = 0
LEFT JOIN
transfer_notes AS n ON f.tid = n.tid
WHERE f.type = -1;
-- View: deals_ext
DROP VIEW IF EXISTS deals_ext;
CREATE VIEW deals_ext AS
SELECT d.account_id,
ac.name AS account,
d.asset_id,
at.name AS asset,
ot.timestamp AS open_timestamp,
ct.timestamp AS close_timestamp,
ot.price AS open_price,
ct.price AS close_price,
d.qty AS qty,
ot.fee + ct.fee AS fee,
d.qty * (ct.price - ot.price) - (ot.fee + ct.fee) AS profit,
coalesce(100 * (d.qty * (ct.price - ot.price) - (ot.fee + ct.fee) ) / (d.qty * ot.price), 0) AS rel_profit
FROM deals AS d
LEFT JOIN
sequence AS os ON d.open_sid = os.id
LEFT JOIN
trades AS ot ON ot.id = os.operation_id
LEFT JOIN
sequence AS cs ON d.close_sid = cs.id
LEFT JOIN
trades AS ct ON ct.id = cs.operation_id
LEFT JOIN
accounts AS ac ON d.account_id = ac.id
LEFT JOIN
assets AS at ON d.asset_id = at.id
ORDER BY ct.timestamp,
ot.timestamp;
-- Trigger: action_details_after_delete
DROP TRIGGER IF EXISTS action_details_after_delete;
CREATE TRIGGER action_details_after_delete
AFTER DELETE
ON action_details
FOR EACH ROW
BEGIN
DELETE FROM ledger
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = OLD.pid
);
DELETE FROM sequence
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = OLD.pid
);
DELETE FROM ledger_sums
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = OLD.pid
);
END;
-- Trigger: action_details_after_insert
DROP TRIGGER IF EXISTS action_details_after_insert;
CREATE TRIGGER action_details_after_insert
AFTER INSERT
ON action_details
FOR EACH ROW
BEGIN
DELETE FROM ledger
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = NEW.pid
);
DELETE FROM sequence
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = NEW.pid
);
DELETE FROM ledger_sums
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = NEW.pid
);
END;
-- Trigger: action_details_after_update
DROP TRIGGER IF EXISTS action_details_after_update;
CREATE TRIGGER action_details_after_update
AFTER UPDATE
ON action_details
FOR EACH ROW
BEGIN
DELETE FROM ledger
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = OLD.pid
);
DELETE FROM sequence
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = OLD.pid
);
DELETE FROM ledger_sums
WHERE timestamp >= (
SELECT timestamp
FROM actions
WHERE id = OLD.pid
);
END;
-- Trigger: actions_after_delete
DROP TRIGGER IF EXISTS actions_after_delete;
CREATE TRIGGER actions_after_delete
AFTER DELETE
ON actions
FOR EACH ROW
WHEN (
SELECT value
FROM settings
WHERE id = 1
)
BEGIN
DELETE FROM action_details
WHERE pid = OLD.id;
DELETE FROM ledger
WHERE timestamp >= OLD.timestamp;
DELETE FROM sequence
WHERE timestamp >= OLD.timestamp;
DELETE FROM ledger_sums
WHERE timestamp >= OLD.timestamp;
END;
-- Trigger: actions_after_insert
DROP TRIGGER IF EXISTS actions_after_insert;
CREATE TRIGGER actions_after_insert
AFTER INSERT
ON actions
FOR EACH ROW
WHEN (
SELECT value
FROM settings
WHERE id = 1
)
BEGIN
DELETE FROM ledger
WHERE timestamp >= NEW.timestamp;
DELETE FROM sequence
WHERE timestamp >= NEW.timestamp;
DELETE FROM ledger_sums