-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathpostgres.go
More file actions
192 lines (151 loc) · 3.83 KB
/
Copy pathpostgres.go
File metadata and controls
192 lines (151 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"database/sql"
"fmt"
"log"
"time"
"github.com/lib/pq"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "example"
dbname = "pq-demo"
)
type Book struct {
ID int
Name string
Author string
PublicationDate time.Time
Pages int
}
// Create the books table in the database
func createTable() {
query := `
CREATE TABLE books
(
id serial NOT NULL,
name character varying NOT NULL,
author character varying,
pages integer,
publication_date date,
CONSTRAINT pk_books PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE books
OWNER TO postgres;`
if _, err := db.Exec(query); err != nil {
log.Fatal(err)
}
}
// Drop the book table from the database
func dropTable() {
query := `DROP TABLE books;`
if _, err := db.Exec(query); err != nil {
log.Fatal(err)
}
}
// Get a specific book using the book id
func getBook(bookID int) (Book, error) {
res := Book{}
var id int
var name string
var author string
var pages int
var publicationDate pq.NullTime
err := db.QueryRow(`SELECT id, name, author, pages, publication_date FROM books where id = $1`, bookID).Scan(&id, &name, &author, &pages, &publicationDate)
if err == nil {
res = Book{ID: id, Name: name, Author: author, Pages: pages, PublicationDate: publicationDate.Time}
}
return res, err
}
// Get all books from the database
func getAllBooks() ([]Book, error) {
books := []Book{}
rows, err := db.Query(`SELECT id, name, author, pages, publication_date FROM books order by id`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var id int
var name string
var author string
var pages int
var publicationDate pq.NullTime
err = rows.Scan(&id, &name, &author, &pages, &publicationDate)
if err != nil {
return books, err
}
currentBook := Book{ID: id, Name: name, Author: author, Pages: pages}
if publicationDate.Valid {
currentBook.PublicationDate = publicationDate.Time
}
books = append(books, currentBook)
}
return books, err
}
// Insert a book into the database
func insertBook(name, author string, pages int, publicationDate time.Time) (int, error) {
var bookID int
err := db.QueryRow(`INSERT INTO books(name, author, pages, publication_date) VALUES($1, $2, $3, $4) RETURNING id`, name, author, pages, publicationDate).Scan(&bookID)
if err != nil {
return 0, err
}
fmt.Printf("Last inserted ID: %v\n", bookID)
return bookID, err
}
// Update a book in the database
func updateBook(id int, name, author string, pages int, publicationDate time.Time) (int, error) {
res, err := db.Exec(`UPDATE books set name=$1, author=$2, pages=$3, publication_date=$4 where id=$5 RETURNING id`, name, author, pages, publicationDate, id)
if err != nil {
return 0, err
}
rowsUpdated, err := res.RowsAffected()
if err != nil {
return 0, err
}
return int(rowsUpdated), err
}
// Remove a book from the database
func removeBook(bookID int) (int, error) {
res, err := db.Exec(`delete from books where id = $1`, bookID)
if err != nil {
return 0, err
}
rowsDeleted, err := res.RowsAffected()
if err != nil {
return 0, err
}
return int(rowsDeleted), nil
}
var db *sql.DB
func main() {
// Database connection string
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
var err error
// Connect to the database
db, err = sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
// Check the database connection
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
createTable()
insertBook("Hello World", "Author", 100, time.Now())
fmt.Println(getAllBooks())
updateBook(1, "Hello World 2!", "Author 2", 205, time.Now())
fmt.Println(getBook(1))
removeBook(1)
dropTable()
}