diff --git a/cache/database/database.go b/cache/database/database.go index 37de3b0..621cee2 100644 --- a/cache/database/database.go +++ b/cache/database/database.go @@ -24,7 +24,9 @@ type IDatabase interface { } // SDatabase implements IDatabase -type SDatabase struct{} +type SDatabase struct { + queries *[]string +} var ( db *sql.DB @@ -42,7 +44,7 @@ func (s *SDatabase) Initialize() error { return nil // Already initialized successfully } - dbPath := "/tmp/amadla.cache" + dbPath := "/tmp/hery.test.cache" var err error db, err = sql.Open("sqlite3", dbPath) if err != nil { @@ -105,10 +107,10 @@ func (s *SDatabase) IsInitialized() bool { } // CreateTable creates a new table -func (s *SDatabase) CreateTable(table Table) string { +func (s *SDatabase) CreateTable(table Table) { var sqlColumns string for _, column := range table.Columns { - sqlColumn := fmt.Sprintf(",\n%s %s %s", column.ColumnName, column.DataType, column.Constraint) + sqlColumn := fmt.Sprintf(",\n%s %s %s", column.ColumnName, column.DataType, column.Constraints) sqlColumns = fmt.Sprintf("%s %s", sqlColumns, sqlColumn) } @@ -139,17 +141,17 @@ func (s *SDatabase) CreateTable(table Table) string { sqlIndexes = fmt.Sprintf("%s\n%s", sqlIndexes, sqlIndexe) } - return fmt.Sprintf("%s\n%s", createTableSQL, sqlIndexes) + *s.queries = append(*s.queries, fmt.Sprintf("%s\n%s", createTableSQL, sqlIndexes)) } // Insert inserts records into the table -func (s *SDatabase) Insert(table Table, names []string) string { - return fmt.Sprintf(`INSERT INTO %s (name) VALUES (?)`, table.Name) +func (s *SDatabase) Insert(table Table, names []string) { + *s.queries = append(*s.queries, fmt.Sprintf(`INSERT INTO %s (name) VALUES (?)`, table.Name)) } // Update updates a record in the table -func (s *SDatabase) Update(table Table, id int, newName string) string { - return fmt.Sprintf(`UPDATE %s SET name = ? WHERE id = ?`, table.Name) +func (s *SDatabase) Update(table Table, id int, newName string) { + *s.queries = append(*s.queries, fmt.Sprintf(`UPDATE %s SET name = ? WHERE id = ?`, table.Name)) } // Select retrieves a record from the table @@ -172,25 +174,26 @@ func (s *SDatabase) Select(table Table, name string) (string, error) { } // Delete deletes a record from the table -func (s *SDatabase) Delete(table Table, id int) string { - return fmt.Sprintf(`DELETE FROM %s WHERE id = ?`, table.Name) +func (s *SDatabase) Delete(table Table, id int) { + *s.queries = append(*s.queries, fmt.Sprintf(`DELETE FROM %s WHERE id = ?`, table.Name)) } // DropTable drops the table from the database -func (s *SDatabase) DropTable(table Table) string { - return fmt.Sprintf(`DROP TABLE IF EXISTS %s`, table.Name) +func (s *SDatabase) DropTable(table Table) { + *s.queries = append(*s.queries, fmt.Sprintf(`DROP TABLE IF EXISTS %s`, table.Name)) } // Apply all the SQL scripts that are in a string array that are combined into one SQL script -func (s *SDatabase) Apply(sqlQueries *[]string) error { +func (s *SDatabase) Apply() error { if !s.IsInitialized() { return fmt.Errorf(ErrorDatabaseNotInitialized) } - stmt, err := db.Prepare(mergeSqlQueries(sqlQueries)) + stmt, err := db.Prepare(mergeSqlQueries(s.queries)) if err != nil { return fmt.Errorf("error preparing insert statement: %v", err) } + defer func(stmt *sql.Stmt) { err := stmt.Close() if err != nil { diff --git a/cache/database/database_integration_test.go b/cache/database/database_integration_test.go new file mode 100644 index 0000000..a853bb5 --- /dev/null +++ b/cache/database/database_integration_test.go @@ -0,0 +1,60 @@ +package database + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_Integration_Initialize(t *testing.T) { + databaseService := NewDatabaseService() + err := databaseService.Initialize() + assert.NoError(t, err) + + isInitialized := databaseService.IsInitialized() + assert.True(t, isInitialized) + + err = databaseService.Close() + assert.NoError(t, err) +} + +func Test_Integration_IsInitialized(t *testing.T) {} + +func Test_Integration_CreateTable(t *testing.T) { + databaseService := NewDatabaseService() + err := databaseService.Initialize() + assert.NoError(t, err) + + err = databaseService.CreateTable(Table{Name: "test"}) + assert.NoError(t, err) + + err = databaseService.Close() + assert.NoError(t, err) +} + +func Test_Integration_Insert(t *testing.T) { + databaseService := NewDatabaseService() + err := databaseService.Initialize() + assert.NoError(t, err) + + err = databaseService.Insert(Table{Name: "test"}, []string{"Joe", "Jack"}) + assert.NoError(t, err) + + err = databaseService.Close() + assert.NoError(t, err) +} + +func Test_Integration_Select(t *testing.T) {} + +func Test_Integration_Delete(t *testing.T) {} + +func Test_Integration_DropTable(t *testing.T) { + databaseService := NewDatabaseService() + err := databaseService.Initialize() + assert.NoError(t, err) + + err = databaseService.DropTable(Table{Name: "test"}) + assert.NoError(t, err) + + err = databaseService.Close() + assert.NoError(t, err) +} diff --git a/cache/database/database_test.go b/cache/database/database_test.go index a0e46ff..486d69b 100644 --- a/cache/database/database_test.go +++ b/cache/database/database_test.go @@ -1,64 +1,17 @@ package database import ( - "github.com/stretchr/testify/assert" "testing" ) -func TestInitialize(t *testing.T) { - databaseService := NewDatabaseService() - err := databaseService.Initialize() - assert.NoError(t, err) - - isInitialized := databaseService.IsInitialized() - assert.True(t, isInitialized) - - err = databaseService.Close() - assert.NoError(t, err) -} - -func TestClose(t *testing.T) {} - -func TestIsInitialized(t *testing.T) {} - func TestCreateTable(t *testing.T) { - databaseService := NewDatabaseService() - err := databaseService.Initialize() - assert.NoError(t, err) - - err = databaseService.CreateTable(Table{Name: "test"}) - assert.NoError(t, err) - - err = databaseService.Close() - assert.NoError(t, err) -} - -func TestInsert(t *testing.T) { - databaseService := NewDatabaseService() - err := databaseService.Initialize() - assert.NoError(t, err) - err = databaseService.Insert(Table{Name: "test"}, []string{"Joe", "Jack"}) - assert.NoError(t, err) - - err = databaseService.Close() - assert.NoError(t, err) } -func TestDelete(t *testing.T) {} +func TestInsert(t *testing.T) {} func TestUpdate(t *testing.T) {} -func TestSelect(t *testing.T) {} - -func TestDropTable(t *testing.T) { - databaseService := NewDatabaseService() - err := databaseService.Initialize() - assert.NoError(t, err) - - err = databaseService.DropTable(Table{Name: "test"}) - assert.NoError(t, err) +func TestDelete(t *testing.T) {} - err = databaseService.Close() - assert.NoError(t, err) -} +func TestDropTable(t *testing.T) {} diff --git a/cache/database/types.go b/cache/database/types.go index a4eb565..adc32fd 100644 --- a/cache/database/types.go +++ b/cache/database/types.go @@ -68,7 +68,6 @@ type Column struct { ColumnName string DataType DataType Constraints []Constraint - Default string } // Relationships so to create relationships diff --git a/cache/database/util.go b/cache/database/util.go index 2616da6..4e1cc02 100644 --- a/cache/database/util.go +++ b/cache/database/util.go @@ -10,7 +10,18 @@ func mergeSqlQueries(sqlQueries *[]string) string { return strings.Replace(strings.Join(*sqlQueries, ";\n")+";", ";;", ";", -1) } -func (c Constraint) ToSQL(columnName string) string { +// ToSQL for Column +func (col Column) ToSQL() string { + var constraints []string + for _, constraint := range col.Constraints { + constraints = append(constraints, constraint.ToSQL()) + } + + return fmt.Sprintf("%s %s %s", col.ColumnName, col.DataType, strings.Join(constraints, " ")) +} + +// ToSQL for Constraint +func (c Constraint) ToSQL() string { switch c.Type { case ConstraintNotNull: return "NOT NULL" @@ -28,7 +39,7 @@ func (c Constraint) ToSQL(columnName string) string { } case ConstraintForeignKey: if c.References != "" { - return fmt.Sprintf("FOREIGN KEY (%s)", c.References) + return fmt.Sprintf("FOREIGN KEY REFERENCES %s", c.References) } case ConstraintAutoincrement: return "AUTOINCREMENT"