Skip to content

Commit 796d945

Browse files
committed
more examples and a few tweaks
1 parent f295086 commit 796d945

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ sqltime.Truncate = time.Microsecond
3535
example
3636
----
3737

38-
see a full example [here](/example/postgres.go). The example assumes that the database is set
38+
see a full example [here](/example/). The example assumes that the database is set
3939
to the default timezone of `UTC` if not please update the `sqltime.DatabaseLocation`.
4040

4141

4242

43+
4344
Usage with with GORM
4445
---
4546

@@ -53,7 +54,7 @@ package Model
5354
import "github.com/SamuelTissot/sqltime"
5455

5556
// define the model
56-
type BaseModel struct {
57+
type Model struct {
5758
ID uint `gorm:"primary_key"`
5859
CreatedAt sqltime.Time `gorm:"type:timestamp"`
5960
UpdatedAt sqltime.Time `gorm:"type:timestamp"`

example/postgres.go renamed to example/gorm/gorm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type TestModel struct {
2222
}
2323

2424
func main() {
25+
// change the dataSourceName value
2526
db, err := gorm.Open("postgres", "host=127.0.0.1 port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
2627
if err != nil {
2728
panic(err)
@@ -66,5 +67,4 @@ func setup(db *gorm.DB) {
6667
deleted_at timestamp
6768
);
6869
`)
69-
7070
}

example/postgres/postgres.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"github.com/SamuelTissot/sqltime"
7+
_ "github.com/lib/pq"
8+
"reflect"
9+
)
10+
11+
type Memory struct {
12+
ID int64
13+
Moment sqltime.Time
14+
Content string
15+
}
16+
17+
func main() {
18+
// change the dataSourceName value
19+
db, err := sql.Open("postgres", "host=127.0.0.1 port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
20+
if err != nil {
21+
panic(err)
22+
}
23+
defer db.Close()
24+
25+
setup(db)
26+
27+
m := Memory{
28+
Moment: sqltime.Now(),
29+
Content: "When a beer is ravishing, the shabby Hops Alligator Ale throws a nuclear Heineken at a psychotic lager",
30+
}
31+
32+
query := `INSERT INTO memories (moment, content)
33+
VALUES ($1, $2)
34+
RETURNING id`
35+
36+
stmt, err := db.Prepare(query)
37+
defer stmt.Close()
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
err = stmt.QueryRow(m.Moment, m.Content).Scan(&m.ID)
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
var fetchMemory Memory
48+
err = db.QueryRow("SELECT id, moment, content FROM memories WHERE id = $1", m.ID).Scan(&fetchMemory.ID, &fetchMemory.Moment, &fetchMemory.Content)
49+
if err != nil {
50+
panic(err)
51+
}
52+
53+
if reflect.DeepEqual(m, fetchMemory) {
54+
fmt.Println("Wow equal")
55+
} else {
56+
fmt.Println("NOT EQUAL :/")
57+
fmt.Println(m)
58+
fmt.Println(fetchMemory)
59+
}
60+
}
61+
62+
func setup(db *sql.DB) {
63+
_, err := db.Exec(`
64+
CREATE TABLE IF NOT EXISTS memories
65+
(
66+
id SERIAL,
67+
moment timestamp,
68+
content varchar(255)
69+
);
70+
`)
71+
72+
if err != nil {
73+
panic(err)
74+
}
75+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/SamuelTissot/sqltime
22

33
go 1.13
44

5-
require github.com/jinzhu/gorm v1.9.11
5+
require (
6+
github.com/jinzhu/gorm v1.9.11
7+
github.com/lib/pq v1.1.1
8+
)

sqltime.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Time struct {
2828
time.Time
2929
}
3030

31+
// satisfy the sql.scanner interface
3132
func (t *Time) Scan(value interface{}) error {
3233
rt, ok := value.(time.Time)
3334
if !ok {
@@ -37,8 +38,9 @@ func (t *Time) Scan(value interface{}) error {
3738
return nil
3839
}
3940

41+
// satifies the driver.Value interface
4042
func (t Time) Value() (driver.Value, error) {
41-
return format(t.Time), nil
43+
return format(t.Time), nil //format just in case
4244
}
4345

4446
// Now wrapper around the time.Now() function
@@ -51,6 +53,7 @@ func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.L
5153
return Time{format(time.Date(year, month, day, hour, min, sec, nsec, loc))}
5254
}
5355

56+
// insure the correct format
5457
func format(t time.Time) time.Time {
5558
return t.In(DatabaseLocation).Truncate(TruncateOff)
5659
}

0 commit comments

Comments
 (0)