Skip to content

Commit f0e0b4d

Browse files
committed
feat: unify Insert/Update/Delete API for SQL and vector databases
- Remove all preset constructors (QdrantHighPrecision/HighSpeed/Balanced) - Remove all preset constructors (MySQLWithUpsert/WithIgnore) - Remove InsertPoint/InsertPoints methods (not needed) - Support []float32/[]float64 in InsertBuilder and UpdateBuilder - JsonOfDelete() auto-sets built.Delete flag internally - Add Built.SqlOfUpsert() and Built.SqlOfInsertIgnore() convenience methods - Smart format detection in QdrantCustom for Insert(func) API Complete API Unification: - Insert(func) works for both SQL and Qdrant - Update(func) works for both SQL and Qdrant - JsonOfDelete() works without extra Delete() method - Only NewQdrantCustom() and NewMySQLCustom() constructors Design Philosophy: Don't add concepts to solve problems. Let users configure via fields or existing closures (QdrantX). All 140+ tests passing. Documentation fully updated.
1 parent f367098 commit f0e0b4d

12 files changed

+419
-479
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,44 @@ sql, args := built.SqlOfUpsert()
4141
// INSERT INTO users ... ON DUPLICATE KEY UPDATE ...
4242

4343
// Qdrant Vector Search (v1.1.0)
44-
// 方式1: 使用默认 Custom
4544
built := xb.Of(&CodeVector{}).
4645
Custom(xb.NewQdrantCustom()).
4746
Eq("language", "golang").
4847
VectorSearch("embedding", queryVector, 10).
48+
QdrantX(func(qx *xb.QdrantBuilderX) {
49+
qx.HnswEf(512).ScoreThreshold(0.85) // 配置参数
50+
}).
4951
Build()
5052
json, _ := built.JsonOfSelect()
5153

52-
// 方式2: 使用 QdrantX 闭包配置(推荐)
54+
// Qdrant CRUD (v1.1.0) - 与 SQL 完全一致的 API
55+
// Insert
5356
built := xb.Of(&CodeVector{}).
5457
Custom(xb.NewQdrantCustom()).
55-
Eq("language", "golang").
56-
VectorSearch("embedding", queryVector, 10).
57-
QdrantX(func(qx *xb.QdrantBuilderX) {
58-
qx.HnswEf(512).ScoreThreshold(0.85)
58+
Insert(func(ib *xb.InsertBuilder) {
59+
ib.Set("id", 123).
60+
Set("vector", []float32{0.1, 0.2, 0.3}).
61+
Set("language", "golang")
5962
}).
6063
Build()
61-
json, _ := built.JsonOfSelect()
64+
json, _ := built.JsonOfInsert()
65+
66+
// Update
67+
built := xb.Of(&CodeVector{}).
68+
Custom(xb.NewQdrantCustom()).
69+
Eq("id", 123).
70+
Update(func(ub *xb.UpdateBuilder) {
71+
ub.Set("language", "rust")
72+
}).
73+
Build()
74+
json, _ := built.JsonOfUpdate()
75+
76+
// Delete
77+
built := xb.Of(&CodeVector{}).
78+
Custom(xb.NewQdrantCustom()).
79+
Eq("id", 123).
80+
Build()
81+
json, _ := built.JsonOfDelete()
6282

6383
// Standard SQL (no Custom needed)
6484
built := xb.Of(&User{}).

builder_insert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (b *InsertBuilder) Set(k string, v interface{}) *InsertBuilder {
6666
// Vector 类型:不做处理,保持原样
6767
// 让 database/sql 调用 driver.Valuer 接口
6868
// Vector.Value() 会返回正确的数据库格式
69+
case []float32, []float64:
70+
// ⭐ 向量数组:保持原样(用于 Qdrant/Milvus)
71+
// 不做 JSON 序列化
6972
case interface{}:
7073
bytes, _ := json.Marshal(v)
7174
v = string(bytes)

builder_update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func (ub *UpdateBuilder) Set(k string, v interface{}) *UpdateBuilder {
6969
// Vector 类型:不做处理,保持原样
7070
// 让 database/sql 调用 driver.Valuer 接口
7171
// Vector.Value() 会返回正确的数据库格式
72+
case []float32, []float64:
73+
// ⭐ 向量数组:保持原样(用于 Qdrant/Milvus)
74+
// 不做 JSON 序列化
7275
case interface{}:
7376
bytes, _ := json.Marshal(v)
7477
v = string(bytes)

doc/CUSTOM_INTERFACE_README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ var custom xb.Custom
206206

207207
switch config.VectorDB {
208208
case "qdrant":
209-
custom = xb.QdrantBalanced()
209+
custom = xb.NewQdrantCustom()
210210
case "milvus":
211211
custom = NewMilvusCustom()
212212
case "weaviate":
@@ -256,10 +256,18 @@ built := xb.Of("t").
256256
### Qdrant(官方支持)
257257

258258
```go
259-
xb.QdrantDefault() // 默认配置
260-
xb.QdrantHighPrecision() // 高精度
261-
xb.QdrantHighSpeed() // 高速
262-
xb.QdrantBalanced() // 平衡
259+
// 基础构造函数
260+
xb.NewQdrantCustom() // 默认配置
261+
xb.NewMySQLCustom() // 默认配置
262+
263+
// 手动配置
264+
custom := xb.NewQdrantCustom()
265+
custom.DefaultHnswEf = 512
266+
267+
// 或使用闭包
268+
xb.Of(...).QdrantX(func(qx *QdrantBuilderX) {
269+
qx.HnswEf(512)
270+
})
263271
```
264272

265273
### 用户自定义

doc/CUSTOM_QUICKSTART.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,13 @@ func main() {
482482

483483
switch os.Getenv("VECTOR_DB") {
484484
case "qdrant":
485-
custom = xb.QdrantBalanced()
485+
custom = xb.NewQdrantCustom()
486486
case "milvus":
487487
custom = NewMilvusCustom()
488488
case "weaviate":
489489
custom = NewWeaviateCustom()
490490
default:
491-
custom = xb.QdrantBalanced()
491+
custom = xb.NewQdrantCustom()
492492
}
493493

494494
// ⭐ 统一的查询构建

doc/CUSTOM_VECTOR_DB_GUIDE.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ json, _ := built.ToMilvusJSON() // 自动使用默认 Custom
207207
var custom xb.Custom
208208
switch config.VectorDB {
209209
case "qdrant":
210-
custom = xb.QdrantBalanced()
210+
custom = xb.NewQdrantCustom()
211211
case "milvus":
212212
custom = xb.NewMilvusCustom()
213213
case "weaviate":
@@ -367,7 +367,7 @@ built := xb.Of("code_vectors").
367367
Build()
368368

369369
// ⭐ 只需切换 Custom
370-
qdrantJSON, _ := built.Custom(xb.QdrantBalanced()).JsonOfSelect()
370+
qdrantJSON, _ := built.Custom(xb.NewQdrantCustom()).JsonOfSelect()
371371
milvusJSON, _ := built.Custom(xb.NewMilvusCustom()).JsonOfSelect()
372372
weaviateJSON, _ := built.Custom(xb.NewWeaviateCustom()).JsonOfSelect()
373373
```
@@ -594,7 +594,7 @@ func SearchDocuments(config Config, query string) ([]Document, error) {
594594
var custom xb.Custom
595595
switch config.VectorDB {
596596
case "qdrant":
597-
custom = xb.QdrantBalanced()
597+
custom = xb.NewQdrantCustom()
598598
case "milvus":
599599
custom = xb.NewMilvusCustom()
600600
case "weaviate":
@@ -657,10 +657,9 @@ func (c *QdrantCustom) ToJSON(built *Built) (string, error) {
657657
return built.toQdrantJSON()
658658
}
659659

660-
// 预设模式
661-
func QdrantHighPrecision() *QdrantCustom { ... }
662-
func QdrantHighSpeed() *QdrantCustom { ... }
663-
func QdrantBalanced() *QdrantCustom { ... }
660+
// 使用说明:
661+
// 1. 基础构造函数:NewQdrantCustom()
662+
// 2. 手动配置字段或使用 QdrantX() 闭包
664663
```
665664

666665
---

doc/QDRANT_FULL_CRUD_SUMMARY.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,12 @@ json, _ := built.JsonOfUpdate()
135135
### 3. **DELETE - 删除向量**
136136

137137
```go
138-
built := xb.X().
138+
built := xb.Of(&CodeVector{}).
139139
Custom(xb.NewQdrantCustom()).
140140
Eq("id", 456).
141141
Build()
142142

143-
built.Delete = true
144-
143+
// ⭐ JsonOfDelete() 自动设置 Delete = true
145144
json, _ := built.JsonOfDelete()
146145
```
147146

@@ -158,7 +157,7 @@ json, _ := built.JsonOfDelete()
158157

159158
```go
160159
built := xb.Of(&CodeVector{}).
161-
Custom(xb.QdrantHighPrecision()).
160+
Custom(xb.NewQdrantCustom()).
162161
Eq("language", "golang").
163162
VectorSearch("embedding", queryVector, 10).
164163
Build()

doc/XB_ORM_PHILOSOPHY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ switch config.Database {
164164
case "oracle":
165165
custom = oracle_custom.New()
166166
case "mysql":
167-
custom = xb.MySQLWithUpsert()
167+
custom = xb.NewMySQLCustom()
168168
case "postgresql":
169169
custom = nil // 默认即可
170170
}

0 commit comments

Comments
 (0)