Skip to content

Commit

Permalink
rollback shortit.go
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Sep 26, 2012
1 parent cc209c8 commit d9e9da6
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions golang/shortit.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"database/sql"
"database/sql/driver"
"flag"
"fmt"
_ "github.com/mattn/go-sqlite3"
Expand All @@ -19,7 +18,6 @@ var (
dbType = flag.String("dbType", "sqlite3", "db type")
dbUrl = flag.String("dbUrl", "sdata.db", "db url")
base = flag.String("base", "../WebContent", "static file baseDir")
driver driver.Driver
)

var sdataPatten, _ = regexp.Compile("^/[1234567890abcdef]+$")
Expand All @@ -44,7 +42,7 @@ func main() {
log.Fatal(e)
}
}
driver = db.Driver()
db.Close()

http.HandleFunc("/api/create", Create)
http.HandleFunc("/api/create/bin", CreateBin)
Expand Down Expand Up @@ -94,9 +92,9 @@ func Create(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "{'ok':true, 'code': '%s'}", strconv.FormatInt(id, 16))
return
}
conn, _ := driver.Open(*dbUrl)
defer conn.Close()
res, err := conn.Exec("insert into t_sdata (data,tp) values(?,?)", data, _type)
db, _ := sql.Open(*dbType, *dbUrl)
defer db.Close()
res, err := db.Exec("insert into t_sdata (data,tp) values(?,?)", data, _type)
if err != nil {
log.Print(err)
id = queryByDataAndType(data, _type)
Expand All @@ -115,18 +113,18 @@ func Query(w http.ResponseWriter, r *http.Request) {
}

func queryById(id int64) (data string, _type int) {
conn, _ := driver.Open(*dbUrl)
defer conn.Close()
row := conn.QueryRow("select data,tp from t_sdata where id=?", id)
db, _ := sql.Open(*dbType, *dbUrl)
defer db.Close()
row := db.QueryRow("select data,tp from t_sdata where id=?", id)

row.Scan(&data, &_type)
return
}

func queryByDataAndType(data string, _type int) (id int64) {
conn, _ := driver.Open(*dbUrl)
defer conn.Close()
row := conn.QueryRow("select id from t_sdata where data=? and tp=?", data, _type)
db, _ := sql.Open(*dbType, *dbUrl)
defer db.Close()
row := db.QueryRow("select id from t_sdata where data=? and tp=?", data, _type)

if row.Scan(&id) != nil {
id = -1
Expand Down

0 comments on commit d9e9da6

Please sign in to comment.