Added plan of tests
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type Student struct {
|
||||
ID int
|
||||
Fname string
|
||||
Lname string
|
||||
DateOfBirth time.Time
|
||||
Email string
|
||||
Address string
|
||||
Gender string
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
db, err = dbSqlConnect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
s := Student{
|
||||
Fname: "Leon",
|
||||
Lname: "Ashling",
|
||||
DateOfBirth: time.Date(1994, time.August, 14, 23, 51, 42, 0, time.UTC),
|
||||
Email: "lashling5@senate.gov",
|
||||
Address: "39 Kipling Pass",
|
||||
Gender: "Male",
|
||||
}
|
||||
|
||||
//adding student record to table
|
||||
sID, err := addStudent(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Printf("addSudent id: %v \n", sID)
|
||||
|
||||
//selecting student by ID
|
||||
st, err := studentByID(sID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Printf("studentByID id: %v \n", st)
|
||||
|
||||
students, err := fetchStudents()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
fmt.Printf("fetchStudents count: %v \n", len(students))
|
||||
}
|
||||
|
||||
func dbSqlConnect() (*sql.DB, error) {
|
||||
// Opening a database connection.
|
||||
db, err := sql.Open("mysql", "theuser:thepass@tcp(localhost:3306)/thedb?parseTime=true")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("Connected!")
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func addStudent(s Student) (int64, error) {
|
||||
query := "insert into students (fname, lname, date_of_birth, email, gender, address) values (?, ?, ?, ?, ?, ?);"
|
||||
result, err := db.Exec(query, s.Fname, s.Lname, s.DateOfBirth, s.Email, s.Gender, s.Address)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("addStudent Error: %v", err)
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("addSudent Error: %v", err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func fetchStudents() ([]Student, error) {
|
||||
// A slice of Students to hold data from returned rows.
|
||||
var students []Student
|
||||
|
||||
rows, err := db.Query("SELECT * FROM students")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetchStudents %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// Loop through rows, using Scan to assign column data to struct fields.
|
||||
for rows.Next() {
|
||||
var s Student
|
||||
if err := rows.Scan(&s.ID, &s.Fname, &s.Lname, &s.DateOfBirth, &s.Email, &s.Address, &s.Gender); err != nil {
|
||||
return nil, fmt.Errorf("fetchStudents %v", err)
|
||||
}
|
||||
students = append(students, s)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("fetchStudents %v", err)
|
||||
}
|
||||
|
||||
return students, nil
|
||||
}
|
||||
|
||||
func studentByID(id int64) (Student, error) {
|
||||
var st Student
|
||||
|
||||
row := db.QueryRow("SELECT * FROM students WHERE id = ?", id)
|
||||
if err := row.Scan(&st.ID, &st.Fname, &st.Lname, &st.DateOfBirth, &st.Email, &st.Address, &st.Gender); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return st, fmt.Errorf("studentById %d: no such student", id)
|
||||
}
|
||||
return st, fmt.Errorf("studentById %d: %v", id, err)
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Student struct {
|
||||
ID int
|
||||
Fname string
|
||||
Lname string
|
||||
DateOfBirth time.Time
|
||||
Email string
|
||||
Address string
|
||||
Gender string
|
||||
}
|
||||
|
||||
func main() {
|
||||
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
|
||||
db, err := gorm.Open(mysql.Open("theuser:thepass@tcp(127.0.0.1:3306)/thedb?charset=utf8mb4&parseTime=True&loc=Local"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Connected!")
|
||||
|
||||
//initializing record to be inserted
|
||||
s := Student{
|
||||
Fname: "Leon",
|
||||
Lname: "Ashling",
|
||||
DateOfBirth: time.Date(1994, time.August, 14, 23, 51, 42, 0, time.UTC),
|
||||
Email: "lashling5@senate.gov",
|
||||
Address: "39 Kipling Pass",
|
||||
Gender: "Male",
|
||||
}
|
||||
//adds student record and returns the ID into the ID field
|
||||
db.Create(&s)
|
||||
fmt.Printf("addSudent id: %v \n", s.ID)
|
||||
|
||||
//selecting multiple record
|
||||
var students []Student
|
||||
db.Limit(10).Find(&students)
|
||||
fmt.Printf("fetchStudents count: %v \n", len(students))
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
DROP TABLE IF EXISTS students;
|
||||
CREATE TABLE `students` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`fname` varchar(50) not null,
|
||||
`lname` varchar(50) not null,
|
||||
`date_of_birth` datetime not null,
|
||||
`email` varchar(50),
|
||||
`address` varchar(50),
|
||||
`gender` varchar(50),
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (1, 'Caddric', 'Likely', '2000-07-06 02:43:37', 'clikely0@wp.com', 'Male', '9173 Boyd Street');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (2, 'Jerad', 'Ciccotti', '1993-02-11 15:59:56', 'jciccotti1@bravesites.com', 'Male', '34 Declaration Drive');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (3, 'Hillier', 'Caslett', '1992-09-04 13:38:46', 'hcaslett2@hostgator.com', 'Male', '36 Duke Trail');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (4, 'Bertine', 'Roddan', '1991-02-18 09:10:05', 'broddan3@independent.co.uk', 'Female', '2896 Kropf Road');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (5, 'Theda', 'Brockton', '1991-10-29 09:08:48', 'tbrockton4@lycos.com', 'Female', '93 Hermina Plaza');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (6, 'Leon', 'Ashling', '1994-08-14 23:51:42', 'lashling5@senate.gov', 'Male', '39 Kipling Pass');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (7, 'Aldo', 'Pettitt', '1994-08-14 22:03:40', 'apettitt6@hexun.com', 'Male', '38 Dryden Road');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (8, 'Filmore', 'Cordingly', '1999-11-20 02:35:48', 'fcordingly7@163.com', 'Male', '34 Pawling Park');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (9, 'Katalin', 'MacCroary', '1994-11-08 11:59:19', 'kmaccroary8@cargocollective.com', 'Female', '2540 Maryland Parkway');
|
||||
insert into students (id, fname, lname, date_of_birth, email, gender, address) values (10, 'Franky', 'Puddan', '1995-04-23 17:07:29', 'fpuddan9@psu.edu', 'Female', '3214 Washington Road');
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.17.2
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.17.2
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Student struct {
|
||||
ID int64
|
||||
Fname string
|
||||
Lname string
|
||||
DateOfBirth time.Time
|
||||
Email string
|
||||
Address string
|
||||
Gender string
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
-- name: addStudent :execlastid
|
||||
insert into students (fname, lname, date_of_birth, email, gender, address) values (?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: studentByID :one
|
||||
SELECT * FROM students WHERE id = ?;
|
||||
|
||||
-- name: fetchStudents :many
|
||||
SELECT * FROM students LIMIT 10;
|
||||
@@ -0,0 +1,93 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.17.2
|
||||
// source: query.sql
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const addStudent = `-- name: addStudent :execlastid
|
||||
insert into students (fname, lname, date_of_birth, email, gender, address) values (?, ?, ?, ?, ?, ?)
|
||||
`
|
||||
|
||||
type addStudentParams struct {
|
||||
Fname string
|
||||
Lname string
|
||||
DateOfBirth time.Time
|
||||
Email string
|
||||
Gender string
|
||||
Address string
|
||||
}
|
||||
|
||||
func (q *Queries) addStudent(ctx context.Context, arg addStudentParams) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, addStudent,
|
||||
arg.Fname,
|
||||
arg.Lname,
|
||||
arg.DateOfBirth,
|
||||
arg.Email,
|
||||
arg.Gender,
|
||||
arg.Address,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.LastInsertId()
|
||||
}
|
||||
|
||||
const fetchStudents = `-- name: fetchStudents :many
|
||||
SELECT id, fname, lname, date_of_birth, email, address, gender FROM students LIMIT 10
|
||||
`
|
||||
|
||||
func (q *Queries) fetchStudents(ctx context.Context) ([]Student, error) {
|
||||
rows, err := q.db.QueryContext(ctx, fetchStudents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Student
|
||||
for rows.Next() {
|
||||
var i Student
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Fname,
|
||||
&i.Lname,
|
||||
&i.DateOfBirth,
|
||||
&i.Email,
|
||||
&i.Address,
|
||||
&i.Gender,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const studentByID = `-- name: studentByID :one
|
||||
SELECT id, fname, lname, date_of_birth, email, address, gender FROM students WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) studentByID(ctx context.Context, id int64) (Student, error) {
|
||||
row := q.db.QueryRowContext(ctx, studentByID, id)
|
||||
var i Student
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Fname,
|
||||
&i.Lname,
|
||||
&i.DateOfBirth,
|
||||
&i.Email,
|
||||
&i.Address,
|
||||
&i.Gender,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE `students` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||||
`fname` varchar(50) not null,
|
||||
`lname` varchar(50) not null,
|
||||
`date_of_birth` datetime not null,
|
||||
`email` varchar(50) not null,
|
||||
`address` varchar(50) not null,
|
||||
`gender` varchar(50) not null,
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
||||
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conn, err := sql.Open("mysql", "theuser:thepass@tcp(localhost:3306)/thedb?parseTime=true")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Connected!")
|
||||
|
||||
db := New(conn)
|
||||
|
||||
//initializing record to be inserted
|
||||
newSt := addStudentParams{
|
||||
Fname: "Leon",
|
||||
Lname: "Ashling",
|
||||
DateOfBirth: time.Date(1994, time.August, 14, 23, 51, 42, 0, time.UTC),
|
||||
Email: "lashling5@senate.gov",
|
||||
Gender: "Male",
|
||||
Address: "39 Kipling Pass",
|
||||
}
|
||||
// inserting the record
|
||||
sID, err := db.addStudent(context.Background(), newSt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("addSudent id: %v \n", sID)
|
||||
|
||||
//retreive record by id
|
||||
st, err := db.studentByID(context.Background(), sID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
fmt.Printf("studentByID record: %v \n", st)
|
||||
|
||||
//fetching multiple records
|
||||
students, err := db.fetchStudents(context.Background())
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
fmt.Printf("fetchStudents count: %v \n", len(students))
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
version: 1
|
||||
packages:
|
||||
- path: "./"
|
||||
name: "main"
|
||||
engine: "mysql"
|
||||
schema: "schema.sql"
|
||||
queries: "query.sql"
|
||||
@@ -0,0 +1,114 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Student struct {
|
||||
ID int
|
||||
Fname string
|
||||
Lname string
|
||||
DateOfBirth time.Time `db:"date_of_birth"`
|
||||
Email string
|
||||
Address string
|
||||
Gender string
|
||||
}
|
||||
|
||||
var db *sqlx.DB
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
db, err = sqlxConnect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
s := Student{
|
||||
Fname: "Leon",
|
||||
Lname: "Ashling",
|
||||
DateOfBirth: time.Date(1994, time.August, 14, 23, 51, 42, 0, time.UTC),
|
||||
Email: "lashling5@senate.gov",
|
||||
Address: "39 Kipling Pass",
|
||||
Gender: "Male",
|
||||
}
|
||||
|
||||
//adding student record to table
|
||||
sID, err := addStudent(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Printf("addStudent id: %v \n", sID)
|
||||
|
||||
//selecting student by ID
|
||||
st, err := studentByID(sID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Printf("studentByID record: %v \n", st)
|
||||
|
||||
students, err := fetchStudents()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
fmt.Printf("fetchStudents count: %v \n", len(students))
|
||||
}
|
||||
|
||||
func sqlxConnect() (*sqlx.DB, error) {
|
||||
// Opening a database connection.
|
||||
db, err := sqlx.Open("mysql", "theuser:thepass@tcp(localhost:3306)/thedb?parseTime=true")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("Connected!")
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func addStudent(s Student) (int64, error) {
|
||||
query := "insert into students (fname, lname, date_of_birth, email, gender, address) values (?, ?, ?, ?, ?, ?);"
|
||||
result := db.MustExec(query, s.Fname, s.Lname, s.DateOfBirth, s.Email, s.Gender, s.Address)
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("addSudent Error: %v", err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func fetchStudents() ([]Student, error) {
|
||||
// A slice of Students to hold data from returned rows.
|
||||
var students []Student
|
||||
|
||||
err := db.Select(&students, "SELECT * FROM students LIMIT 10")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetchStudents %v", err)
|
||||
}
|
||||
|
||||
return students, nil
|
||||
}
|
||||
|
||||
func studentByID(id int64) (Student, error) {
|
||||
var st Student
|
||||
|
||||
//if err := db.QueryRowx("SELECT * FROM students WHERE id = ?", id).StructScan(&st); err != nil {
|
||||
// if err == sql.ErrNoRows {
|
||||
// return st, fmt.Errorf("studentById %d: no such student", id)
|
||||
// }
|
||||
// return st, fmt.Errorf("studentById %d: %v", id, err)
|
||||
//}
|
||||
|
||||
if err := db.Get(&st, "SELECT * FROM students WHERE id = ?", id); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return st, fmt.Errorf("studentById %d: no such student", id)
|
||||
}
|
||||
return st, fmt.Errorf("studentById %d: %v", id, err)
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
Reference in New Issue
Block a user