From 2006fde50591612a23cf0cc58bf01e28e64caa7b Mon Sep 17 00:00:00 2001 From: jnbdz Date: Sun, 17 Nov 2024 09:57:45 -0500 Subject: [PATCH] Added more logic to generate SQL for the creation of SQL tables. --- cache/database/database.go | 46 ++++++++++++++++++++++++++++++++----- cache/database/types.go | 14 ++++++++--- cache/parser/parser_test.go | 15 ++++++++++-- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/cache/database/database.go b/cache/database/database.go index 4b287ce..5d40f40 100644 --- a/cache/database/database.go +++ b/cache/database/database.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "fmt" + "strings" "sync" _ "github.com/mattn/go-sqlite3" @@ -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) } diff --git a/cache/database/types.go b/cache/database/types.go index 7438123..b0b3138 100644 --- a/cache/database/types.go +++ b/cache/database/types.go @@ -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 } diff --git a/cache/parser/parser_test.go b/cache/parser/parser_test.go index d58f5cc..3550290 100644 --- a/cache/parser/parser_test.go +++ b/cache/parser/parser_test.go @@ -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", @@ -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{