Skip to content

Commit a90b31a

Browse files
authored
Merge pull request #15 from database-playground/pan93412/dbp-61-implement-database-structure-retrieval
DBP-61: implement database structure retrieval
2 parents b47f1e6 + 6e2f4c3 commit a90b31a

File tree

11 files changed

+296
-11
lines changed

11 files changed

+296
-11
lines changed

graph/database.graphqls

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extend type Database {
2+
structure: DatabaseStructure!
3+
}
4+
5+
type DatabaseStructure {
6+
tables: [DatabaseTable!]!
7+
}
8+
9+
type DatabaseTable {
10+
name: String!
11+
columns: [String!]!
12+
}

graph/database.resolvers.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graph/ent.resolvers.go

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graph/event.resolvers.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graph/model/models_gen.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graph/question.graphqls

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ extend type Question {
6464
referenceAnswerResult: SQLExecutionResult! @scope(scope: "question:read")
6565

6666
"""
67-
List of your submissions for this question.
67+
List of your submissions for this question, ordered by submitted at descending.
6868
"""
6969
userSubmissions: [Submission!]!
7070

71+
"""
72+
Get the last submission for this question.
73+
"""
74+
lastSubmission: Submission
75+
7176
"""
7277
Have you tried to solve the question?
7378
"""

graph/question.resolvers.go

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graph/user.resolvers.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/sqlrunner/models.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ type ErrorResponse struct {
4545
func (e ErrorResponse) Error() string {
4646
return fmt.Sprintf("%s: %s", e.Code, e.Message)
4747
}
48+
49+
// DatabaseStructure is the database structure of a schema.
50+
type DatabaseStructure struct {
51+
Tables []DatabaseTable `json:"tables"`
52+
}
53+
54+
// DatabaseTable is the table structure of a schema.
55+
type DatabaseTable struct {
56+
Name string `json:"name"`
57+
Columns []string `json:"columns"`
58+
}

internal/sqlrunner/sqlrunner.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,50 @@ func (s *SqlRunner) Query(ctx context.Context, schema, query string) (DataRespon
6666
return respBody.Data, nil
6767
}
6868

69+
func (s *SqlRunner) GetDatabaseStructure(ctx context.Context, schema string) (DatabaseStructure, error) {
70+
// Query SQLite's master table to get all table names
71+
tablesQuery := "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name"
72+
tablesResp, err := s.Query(ctx, schema, tablesQuery)
73+
if err != nil {
74+
return DatabaseStructure{}, fmt.Errorf("failed to query tables: %w", err)
75+
}
76+
77+
var tables []DatabaseTable
78+
79+
// For each table, get its column information
80+
for _, row := range tablesResp.Rows {
81+
if len(row) == 0 {
82+
continue
83+
}
84+
tableName := row[0]
85+
86+
// Use PRAGMA table_info to get column information
87+
columnsQuery := fmt.Sprintf("PRAGMA table_info(%s)", tableName)
88+
columnsResp, err := s.Query(ctx, schema, columnsQuery)
89+
if err != nil {
90+
return DatabaseStructure{}, fmt.Errorf("query columns for table %s: %w", tableName, err)
91+
}
92+
93+
var columns []string
94+
// PRAGMA table_info returns: cid, name, type, notnull, dflt_value, pk
95+
// We only need the name (index 1)
96+
for _, columnRow := range columnsResp.Rows {
97+
if len(columnRow) > 1 {
98+
columns = append(columns, columnRow[1])
99+
}
100+
}
101+
102+
tables = append(tables, DatabaseTable{
103+
Name: tableName,
104+
Columns: columns,
105+
})
106+
}
107+
108+
return DatabaseStructure{
109+
Tables: tables,
110+
}, nil
111+
}
112+
69113
func (s *SqlRunner) IsHealthy(ctx context.Context) bool {
70114
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/healthz", s.cfg.URI), nil)
71115
if err != nil {

0 commit comments

Comments
 (0)