Skip to content

Commit

Permalink
use list instead of object statement
Browse files Browse the repository at this point in the history
  • Loading branch information
maurerbot committed Feb 17, 2023
1 parent 2fa6315 commit e21d531
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
18 changes: 8 additions & 10 deletions platform/edges/src/db/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {

await g.db
.prepare(
'INSERT INTO node (urn, nid, nss, fragment) VALUES (?1, ?2, ?3, ?4) \
'INSERT INTO node (urn, nid, nss, fragment) VALUES (?, ?, ?, ?) \
ON CONFLICT(urn) DO UPDATE SET fragment = excluded.fragment'
)
.bind(id, nid, nss, fc)
Expand All @@ -48,7 +48,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {
// Add an entry to the join table for each q-component row that is
// used in the node URN.
const qcJoinStmt = g.db.prepare(
'INSERT INTO node_qcomp (nodeUrn, key, value) VALUES (?1, ?2, ?3) \
'INSERT INTO node_qcomp (nodeUrn, key, value) VALUES (?, ?, ?) \
ON CONFLICT(nodeUrn, key) DO UPDATE SET value=excluded.value'
)
const stmts = []
Expand All @@ -70,7 +70,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {
// Add an entry to the join table for each q-component row that is
// used in the node URN.
const rcJoinStmt = g.db.prepare(
'INSERT INTO node_rcomp (nodeUrn, key, value) VALUES (?1, ?2, ?3) \
'INSERT INTO node_rcomp (nodeUrn, key, value) VALUES (?, ?, ?) \
ON CONFLICT(nodeUrn, key) DO UPDATE SET value=excluded.value'
)
const stmts = []
Expand All @@ -82,7 +82,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {

// Get the ID of the inserted node.
const node = g.db
.prepare('SELECT * FROM node WHERE urn = ?1')
.prepare('SELECT * FROM node WHERE urn = ?')
.bind(id)
.first() as unknown

Expand Down Expand Up @@ -118,9 +118,9 @@ export async function edge(
createdTimestamp
)
VALUES (
?1,
?2,
?3,
?,
?,
?,
datetime('now')
)
ON CONFLICT DO NOTHING
Expand All @@ -137,9 +137,7 @@ export async function edge(
}

const edge = await g.db
.prepare(
'SELECT * FROM edge WHERE src = ?1 AND dst = ?2 AND tag = ?3'
)
.prepare('SELECT * FROM edge WHERE src = ? AND dst = ? AND tag = ?')
.bind(srcParam, dstParam, tagParam)
.first()

Expand Down
2 changes: 1 addition & 1 deletion platform/edges/src/db/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function edge(
): Promise<any> {
return new Promise((resolve, reject) => {
g.db
.prepare('DELETE FROM edge WHERE src = ?1 AND dst = ?2 AND tag = ?3')
.prepare('DELETE FROM edge WHERE src = ? AND dst = ? AND tag = ?')
.bind(src, dst, tag)
.run()
.then((result) => {
Expand Down
22 changes: 12 additions & 10 deletions platform/edges/src/db/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function qc(g: GraphDB, nodeId: AnyURN): Promise<QComponents> {
FROM
node_qcomp
WHERE
nodeUrn = ?1
nodeUrn = ?
`
const qcomp = await g.db
.prepare(query)
Expand Down Expand Up @@ -77,7 +77,7 @@ export async function rc(g: GraphDB, nodeId: AnyURN): Promise<RComponents> {
FROM
node_rcomp
WHERE
nodeUrn = ?1
nodeUrn = ?
`
const rcomp = await g.db
.prepare(query)
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function node(
FROM
node
WHERE
urn = ?1
urn = ?
`
const node = await g.db.prepare(query).bind(nodeId.toString()).first<Node>()

Expand Down Expand Up @@ -160,18 +160,20 @@ export async function edges(
if (query.id) {
switch (query.dir) {
case Graph.EdgeDirection.Incoming:
sql = `SELECT * FROM edge e WHERE (e.dst = ?1)`
sql = `SELECT * FROM edge e WHERE (e.dst = ?)`
break
case Graph.EdgeDirection.Outgoing:
sql = `SELECT * FROM edge e WHERE (e.src = ?1)`
sql = `SELECT * FROM edge e WHERE (e.src = ?)`
break
default:
sql = `SELECT * FROM edge e WHERE (e.src = ?1 OR e.dst = ?1)`
throw 'no direction currently not supported. see https://github.com/cloudflare/miniflare/issues/504'
// default:
// sql = `SELECT * FROM edge e WHERE (e.src = ? OR e.dst = ?)`
}

// Filter edges by tag, if provided.
if (query.tag) {
sql += ' AND e.tag = ?2 ORDER BY createdTimestamp ASC'
sql += ' AND e.tag = ? ORDER BY createdTimestamp ASC'
statement = g.db.prepare(sql).bind(query.id.toString(), query.tag)
} else {
sql += ' ORDER BY createdTimestamp ASC'
Expand All @@ -181,7 +183,7 @@ export async function edges(
sql = `SELECT * FROM edge e`

if (query.tag) {
sql += ' WHERE e.tag = ?1 ORDER BY createdTimestamp ASC'
sql += ' WHERE e.tag = ? ORDER BY createdTimestamp ASC'
statement = g.db.prepare(sql).bind(query.tag)
} else {
sql += ' ORDER BY createdTimestamp ASC'
Expand Down Expand Up @@ -342,7 +344,7 @@ export async function edges(
export async function incoming(g: GraphDB, nodeId: AnyURN): Promise<Edge[]> {
return new Promise((resolve, reject) => {
g.db
.prepare('SELECT * FROM edge WHERE dst = ?1')
.prepare('SELECT * FROM edge WHERE dst = ?')
.bind(nodeId.toString())
.all()
.then((result) => {
Expand All @@ -363,7 +365,7 @@ export async function incoming(g: GraphDB, nodeId: AnyURN): Promise<Edge[]> {
export async function outgoing(g: GraphDB, nodeId: AnyURN): Promise<Edge[]> {
return new Promise((resolve, reject) => {
g.db
.prepare('SELECT * FROM edge WHERE src = ?1')
.prepare('SELECT * FROM edge WHERE src = ?')
.bind(nodeId.toString())
.all()
.then((result) => {
Expand Down
18 changes: 8 additions & 10 deletions platform/edges/src/db/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {

await g.db
.prepare(
'INSERT INTO node (urn, nid, nss, fragment) VALUES (?1, ?2, ?3, ?4) \
'INSERT INTO node (urn, nid, nss, fragment) VALUES (?, ?, ?, ?) \
ON CONFLICT(urn) DO UPDATE SET fragment = excluded.fragment'
)
.bind(id, nid, nss, fc)
Expand All @@ -47,7 +47,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {
// Add an entry to the join table for each q-component row that is
// used in the node URN.
const qcJoinStmt = g.db.prepare(
'INSERT INTO node_qcomp (nodeUrn, key, value) VALUES (?1, ?2, ?3) \
'INSERT INTO node_qcomp (nodeUrn, key, value) VALUES (?, ?, ?) \
ON CONFLICT(nodeUrn, key) DO UPDATE SET value=excluded.value'
)
const stmts = []
Expand All @@ -69,7 +69,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {
// Add an entry to the join table for each q-component row that is
// used in the node URN.
const rcJoinStmt = g.db.prepare(
'INSERT INTO node_rcomp (nodeUrn, key, value) VALUES (?1, ?2, ?3) \
'INSERT INTO node_rcomp (nodeUrn, key, value) VALUES (?, ?, ?) \
ON CONFLICT(nodeUrn, key) DO UPDATE SET value=excluded.value'
)
const stmts = []
Expand All @@ -81,7 +81,7 @@ export async function node(g: Graph, urn: AnyURN): Promise<NodeRecord> {

// Get the ID of the inserted node.
const node = g.db
.prepare('SELECT * FROM node WHERE urn = ?1')
.prepare('SELECT * FROM node WHERE urn = ?')
.bind(id)
.first() as unknown

Expand Down Expand Up @@ -116,9 +116,9 @@ export async function edge(
tag
)
VALUES (
?1,
?2,
?3
?,
?,
?
)
ON CONFLICT DO NOTHING
`
Expand All @@ -134,9 +134,7 @@ export async function edge(
}

const edge = await g.db
.prepare(
'SELECT * FROM edge WHERE src = ?1 AND dst = ?2 AND tag = ?3'
)
.prepare('SELECT * FROM edge WHERE src = ? AND dst = ? AND tag = ?')
.bind(srcParam, dstParam, tagParam)
.first()

Expand Down

0 comments on commit e21d531

Please sign in to comment.