Skip to content

Commit

Permalink
insert topic
Browse files Browse the repository at this point in the history
  • Loading branch information
bbkane committed Jan 22, 2022
1 parent 3ce4e0f commit 6429409
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
90 changes: 89 additions & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (p *SqlitePrinter) Line(sr *starredRepositoryEdge) error {
p.err = err
return err
}
stmt.QueryRowContext(
err = stmt.QueryRowContext(
p.ctx,
langName,
).Scan(&langID)
Expand All @@ -339,6 +339,7 @@ func (p *SqlitePrinter) Line(sr *starredRepositoryEdge) error {
p.err = err
return err
}

// insert Language_Repo
stmt, err = p.Prep(
`
Expand Down Expand Up @@ -372,6 +373,93 @@ func (p *SqlitePrinter) Line(sr *starredRepositoryEdge) error {

}

// Topic
{
for i := range sr.Node.RepositoryTopics.Nodes {
topicName := sr.Node.RepositoryTopics.Nodes[i].Topic.Name
topicURL := sr.Node.RepositoryTopics.Nodes[i].URL

// insert
stmt, err := p.Prep(
`
INSERT INTO Topic (
Name,
Url
)
VALUES (?, ?)
ON CONFLICT(Name)
DO NOTHING
ON CONFLICT(Url)
DO NOTHING
`,
)
if err != nil {
err = fmt.Errorf("topic insert prep err: %w", err)
p.err = err
return err
}
_, err = stmt.ExecContext(
p.ctx,
topicName,
topicURL,
)
if err != nil {
err = fmt.Errorf("topic insert err: %w", err)
p.err = err
return err
}

// get the id
var topicID int
stmt, err = p.Prep(
`
SELECT id FROM Topic
WHERE Name = ?
`,
)
if err != nil {
err = fmt.Errorf("topic select prep err: %w", err)
p.err = err
return err
}
err = stmt.QueryRowContext(
p.ctx,
topicName,
).Scan(&topicID)
if err != nil {
err = fmt.Errorf("topic select scan err: %w", err)
p.err = err
return err
}

// insert Repo_Topic
stmt, err = p.Prep(
`
INSERT INTO Repo_Topic (
Repo_id,
Topic_id
)
VALUES (?, ?)
`,
)
if err != nil {
err = fmt.Errorf("repo_topic insert prep err: %w", err)
p.err = err
return err
}
_, err = stmt.ExecContext(
p.ctx,
repoID,
topicID,
)
if err != nil {
err = fmt.Errorf("repo_topic insert err: %s: %w", sr.Node.NameWithOwner, err)
p.err = err
return err
}
}
}

return nil
}

Expand Down
6 changes: 4 additions & 2 deletions sqlite_migrations/2022-01-19-21.08.47_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ CREATE TABLE Language_Repo (
CREATE TABLE Topic (
id INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
UNIQUE(Name)
Url TEXT NOT NULL,
UNIQUE(Name),
UNIQUE(Url)
) STRICT;

CREATE TABLE Repo_Topic (
Expand All @@ -38,4 +40,4 @@ CREATE TABLE Repo_Topic (
FOREIGN KEY(Repo_id) REFERENCES Repo(id) ON DELETE CASCADE,
FOREIGN KEY(Topic_id) REFERENCES Topic(id) ON DELETE CASCADE,
PRIMARY KEY (Repo_id, Topic_id)
) STRICT;
) STRICT;

0 comments on commit 6429409

Please sign in to comment.