-
Notifications
You must be signed in to change notification settings - Fork 990
Expand file tree
/
Copy pathsql-template.json
More file actions
302 lines (302 loc) · 8.79 KB
/
sql-template.json
File metadata and controls
302 lines (302 loc) · 8.79 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
{
"$schema": "../rpc-schema-draft.json",
"type": "object",
"added": "v23.02",
"rpc": "sql",
"title": "Command to do complex queries on list commands",
"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 below 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.",
"",
"Note that you may need to use `-o` if you use queries which contain `=` (which make lightning-cli(1) default to keyword style)"
],
"request": {
"required": [
"query"
],
"additionalProperties": false,
"properties": {
"query": {
"type": "string",
"description": [
"The standard sqlite3 query to run.",
"Note that queries like \"SELECT *\" are fragile, as columns will change across releases; see lightning-listsqlschemas(7)."
]
}
}
},
"response": {
"required": [
"rows"
],
"additionalProperties": false,
"properties": {
"rows": {
"type": "array",
"items": {
"type": "array"
}
},
"warning_db_failure": {
"type": "string",
"description": [
"A message if the database encounters an error partway through."
]
}
},
"pre_return_value_notes": [
"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."
]
},
"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. Stored as an integer internally for efficient indexing.",
" * JSON: string",
" * sqlite3: SCID (INTEGER affinity)",
"",
"You can use the `scid()` function to convert a short_channel_id string to its integer representation for queries, e.g. `WHERE in_channel = scid('1x2x3')`. The `fmt_scid()` function converts back to string form."
],
"permitted_sqlite3_functions": [
"Writing to the database is not permitted, and limits are placed on various other query parameters.",
"",
"Additionally, only the following functions are allowed:",
"",
"* abs",
"* avg",
"* coalesce",
"* count",
"* date",
"* datetime",
"* julianday",
"* hex",
"* quote",
"* length",
"* like",
"* lower",
"* upper",
"* min",
"* max",
"* strftime",
"* sum",
"* time",
"* timediff",
"* total",
"* unixepoch",
"* json_object",
"* json_group_array",
"* scid",
"* fmt_scid"
],
"tables": [
"Note that tables which have a `created_index` field use that as the primary key (and `rowid` is an alias to this), otherwise an explicit `rowid` integer primary key is generated, whose value changes on each refresh. This field is used for related tables to refer to specific rows in their parent. (sqlite3 usually has this as an implicit column, but we make it explicit as the implicit version is not allowed to be used as a foreign key).",
""
],
"errors": [
"On failure, an error is returned."
],
"author": [
"Rusty Russell [rusty@rustcorp.com.au](mailto:rusty@rustcorp.com.au) is mainly responsible."
],
"see_also": [
"lightning-listtransactions(7)",
"lightning-listhtlcs(7)",
"lightning-listinvoices(7)",
"lightning-listoffers(7)",
"lightning-listchannels(7)",
"lightning-listpeers(7)",
"lightning-listpeerchannels(7)",
"lightning-listsendpays(7)",
"lightning-listchainmoves(7)",
"lightning-listchannelmoves(7)",
"lightning-bkpr-listaccountevents(7)",
"lightning-bkpr-listincome(7)",
"lightning-listnodes(7)",
"lightning-listforwards(7)"
],
"resources": [
"Main web site: [https://github.com/ElementsProject/lightning](https://github.com/ElementsProject/lightning)"
],
"examples": [
{
"description": [
"A simple peers selection query:"
],
"request": {
"id": "example:sql#1",
"method": "sql",
"params": {
"query": "SELECT id FROM peers"
}
},
"response": {
"rows": [
[
"nodeid020202020202020202020202020202020202020202020202020202020202"
]
]
}
},
{
"description": [
"A statement containing `=` needs `-o` in shell:"
],
"request": {
"id": "example:sql#2",
"method": "sql",
"params": [
"SELECT label, description, status FROM invoices WHERE label='label inv_l12'"
]
},
"response": {
"rows": [
[
"label inv_l12",
"description inv_l12",
"unpaid"
]
]
}
},
{
"description": [
"If you want to get specific nodeid values from the nodes table:"
],
"request": {
"id": "example:sql#3",
"method": "sql",
"params": [
"SELECT nodeid FROM nodes WHERE nodeid != x'nodeid030303030303030303030303030303030303030303030303030303030303'"
]
},
"response": {
"rows": [
[
"nodeid020202020202020202020202020202020202020202020202020202020202"
],
[
"nodeid010101010101010101010101010101010101010101010101010101010101"
],
[
"nodeid040404040404040404040404040404040404040404040404040404040404"
]
]
}
},
{
"description": [
"If you want to compare a BLOB column, `x'hex'` or `X'hex'` are needed:"
],
"request": {
"id": "example:sql#4",
"method": "sql",
"params": [
"SELECT nodeid FROM nodes WHERE nodeid IN (x'nodeid010101010101010101010101010101010101010101010101010101010101', x'nodeid030303030303030303030303030303030303030303030303030303030303')"
]
},
"response": {
"rows": [
[
"nodeid010101010101010101010101010101010101010101010101010101010101"
],
[
"nodeid030303030303030303030303030303030303030303030303030303030303"
]
]
}
},
{
"description": [
"Related tables are usually referenced by JOIN:"
],
"request": {
"id": "example:sql#5",
"method": "sql",
"params": [
"SELECT peer_id, to_us_msat, total_msat, peerchannels_status.status FROM peerchannels INNER JOIN peerchannels_status ON peerchannels_status.row = peerchannels.rowid"
]
},
"response": {
"rows": [
[
"nodeid020202020202020202020202020202020202020202020202020202020202",
490493792,
1000000000,
"CHANNELD_NORMAL:Channel ready for use."
]
]
}
},
{
"description": [
"Simple function usage, in this case COUNT. Strings inside arrays need \", and ' to protect them from the shell:"
],
"request": {
"id": "example:sql#6",
"method": "sql",
"params": [
"SELECT COUNT(*) FROM forwards"
]
},
"response": {
"rows": [
[
9
]
]
}
},
{
"request": {
"id": "example:sql#7",
"method": "sql",
"params": [
"SELECT * from peerchannels_features"
]
},
"response": {
"rows": [
[
28,
23,
0,
"option_static_remotekey"
],
[
30,
23,
2,
"option_anchors"
]
]
}
}
]
}