-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilSQLite.go
179 lines (156 loc) · 5 KB
/
utilSQLite.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2025 minRAG Authors.
//
// This file is part of minRAG.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses>.
package main
import (
"bytes"
"context"
"database/sql"
"encoding/binary"
"fmt"
"os"
"gitee.com/chunanyong/zorm"
// 00.引入数据库驱动
"github.com/mattn/go-sqlite3"
)
var dbDao *zorm.DBDao
var dbDaoConfig = zorm.DataSourceConfig{
DSN: sqliteDBfile,
DriverName: "sqlite3_simple", // 使用simple分词器会注册这个驱动名
Dialect: "sqlite",
MaxOpenConns: 1,
MaxIdleConns: 1,
ConnMaxLifetimeSecond: 600,
SlowSQLMillis: -1,
}
// checkSQLiteStatus 初始化sqlite数据库,并检查是否成功
func checkSQLiteStatus() bool {
const failSuffix = ".fail"
if failDB := datadir + "minrag.db" + failSuffix; pathExist(failDB) {
FuncLogError(nil, fmt.Errorf(funcT("Please confirm if [%s] needs to be manually renamed to [minrag.db]. If not, please manually delete [%s]"), failDB, failDB))
return false
}
fts5File := datadir + "extensions/libsimple"
vecFile := datadir + "extensions/vec0"
//注册fts5的simple分词器,建议使用jieba分词
//需要 --tags "fts5"
sql.Register("sqlite3_simple", &sqlite3.SQLiteDriver{
Extensions: []string{
fts5File, //不要加后缀,它会自己处理,这样代码也统一
vecFile,
},
})
var err error
dbDao, err = zorm.NewDBDao(&dbDaoConfig)
if dbDao == nil || err != nil { //数据库初始化失败
if db := datadir + "minrag.db"; pathExist(db) {
_ = os.Rename(db, db+failSuffix)
}
return false
}
//初始化结巴分词的字典
finder := zorm.NewFinder().Append("SELECT jieba_dict(?)", datadir+"dict")
fts5jieba := ""
_, err = zorm.QueryRow(context.Background(), finder, &fts5jieba)
if err != nil {
return false
}
finder = zorm.NewFinder().Append("select jieba_query(?)", "让数据自由一点点,让世界美好一点点")
_, err = zorm.QueryRow(context.Background(), finder, &fts5jieba)
if err != nil {
return false
}
// 查询sqlite_vec版本
var vecVersion string
finder = zorm.NewFinder().Append("select vec_version()")
_, err = zorm.QueryRow(context.Background(), finder, &vecVersion)
if err != nil {
panic(err)
}
//fmt.Println("vec_version:" + vecVersion)
isInit := pathExist(datadir + "minrag.db")
if !isInit { //需要初始化数据库
return isInit
}
if tableExist(tableDocumentName) {
return true
}
sqlByte, err := os.ReadFile("minragdatadir/minrag.sql")
if err != nil {
panic(err)
}
createTableSQL := string(sqlByte)
if createTableSQL == "" {
panic("minragdatadir/minrag.sql " + funcT("File anomaly"))
}
ctx := context.Background()
_, err = execNativeSQL(ctx, createTableSQL)
if err != nil {
panic(err)
}
return true
}
// tableExist 数据表是否存在
func tableExist(tableName string) bool {
finder := zorm.NewSelectFinder("sqlite_master", "count(*)").Append("WHERE type=? and name=?", "table", tableName)
count := 0
zorm.QueryRow(context.Background(), finder, &count)
return count > 0
}
// deleteById 根据Id删除数据
func deleteById(ctx context.Context, tableName string, id string) error {
finder := zorm.NewDeleteFinder(tableName).Append(" WHERE id=?", id)
_, err := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
_, err := zorm.UpdateFinder(ctx, finder)
return nil, err
})
return err
}
// deleteAll 删除所有数据
func deleteAll(ctx context.Context, tableName string) error {
finder := zorm.NewDeleteFinder(tableName)
_, err := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
_, err := zorm.UpdateFinder(ctx, finder)
return nil, err
})
return err
}
// execNativeSQL 执行SQL语句
func execNativeSQL(ctx context.Context, nativeSQL string) (bool, error) {
finder := zorm.NewFinder().Append(nativeSQL)
finder.InjectionCheck = false
_, err := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
_, err := zorm.UpdateFinder(ctx, finder)
return nil, err
})
if err != nil {
return false, err
}
return true, nil
}
// vecSerializeFloat64 Serializes a float64 list into a vector BLOB that sqlite-vec accepts
func vecSerializeFloat64(vector []float64) ([]byte, error) {
vector32 := make([]float32, len(vector))
for i, v := range vector {
vector32[i] = float32(v)
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, vector32)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}