()
| 38 | } |
| 39 | |
| 40 | func (this *SQLiteDB) Init() error { |
| 41 | // 检查目录是否存在 |
| 42 | var dir = filepath.Dir(this.path) |
| 43 | |
| 44 | _, err := os.Stat(dir) |
| 45 | if err != nil { |
| 46 | err = os.MkdirAll(dir, 0777) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | remotelogs.Println("DB", "create database dir '"+dir+"'") |
| 51 | } |
| 52 | |
| 53 | // TODO 思考 data.db 的数据安全性 |
| 54 | db, err := dbs.OpenWriter("file:" + this.path + "?cache=shared&mode=rwc&_journal_mode=WAL&_locking_mode=EXCLUSIVE") |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | db.SetMaxOpenConns(1) |
| 59 | |
| 60 | /**_, err = db.Exec("VACUUM") |
| 61 | if err != nil { |
| 62 | return err |
| 63 | }**/ |
| 64 | |
| 65 | _, err = db.Exec(`CREATE TABLE IF NOT EXISTS "` + tableAgentIPs + `" ( |
| 66 | "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, |
| 67 | "ip" varchar(64), |
| 68 | "agentCode" varchar(128) |
| 69 | );`) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | // 预编译语句 |
| 75 | |
| 76 | // agent ip record statements |
| 77 | this.insertAgentIPStmt, err = db.Prepare(`INSERT INTO "` + tableAgentIPs + `" ("id", "ip", "agentCode") VALUES (?, ?, ?)`) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | this.listAgentIPsStmt, err = db.Prepare(`SELECT "id", "ip", "agentCode" FROM "` + tableAgentIPs + `" ORDER BY "id" ASC LIMIT ? OFFSET ?`) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | this.db = db |
| 88 | |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (this *SQLiteDB) InsertAgentIP(ipId int64, ip string, agentCode string) error { |
| 93 | if this.db == nil { |
nothing calls this directly
no test coverage detected