In particular, we generate the schema part from the plugin itself. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Changelog-Added: Plugins: `sql` plugin command to perform server-side complex queries.
14 KiB
lightning-sql -- Command to do complex queries on list commands
SYNOPSIS
sql query
DESCRIPTION
The sql RPC command runs the given query across a sqlite3 database created from various list commands.
When tables are accessed, it calls the above commands, so it's no
faster than any other local access (though it goes to great length to
cache listnodes and listchannels) which then processes the results.
It is, however faster for remote access if the result of the query is much smaller than the list commands would be.
TREATMENT OF TYPES
The following types are supported in schemas, and this shows how they are presented in the database. This matters: a JSON boolean is represented as an integer in the database, so a query will return 0 or 1, not true or false.
-
hex. A hex string.
- JSON: a string
- sqlite3: BLOB
-
hash/secret/pubkey/txid: just like hex.
-
msat/integer/u64/u32/u16/u8. Normal numbers.
- JSON: an unsigned integer
- sqlite3: INTEGER
-
boolean. True or false.
- JSON: literal true or false
- sqlite3: INTEGER
-
number. A floating point number (used for times in some places).
- JSON: number
- sqlite3: REAL
-
string. Text.
- JSON: string
- sqlite3: TEXT
-
short_channel_id. A short-channel-id of form 1x2x3.
- JSON: string
- sqlite3: TEXT
TABLES
The following tables are currently supported:
-
channelsindexed byshort_channel_id(see lightning-listchannels(7))source(typepubkey, sqltypeBLOB)destination(typepubkey, sqltypeBLOB)short_channel_id(typeshort_channel_id, sqltypeTEXT)direction(typeu32, sqltypeINTEGER)public(typeboolean, sqltypeINTEGER)amount_msat(typemsat, sqltypeINTEGER)message_flags(typeu8, sqltypeINTEGER)channel_flags(typeu8, sqltypeINTEGER)active(typeboolean, sqltypeINTEGER)last_update(typeu32, sqltypeINTEGER)base_fee_millisatoshi(typeu32, sqltypeINTEGER)fee_per_millionth(typeu32, sqltypeINTEGER)delay(typeu32, sqltypeINTEGER)htlc_minimum_msat(typemsat, sqltypeINTEGER)htlc_maximum_msat(typemsat, sqltypeINTEGER)features(typehex, sqltypeBLOB)
-
forwardsindexed byin_channel and in_htlc_id(see lightning-listforwards(7))in_channel(typeshort_channel_id, sqltypeTEXT)in_htlc_id(typeu64, sqltypeINTEGER)in_msat(typemsat, sqltypeINTEGER)status(typestring, sqltypeTEXT)received_time(typenumber, sqltypeREAL)out_channel(typeshort_channel_id, sqltypeTEXT)out_htlc_id(typeu64, sqltypeINTEGER)style(typestring, sqltypeTEXT)fee_msat(typemsat, sqltypeINTEGER)out_msat(typemsat, sqltypeINTEGER)resolved_time(typenumber, sqltypeREAL)failcode(typeu32, sqltypeINTEGER)failreason(typestring, sqltypeTEXT)
-
htlcsindexed byshort_channel_id and id(see lightning-listhtlcs(7))short_channel_id(typeshort_channel_id, sqltypeTEXT)id(typeu64, sqltypeINTEGER)expiry(typeu32, sqltypeINTEGER)amount_msat(typemsat, sqltypeINTEGER)direction(typestring, sqltypeTEXT)payment_hash(typehash, sqltypeBLOB)state(typestring, sqltypeTEXT)
-
invoicesindexed bypayment_hash(see lightning-listinvoices(7))label(typestring, sqltypeTEXT)description(typestring, sqltypeTEXT)payment_hash(typehash, sqltypeBLOB)status(typestring, sqltypeTEXT)expires_at(typeu64, sqltypeINTEGER)amount_msat(typemsat, sqltypeINTEGER)bolt11(typestring, sqltypeTEXT)bolt12(typestring, sqltypeTEXT)local_offer_id(typehash, sqltypeBLOB)invreq_payer_note(typestring, sqltypeTEXT)pay_index(typeu64, sqltypeINTEGER)amount_received_msat(typemsat, sqltypeINTEGER)paid_at(typeu64, sqltypeINTEGER)payment_preimage(typesecret, sqltypeBLOB)
-
nodesindexed bynodeid(see lightning-listnodes(7))nodeid(typepubkey, sqltypeBLOB)last_timestamp(typeu32, sqltypeINTEGER)alias(typestring, sqltypeTEXT)color(typehex, sqltypeBLOB)features(typehex, sqltypeBLOB)- related table
nodes_addressesrow(reference tonodes.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)type(typestring, sqltypeTEXT)port(typeu16, sqltypeINTEGER)address(typestring, sqltypeTEXT)
option_will_fund_lease_fee_base_msat(typemsat, sqltypeINTEGER, from JSON objectoption_will_fund)option_will_fund_lease_fee_basis(typeu32, sqltypeINTEGER, from JSON objectoption_will_fund)option_will_fund_funding_weight(typeu32, sqltypeINTEGER, from JSON objectoption_will_fund)option_will_fund_channel_fee_max_base_msat(typemsat, sqltypeINTEGER, from JSON objectoption_will_fund)option_will_fund_channel_fee_max_proportional_thousandths(typeu32, sqltypeINTEGER, from JSON objectoption_will_fund)option_will_fund_compact_lease(typehex, sqltypeBLOB, from JSON objectoption_will_fund)
-
offersindexed byoffer_id(see lightning-listoffers(7))offer_id(typehash, sqltypeBLOB)active(typeboolean, sqltypeINTEGER)single_use(typeboolean, sqltypeINTEGER)bolt12(typestring, sqltypeTEXT)used(typeboolean, sqltypeINTEGER)label(typestring, sqltypeTEXT)
-
peerchannelsindexed bypeer_id(see lightning-listpeerchannels(7))peer_id(typepubkey, sqltypeBLOB)peer_connected(typeboolean, sqltypeINTEGER)state(typestring, sqltypeTEXT)scratch_txid(typetxid, sqltypeBLOB)feerate_perkw(typeu32, sqltypeINTEGER, from JSON objectfeerate)feerate_perkb(typeu32, sqltypeINTEGER, from JSON objectfeerate)owner(typestring, sqltypeTEXT)short_channel_id(typeshort_channel_id, sqltypeTEXT)channel_id(typehash, sqltypeBLOB)funding_txid(typetxid, sqltypeBLOB)funding_outnum(typeu32, sqltypeINTEGER)initial_feerate(typestring, sqltypeTEXT)last_feerate(typestring, sqltypeTEXT)next_feerate(typestring, sqltypeTEXT)next_fee_step(typeu32, sqltypeINTEGER)- related table
peerchannels_inflightrow(reference topeerchannels.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)funding_txid(typetxid, sqltypeBLOB)funding_outnum(typeu32, sqltypeINTEGER)feerate(typestring, sqltypeTEXT)total_funding_msat(typemsat, sqltypeINTEGER)our_funding_msat(typemsat, sqltypeINTEGER)scratch_txid(typetxid, sqltypeBLOB)
close_to(typehex, sqltypeBLOB)private(typeboolean, sqltypeINTEGER)opener(typestring, sqltypeTEXT)closer(typestring, sqltypeTEXT)- related table
peerchannels_featuresrow(reference topeerchannels.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)features(typestring, sqltypeTEXT)
funding_pushed_msat(typemsat, sqltypeINTEGER, from JSON objectfunding)funding_local_funds_msat(typemsat, sqltypeINTEGER, from JSON objectfunding)funding_remote_funds_msat(typemsat, sqltypeINTEGER, from JSON objectfunding)funding_fee_paid_msat(typemsat, sqltypeINTEGER, from JSON objectfunding)funding_fee_rcvd_msat(typemsat, sqltypeINTEGER, from JSON objectfunding)to_us_msat(typemsat, sqltypeINTEGER)min_to_us_msat(typemsat, sqltypeINTEGER)max_to_us_msat(typemsat, sqltypeINTEGER)total_msat(typemsat, sqltypeINTEGER)fee_base_msat(typemsat, sqltypeINTEGER)fee_proportional_millionths(typeu32, sqltypeINTEGER)dust_limit_msat(typemsat, sqltypeINTEGER)max_total_htlc_in_msat(typemsat, sqltypeINTEGER)their_reserve_msat(typemsat, sqltypeINTEGER)our_reserve_msat(typemsat, sqltypeINTEGER)spendable_msat(typemsat, sqltypeINTEGER)receivable_msat(typemsat, sqltypeINTEGER)minimum_htlc_in_msat(typemsat, sqltypeINTEGER)minimum_htlc_out_msat(typemsat, sqltypeINTEGER)maximum_htlc_out_msat(typemsat, sqltypeINTEGER)their_to_self_delay(typeu32, sqltypeINTEGER)our_to_self_delay(typeu32, sqltypeINTEGER)max_accepted_htlcs(typeu32, sqltypeINTEGER)alias_local(typeshort_channel_id, sqltypeTEXT, from JSON objectalias)alias_remote(typeshort_channel_id, sqltypeTEXT, from JSON objectalias)- related table
peerchannels_state_changesrow(reference topeerchannels.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)timestamp(typestring, sqltypeTEXT)old_state(typestring, sqltypeTEXT)new_state(typestring, sqltypeTEXT)cause(typestring, sqltypeTEXT)message(typestring, sqltypeTEXT)
- related table
peerchannels_statusrow(reference topeerchannels.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)status(typestring, sqltypeTEXT)
in_payments_offered(typeu64, sqltypeINTEGER)in_offered_msat(typemsat, sqltypeINTEGER)in_payments_fulfilled(typeu64, sqltypeINTEGER)in_fulfilled_msat(typemsat, sqltypeINTEGER)out_payments_offered(typeu64, sqltypeINTEGER)out_offered_msat(typemsat, sqltypeINTEGER)out_payments_fulfilled(typeu64, sqltypeINTEGER)out_fulfilled_msat(typemsat, sqltypeINTEGER)- related table
peerchannels_htlcsrow(reference topeerchannels.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)direction(typestring, sqltypeTEXT)id(typeu64, sqltypeINTEGER)amount_msat(typemsat, sqltypeINTEGER)expiry(typeu32, sqltypeINTEGER)payment_hash(typehash, sqltypeBLOB)local_trimmed(typeboolean, sqltypeINTEGER)status(typestring, sqltypeTEXT)state(typestring, sqltypeTEXT)
close_to_addr(typestring, sqltypeTEXT)last_tx_fee_msat(typemsat, sqltypeINTEGER)direction(typeu32, sqltypeINTEGER)
-
peersindexed byid(see lightning-listpeers(7))id(typepubkey, sqltypeBLOB)connected(typeboolean, sqltypeINTEGER)- related table
peers_netaddrrow(reference topeers.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)netaddr(typestring, sqltypeTEXT)
remote_addr(typestring, sqltypeTEXT)features(typehex, sqltypeBLOB)
-
sendpaysindexed bypayment_hash(see lightning-listsendpays(7))id(typeu64, sqltypeINTEGER)groupid(typeu64, sqltypeINTEGER)partid(typeu64, sqltypeINTEGER)payment_hash(typehash, sqltypeBLOB)status(typestring, sqltypeTEXT)amount_msat(typemsat, sqltypeINTEGER)destination(typepubkey, sqltypeBLOB)created_at(typeu64, sqltypeINTEGER)amount_sent_msat(typemsat, sqltypeINTEGER)label(typestring, sqltypeTEXT)bolt11(typestring, sqltypeTEXT)description(typestring, sqltypeTEXT)bolt12(typestring, sqltypeTEXT)payment_preimage(typesecret, sqltypeBLOB)erroronion(typehex, sqltypeBLOB)
-
transactionsindexed byhash(see lightning-listtransactions(7))hash(typetxid, sqltypeBLOB)rawtx(typehex, sqltypeBLOB)blockheight(typeu32, sqltypeINTEGER)txindex(typeu32, sqltypeINTEGER)locktime(typeu32, sqltypeINTEGER)version(typeu32, sqltypeINTEGER)- related table
transactions_inputsrow(reference totransactions.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)txid(typetxid, sqltypeBLOB)idx(typeu32, sqltypeINTEGER, from JSON fieldindex)sequence(typeu32, sqltypeINTEGER)type(typestring, sqltypeTEXT)channel(typeshort_channel_id, sqltypeTEXT)
- related table
transactions_outputsrow(reference totransactions.rowid, sqltypeINTEGER)arrindex(index within array, sqltypeINTEGER)idx(typeu32, sqltypeINTEGER, from JSON fieldindex)amount_msat(typemsat, sqltypeINTEGER)scriptPubKey(typehex, sqltypeBLOB)type(typestring, sqltypeTEXT)channel(typeshort_channel_id, sqltypeTEXT)
RETURN VALUE
On success, an object containing rows is returned. It is an array. Each array entry contains an array of values, each an integer, real number, string or null, depending on the sqlite3 type.
The object may contain warning_db_failure if the database fails partway through its operation.
On failure, an error is returned.
AUTHOR
Rusty Russell <rusty@rustcorp.com.au> is mainly responsible.
SEE ALSO
lightning-listtransactions(7), lightning-listchannels(7), lightning-listpeers(7), lightning-listnodes(7), lightning-listforwards(7).
RESOURCES
Main web site: https://github.com/ElementsProject/lightning comment: # ( SHA256STAMP:93309f8c45ea3aa153bfd8822f2748c1254812d41a408de39bacefa292e11374)