Skip to content

Commit 1f67d1b

Browse files
committed
wip
1 parent 2884cbd commit 1f67d1b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,24 @@ db[1, 2]
3838
# "{\"a\":1,\"b\":2}"
3939
```
4040

41-
### Querying Edges
41+
### Querying Edges Based on Node ID
4242

4343
```julia
4444
db[1, :] # all outgoing edges from node 1
4545

4646
db[1, 2:5] # outgoing edges from node 1 to any of nodes 2,3,4,5
4747

4848
db[:, 1] # all incoming edges to node 1
49+
```
50+
51+
### Querying Based on Properties
52+
53+
- multiple keyword args are a logical "AND"
54+
55+
```julia
56+
SQLiteGraph.findnodes(db, x=1)
4957

58+
SQLiteGraph.findedges(db, b=2)
5059
```
5160

5261
## Attribution

src/SQLiteGraph.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ Broadcast.broadcastable(db::DB) = Ref(db)
108108

109109
function Base.iterate(db::DB, state = (length(db), 1))
110110
state[2] > state[1] && return nothing
111-
single_result_execute(db, "SELECT props FROM nodes WHERE id = ?", (state[2],)), (state[1], state[2] + 1)
111+
res = single_result_execute(db, "SELECT props FROM nodes WHERE id = ?", (state[2],))
112+
Node(state[2], res), (state[1], state[2] + 1)
112113
end
113114

114115
# #-----------------------------------------------------------------------------# ReadAs
@@ -146,6 +147,16 @@ function Base.deleteat!(db::DB, id::Integer)
146147
db
147148
end
148149

150+
function findnodes(db::DB; kw...)
151+
param = join(map(collect(kw)) do kw
152+
k, v = kw
153+
"json_extract(props, '\$.$k') = $v"
154+
end, " AND ")
155+
156+
res = execute(db, "SELECT * FROM nodes WHERE $param")
157+
isempty(res) ? nothing : [Node(row...) for row in res]
158+
end
159+
149160
#-----------------------------------------------------------------------------# edges
150161
function Base.setindex!(db::DB, props, i::Integer, j::Integer)
151162
execute(db, "INSERT INTO edges VALUES(?, ?, json(?))", (i, j, JSON3.write(props)))
@@ -181,4 +192,14 @@ function Base.deleteat!(db::DB, i::Integer, j::Integer)
181192
db
182193
end
183194

195+
function findedges(db::DB; kw...)
196+
param = join(map(collect(kw)) do kw
197+
k, v = kw
198+
"json_extract(props, '\$.$k') = $v"
199+
end, " AND ")
200+
201+
res = execute(db, "SELECT * FROM edges WHERE $param")
202+
isempty(res) ? nothing : [Edge(row...) for row in res]
203+
end
204+
184205
end

0 commit comments

Comments
 (0)