Skip to content

Commit aa10564

Browse files
committed
wip
1 parent 8ed1ef2 commit aa10564

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,19 @@ db[:, :, :] # All edges
105105
## ✨ Attribution ✨
106106

107107
SQLiteGraph is **STRONGLY** influenced by [https://github.com/dpapathanasiou/simple-graph](https://github.com/dpapathanasiou/simple-graph).
108+
109+
110+
<br><br>
111+
112+
## Under the Hood Details
113+
114+
- Nodes and edges are saved in the `nodes` and `edges` tables, respectively.
115+
- `nodes`
116+
- `id` (`INTEGER`): unique identifier of a node
117+
- `labels` (`TEXT`): stored as `;`-delimited (thus `;` cannot be used in a label)
118+
- `props` (`TEXT`): stored as `JSON3.write(props)`
119+
- `edges`
120+
- `source` (`INTEGER`): id of "from" node (`nodes(id)` is a foreign key)
121+
- `target` (`INTEGER`): id of "to" node (`nodes(id)` is a foreign key)
122+
- `type` (`TEXT`): the "class" of the edge/relationship
123+
- `props` (`TEXT`)

src/SQLiteGraph.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ function single_result_execute(db, stmt, args...)
1414
end
1515

1616
function print_props(io::IO, o::Config)
17-
for (k,v) in pairs(o)
18-
printstyled(io, "\n$k: ", color=:light_black)
19-
print(IOContext(io, :compat=>true), v)
17+
for (i,(k,v)) in enumerate(pairs(o))
18+
print(io, k, '=', v)
19+
i == length(o) || print(io, ", ")
2020
end
2121
end
2222

@@ -32,11 +32,11 @@ Node(row::SQLite.Row) = Node(row.id, split(row.labels, ';', keepempty=false), JS
3232
function Base.show(io::IO, o::Node)
3333
print(io, "Node($(o.id)")
3434
!isempty(o.labels) && print(io, ", ", join(repr.(o.labels), ", "))
35-
!isempty(o.props) && print(io, "; ", ("$k=$v" for (k,v) in pairs(o.props))...)
35+
!isempty(o.props) && print(io, "; "); print_props(io, o.props)
3636
print(io, ')')
3737
end
3838
args(n::Node) = (n.id, isempty(n.labels) ? "" : join(n.labels, ';'), JSON3.write(n.props))
39-
Base.:(==)(a::Node, b::Node) = all(getfield(a,f) == getfield(b,f) for f in fieldnames(Node))
39+
4040

4141

4242
struct Edge
@@ -49,12 +49,21 @@ Edge(src::Int, tgt::Int, type::String; props...) = Edge(src, tgt, type, Config(p
4949
Edge(row::SQLite.Row) = Edge(row.source, row.target, row.type, JSON3.read(row.props, Config))
5050
function Base.show(io::IO, o::Edge)
5151
print(io, "Edge($(o.source), $(o.target), ", repr(o.type))
52-
!isempty(o.props) && print(io, "; ", ("$k=$v" for (k,v) in pairs(o.props))...)
52+
!isempty(o.props) && print(io, "; "); print_props(io, o.props)
5353
print(io, ')')
5454
end
5555
args(e::Edge) = (e.source, e.target, e.type, JSON3.write(e.props))
56+
57+
58+
#-----------------------------------------------------------------------------# Base methods
59+
Base.:(==)(a::Node, b::Node) = all(getfield(a,f) == getfield(b,f) for f in fieldnames(Node))
5660
Base.:(==)(a::Edge, b::Edge) = all(getfield(a,f) == getfield(b,f) for f in fieldnames(Edge))
5761

62+
Base.pairs(o::T) where {T<: Union{Node, Edge}} = (f => getfield(o,f) for f in fieldnames(T))
63+
64+
Base.NamedTuple(o::Union{Node,Edge}) = NamedTuple(pairs(o))
65+
66+
5867

5968
#-----------------------------------------------------------------------------# DB
6069
struct DB
@@ -70,7 +79,6 @@ struct DB
7079
labels TEXT NOT NULL,
7180
props TEXT NOT NULL
7281
);",
73-
7482
# edges
7583
"CREATE TABLE IF NOT EXISTS edges (
7684
source INTEGER NOT NULL REFERENCES nodes(id),

0 commit comments

Comments
 (0)