Skip to content

Commit

Permalink
Added more logic to generate SQL for the creation of SQL tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbdz committed Nov 17, 2024
1 parent c5bf22a commit 2006fde
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
46 changes: 40 additions & 6 deletions cache/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"strings"
"sync"

_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -109,18 +110,51 @@ func (s *SDatabase) CreateTable(table Table) error {
return fmt.Errorf(ErrorDatabaseNotInitialized)
}

createTableSQL := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`, table.Name)
// TODO: Example of what Id and other columns look like:
/*
Id TEXT PRIMARY KEY,
name TEXT NOT NULL
*/

var sqlColumns string
for _, column := range table.Columns {
sqlColumn := fmt.Sprintf(",\n%s %s %s", column.ColumnName, column.DataType, column.Constraint)
sqlColumns = fmt.Sprintf("%s %s", sqlColumns, sqlColumn)
}

sqlColumns = strings.TrimPrefix(sqlColumns, ",")

var sqlRelationships string
for _, relationship := range table.Relationships {
sqlRelationship := fmt.Sprintf(
",\nFOREIGN KEY(%s) REFERENCES %s(%s)",
relationship.ColumnName,
relationship.ReferencesTableName,
relationship.ReferencesColumnName)
sqlRelationships = fmt.Sprintf("%s %s", sqlRelationships, sqlRelationship)
}

createTableSQL := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (%s%s);`, table.Name, sqlColumns, sqlRelationships)

_, err := db.Exec(createTableSQL)
if err != nil {
return fmt.Errorf("error creating table: %v", err)
}

createIndexSQL := fmt.Sprintf(`CREATE INDEX IF NOT EXISTS idx_%s_name ON %s(name);`, table.Name, table.Name)
_, err = db.Exec(createIndexSQL)
var sqlIndexes string
for _, index := range table.Columns {
// TODO: `idx_` what is this? Is this needed?
//createIndexSQL := fmt.Sprintf(`CREATE INDEX IF NOT EXISTS idx_%s_name ON %s(name);`, table.Name, table.Name)
sqlIndexe := fmt.Sprintf(
"CREATE INDEX IF NOT EXISTS idx_%s_%s ON %s(%s);",
table.Name,
index.ColumnName,
table.Name,
index.ColumnName)
sqlIndexes = fmt.Sprintf("%s\n%s", sqlIndexes, sqlIndexe)
}

_, err = db.Exec(sqlIndexes)
if err != nil {
return fmt.Errorf("error creating index: %v", err)
}
Expand Down
14 changes: 11 additions & 3 deletions cache/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ const (

// Table is a basic representation in a struct of a table in a SQL DB
type Table struct {
Name string
Columns []Column
Rows []map[string]any
Name string
Columns []Column
Relationships []Relationships
Rows []map[string]any
}

// Column is a basic representation in a struct of a column in a SQL DB
type Column struct {
ColumnName string
DataType string
Constraint string
}

type Relationships struct {
ColumnName string
ReferencesTableName string
ReferencesColumnName string
}
15 changes: 13 additions & 2 deletions cache/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ func TestParseEntity(t *testing.T) {
DataType: "string",
},
},
Rows: []map[string]any{},
Rows: []map[string]any{
{
"Id": "b5de2fb8-80d2-4506-b82b-66cca70f7d3e",
"server_name": "localhost",
"listen": "c6beaec1-90c4-4d2a-aaef-211ab00b86bd",
},
},
},
{
Name: "Net",
Expand All @@ -125,7 +131,12 @@ func TestParseEntity(t *testing.T) {
DataType: "string",
},
},
Rows: []map[string]any{},
Rows: []map[string]any{
{
"Id": "c6beaec1-90c4-4d2a-aaef-211ab00b86bd",
"ports": "[80, 443]",
},
},
},
}
e := entity.Entity{
Expand Down

0 comments on commit 2006fde

Please sign in to comment.