/
lasting.go
105 lines (90 loc) · 2.36 KB
/
lasting.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
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
// Copyright 2014 mint.zhao.chiu@gmail.com. All rights reserved.
// Use of this source code is governed by a Apache License 2.0
// that can be found in the LICENSE file.
package goAcq
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
."goAcq/conf"
"log"
"path/filepath"
"time"
"os"
)
var (
Engine *xorm.Engine
)
// lasting for AcqNodes
type TblAcqNode struct {
Id int64
NodeName string `xorm:"unique"`
NodeEncode string
MatchMode int32
// targetUrlMatch `xorm:"extends"`
AcqCnt int32
Created time.Time `xorm:"created"`
LastAcq time.Time `xomr:"updated"`
}
// lasting for lists
type TblTargetList struct {
Id int64
NodeId int64
ListUrl string
AcqCnt int32
Created time.Time `xorm:"created"`
LastAcq time.Time `xorm:"updated"`
}
// lasting for targets
type TblTarget struct {
Id int64
NodeId int64
ListId int64
TargetUrl string
Keyword string
Summary string
Title string
From string
PostTime string
Content string
SavePath string
AcqCnt int32
Created time.Time `xorm:"created"`
LastAcq time.Time `xorm:"updated"`
}
func init() {
var err error
Engine, err = setDbEngine()
if err != nil {
log.Fatalf("create database connection return error: %v", err)
}
// sync table
if err := Engine.Sync(new(TblAcqNode), new(TblTargetList), new(TblTarget)); err != nil {
log.Fatalf("sync database's table return error: %v", err)
}
}
func setDbEngine() (*xorm.Engine, error) {
engine, err := xConnDb()
if engine == nil || err != nil {
return nil, err
}
engine.SetMapper(&core.SameMapper{})
engine.ShowSQL = true
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
engine.SetDefaultCacher(cacher)
return engine, nil
}
func xConnDb() (*xorm.Engine, error) {
switch DbType {
case "sqlite":
return xorm.NewEngine("sqlite3", filepath.Join(os.Getenv("GOPATH"), "src", "goAcq", "data", fmt.Sprintf("%s.db", DbName)))
case "mysql":
return xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%v)/%s?charset=%v", DbUser, DbPwd, DbHost, DbName, DbCharSet))
case "pgsql":
return xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", DbUser, DbPwd, DbName))
}
return nil, fmt.Errorf("UnSopported database type: %v", DbType)
}