c++mysql-x-devapi

How to use results from mysql built-in functions when inserting via mysql-x-devapi in C++?


I am using mysql-x-devapi and need to insert a row to a table and put UNIX_TIMESTAMP() of the server in a column:

sql_client_.getSession().getDefaultSchema()
    .getTable("event")
    .insert("title", "time")
    .values("event title", "UNIX_TIMESTAMP()")
    .execute();

This code gives me: CDK Error: Incorrect integer value 'UNIX_TIMESTAMP()' for column 'time' at row 1

How can I do this using xdevapi (not sql command that needs sql string)?

I am able to use mysqlx::expr("UNIX_TIMESTAMP()") in set function when updating the table. The same doesn't work with insert and fails with the following error:

/usr/include/mysql-cppconn-8/mysqlx/devapi/table_crud.h:157:17: error: ‘mysqlx::abi2::r0::internal::Expression::Expression(V&&) [with V = mysqlx::abi2::r0::internal::Expression&]’ is private within this context
  157 |       add_values(get_impl(), rest...);

Solution

  • I used mysqlx::expr("UNIX_TIMESTAMP()"):

    sql_client_.getSession().getDefaultSchema()
        .getTable("event")
        .insert("title", "time")
        .values("event title", mysqlx::expr("UNIX_TIMESTAMP()"))
        .execute();
    

    then fixed the compile error by touching:

    /usr/include/mysql-cppconn-8/mysqlx/devapi/table_crud.h replaced:

      template<typename... Types>
      TableInsert& values(Types... rest)
      {
        try {
          add_values(get_impl(), rest...);
          return *this;
        }
        CATCH_AND_WRAP
      }
    

    with:

      template<typename... Types>
      TableInsert& values(Types&&... rest)
      {
        try {
          add_values(get_impl(), std::forward<Types>(rest)...);
          return *this;
        }
        CATCH_AND_WRAP
      }
    

    /usr/include/mysql-cppconn-8/mysqlx/devapi/detail/crud.h replaced:

      template <typename... T>
      static void add_values(Impl *impl, T... args)
      {
        Add_value::Impl row{ {}, 0 };
        Args_processor<Add_value>::process_args(&row, args...);
        Add_row::process_one(impl, row.first);
      }
    

    with:

      template <typename... T>
      static void add_values(Impl *impl, T&&... args)
      {
        Add_value::Impl row{ {}, 0 };
        Args_processor<Add_value>::process_args(&row, std::forward<T>(args)...);
        Add_row::process_one(impl, row.first);
      }