sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

'testtable' is not a function: SQL logic error

Open raidenluikang opened this issue 2 years ago • 1 comments

Following example success compiled with latest dev sqlite_orm, but on runtime gives 'testtable' is not a function: SQL logic error

#include <cstdint>
#include <string>

#include "sqlite_orm_dev.h"

namespace orm = sqlite_orm;



struct testtable
{
    std::int64_t id = 0;
    std::int64_t value = 0;
};

static inline auto init_storage(const std::string &path){
    
    return orm::make_storage(path,
			orm::make_table("testtable",
                            orm::make_column("id", &testtable::id, /*orm::autoincrement(),*/ orm::primary_key().autoincrement()),
                            orm::make_column("value", &testtable::value)
                        )
    );
}

using storage_t = decltype(init_storage(""));

int main(){

    storage_t storage = init_storage("database.sql");
    storage.sync_schema();
	testtable table;
	table.id = 20;
	table.value = 1;
	storage.replace<testtable>(table);
    auto list = storage.get_all<testtable>(
            //orm::where( //--> Imagine that, I forgotten write there orm::where
                orm::and_(orm::is_equal(&testtable::id, 20),
                           orm::is_equal(&testtable::value, 1)
                                  
                ),
            //),
            orm::limit( 1 ) 
            );
    
    for (auto&& row : list)
    {
		printf("id = %lld value = %lld\n", (long long)row.id, (long long)row.value);
	}
    return 0;
}

OUTPUT:

$ ./main
terminate called after throwing an instance of 'std::system_error'
  what():  'testtable' is not a function: SQL logic error
Aborted (core dumped)

Q: Can be detect this error in compile time ?

raidenluikang avatar May 25 '23 00:05 raidenluikang

Hi @raidenluikang . Thanks for submitting this issue. It is a very good suggestion. get_all can't accept everything but argument check logic is forwarded to SQLite engine at run-time. Of course we can update static checks and add static_assert. The easiest way is to add static_assert which checks that get_all arguments list don't contain and product but it is not right cause we better fix it in a different way: to mark some expressions available as get_all arguments (they are called already select constraints inside the code) and allow passing only select constraints inside. It must not break existing working queries but it will break compilation for existing queries which contain run-time error. Is it what you expect?

fnc12 avatar Jun 04 '23 19:06 fnc12