-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
68 lines (53 loc) · 1.43 KB
/
db.go
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
package main
import (
"database/sql"
"time"
_ "github.com/mattn/go-sqlite3"
)
type URLData struct {
id int
long_url string
short_url string
timestamp string
}
var db *sql.DB
func initDB() error {
var err error
db, err = sql.Open("sqlite3", "urlshort.db")
return err
}
func closeDB() {
db.Close()
}
func insertURLToDatabase(long string, short string) (string, error) {
currentTime := time.Now().Format("2006-01-02 15:04:05")
_, err := db.Exec("INSERT INTO items (long_url, short_url, timestamp) VALUES (?, ?, ?)", long, short, currentTime)
return currentTime, err
}
func getLongURLFromDatabase(short_url string) (string, error) {
query := `SELECT long_url FROM items WHERE short_url = ?`
rows := db.QueryRow(query, short_url)
var original_url string
original_url_not_found_message := original_url + " not found in database"
err := rows.Scan(&original_url)
if err != nil {
if err == sql.ErrNoRows {
return original_url_not_found_message, err
}
return original_url_not_found_message, err
}
return original_url, err
}
func getURLInfoFromDatabase(short_url string) (URLData, error) {
query := `SELECT * FROM items WHERE short_url = ?`
rows := db.QueryRow(query, short_url)
var url_data URLData
err := rows.Scan(&url_data.id, &url_data.long_url, &url_data.short_url, &url_data.timestamp)
if err != nil {
if err == sql.ErrNoRows {
return url_data, err
}
return url_data, err
}
return url_data, err
}