// 织忆 MemoryWeave — SQLite 知识图谱存储(CGO,实现 GraphStore 接口) // 设计要求: SQLite 仅存 graph_nodes / graph_edges / version_history // 不存 memories / episodes / tombstones(那属于 LanceDB 职责) package governance /* #cgo LDFLAGS: -lsqlite3 #include #include */ import "C" import ( "encoding/json" "fmt" "strings" "sync" "unicode" "unsafe" "github.com/xiaoxue/memoryweave/internal/models" ) // SQLiteGraphStore CGO 直连 SQLite,独立于主存储的 LanceDB SQLite 文件 type SQLiteGraphStore struct { mu sync.RWMutex db *C.sqlite3 path string } func NewSQLiteGraphStore(dbPath string) (*SQLiteGraphStore, error) { if dbPath == "" { dbPath = "/var/lib/memoryweave/graph.db" } cPath := C.CString(dbPath) defer C.free(unsafe.Pointer(cPath)) var db *C.sqlite3 rc := C.sqlite3_open(cPath, &db) if rc != C.SQLITE_OK { msg := C.GoString(C.sqlite3_errmsg(db)) C.sqlite3_close(db) return nil, fmt.Errorf("sqlite open graph: %s", msg) } gs := &SQLiteGraphStore{db: db, path: dbPath} if err := gs.migrate(); err != nil { C.sqlite3_close(db) return nil, err } return gs, nil } func (gs *SQLiteGraphStore) migrate() error { sqls := []string{ `CREATE TABLE IF NOT EXISTS graph_nodes ( id TEXT PRIMARY KEY, name TEXT NOT NULL, type TEXT NOT NULL, namespace TEXT NOT NULL DEFAULT '', properties TEXT DEFAULT '{}', pagerank REAL DEFAULT 1.0, created_at TEXT NOT NULL )`, `CREATE TABLE IF NOT EXISTS graph_edges ( id TEXT PRIMARY KEY, source TEXT NOT NULL, target TEXT NOT NULL, relation TEXT NOT NULL, weight REAL DEFAULT 1.0, evidence_count INTEGER DEFAULT 1, namespace TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL, FOREIGN KEY (source) REFERENCES graph_nodes(id), FOREIGN KEY (target) REFERENCES graph_nodes(id) )`, `CREATE TABLE IF NOT EXISTS version_history ( id INTEGER PRIMARY KEY AUTOINCREMENT, memory_id TEXT NOT NULL, version INTEGER NOT NULL, content TEXT NOT NULL, updated_by TEXT DEFAULT '', source TEXT DEFAULT '', trigger TEXT DEFAULT '', reason TEXT DEFAULT '', timestamp TEXT NOT NULL )`, `CREATE INDEX IF NOT EXISTS idx_gn_namespace ON graph_nodes(namespace)`, `CREATE INDEX IF NOT EXISTS idx_ge_source ON graph_edges(source)`, `CREATE INDEX IF NOT EXISTS idx_ge_target ON graph_edges(target)`, `CREATE INDEX IF NOT EXISTS idx_ge_namespace ON graph_edges(namespace)`, `CREATE INDEX IF NOT EXISTS idx_vh_memory ON version_history(memory_id)`, } for _, s := range sqls { cSQL := C.CString(s) rc := C.sqlite3_exec(gs.db, cSQL, nil, nil, nil) C.free(unsafe.Pointer(cSQL)) if rc != C.SQLITE_OK { return fmt.Errorf("migrate graph: %s", C.GoString(C.sqlite3_errmsg(gs.db))) } } // 迁移:已有库可能缺少 pagerank 列(DESIGN.md 要求但原建表 SQL 漏了) gs.migrateAddColumn("graph_nodes", "pagerank", "REAL DEFAULT 1.0") // 修复孤儿边:自动补充缺失的节点 gs.repairOrphanEdges() return nil } func (gs *SQLiteGraphStore) repairOrphanEdges() { sql := `INSERT OR IGNORE INTO graph_nodes (id, name, type, namespace, created_at) SELECT DISTINCT source, source, 'entity', COALESCE(namespace,''), datetime('now') FROM graph_edges WHERE source NOT IN (SELECT id FROM graph_nodes); INSERT OR IGNORE INTO graph_nodes (id, name, type, namespace, created_at) SELECT DISTINCT target, target, 'entity', COALESCE(namespace,''), datetime('now') FROM graph_edges WHERE target NOT IN (SELECT id FROM graph_nodes);` cSQL := C.CString(sql) C.sqlite3_exec(gs.db, cSQL, nil, nil, nil) C.free(unsafe.Pointer(cSQL)) } // migrateAddColumn 幂等添加列(列存在时忽略错误) func (gs *SQLiteGraphStore) migrateAddColumn(table, column, def string) { sql := fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", table, column, def) cSQL := C.CString(sql) rc := C.sqlite3_exec(gs.db, cSQL, nil, nil, nil) C.free(unsafe.Pointer(cSQL)) // SQLITE_ERROR 表示列已存在,忽略即可 if rc != C.SQLITE_OK && rc != C.SQLITE_ERROR { // log but don't fail } } func (gs *SQLiteGraphStore) Close() error { if gs.db != nil { C.sqlite3_close(gs.db) gs.db = nil } return nil } // ─── GraphStore 接口实现 ──────────────────────────────── func (gs *SQLiteGraphStore) AddNode(id, name, nodeType, namespace string) error { gs.mu.Lock() defer gs.mu.Unlock() sql := fmt.Sprintf( "INSERT OR REPLACE INTO graph_nodes (id, name, type, namespace, created_at) VALUES ('%s', '%s', '%s', '%s', datetime('now'))", escape(id), escape(name), escape(nodeType), escape(namespace)) return execSQL(gs.db, sql) } func (gs *SQLiteGraphStore) AddEdge(id, source, target, relation, namespace string, weight float64) error { gs.mu.Lock() defer gs.mu.Unlock() // 自动创建缺失的源/目标节点;已存在的升级 namespace(default → 具体ns) for _, node := range []struct{ id, name string }{{source, source}, {target, target}} { // 先尝试 INSERT — 如果节点不存在就创建 sql := fmt.Sprintf( "INSERT OR IGNORE INTO graph_nodes (id, name, type, namespace, created_at) VALUES ('%s', '%s', 'entity', '%s', datetime('now'))", escape(node.id), escape(node.name), escape(namespace)) if err := execSQL(gs.db, sql); err != nil { return err } // 如果 namespace 非空非 default,顺手升级 default 节点 if namespace != "" && namespace != "default" { updateSQL := fmt.Sprintf( "UPDATE graph_nodes SET namespace='%s' WHERE id='%s' AND namespace='default'", escape(namespace), escape(node.id)) _ = execSQL(gs.db, updateSQL) // 忽略错误(节点不存在)不影响 } } sql := fmt.Sprintf( "INSERT OR REPLACE INTO graph_edges (id, source, target, relation, weight, namespace, created_at) VALUES ('%s', '%s', '%s', '%s', %f, '%s', datetime('now'))", escape(id), escape(source), escape(target), escape(relation), weight, escape(namespace)) return execSQL(gs.db, sql) } func (gs *SQLiteGraphStore) Navigate(entity string, maxHops int, namespace string) ([]map[string]interface{}, error) { // 单源 BFS:从 entity 展开到邻居,不找路径 gs.mu.RLock() defer gs.mu.RUnlock() nsClause := buildNamespaceClause(namespace) visited := map[string]bool{entity: true} queue := []string{entity} var paths []map[string]interface{} for hop := 1; hop <= maxHops && len(queue) > 0; hop++ { var next []string for _, node := range queue { sql := fmt.Sprintf( "SELECT e.id, e.target, e.relation, e.weight, n.name FROM graph_edges e JOIN graph_nodes n ON e.target = n.id WHERE e.source = '%s' AND %s", escape(node), nsClause) edges := queryRows(gs.db, sql) for _, edge := range edges { target := edge["target"].(string) if !visited[target] { visited[target] = true next = append(next, target) paths = append(paths, map[string]interface{}{ "from": node, "to": target, "relation": edge["relation"], "weight": edge["weight"], "hop": hop, }) } } } queue = next } return paths, nil } // NavigateBiDir 真正的双向 BFS 路径查找(§2.5.4) // 从 source 正向 BFS maxHops 跳,从 target 反向 BFS maxHops 跳 // 找到相遇节点 → 重建完整路径 → 按 score 降序返回 top 3 func (gs *SQLiteGraphStore) NavigateBiDir(source, target string, maxHops int, namespace string) ([]map[string]interface{}, error) { if source == target { return []map[string]interface{}{ { "nodes": []string{source}, "edges": []struct{}{}, "score": 1.0, }, }, nil } gs.mu.RLock() defer gs.mu.RUnlock() nsClause := buildNamespaceClause(namespace) // 正向 BFS 数据结构 type fwdNode struct { parent string edgeID string relation string weight float64 hop int pathProd float64 // 累积权重乘积 } fwd := make(map[string]*fwdNode) fwd[source] = &fwdNode{hop: 0, pathProd: 1.0} fwdQ := []string{source} fwdVisited := map[string]bool{source: true} // 反向 BFS 数据结构 type bwdNode struct { parent string edgeID string relation string weight float64 hop int pathProd float64 } bwd := make(map[string]*bwdNode) bwd[target] = &bwdNode{hop: 0, pathProd: 1.0} bwdQ := []string{target} bwdVisited := map[string]bool{target: true} // 正向步长:ceil(maxHops/2) fwdHops := (maxHops + 1) / 2 // 反向步长:floor(maxHops/2) bwdHops := maxHops / 2 // 执行正向 BFS for hop := 1; hop <= fwdHops && len(fwdQ) > 0; hop++ { var next []string for _, node := range fwdQ { sql := fmt.Sprintf( "SELECT e.id, e.target, e.relation, e.weight FROM graph_edges e WHERE e.source = '%s' AND %s", escape(node), nsClause) edges := queryRows(gs.db, sql) for _, edge := range edges { targetID := edge["target"].(string) if fwdVisited[targetID] { continue } fwdVisited[targetID] = true w := edge["weight"].(float64) fwd[targetID] = &fwdNode{ parent: node, edgeID: edge["id"].(string), relation: edge["relation"].(string), weight: w, hop: hop, pathProd: fwd[node].pathProd * w, } next = append(next, targetID) } } fwdQ = next } // 执行反向 BFS for hop := 1; hop <= bwdHops && len(bwdQ) > 0; hop++ { var next []string for _, node := range bwdQ { // 反向要找所有指向 node 的边(target = node) sql := fmt.Sprintf( "SELECT e.id, e.source, e.relation, e.weight FROM graph_edges e WHERE e.target = '%s' AND %s", escape(node), nsClause) edges := queryRows(gs.db, sql) for _, edge := range edges { srcID := edge["source"].(string) if bwdVisited[srcID] { continue } bwdVisited[srcID] = true w := edge["weight"].(float64) bwd[srcID] = &bwdNode{ parent: node, edgeID: edge["id"].(string), relation: edge["relation"].(string), weight: w, hop: hop, pathProd: bwd[node].pathProd * w, } next = append(next, srcID) } } bwdQ = next } // 寻找相遇节点 var results []PathResult for nodeID, fn := range fwd { if bn, ok := bwd[nodeID]; ok { // 重建从 source → nodeID 的路径(正向) fwdPathNodes := []string{nodeID} fwdPathEdges := make([]map[string]interface{}, 0) cur := nodeID for cur != source { n := fwd[cur] if n == nil { break } fwdPathEdges = append(fwdPathEdges, map[string]interface{}{ "source": n.parent, "target": cur, "relation": n.relation, "weight": n.weight, }) fwdPathNodes = append(fwdPathNodes, n.parent) cur = n.parent } // 逆序:从 source 到 meeting for i, j := 0, len(fwdPathNodes)-1; i < j; i, j = i+1, j-1 { fwdPathNodes[i], fwdPathNodes[j] = fwdPathNodes[j], fwdPathNodes[i] } for i, j := 0, len(fwdPathEdges)-1; i < j; i, j = i+1, j-1 { fwdPathEdges[i], fwdPathEdges[j] = fwdPathEdges[j], fwdPathEdges[i] } // 重建从 nodeID → target 的路径(反向,翻转方向) cur = nodeID for cur != target { n := bwd[cur] if n == nil { break } // 反向 BFS 的 parent 是 target 侧,所以边是从 cur → n.parent fwdPathEdges = append(fwdPathEdges, map[string]interface{}{ "source": cur, "target": n.parent, "relation": n.relation, "weight": n.weight, }) fwdPathNodes = append(fwdPathNodes, n.parent) cur = n.parent } score := fn.pathProd * bn.pathProd if score > 0 { results = append(results, PathResult{ Nodes: fwdPathNodes, Edges: fwdPathEdges, Score: score, }) } } } // 按 score 降序排序,取 top 3 sortResultsByScore(results) // 转换为接口格式 out := make([]map[string]interface{}, 0, len(results)) for _, r := range results { if len(out) >= 3 { break } out = append(out, map[string]interface{}{ "nodes": r.Nodes, "edges": r.Edges, "score": r.Score, }) } if len(out) == 0 { // 没有路径时的降级:返回各自邻居展开 fwd, _ := gs.Navigate(source, maxHops, namespace) bwd, _ := gs.Navigate(target, maxHops, namespace) return append(fwd, bwd...), nil } return out, nil } // PathResult 路径查找结果 type PathResult struct { Nodes []string Edges []map[string]interface{} Score float64 } func buildNamespaceClause(namespace string) string { if namespace == "" { return "1=1" } return fmt.Sprintf("(e.namespace = '%s' OR e.namespace = 'default')", escape(namespace)) } // sortResultsByScore 简单选择排序 func sortResultsByScore(results []PathResult) { for i := 0; i < len(results); i++ { for j := i + 1; j < len(results); j++ { if results[j].Score > results[i].Score { results[i], results[j] = results[j], results[i] } } } } func (gs *SQLiteGraphStore) Query(entity, relation, namespace string) []map[string]interface{} { gs.mu.RLock() defer gs.mu.RUnlock() // 命名空间兼容 nsClause := fmt.Sprintf("e.namespace IN ('%s', 'default')", escape(namespace)) if namespace == "" { nsClause = "1=1" } sql := fmt.Sprintf( "SELECT e.source, e.target, e.relation, e.weight FROM graph_edges e WHERE e.relation = '%s' AND %s AND (e.source = '%s' OR e.target = '%s')", escape(relation), nsClause, escape(entity), escape(entity)) return queryRows(gs.db, sql) } func (gs *SQLiteGraphStore) Stats() (nodeCount, edgeCount int, density float64) { gs.mu.RLock() defer gs.mu.RUnlock() nodeCount = queryInt(gs.db, "SELECT COUNT(*) FROM graph_nodes") edgeCount = queryInt(gs.db, "SELECT COUNT(*) FROM graph_edges") if nodeCount > 0 { maxEdges := nodeCount * (nodeCount - 1) density = float64(edgeCount) / float64(maxEdges) if density > 1 { density = 1 } } return } func (gs *SQLiteGraphStore) Prune(minWeight float64) { gs.mu.Lock() defer gs.mu.Unlock() execSQL(gs.db, fmt.Sprintf("DELETE FROM graph_edges WHERE weight < %f", minWeight)) execSQL(gs.db, `DELETE FROM graph_nodes WHERE id NOT IN (SELECT DISTINCT source FROM graph_edges UNION SELECT DISTINCT target FROM graph_edges)`) } func (gs *SQLiteGraphStore) GetGraph(namespace string, limit int) ([]map[string]interface{}, []map[string]interface{}) { nodes := []map[string]interface{}{} edges := []map[string]interface{}{} var nodeSQL string if namespace == "" || namespace == "all" { if limit > 0 { nodeSQL = fmt.Sprintf("SELECT id, name, type, namespace, properties FROM graph_nodes ORDER BY pagerank DESC LIMIT %d", limit) } else { nodeSQL = "SELECT id, name, type, namespace, properties FROM graph_nodes" } } else { if limit > 0 { nodeSQL = fmt.Sprintf("SELECT id, name, type, namespace, properties FROM graph_nodes WHERE namespace = '%s' ORDER BY pagerank DESC LIMIT %d", escape(namespace), limit) } else { nodeSQL = fmt.Sprintf("SELECT id, name, type, namespace, properties FROM graph_nodes WHERE namespace = '%s'", escape(namespace)) } } rows := queryRows(gs.db, nodeSQL) for _, n := range rows { props := n["properties"].(string) var properties map[string]interface{} if props != "" { json.Unmarshal([]byte(props), &properties) } nodes = append(nodes, map[string]interface{}{ "id": n["id"], "name": n["name"], "type": n["type"], "namespace": n["namespace"], "properties": properties, }) } // 只获取这些节点的边 if len(nodes) > 0 { nids := make([]string, len(nodes)) for i, n := range nodes { nids[i] = "'" + escape(n["id"].(string)) + "'" } nidList := strings.Join(nids, ",") var edgeSQL string if namespace == "" || namespace == "all" { edgeSQL = fmt.Sprintf("SELECT id, source, target, relation, weight, namespace FROM graph_edges WHERE source IN (%s) AND target IN (%s)", nidList, nidList) } else { edgeSQL = fmt.Sprintf("SELECT id, source, target, relation, weight, namespace FROM graph_edges WHERE namespace = '%s' AND source IN (%s) AND target IN (%s)", escape(namespace), nidList, nidList) } edgeRows := queryRows(gs.db, edgeSQL) for _, e := range edgeRows { edges = append(edges, map[string]interface{}{ "id": e["id"], "source": e["source"], "target": e["target"], "relation": e["relation"], "weight": e["weight"], "namespace": e["namespace"], }) } } return nodes, edges } func (gs *SQLiteGraphStore) ExpandFromResults(results []models.RecallResult, namespace string, maxHops int) []models.RecallResult { // 从 recall 结果提取实体,展开图谱邻居 expanded := make([]models.RecallResult, len(results)) copy(expanded, results) seen := make(map[string]bool) for _, r := range results { if seen[r.ID] { continue } seen[r.ID] = true // 从内容中提取可能作为实体的关键词 entities := extractPotentialEntities(r.Content) for _, entity := range entities { nodeID := normalizeEntityID(entity) paths, _ := gs.Navigate(nodeID, maxHops, namespace) for _, p := range paths { if target, ok := p["to"].(string); ok && !seen[target] { seen[target] = true expanded = append(expanded, models.RecallResult{ Content: fmt.Sprintf("[graph] %s --[%s]--> %s", entity, p["relation"], p["to"]), Score: r.Score * 0.5, }) } } } } return expanded } // extractPotentialEntities 从文本中提取可能作为图谱实体的关键词 func extractPotentialEntities(text string) []string { var entities []string seen := make(map[string]bool) for _, w := range strings.Fields(text) { w = strings.Trim(w, ",.;:!?,。;:!?、\"'()()[]【】") if len(w) < 2 { continue } // 大写开头(英文命名实体) runes := []rune(w) if len(runes) >= 2 && runes[0] >= 'A' && runes[0] <= 'Z' { if !seen[strings.ToLower(w)] { seen[strings.ToLower(w)] = true entities = append(entities, w) } } // 纯中文字符 2-8 字 chineseOnly := true chineseRunes := 0 for _, r := range runes { if r >= 0x4E00 && r <= 0x9FFF { chineseRunes++ } else { chineseOnly = false } } if chineseOnly && chineseRunes >= 2 && chineseRunes <= 8 { if !seen[w] { seen[w] = true entities = append(entities, w) } } } return entities } // normalizeEntityID 将自由文本转为实体 ID 格式 func normalizeEntityID(name string) string { clean := strings.Map(func(r rune) rune { if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' || r == '-' || r == ' ' { return r } if unicode.IsLetter(r) { return r } return '_' }, strings.TrimSpace(name)) clean = strings.ToLower(clean) clean = strings.ReplaceAll(clean, " ", "_") for strings.Contains(clean, "__") { clean = strings.ReplaceAll(clean, "__", "_") } clean = strings.Trim(clean, "_") if clean == "" { return "n_unknown" } return "n_" + clean } // SearchNodes 按 label 模糊搜索节点(§2.5.4 match 格式兼容) func (gs *SQLiteGraphStore) SearchNodes(label, namespace string) []map[string]interface{} { gs.mu.RLock() defer gs.mu.RUnlock() nsClause := "1=1" if namespace != "" { nsClause = fmt.Sprintf("namespace = '%s'", escape(namespace)) } sql := fmt.Sprintf( "SELECT id, name, type, namespace, pagerank FROM graph_nodes WHERE name LIKE '%%%s%%' AND %s LIMIT 100", escape(label), nsClause) return queryRows(gs.db, sql) } // ListNodesByType 按 type 列出节点 func (gs *SQLiteGraphStore) ListNodesByType(nodeType, namespace string) []map[string]interface{} { gs.mu.RLock() defer gs.mu.RUnlock() nsClause := "1=1" if namespace != "" { nsClause = fmt.Sprintf("namespace = '%s'", escape(namespace)) } sql := fmt.Sprintf( "SELECT id, name, type, namespace, pagerank FROM graph_nodes WHERE type = '%s' AND %s ORDER BY pagerank DESC LIMIT 100", escape(nodeType), nsClause) return queryRows(gs.db, sql) } // ListNodes 列出所有节点 func (gs *SQLiteGraphStore) ListNodes(namespace string) []map[string]interface{} { gs.mu.RLock() defer gs.mu.RUnlock() nsClause := "1=1" if namespace != "" { nsClause = fmt.Sprintf("namespace = '%s'", escape(namespace)) } sql := fmt.Sprintf( "SELECT id, name, type, namespace, pagerank FROM graph_nodes WHERE %s ORDER BY pagerank DESC LIMIT 100", nsClause) return queryRows(gs.db, sql) } func (gs *SQLiteGraphStore) PageRank(damping float64, iterations int) map[string]float64 { gs.mu.RLock() defer gs.mu.RUnlock() // 获取所有节点 nodes := queryStrSlice(gs.db, "SELECT id FROM graph_nodes") n := float64(len(nodes)) if n == 0 { return nil } ranks := make(map[string]float64) base := (1.0 - damping) / n for _, id := range nodes { ranks[id] = 1.0 / n } outEdges := make(map[string][]struct { target string weight float64 }) for _, node := range nodes { sql := fmt.Sprintf("SELECT target, weight FROM graph_edges WHERE source = '%s'", escape(node)) rows := queryRows(gs.db, sql) for _, r := range rows { outEdges[node] = append(outEdges[node], struct { target string weight float64 }{r["target"].(string), r["weight"].(float64)}) } } for iter := 0; iter < iterations; iter++ { newRanks := make(map[string]float64) for _, node := range nodes { rank := base for src, edges := range outEdges { totalWt := 0.0 for _, e := range edges { totalWt += e.weight } for _, e := range edges { if e.target == node && totalWt > 0 { rank += damping * ranks[src] * e.weight / totalWt } } } newRanks[node] = rank } ranks = newRanks } return ranks } func (gs *SQLiteGraphStore) EvidenceCount(entity string) int { gs.mu.RLock() defer gs.mu.RUnlock() var sum int for _, r := range queryRows(gs.db, fmt.Sprintf("SELECT SUM(evidence_count) as s FROM graph_edges WHERE source = '%s' OR target = '%s'", escape(entity), escape(entity))) { if v, ok := r["s"]; ok { switch x := v.(type) { case int: sum += x case int64: sum += int(x) case float64: sum += int(x) } } } return sum } // ─── CGO 工具 ────────────────────────────────────────── // UpdatePageRanks 批量更新节点的 pagerank 值(§2.5.5) func (gs *SQLiteGraphStore) UpdatePageRanks(ranks map[string]float64) { gs.mu.Lock() defer gs.mu.Unlock() for nodeID, rank := range ranks { sql := fmt.Sprintf("UPDATE graph_nodes SET pagerank = %f WHERE id = '%s'", rank, escape(nodeID)) _ = execSQL(gs.db, sql) } } func execSQL(db *C.sqlite3, sql string) error { cSQL := C.CString(sql) defer C.free(unsafe.Pointer(cSQL)) rc := C.sqlite3_exec(db, cSQL, nil, nil, nil) if rc != C.SQLITE_OK { return fmt.Errorf("sqlite: %s", C.GoString(C.sqlite3_errmsg(db))) } return nil } func queryInt(db *C.sqlite3, sql string) int { cSQL := C.CString(sql) defer C.free(unsafe.Pointer(cSQL)) var stmt *C.sqlite3_stmt rc := C.sqlite3_prepare_v2(db, cSQL, C.int(len(sql)), &stmt, nil) if rc != C.SQLITE_OK { return 0 } defer C.sqlite3_finalize(stmt) if C.sqlite3_step(stmt) == C.SQLITE_ROW { return int(C.sqlite3_column_int(stmt, 0)) } return 0 } func queryStrSlice(db *C.sqlite3, sql string) []string { var result []string cSQL := C.CString(sql) defer C.free(unsafe.Pointer(cSQL)) var stmt *C.sqlite3_stmt rc := C.sqlite3_prepare_v2(db, cSQL, C.int(len(sql)), &stmt, nil) if rc != C.SQLITE_OK { return result } defer C.sqlite3_finalize(stmt) for C.sqlite3_step(stmt) == C.SQLITE_ROW { result = append(result, C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(stmt, 0))))) } return result } func queryRows(db *C.sqlite3, sql string) []map[string]interface{} { var results []map[string]interface{} cSQL := C.CString(sql) defer C.free(unsafe.Pointer(cSQL)) var stmt *C.sqlite3_stmt rc := C.sqlite3_prepare_v2(db, cSQL, C.int(len(sql)), &stmt, nil) if rc != C.SQLITE_OK { return results } defer C.sqlite3_finalize(stmt) colCount := int(C.sqlite3_column_count(stmt)) for C.sqlite3_step(stmt) == C.SQLITE_ROW { row := make(map[string]interface{}) for i := 0; i < colCount; i++ { name := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_name(stmt, C.int(i))))) switch C.sqlite3_column_type(stmt, C.int(i)) { case C.SQLITE_INTEGER: row[name] = int(C.sqlite3_column_int(stmt, C.int(i))) case C.SQLITE_FLOAT: row[name] = float64(C.sqlite3_column_double(stmt, C.int(i))) case C.SQLITE_TEXT: row[name] = C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(stmt, C.int(i))))) default: row[name] = nil } } results = append(results, row) } return results } func escape(s string) string { result := "" for _, ch := range s { if ch == '\'' { result += "''" } else { result += string(ch) } } return result }