Full Table
To query all data from a table use: def PRICES = db.table("PRICES") PRICES.should == ["ID" | "DESCRIPTION" | "PRICE"] { ___________________________________ "id1" | "nice set" | 1000 "id2" | "another set" | 2000 }
To query all data from a table use: def PRICES = db.table("PRICES") PRICES.should == ["ID" | "DESCRIPTION" | "PRICE"] { ___________________________________ "id1" | "nice set" | 1000 "id2" | "another set" | 2000 }
def prices = db.query("select * from PRICES where id=:id", [id: "id1"]) prices.should == ["ID" | "DESCRIPTION" | "PRICE"] { ___________________________________ "id1" | "nice set" | 1000 } def prices = db.query("select * from PRICES where id=:id", [id: "id1"]) prices.should == [ID: "id1", "DESCRIPTION": "nice set", PRICE: 1000] def prices = db.query("select * from PRICES where id in (:ids)", [ids: ["id1", "id2"]]) prices.should == ["ID" | "DESCRIPTION" | "PRICE"] { ___________________________________ "id1" | "nice set" | 1000 "id2" | "another set" | 2000 }
If your query uses a single unique placeholder name, you can pass a regular value instead of a java.util.Map def prices = db.query("select * from PRICES where id=:id or external_id=:id", "id1") prices.should == [ID: "id1", "DESCRIPTION": "nice set", PRICE: 1000]
query doesn't query database at the call time. It defines a query to be used later. def prices = db.query("select * from PRICES") prices.shouldNot == [] db.update("delete from PRICES") prices.should == [] def PRICES = db.table("PRICES") def numberOfItems = PRICES.queryCount() numberOfItems.shouldNot == 0 db.update("delete from PRICES") numberOfItems.should == 0
def price = db.query("select price from PRICES where id=:id", [id: 'id1']) price.should == 1000 price.shouldNot == 2000
Use waitTo on query result to continuously query database until condition is met or timeout is reached. def count = db.query("select count(*) from PRICES") count.should == 2 // event happen somewhere to increase the number of rows... count.waitTo == 3
Value returned from query methods is an instance of DbQuery type. No actual query is performed when DbQuery instance is created. It holds information about what query is, and what its parameters and only performs query when validation is triggered.Use queryXXX to access underlying value. def price = db.query("select price from PRICES where id=:id", [id: 'id1']) if (price.singleValue() > 100) { println("do something") } Avoid: When you use queryXXX for assertions you may lose additional report information