sqlite_orm v1.6 Release Notes
Release Date: 2020-10-08 // over 4 years ago-
๐ฑ โญ Added
CHECK
constraintauto storage = make\_storage("database.sqlite", make\_table("contacts", make\_column("contact\_id", &Contact::id, primary\_key()), make\_column("phone", &Contact::phone), check(length(&Contact::phone) \>= 10)));
means
CREATE TABLE contacts ( contact\_id INTEGER NOT NULL PRIMARY KEY, phone TEXT NOT NULL, CHECK(LENGTH(phone \>= 10)) )
or
auto storage = make\_storage("database.sqlite", make\_table("BOOK", make\_column("Book\_id", &Book::id, primary\_key()), make\_column("Book\_name", &Book::name), make\_column("Pub\_name", &Book::pubName), make\_column("PRICE", &Book::price, check(c(&Book::price) \> 0))));
means
CREATE TABLE BOOK( Book\_id INTEGER NOT NULL PRIMARY KEY, Book\_name TEXT NOT NULL, Pub\_name TEXT NOT NULL, PRICE NUMERIC NOT NULL CHECK(PRICE \> 0) )
๐ฑ โญ Added bitwise operators support
storage.select(bitwise\_or(60, 13)); // SELECT 60 | 13storage.select(bitwise\_and(60, 13)); // SELECT 60 & 13storage.select(bitwise\_shift\_left(60, 2)); // SELECT 60 \<\< 2storage.select(bitwise\_shift\_right(60, 2)); // SELECT 60 \>\> 2storage.select(bitwise\_not(60)); // SELECT ~60
๐ฑ โญ Added
indexed_column
function to specify order (ASC
,DESC
) and collation for indexed columnsauto storage = make\_storage({}, make\_index("name\_index", indexed\_column(&User::name).collate("binary").asc()), make\_table("users", make\_column("id", &User::id), make\_column("name", &User::name));
will translate to
CREATE TABLE users ( id INTEGER NOT NULL, name TEXT NOT NULL);CREATE INDEX name\_index ON users (name COLLATE binary ASC);
๐ฑ โญ New core functions
HEX
QUOTE
RANDOMBLOB
INSTR
REPLACE
ROUND
SOUNDEX
๐ฑ โญ New date & time functions - all date & time functions are supported from now!TIME
STRFTIME
๐ฑ โญ Addedstorage.dump
function for prepared statementsauto statement = storage.prepare(select(&User::id, where(length(&User::name) > 5 and like(&User::name, "T%"))));auto str = storage.dump(statement); // str is something like 'SELECT \"users\".\"name\", \"users\".\"id\" FROM 'users' WHERE ( ((\"id\" % 2) = 0)) ORDER BY \"users\".\"name\" '
๐จ The difference between
statement.sql
is thatdump
function prints real values instead of question marks. Also it does not call anysqlite3
functions - it callssqlite_orm
serializer instead.๐ฑ โญ Added custom container support for
get_all
prepared statementExample:
auto statement = storage.prepare(get\_all\<User, std::list\<User\>\>());
๐ฑ โญ
UNIQUE
constraint supports more than one column
Example:make\_table("shapes", make\_column("shape\_id", &Shape::id, primary\_key()), make\_column("background\_color", &Shape::backgroundColor), make\_column("foreground\_color", &Shape::foregroundColor), sqlite\_orm::unique(&Shape::backgroundColor, &Shape::foregroundColor))
๐ฑ โญ New table operating API
Example:
* storage.rename_table<User>("new_table_name") -> change name in table information not database * storage.rename_table("old_name", "new_name"); -> rename table using SQL query * storage.tablename<User>(); -> get table name as `std::string` from table info not database
๐ฑ โญ Added
VALUES
APIExample:
// DELETE FROM devices// WHERE (serial\_number, device\_id) IN (VALUES ('abc', '123'), ('def', '456'))storage.remove\_all\<Device\>(where(in(std::make\_tuple(&Device::serialNumber, &Device::deviceId), values(std::make\_tuple("abc", "123"), std::make\_tuple("def", "456")))));// orstorage.remove\_all\<Device\>( where(in(std::make\_tuple(&Device::serialNumber, &Device::deviceId), values(std::vector\<std::tuple\<std::string, std::string\>\>{std::make\_tuple("abc", "123"), std::make\_tuple("def", "456")}))));
These queries are the same. The difference between them is that the first is static and the second is dynamic (
std::vector
based). It may be useful if you change bound values usingget
API.- ๐ โ๏ธ
sync_schema
behavior changes: now types are ignored cause SQLite ignores them too. It allows using custom types. - ๐ฑ โ๏ธ Fixed all clang and GCC warnings.
- ๐ฑ ๐ Fixed bug: unable to use reserved keywords as foreign key columns
- ๐ฑ ๐ Fixed bug: compilation error during using any core function within
CASE
operator - ๐ Fixed bug in
sync_schema
: #521 - ๐ฑ ๐ Fixed backup bug: #540
Special thanks to:
@undisputed-seraphim
@Leon0402
@air-h-128k-il
Previous changes from v1.4
-
๐ โญ CASE support
// SELECT ID, NAME, MARKS,// CASE// WHEN MARKS \>=80 THEN 'A+'// WHEN MARKS \>=70 THEN 'A'// WHEN MARKS \>=60 THEN 'B'// WHEN MARKS \>=50 THEN 'C'// ELSE 'Sorry!! Failed'// END// FROM STUDENT;auto rows = storage.select(columns(&Student::id, &Student::name, &Student::marks, case\_\<std::string\>() .when(greater\_or\_equal(&Student::marks, 80), then("A+")) .when(greater\_or\_equal(&Student::marks, 70), then("A")) .when(greater\_or\_equal(&Student::marks, 60), then("B")) .when(greater\_or\_equal(&Student::marks, 50), then("C")) .else\_("Sorry!! Failed") .end()));// decltype(rows) is std::vector\<std::tuple\<decltype(Student::id), decltype(Student::name), decltype(Student::marks), std::string\>\>
or
// SELECT CASE country WHEN 'USA' THEN 'Dosmetic' ELSE 'Foreign' END// FROM usersauto rows = storage.select(columns(case\_\<std::string\>(&User::country) .when("USA", then("Dosmetic")) .else\_("Foreign") .end()), multi\_order\_by(order\_by(&User::lastName), order\_by(&User::firstName)));// decltype(rows) is std::vector\<std::string\>
โญ Added core functions: COALESCE, ZEROBLOB, SUBSTR
// SELECT coalesce(10,20);cout \<\< "coalesce(10,20) = " \<\< storage.select(coalesce\<int\>(10, 20)).front() \<\< endl;// SELECT substr('SQLite substr', 8);cout \<\< "substr('SQLite substr', 8) = " \<\< storage.select(substr("SQLite substr", 8)).front() \<\< endl;// SELECT substr('SQLite substr', 1, 6);cout \<\< "substr('SQLite substr', 1, 6) = " \<\< storage.select(substr("SQLite substr", 1, 6)).front() \<\< endl;// SELECT zeroblob(5);cout \<\< "zeroblob(5) = " \<\< storage.select(zeroblob(5)).front().size() \<\< endl;
โญ Dynamic ORDER BY
order_by
andmulti_order_by
are strong typed so you cannot specify ORDER BY column type at runtime.dynamic_order_by
solves this problem.dynamic_order_by
is amulti_order_by
that acceptsorder_by
conditions at runtime. Example:auto orderBy = dynamic\_order\_by(storage);if(shouldOrderByNameAndId){ orderBy.push\_back(order\_by(&User::name)); orderBy.push\_back(order\_by(&User::id)); }else{ orderBy.push\_back(order\_by(&User::id)); }auto rows = storage.get\_all\<User\>(where(...), orderBy);
โญ Added LIKE as a query result
Now
LIKE
can also be used as a core function to retrieve a result:auto rows = storage.select(like(&User::name, "J%"));// decltype(rows) is std::vector\<bool\>
๐ โญ Added LIKE ESCAPE option support
LIKE
has a third argument and now it is available insqlite_orm
:// SELECT name LIKE 'J%' ESCAPE '\_'// FROM usersauto rows = storage.select(like(&User::name, "J%").escape("\_"));
or
// SELECT LIKE(name, 'J%', '\_')// FROM usersauto rows = storage.select(like(&User::name, "J%", "\_"));
- ๐ฑ โ๏ธ Added Catch2 unit tests framework into unit tests project
- ๐ง โ๏ธ Added unit tests configurations for even more platforms and compilers (thanks to @Farwaykorse)
- ๐ฑ โ๏ธ Added contributing doc
- ๐ฑ ๐ Added
nullptr
binding to WHERE conditions - ๐ฑ ๐ Reduced binary size
- ๐ฑ ๐ Added composite key support for
storage_t::remove
function - ๐ฑ ๐ Reduces memory consumption
๐ฑ ๐ Better error reporting
๐ป Before once you get an exception thrown asking
e.what()
gave you a poor text likeNOT NULL constraint failed
. Now it is more detailed (thanks tosqlite3_errmsg
function) like:NOT NULL constraint failed: users.age: constraint failed
๐ฑ ๐ Bug fixes
- ๐ Fixed GCC6 compilation bug
- ๐ Fixed runtime error on ARM architecture
- Fixed getter by value support for
storage_t::replace
andstorage_t::update
functions - ๐ Fixed bug with iterating over blob values
- Fixed on_copy coping on
storage_t
copy - ๐ Fixed silencing binding failure - now exception is thrown
- Fixed using
std::unique_ptr
instorage_t::update_all
set arguments - ๐ Fixed incorrect (reverse) arguments order in GROUP BY