Giter Club home page Giter Club logo

Comments (18)

rbock avatar rbock commented on July 22, 2024

Here are two options:

if (db(select(count(t.alpha)).from(t).where(true)).front().count > 0) 
{ /* do something */ }

if (db(select(exists(select(t.alpha).from(t).where(true)))).front().exists) 
{ /* do something */ }

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

Why is so difficult and not obvious?
I expected something like this:

if ( db(select(t).count(t.nickname).where(t.nickname="Bob")) )
{ /* do something */ }

from sqlpp11.

rbock avatar rbock commented on July 22, 2024

Why do you think it is difficult?

auto query = select(count(t.id)).from(t).where(t.nickname == "Bob");

That is almost verbatim the query you wanted to use. Now you have to hand that to the database connection

auto result = db(query);

And you need to evaluate the value of count in the first row

if (result.front().count > 0)
{ /* do something */ }

I just chained those things together:

if (db(select(count(t.id)).from(t).where(t.nickname == "Bob")).front().count > 0) 
{ /* do something */ }

If you insist on something shorter, you could also do this.

if (!db.select(t.id).from(t).where(t.nickname=="Bob").empty()))
{}

That is shorter but not very efficient if there are thousands of entries with nickname Bob.

Or you could do this in a function or lambda:

for (db(select(t.id).from(t).where(nickname == "Bob")))
{
   return true;
}
return false;

That being said, I am not sure how to interpret your suggestion. The syntax is quite different from all examples. There is no from and the selected column (the count) is not in the argument list of select. count seems to be a member function of the select statement. That would be quite tough do do (you would also want to add other aggregating functions, too).

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

Hmm...
Using this expression:

const bool exists = db(sqlpp::select(sqlpp::count(users_tbl.nickname)).from(users_tbl).where(users_tbl.nickname=nickname)).front().count > 0;

I get the following errors:

db_controller.cpp: In member function ‘bool support_server::db_controller::user_exists(const string&)’:
db_controller.cpp:111:131: error: no match for call to ‘(sqlpp::sqlite3::connection) (sqlpp::no_where_t<true>::_base_t<sqlpp::detail::statement_policies_t<void, sqlpp::no_with_t, sqlpp::select_t, sqlpp::no_select_flag_list_t, sqlpp::select_column_list_t<void, sqlpp::count_t<sqlpp::noop, sqlpp::column_t<support_server::users::users, support_server::users::users_::nickname> > >, sqlpp::from_t<void, support_server::users::users>, sqlpp::no_extra_tables_t, sqlpp::no_where_t<true>, sqlpp::no_group_by_t, sqlpp::no_having_t, sqlpp::no_order_by_t, sqlpp::no_limit_t, sqlpp::no_offset_t, sqlpp::no_union_t> >::_new_statement_t<std::integral_constant<bool, false>, sqlpp::where_t<void, sqlpp::assignment_t<sqlpp::column_t<support_server::users::users, support_server::users::users_::nickname>, sqlpp::text_operand> > >)’
   const bool exists = pimpl->db(sqlpp::select(sqlpp::count(users_tbl.nickname)).from(users_tbl).where(users_tbl.nickname=nickname)).front().count > 0;
                                                                                                                                   ^
In file included from db_controller.cpp:9:0:
/usr/local/include/sqlpp11/sqlite3/connection.h:88:9: note: candidate is:
   class connection: public sqlpp::connection
         ^
/usr/local/include/sqlpp11/sqlite3/connection.h:280:10: note: template<class T> decltype (t._run((*(sqlpp::sqlite3::connection*)this))) sqlpp::sqlite3::connection::operator()(const T&)
     auto operator()(const T& t) -> decltype(t._run(*this))
          ^
/usr/local/include/sqlpp11/sqlite3/connection.h:280:10: note:   template argument deduction/substitution failed:
/usr/local/include/sqlpp11/sqlite3/connection.h: In substitution of ‘template<class T> decltype (t._run((*(sqlpp::sqlite3::connection*)this))) sqlpp::sqlite3::connection::operator()(const T&) [with T = sqlpp::bad_statement]’:
db_controller.cpp:111:131:   required from here
/usr/local/include/sqlpp11/sqlite3/connection.h:280:10: error: ‘const struct sqlpp::bad_statement’ has no member named ‘_run’

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

My bad %)

... where(users_tbl.nickname=nickname)

should be

... where(users_tbl.nickname==nickname)

Sorry for the noise.

from sqlpp11.

rbock avatar rbock commented on July 22, 2024

:-)

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

I understand, but I think it would be nice to add a little "syntactic sugar" for expressions like that I gave. But that's just my opinion...

if ( db(count(t).where(t.nickname=="Bob")) )
{}

or

if ( db(exists(t).where(t.nickname=="Bob")) )
{}

from sqlpp11.

rbock avatar rbock commented on July 22, 2024

Got it. I'll think about it.

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

if (!db.select(t.id).from(t).where(t.nickname=="Bob").empty()))
{}

There is one extra closing parenthesis(')').
In any case, something is wrong with this expression, because I get the following error:

db_controller.cpp:112:53: error: ‘class sqlpp::sqlite3::bind_result_t’ has no member named ‘from’
  const bool exists = db.select(users_tbl.id).from(users_tbl).where(users_tbl.nickname == nickname).empty();

Ideas?

from sqlpp11.

rbock avatar rbock commented on July 22, 2024
if (!db.select(t.id).from(t).where(t.nickname=="Bob")).empty())
{}

This should be OK (I cannot test right now).

empty() is a member function of result.

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

The same error.
Example:

const bool exists = (db.select(users_tbl.id).from(users_tbl).where(users_tbl.nickname == nickname)).empty();
db_controller.cpp:112:54: error: ‘class sqlpp::sqlite3::bind_result_t’ has no member named ‘from’
  const bool exists = !(db.select(users_tbl.id).from(users_tbl).where(users_tbl.nickname == nickname)).empty();

from sqlpp11.

rbock avatar rbock commented on July 22, 2024
if (!db(select(t.id).from(t).where(t.nickname=="Bob")).empty())
{}

Note to self: Do not write code when distracted...

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

I distract you too, I'm sorry :(
But I doubt that someone other will be able to answer my questions about sqlpp11. Thank you so much that you did not leave me alone with sqlpp11! =)

from sqlpp11.

rbock avatar rbock commented on July 22, 2024

Well, the good thing is: You are becoming knowledgeable too, I guess, and might be able to answer future questions by other users, or help documenting ;-)

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

You're right, of course ;)

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

one more question...

db(sqlpp::insert_into(users_tbl).set(users_tbl.nickname=nickname));

error:

/usr/local/include/sqlpp11/insert_value_list.h:434:7: error: static assertion failed: At least one required column is missing in set()
       static_assert(not std::is_same<_database_t, void>::value or detail::have_all_required_columns<lhs_t<Assignments>...>::value, "At least one required column is missing in set()");
       ^

from sqlpp11.

rbock avatar rbock commented on July 22, 2024

If you look through the generated table code, you will find that one or more columns contain a flag that they are required in insert statements. This is decided by the code generator (e.g. your PP code).

Personally, if a column cannot be NULL, I like to enforce it in the insert statement.

I'll close this issue now, OK? We kind of left the original topic :-)

from sqlpp11.

niXman avatar niXman commented on July 22, 2024

Oh, my bad, again %)
Thank you!

I'll close this issue now, OK?

Sure.

from sqlpp11.

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.