点杀妖怪
89.49M · 2026-03-22
sfsDb 是一款轻量级、高性能的嵌入式数据库,专为 Go 语言应用设计。它采用了灵活的数据模型和高效的索引机制,支持复杂的查询操作,并可以通过 TableIter 和 Match 等mach接口自定义强大和灵活的查询功能,特别适合需要快速开发、低部署成本的应用场景。
sfsDb 支持多表组合查询功能,允许开发者在多个表之间进行数据关联和查询。本示例将演示如何使用 sfsDb 创建多个表,插入测试数据,并执行多表组合查询。
import (
"github.com/liaoran123/sfsDb/engine"
)
// 创建第一个测试表
table1, err := engine.TableNew("test_search_comprehensive1")
if err != nil {
panic(err)
}
// 设置表字段
fields := map[string]any{"id": 0, "name": "", "age": 0, "score": 0.0, "active": false}
err = table1.SetFields(fields)
if err != nil {
panic(err)
}
// 为id字段创建主键索引
pk, _ := engine.DefaultPrimaryKeyNew("pk")
pk.AddFields("id")
err = table1.CreateIndex(pk)
if err != nil {
panic(err)
}
// 为age字段创建二级索引
ageIdx, _ := engine.DefaultNormalIndexNew("age_index")
ageIdx.AddFields("age")
err = table1.CreateIndex(ageIdx)
if err != nil {
panic(err)
}
// 插入测试数据
testData := []map[string]any{
{"id": 1, "name": "Alice", "age": 20, "score": 85.5, "active": true},
{"id": 2, "name": "Bob", "age": 25, "score": 90.0, "active": true},
{"id": 3, "name": "Charlie", "age": 30, "score": 75.5, "active": false},
{"id": 4, "name": "David", "age": 35, "score": 95.0, "active": true},
{"id": 5, "name": "Eve", "age": 40, "score": 80.0, "active": false},
}
for _, data := range testData {
_, err := table1.Insert(&data)
if err != nil {
panic(err)
}
}
// 创建第二个测试表
table2, err := engine.TableNew("test_search_comprehensive2")
if err != nil {
panic(err)
}
// 设置表字段
fields2 := map[string]any{"id": 0, "name": "", "age": 0, "score": 0.0, "active": false}
err = table2.SetFields(fields2)
if err != nil {
panic(err)
}
// 为id字段创建主键索引
pk2, _ := engine.DefaultPrimaryKeyNew("pk")
pk2.AddFields("id")
err = table2.CreateIndex(pk2)
if err != nil {
panic(err)
}
// 为age字段创建二级索引
ageIdx2, _ := engine.DefaultNormalIndexNew("age_index")
ageIdx2.AddFields("age")
err = table2.CreateIndex(ageIdx2)
if err != nil {
panic(err)
}
// 插入测试数据
testData2 := []map[string]any{
{"id": 3, "name": "Charlie", "age": 30, "score": 75.5, "active": false},
{"id": 4, "name": "David", "age": 35, "score": 95.0, "active": true},
{"id": 5, "name": "Eve", "age": 40, "score": 80.0, "active": false},
{"id": 6, "name": "Frank", "age": 45, "score": 88.5, "active": true},
{"id": 7, "name": "Grace", "age": 50, "score": 92.0, "active": true},
}
for _, data := range testData2 {
_, err := table2.Insert(&data)
if err != nil {
panic(err)
}
}
// 创建第三个测试表
table3, err := engine.TableNew("test_search_comprehensive3")
if err != nil {
panic(err)
}
// 设置表字段
fields3 := map[string]any{"id": 0, "name": "", "age": 0, "score": 0.0, "active": false}
err = table3.SetFields(fields3)
if err != nil {
panic(err)
}
// 为id字段创建主键索引
pk3, _ := engine.DefaultPrimaryKeyNew("pk")
pk3.AddFields("id")
err = table3.CreateIndex(pk3)
if err != nil {
panic(err)
}
// 为age字段创建二级索引
ageIdx3, _ := engine.DefaultNormalIndexNew("age_index")
ageIdx3.AddFields("age")
err = table3.CreateIndex(ageIdx3)
if err != nil {
panic(err)
}
// 搜索第一个表中id=1的数据
iter1 := table1.Search(&map[string]any{"id": 1})
defer iter1.Release()
records1 := iter1.GetRecords(true)
fmt.Printf("表1查询结果: %vn", records1)
// 搜索第二个表中age>30的数据
iter2 := table2.Search(&map[string]any{"age": 30}, util.GreaterThan)
defer iter2.Release()
records2 := iter2.GetRecords(true)
fmt.Printf("表2查询结果: %vn", records2)
// 示例:查询所有表中age>30的活跃用户
fmt.Println("n=== 多表组合查询结果 ===")
// 查询第一个表
iter1 = table1.Search(&map[string]any{"age": 30, "active": true}, util.GreaterThan)
defer iter1.Release()
records1 = iter1.GetRecords(true)
// 查询第二个表
iter2 = table2.Search(&map[string]any{"age": 30, "active": true}, util.GreaterThan)
defer iter2.Release()
records2 = iter2.GetRecords(true)
// 合并查询结果
allRecords := append(records1, records2...)
fmt.Printf("合并后的查询结果数: %dn", len(allRecords))
for i, record := range allRecords {
fmt.Printf("结果 %d: %vn", i+1, record)
}
package main
import (
"fmt"
"github.com/liaoran123/sfsDb/engine"
"github.com/liaoran123/sfsDb/util"
)
func main() {
fmt.Println("sfsDb 多表组合查询示例")
fmt.Println("========================")
// 创建第一个测试表
table1, err := engine.TableNew("test_search_comprehensive1")
if err != nil {
panic(err)
}
// 设置表字段
fields := map[string]any{"id": 0, "name": "", "age": 0, "score": 0.0, "active": false}
err = table1.SetFields(fields)
if err != nil {
panic(err)
}
// 为id字段创建主键索引
pk, _ := engine.DefaultPrimaryKeyNew("pk")
pk.AddFields("id")
err = table1.CreateIndex(pk)
if err != nil {
panic(err)
}
// 为age字段创建二级索引
ageIdx, _ := engine.DefaultNormalIndexNew("age_index")
ageIdx.AddFields("age")
err = table1.CreateIndex(ageIdx)
if err != nil {
panic(err)
}
// 插入测试数据
testData := []map[string]any{
{"id": 1, "name": "Alice", "age": 20, "score": 85.5, "active": true},
{"id": 2, "name": "Bob", "age": 25, "score": 90.0, "active": true},
{"id": 3, "name": "Charlie", "age": 30, "score": 75.5, "active": false},
{"id": 4, "name": "David", "age": 35, "score": 95.0, "active": true},
{"id": 5, "name": "Eve", "age": 40, "score": 80.0, "active": false},
}
for _, data := range testData {
_, err := table1.Insert(&data)
if err != nil {
panic(err)
}
}
// 创建第二个测试表
table2, err := engine.TableNew("test_search_comprehensive2")
if err != nil {
panic(err)
}
// 设置表字段
fields2 := map[string]any{"id": 0, "name": "", "age": 0, "score": 0.0, "active": false}
err = table2.SetFields(fields2)
if err != nil {
panic(err)
}
// 为id字段创建主键索引
pk2, _ := engine.DefaultPrimaryKeyNew("pk")
pk2.AddFields("id")
err = table2.CreateIndex(pk2)
if err != nil {
panic(err)
}
// 为age字段创建二级索引
ageIdx2, _ := engine.DefaultNormalIndexNew("age_index")
ageIdx2.AddFields("age")
err = table2.CreateIndex(ageIdx2)
if err != nil {
panic(err)
}
// 插入测试数据
testData2 := []map[string]any{
{"id": 3, "name": "Charlie", "age": 30, "score": 75.5, "active": false},
{"id": 4, "name": "David", "age": 35, "score": 95.0, "active": true},
{"id": 5, "name": "Eve", "age": 40, "score": 80.0, "active": false},
{"id": 6, "name": "Frank", "age": 45, "score": 88.5, "active": true},
{"id": 7, "name": "Grace", "age": 50, "score": 92.0, "active": true},
}
for _, data := range testData2 {
_, err := table2.Insert(&data)
if err != nil {
panic(err)
}
}
// 执行多表组合查询
fmt.Println("n=== 执行多表组合查询 ===")
// 查询第一个表中age>30的活跃用户
iter1 := table1.Search(&map[string]any{"age": 30, "active": true}, util.GreaterThan)
defer iter1.Release()
records1 := iter1.GetRecords(true)
fmt.Printf("表1查询结果数: %dn", len(records1))
// 查询第二个表中age>30的活跃用户
iter2 := table2.Search(&map[string]any{"age": 30, "active": true}, util.GreaterThan)
defer iter2.Release()
records2 := iter2.GetRecords(true)
fmt.Printf("表2查询结果数: %dn", len(records2))
// 合并查询结果
allRecords := append(records1, records2...)
fmt.Printf("n合并后的查询结果数: %dn", len(allRecords))
for i, record := range allRecords {
fmt.Printf("结果 %d: ID=%d, Name=%s, Age=%d, Score=%.1f, Active=%tn",
i+1, record["id"], record["name"], record["age"], record["score"], record["active"])
}
fmt.Println("n多表组合查询示例完成!")
}
以下是 TestTestSelectForJoin 函数的测试输出结果,展示了不同 SQL 查询条件下的多表查询结果:
=== RUN TestTestSelectForJoin
select table1.* from table1,table2 where table1.id=table2.id
查询结果:
map[active:false age:30 id:3 name:Charlie score:75.5]
map[active:true age:35 id:4 name:David score:95]
map[active:false age:40 id:5 name:Eve score:80]
解释:查询两个表中 id 字段相等的记录,返回了 3 条匹配记录。
select table1.* from table1,table2 where table1.id!=table2.id
查询结果:
map[active:true age:20 id:1 name:Alice score:85.5]
map[active:true age:25 id:2 name:Bob score:90]
解释:查询两个表中 id 字段不相等的记录,返回了 2 条匹配记录。
select table1.* from table1,table2,table3 where table1.id=table2.id and table1.id=table3.id
查询结果:
map[active:false age:40 id:5 name:Eve score:80]
解释:查询三个表中 id 字段都相等的记录,返回了 1 条匹配记录。
select table1.* from table1,table2,table3 where table1.id!=table2.id and table1.id!=table3.id
查询结果:
map[active:true age:20 id:1 name:Alice score:85.5]
map[active:true age:25 id:2 name:Bob score:90]
解释:查询三个表中 id 字段都不相等的记录,返回了 2 条匹配记录。
select table1.* from table1,table2 where table1.id=4 and table1.id=table2.id
查询结果:
map[active:true age:35 id:4 name:David score:95]
解释:查询 id=4 且在两个表中都存在的记录,返回了 1 条匹配记录。
select table1.* from table1,table2 where table1.id!=4 and table1.id=table2.id
查询结果:
map[active:false age:30 id:3 name:Charlie score:75.5]
map[active:false age:40 id:5 name:Eve score:80]
解释:查询 id!=4 且在两个表中都存在的记录,返回了 2 条匹配记录。
select table1.* from table1,table2 where table1.id<4 and table1.id=table2.id
查询结果:
map[active:false age:30 id:3 name:Charlie score:75.5]
解释:查询 id<4 且在两个表中都存在的记录,返回了 1 条匹配记录。
select table1.* from table1,table2 where table1.id<=4 and table1.id=table2.id
查询结果:
map[active:false age:30 id:3 name:Charlie score:75.5]
map[active:true age:35 id:4 name:David score:95]
解释:查询 id<=4 且在两个表中都存在的记录,返回了 2 条匹配记录。
select table1.* from table1,table2 where table1.id>4 and table1.id=table2.id
查询结果:
map[active:false age:40 id:5 name:Eve score:80]
解释:查询 id>4 且在两个表中都存在的记录,返回了 1 条匹配记录。
select table1.* from table1,table2 where table1.id>=4 and table1.id=table2.id
查询结果:
map[active:true age:35 id:4 name:David score:95]
map[active:false age:40 id:5 name:Eve score:80]
解释:查询 id>=4 且在两个表中都存在的记录,返回了 2 条匹配记录。
select table1.* from table1,table2 where table1.age=40 and table1.id=table2.id
查询结果:
map[active:false age:40 id:5 name:Eve score:80]
解释:查询 age=40 且在两个表中都存在的记录,返回了 1 条匹配记录。
----Search函数不支持无索引的搜索,如需要支持无索引或自己的匹配策略,可以自定义mach接口实现-----
--- PASS: TestTestSelectForJoin (0.04s)
解释:测试执行成功,耗时 0.04 秒。同时提示 Search 函数不支持无索引的搜索,如需支持可自定义 match 接口实现。
多表组合查询适用于需要在多个表之间进行数据关联和分析的场景,例如:
sfsDb 的查询功能具有以下显著优势:
sfsDb 的多表组合查询功能具有以下特点:
sfsDb 的多表组合查询功能为开发者提供了强大的数据查询能力,允许在多个表之间进行灵活的数据关联和分析。通过合理设计表结构和索引,可以实现高效的多表查询操作。
本示例演示了如何创建多个表,插入测试数据,并执行多表组合查询。开发者可以根据实际业务需求,调整查询条件和表结构,实现更复杂的多表查询功能。
通过以上示例和说明,相信开发者已经掌握了 sfsDb 多表组合查询的基本使用方法,可以在实际项目中灵活应用。