Implement an in-memory databasewqa

This commit is contained in:
2021-06-19 17:47:36 -04:00
parent 9af3f78659
commit b66f5ebe67
14 changed files with 931 additions and 1 deletions
+43
View File
@@ -0,0 +1,43 @@
package ramdb
type database struct {
tables map[string]*table
}
// NewDatabase initializes a new database with no tables.
func NewDatabase() *database {
return &database{
tables: make(map[string]*table),
}
}
// From selects a table for running commands.
func (db *database) From(tablename string) *table {
t, ok := db.tables[tablename]
if !ok {
return &table{}
}
return t
}
// CreateTable creates a new table in the database with indexes for each column specified.
func (db *database) CreateTable(tablename string, indexOnColumns ...string) error {
if _, found := db.tables[tablename]; found {
return ErrTableExists
}
tbl := &table{
exists: true,
}
for _, onColumn := range indexOnColumns {
err := tbl.CreateIndex(onColumn)
if err != nil {
return err
}
}
db.tables[tablename] = tbl
return nil
}