Use existing links

This commit is contained in:
Mikhail Klementyev 2016-07-24 10:29:11 +03:00
parent 37ae632189
commit 623a3d0f3a
2 changed files with 20 additions and 5 deletions

11
main.go
View File

@ -28,7 +28,7 @@ import (
var ( var (
arg_url = kingpin.Flag("url", "Url").String() arg_url = kingpin.Flag("url", "Url").String()
arg_link = kingpin.Flag("link", "Link").Int() arg_link = kingpin.Flag("link", "Link").Int64()
) )
func cmd_url(db *sql.DB, url string) { func cmd_url(db *sql.DB, url string) {
@ -81,9 +81,12 @@ func cmd_url(db *sql.DB, url string) {
panic(err) panic(err)
} }
linkNo, err := storage.AddLink(db, url.String()) linkNo, err := storage.GetLinkID(db, url.String())
if err != nil { if err != nil {
panic(err) linkNo, err = storage.AddLink(db, url.String())
if err != nil {
panic(err)
}
} }
for _, s := range []string{string(value), html.EscapeString(string(value))} { for _, s := range []string{string(value), html.EscapeString(string(value))} {
@ -107,7 +110,7 @@ func cmd_url(db *sql.DB, url string) {
fmt.Println(text) fmt.Println(text)
} }
func cmd_link(db *sql.DB, linkID int) { func cmd_link(db *sql.DB, linkID int64) {
url, err := storage.GetLink(db, linkID) url, err := storage.GetLink(db, linkID)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -42,7 +42,7 @@ func AddLink(db *sql.DB, url string) (linkNo int64, err error) {
return return
} }
func GetLink(db *sql.DB, linkID int) (url string, err error) { func GetLink(db *sql.DB, linkID int64) (url string, err error) {
stmt, err := db.Prepare("SELECT `url` FROM `links` WHERE id=$1;") stmt, err := db.Prepare("SELECT `url` FROM `links` WHERE id=$1;")
if err != nil { if err != nil {
return return
@ -53,3 +53,15 @@ func GetLink(db *sql.DB, linkID int) (url string, err error) {
return return
} }
func GetLinkID(db *sql.DB, url string) (linkID int64, err error) {
stmt, err := db.Prepare("SELECT `id` FROM `links` WHERE url=$1;")
if err != nil {
return
}
defer stmt.Close()
err = stmt.QueryRow(url).Scan(&linkID)
return
}