| 784 | } |
| 785 | |
| 786 | char *account_get_balance(const tal_t *ctx, |
| 787 | struct db *db, |
| 788 | const char *acct_name, |
| 789 | bool calc_sum, |
| 790 | bool skip_ignored, |
| 791 | struct acct_balance ***balances, |
| 792 | bool *account_exists) |
| 793 | { |
| 794 | struct db_stmt *stmt; |
| 795 | |
| 796 | stmt = db_prepare_v2(db, SQL("SELECT" |
| 797 | " CAST(SUM(ce.credit) AS BIGINT) as credit" |
| 798 | ", CAST(SUM(ce.debit) AS BIGINT) as debit" |
| 799 | ", ce.currency" |
| 800 | " FROM chain_events ce" |
| 801 | " LEFT OUTER JOIN accounts a" |
| 802 | " ON a.id = ce.account_id" |
| 803 | " WHERE a.name = ?" |
| 804 | " AND ce.ignored != ?" |
| 805 | " GROUP BY ce.currency")); |
| 806 | |
| 807 | db_bind_text(stmt, 0, acct_name); |
| 808 | /* We populate ignored with a 0 or 1, |
| 809 | * if we want both 0+1, we just ignore everything with a 2 */ |
| 810 | db_bind_int(stmt, 1, skip_ignored ? 1 : 2); |
| 811 | db_query_prepared(stmt); |
| 812 | *balances = tal_arr(ctx, struct acct_balance *, 0); |
| 813 | if (account_exists) |
| 814 | *account_exists = false; |
| 815 | |
| 816 | while (db_step(stmt)) { |
| 817 | struct acct_balance *bal; |
| 818 | |
| 819 | bal = tal(*balances, struct acct_balance); |
| 820 | |
| 821 | bal->currency = db_col_strdup(bal, stmt, "ce.currency"); |
| 822 | db_col_amount_msat(stmt, "credit", &bal->credit); |
| 823 | db_col_amount_msat(stmt, "debit", &bal->debit); |
| 824 | tal_arr_expand(balances, bal); |
| 825 | |
| 826 | if (account_exists) |
| 827 | *account_exists = true; |
| 828 | } |
| 829 | tal_free(stmt); |
| 830 | |
| 831 | stmt = db_prepare_v2(db, SQL("SELECT" |
| 832 | " CAST(SUM(ce.credit) AS BIGINT) as credit" |
| 833 | ", CAST(SUM(ce.debit) AS BIGINT) as debit" |
| 834 | ", ce.currency" |
| 835 | " FROM channel_events ce" |
| 836 | " LEFT OUTER JOIN accounts a" |
| 837 | " ON a.id = ce.account_id" |
| 838 | " WHERE a.name = ?" |
| 839 | " GROUP BY ce.currency")); |
| 840 | db_bind_text(stmt, 0, acct_name); |
| 841 | db_query_prepared(stmt); |
| 842 | |
| 843 | while (db_step(stmt)) { |