Skip to content

Commit

Permalink
Continue restructuing and added some mocks. Started to work on cachin…
Browse files Browse the repository at this point in the history
…g component.
  • Loading branch information
jnbdz committed Nov 16, 2024
1 parent 479804b commit 7dcc698
Show file tree
Hide file tree
Showing 24 changed files with 1,081 additions and 360 deletions.
28 changes: 28 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
with-expecter: True
packages:
github.com/AmadlaOrg/hery/cache:
interfaces:
ICache:
config:
dir: "{{.InterfaceDir}}"
mockName: "MockCache"
inpackage: True
github.com/AmadlaOrg/hery/cache/database:
interfaces:
IDatabase:
config:
dir: "{{.InterfaceDir}}"
mockName: "MockCacheDatabase"
inpackage: True
github.com/AmadlaOrg/hery/cache/parser:
interfaces:
IParser:
config:
dir: "{{.InterfaceDir}}"
mockName: "MockCacheParser"
inpackage: True
github.com/AmadlaOrg/hery/client:
interfaces:
IClient:
config:
dir: "{{.InterfaceDir}}"
mockName: "MockClient"
inpackage: True
github.com/AmadlaOrg/hery/collection:
interfaces:
ICollection:
Expand Down
74 changes: 0 additions & 74 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package cache

import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
)

// ICache
Expand All @@ -23,86 +20,15 @@ type SCache struct{}

// Create
func (s *SCache) Create() {
// Open an in-memory SQLite database
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
fmt.Println("Error opening database:", err)
return
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Error closing database connection: %v", err)
}
}(db)

// Create a table
createTableSQL := `CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`
_, err = db.Exec(createTableSQL)
if err != nil {
fmt.Println("Error creating table:", err)
return
}

// Create an index on the name column
createIndexSQL := `CREATE INDEX idx_name ON users(name);`
_, err = db.Exec(createIndexSQL)
if err != nil {
fmt.Println("Error creating index:", err)
return
}
}

// Insert
func (s *SCache) Insert() {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Unable to close database: %v", err)
}
}(db)

// Insert some records
insertUserSQL := `INSERT INTO users (name) VALUES (?)`
for _, name := range []string{"Alice", "Bob", "Charlie"} {
_, err = db.Exec(insertUserSQL, name)
if err != nil {
log.Fatalf("Error inserting record: %v", err)
return
}
}
}

// Select
func (s *SCache) Select() {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatalf("Error opening database: %v", err)
}

defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Unable to close database: %v", err)
}
}(db)

// Query the record using the index
var name string
querySQL := `SELECT name FROM users WHERE name = 'Alice'`
err = db.QueryRow(querySQL).Scan(&name)
if err != nil {
log.Fatalf("Error querying record: %v", err)
return
}

// Print the result
fmt.Println("User:", name)
}
121 changes: 121 additions & 0 deletions cache/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package database

import (
"database/sql"
"fmt"
"log"
)

// IDatabase
type IDatabase interface {
CreateTable(table Table) error
Insert(table Table) error
Update(table Table) error
Select(table Table) (string, error)
Delete(table Table) error
DropTable(table Table) error
}

// SDatabase
type SDatabase struct{}

var (
db *sql.DB
)

func init() {
conn, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatal(fmt.Errorf("error opening database: %v", err))
}
db = conn
}

// TODO: journal mode: wal
// This will help with performance
// https://stackoverflow.com/questions/57118674/go-sqlite3-with-journal-mode-wal-gives-database-is-locked-error

// Create
func (s *SDatabase) CreateTable(table Table) error {
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Error closing database connection: %v", err)
}
}(db)

// Create a table
createTableSQL := fmt.Sprintf(`CREATE TABLE %s (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`, table.Name)

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

// Create an index on the name column
createIndexSQL := `CREATE INDEX idx_name ON users(name);`
_, err = db.Exec(createIndexSQL)
if err != nil {
return fmt.Errorf("error creating index: %v", err)
}

return nil
}

// Insert
func (s *SDatabase) Insert(table Table) error {
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Unable to close database: %v", err)
}
}(db)

// Insert some records
insertUserSQL := `INSERT INTO users (name) VALUES (?)`
for _, name := range []string{"Alice", "Bob", "Charlie"} {
_, err := db.Exec(insertUserSQL, name)
if err != nil {
return fmt.Errorf("error inserting record: %v", err)
}
}

return nil
}

func (s *SDatabase) Update(table Table) error {
return nil
}

// Select
func (s *SDatabase) Select(table Table) (string, error) {
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("Unable to close database: %v", err)
}
}(db)

// Query the record using the index
var name string
querySQL := `SELECT name FROM users WHERE name = 'Alice'`
err := db.QueryRow(querySQL).Scan(&name)
if err != nil {
return "", fmt.Errorf("error querying record: %v", err)
}

return name, nil
}

func (s *SDatabase) Delete(table Table) error {

return nil
}

func (s *SDatabase) DropTable(table Table) error {

return nil
}
1 change: 1 addition & 0 deletions cache/database/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package database
Loading

0 comments on commit 7dcc698

Please sign in to comment.