Closed duolabmeng6 closed 7 months ago
Hey, @duolabmeng6, what would be the advantage of such a functionality?
Hey, @duolabmeng6, what would be the advantage of such a functionality?
The current writing method is too long, I hope it's simpler
Where similar to SQL statements
I see the value of having a query language, but I don't think it should be part of clover, as it is meant to stay very simple. Also, I would say there are a lot of potential query languages that could be supported, and for such a reason, I would delegate to the user of the library the responsibility of building such an utility on top of the lib
func TestBUG(t *testing.T) {
store, _ := badgerstore.OpenWithOptions(badger.DefaultOptions("").WithInMemory(true))
db, _ := c.OpenWithStore(store)
text := `[
{
"name": "liming",
"age": 18
},
{
"name": "xiaobai",
"age": 20
},
{
"name": "xiaohong",
"age": 22
}
]`
os.WriteFile("text.json", []byte(text), 0666)
db.ImportCollection("names", "text.json")
dataAll, err := db.FindAll(
query.NewQuery("names").
Where(
query.Field("name").Eq("xiaobai").Or(query.Field("name").GtEq("liming")),
))
if err != nil {
println("SEARCH_FAILED")
}
for _, doc := range dataAll {
fmt.Printf("%+v\n", doc)
}
//&{fields:map[_id:26c9acd6-f9b1-46fe-81ce-2da6f96f886c age:20 name:xiaobai]}
//&{fields:map[_id:83dc8aaa-94ad-4333-9945-98ec6198b83d age:18 name:liming]}
//&{fields:map[_id:ff5f6b48-0687-49ee-a4fe-ae392ac488af age:22 name:xiaohong]}
}
Your current API is not simple and has logic bugs
func TestBUG(t *testing.T) {
store, _ := badgerstore.OpenWithOptions(badger.DefaultOptions("").WithInMemory(true))
db, _ := c.OpenWithStore(store)
text := `[
{
"name": "liming",
"age": 18
},
{
"name": "xiaobai",
"age": 20
},
{
"name": "xiaohong",
"age": 22
}
]`
os.WriteFile("text.json", []byte(text), 0666)
db.ImportCollection("names", "text.json")
dataAll, err := db.FindAll(
query.NewQuery("names").
Where(
query.Field("name").Eq("xiaobai").And(query.Field("age").Gt("18")),
))
if err != nil {
println("SEARCH_FAILED")
}
for _, doc := range dataAll {
fmt.Printf("%+v\n", doc)
}
}
Your current API is difficult to use
query.Field("name").Eq("xiaobai").And(query.Field("age").Gt("18")),
could not find it
query.Field("name").Eq("xiaobai").And(query.Field("age").Gt(18)),
have found
Your design concept is very different from tinydb’s query statement A simple query should look like this
>>> User = Query()
>>> # Search for a field value
>>> db.search(User.name == 'John')
[{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}]
>>> # Combine two queries with logical and
>>> db.search((User.name == 'John') & (User.age <= 30))
[{'name': 'John', 'age': 22}]
>>> # Combine two queries with logical or
>>> db.search((User.name == 'John') | (User.name == 'Bob'))
[{'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}, {'name': 'Bob', 'age': 42}]
>>> # Apply transformation to field with `map`
>>> db.search((User.age.map(lambda x: x + x) == 44))
>>> [{'name': 'John', 'age': 22}]
>>> # More possible comparisons: != < > <= >=
>>> # More possible checks: where(...).matches(regex), where(...).test(your_test_func)
If there are bugs, please open separate issue, as they are not related to supporting the query language you suggest. Also, regarding TinyDB API, it's clear that Golang doesn't allow to reach same level of simplicity, but if you have some suggestions on how to improve the API your are welcome. Other than that I'm not going to support a query language from text at the moment
I understand that Golang cannot achieve the syntax effects of Python, but it can refer to the query building API of Laravel. It is very simple and easy to write.
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
Supporting such API would require major effort and time that I currently don't have. Will consider to do it in future if more and more users see an advantage in that
I have a simple implementation here