generated from Soypete/go-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instructions for how to implement a database abstraction. Add a template file for the db abstraction. Explain all the methods and some best practices. Signed-off-by: soypete <[email protected]>
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
// Connection struct holds the database connection | ||
type Connection struct { | ||
db *sql.DB | ||
} | ||
|
||
// User struct holds the user data | ||
type User struct { | ||
//TODO: add user fields | ||
} | ||
|
||
type Connector interface { | ||
QueryUser() (*User, error) | ||
UpdateUser() error | ||
AddUser() error | ||
} | ||
|
||
// Connect to database and return a Connection struct. | ||
// This return value needs to be a pointer because we are | ||
// using it to implement the Connector interface. | ||
func Connect() (*Connection, error) { | ||
fileName := "database/ex-1-connection/sqlite.db" | ||
db, err := sql.Open("sqlite3", fileName) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot open sqlite connection: %w", err) | ||
} | ||
if err = db.Ping(); err != nil { | ||
return nil, fmt.Errorf("cannot ping database: %w", err) | ||
} | ||
return &Connection{db: db}, nil | ||
} | ||
|
||
func (c *Connection) QueryUser() (*User, error) { | ||
// TODO: query database | ||
} | ||
|
||
func (c *Connection) UpdateUser() error { | ||
// TODO: update database | ||
} | ||
|
||
func (c *Connection) AddUser() error { | ||
// TODO: add user to database | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/soypete/WebServices-in-3-weeks/database/ex-2-abstraction/database" | ||
) | ||
|
||
func main() { | ||
|
||
// this will enforge the interface | ||
var db database.Connector | ||
db, err := database.Connect() | ||
if err != nil { | ||
log.Fatal("Error connecting to database") | ||
} | ||
|
||
// call your client methods here | ||
} |