@@ -4,7 +4,7 @@ using SQLite: SQLite
4
4
import DBInterface: execute
5
5
using JSON3: JSON3
6
6
7
- export DB, ReadAs
7
+ export DB, Node, Edge, find_nodes, find_edges
8
8
9
9
# -----------------------------------------------------------------------------# utils
10
10
read_sql (fn:: String ) = read (joinpath (@__DIR__ , " sql" , fn), String)
@@ -19,15 +19,24 @@ struct Node{T}
19
19
props:: T
20
20
end
21
21
Node (id:: Integer , props= nothing ) = Node (id, props)
22
- Base. show (io:: IO , o:: Node ) = (print (io, " Node($(o. id) ) with props: " ); show (io, o. props))
22
+ function Base. show (io:: IO , o:: Node )
23
+ print (io, " Node($(o. id) ) with props: " )
24
+ print (io, o. props)
25
+ end
23
26
24
27
struct Edge{T}
25
28
source:: Int
26
29
target:: Int
27
30
props:: T
28
31
end
29
- Base. show (io:: IO , o:: Edge ) = (print (io, " Edge($(o. source) → $(o. target) ) with props: " ); show (io, o. props))
30
32
Edge (source:: Integer , target:: Integer , props= nothing ) = Edge (source, target, props)
33
+ Edge (T:: DataType , e:: Edge{<:AbstractString} ) = Edge (e. source, e. target, JSON3. read (e. props, T))
34
+ Edge (e:: Edge{<:AbstractString} , T:: DataType ) = Edge (e. source, e. target, JSON3. read (e. props, T))
35
+ function Base. show (io:: IO , o:: Edge )
36
+ print (io, " Edge($(o. source) → $(o. target) ) with props: " )
37
+ print (io, o. props)
38
+ end
39
+
31
40
32
41
# -----------------------------------------------------------------------------# DB
33
42
"""
@@ -61,6 +70,7 @@ struct DB
61
70
62
71
function DB (file = " :memory:" )
63
72
db = SQLite. DB (file)
73
+ SQLite. @register db SQLite. regexp
64
74
statements = [
65
75
" CREATE TABLE IF NOT EXISTS nodes (
66
76
id INTEGER NOT NULL UNIQUE,
@@ -146,8 +156,8 @@ function Base.deleteat!(db::DB, id::Integer)
146
156
execute (db, " DELETE FROM edges WHERE source = ? OR target = ?" , (id, id))
147
157
db
148
158
end
149
-
150
- function findnodes (db:: DB ; kw... )
159
+ # -----------------------------------------------------------------------------# find_nodes
160
+ function find_nodes (db:: DB ; kw... )
151
161
param = join (map (collect (kw)) do kw
152
162
k, v = kw
153
163
" json_extract(props, '\$ .$k ') = $v "
@@ -156,6 +166,11 @@ function findnodes(db::DB; kw...)
156
166
res = execute (db, " SELECT * FROM nodes WHERE $param " )
157
167
isempty (res) ? nothing : [Node (row... ) for row in res]
158
168
end
169
+ function find_nodes (db:: DB , r:: Regex )
170
+ res = execute (db, " SELECT * FROM nodes WHERE props REGEXP ?" , (r. pattern,))
171
+ isempty (res) ? nothing : [Node (row... ) for row in res]
172
+ end
173
+
159
174
160
175
# -----------------------------------------------------------------------------# edges
161
176
function Base. setindex! (db:: DB , props, i:: Integer , j:: Integer )
@@ -192,7 +207,8 @@ function Base.deleteat!(db::DB, i::Integer, j::Integer)
192
207
db
193
208
end
194
209
195
- function findedges (db:: DB ; kw... )
210
+ # -----------------------------------------------------------------------------# find_edges
211
+ function find_edges (db:: DB ; kw... )
196
212
param = join (map (collect (kw)) do kw
197
213
k, v = kw
198
214
" json_extract(props, '\$ .$k ') = $v "
@@ -201,5 +217,9 @@ function findedges(db::DB; kw...)
201
217
res = execute (db, " SELECT * FROM edges WHERE $param " )
202
218
isempty (res) ? nothing : [Edge (row... ) for row in res]
203
219
end
220
+ function find_edges (db:: DB , r:: Regex )
221
+ res = execute (db, " SELECT * FROM edges WHERE props REGEXP ?" , (r. pattern,))
222
+ isempty (res) ? nothing : [Edge (row... ) for row in res]
223
+ end
204
224
205
225
end
0 commit comments