Giter VIP home page Giter VIP logo

Comments (9)

ostafen avatar ostafen commented on May 26, 2024

Hey, @duolabmeng6, what would be the advantage of such a functionality?

from clover.

duolabmeng6 avatar duolabmeng6 commented on May 26, 2024

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

from clover.

ostafen avatar ostafen commented on May 26, 2024

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

from clover.

duolabmeng6 avatar duolabmeng6 commented on May 26, 2024
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

from clover.

duolabmeng6 avatar duolabmeng6 commented on May 26, 2024

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

from clover.

duolabmeng6 avatar duolabmeng6 commented on May 26, 2024

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)

from clover.

ostafen avatar ostafen commented on May 26, 2024

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

from clover.

duolabmeng6 avatar duolabmeng6 commented on May 26, 2024

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();

https://laravel.com/docs/10.x/queries#where-clauses

from clover.

ostafen avatar ostafen commented on May 26, 2024

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

from clover.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.