Skip to content

Commit 7302080

Browse files
committed
simplify query code, udpate copyright
1 parent aa10564 commit 7302080

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 joshday <emailjoshday@gmail.com> and contributors
3+
Copyright (c) 2022: Julia Computing Inc. All rights reserved.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/SQLiteGraph.jl

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,55 +130,51 @@ function Base.replace!(db::DB, edge::Edge)
130130
db
131131
end
132132

133-
#-----------------------------------------------------------------------------# getindex (Node)
134-
function Base.getindex(db::DB, id::Integer)
135-
res = execute(db, "SELECT * FROM nodes WHERE id = ?", (id,))
136-
isempty(res) ? error("Node $id does not exist.") : Node(first(res))
137-
end
138-
function Base.getindex(db::DB, ::Colon)
139-
res = execute(db, "SELECT * FROM nodes")
140-
isempty(res) ? error("No nodes exist yet.") : (Node(row) for row in res)
133+
#-----------------------------------------------------------------------------# query
134+
function query(db::DB, select::String, from::String, whr::String, args=nothing)
135+
stmt = "SELECT $select FROM $from WHERE $whr"
136+
# @info stmt
137+
res = isnothing(args) ? execute(db, stmt) : execute(db, stmt, args)
138+
if isempty(res)
139+
error("No $from found where: $whr")
140+
else
141+
return res
142+
end
141143
end
142144

145+
#-----------------------------------------------------------------------------# getindex (Node)
146+
Base.getindex(db::DB, i::Integer) = Node(first(query(db, "*", "nodes", "id=$i")))
147+
Base.getindex(db::DB, ::Colon) = (Node(row) for row in query(db, "*", "nodes", "TRUE"))
148+
143149
#-----------------------------------------------------------------------------# getindex (Edge)
144150
# all specified
145151
function Base.getindex(db::DB, i::Integer, j::Integer, type::AbstractString)
146-
res = execute(db, "SELECT * FROM edges WHERE source=? AND target=? AND type=?", (i,j,type))
147-
isempty(res) ? error("Edge $i$type$j does not exist.") : Edge(first(res))
152+
Edge(first(query(db, "*", "edges", "source=$i AND target=$j AND type LIKE '$type'")))
148153
end
149154

150155
# one colon
151156
function Base.getindex(db::DB, i::Integer, j::Integer, ::Colon)
152-
res = execute(db, "SELECT * FROM edges WHERE source=? AND target=?", (i, j))
153-
isempty(res) ? error("No edges connect nodes $i$j.") : (Edge(row) for row in res)
157+
(Edge(row) for row in query(db, "*", "edges", "source=$i AND target=$j"))
154158
end
155159
function Base.getindex(db::DB, i::Integer, ::Colon, type::AbstractString)
156-
res = execute(db, "SELECT * FROM edges WHERE source=? AND type=?", (i,type))
157-
isempty(res) ? error("No outgoing edges $type$i") : (Edge(row) for row in res)
160+
(Edge(row) for row in query(db, "*", "edges", "source=$i AND type LIKE '$type'"))
158161
end
159162
function Base.getindex(db::DB, ::Colon, j::Integer, type::AbstractString)
160-
res = execute(db, "SELECT * FROM edges WHERE target=? AND type=?", (j, type))
161-
isempty(res) ? error("No incoming edges $type$j") : (Edge(row) for row in res)
163+
(Edge(row) for row in query(db, "*", "edges", "target=$j AND type LIKE '$type'"))
162164
end
163165

164166
# two colons
165167
function Base.getindex(db::DB, i::Integer, ::Colon, ::Colon)
166-
res = execute(db, "SELECT * FROM edges WHERE source=?", (i,))
167-
isempty(res) ? error("No outgoing edges from node $i") : (Edge(row) for row in res)
168+
(Edge(row) for row in query(db, "*", "edges", "source=$i"))
168169
end
169170
function Base.getindex(db::DB, i::Colon, j::Integer, ::Colon)
170-
res = execute(db, "SELECT * FROM edges WHERE target=?", (j,))
171-
isempty(res) ? error("No incoming edges into node $j") : (Edge(row) for row in res)
171+
(Edge(row) for row in query(db, "*", "edges", "target=$j"))
172172
end
173173
function Base.getindex(db::DB, ::Colon, ::Colon, type::AbstractString)
174-
res = execute(db, "SELECT * FROM edges WHERE type=?", (type,))
175-
isempty(res) ? error("No edges with type $type") : (Edge(row) for row in res)
174+
(Edge(row) for row in query(db, "*", "edges", "type LIKE '$type'"))
176175
end
177176

178177
# all colons
179-
function Base.getindex(db::DB, ::Colon, ::Colon, ::Colon)
180-
res = execute(db, "SELECT * FROM edges")
181-
isempty(res) ? error("No edges exist yet.") : (Edge(row) for row in res)
182-
end
178+
Base.getindex(db::DB, ::Colon, ::Colon, ::Colon) = (Edge(row) for row in query(db,"*", "edges", "TRUE"))
183179

184180
end

0 commit comments

Comments
 (0)