-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrawl_record_sql.go
57 lines (48 loc) · 1.55 KB
/
crawl_record_sql.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
package main
import(
"log"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type sqlite_row struct{
url string
}
type SqliteRecorder struct{
conn chan *sql.DB
}
var ConnectionPool chan *sql.DB
func CreateSqliteRecorder(location string) SqliteRecorder{
conn, err := sql.Open("sqlite3", location)
if err != nil {
log.Fatal("Sqlite connection fail!\nFile: ", SQLITE_FILE)
}
_, err = conn.Exec("create table if not exists download_record(url varchar(512) primary key) ")
if err != nil{
log.Fatal("Sqlite Recorder Table download_record does not exists and cannot be created")
}
ConnectionPool = make(chan *sql.DB, 1)
ConnectionPool <- conn
log.Println("Sqlite Recorder Connected : ", location)
return SqliteRecorder{conn: ConnectionPool}
}
func (self *SqliteRecorder) Execute(sql string, params ...interface{}) (sql.Result, error){
conn := <- self.conn
defer func(){self.conn <- conn}()
return conn.Exec(sql, params...)
}
func (self *SqliteRecorder) QueryRow(sql string, params ...interface{}) *sql.Row{
conn := <- self.conn
defer func(){self.conn <- conn}()
return conn.QueryRow(sql, params...)
}
func (self *SqliteRecorder) MarkAsFinished(url string){
self.Execute("insert into download_record values(?)", url)
}
func (self *SqliteRecorder) HasFinished(url string) bool{
row := self.QueryRow("select url from download_record where url=?", url)
sqlite_record := sqlite_row{}
if err:=row.Scan(&sqlite_record.url); err!=nil{
return false
}
return true
}