C++DB is the database layer that was designed to work with C++CMS. This customized version is used within Ye Ol' Pi Shack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

90 lines
3.1 KiB

  1. /*! \page stat Preparing and Executing Statements
  2. \section stat_prep Creating a Statement
  3. The cppdb::statement objects are usually created by calling cppdb::session::prepare() function or by using
  4. cppdb::session::operator<<(). Prepare statements are usually cached for future reuse. Non-prepared statements
  5. can be also created by calling cppdb::session::create_statement() and prepared statements can be created without
  6. caching by calling cppdb::session::create_prepared_uncached_statement(). It is useful when
  7. such statements are rarely executed or executed only once.
  8. For example:
  9. \code
  10. cppdb::statement st=sql.prepare("DELETE FROM users");
  11. \endcode
  12. \section stat_bind Binding Parameters
  13. The statement may contain placeholders marked with "?" for parameters that should be binded. The
  14. values for placeholders can be binded by their order using cppdb::statement::operator<<() or \ref cppdb::statement "bind()"
  15. functions. The placeholder order can be also specified explicitly starting from 1.
  16. For example:
  17. \code
  18. cppdb::statement st=sql.prepare("DELETE FROM users WHERE age<? AND role<>?");
  19. st.bind(1,13);
  20. st.bind(2,"moderator");
  21. \endcode
  22. Of course syntactic sugar may be used as well:
  23. \code
  24. cppdb::statement st=sql << "DELETE FROM users WHERE age<? AND role<>?" << 13 << "moderator";
  25. \endcode
  26. \section stat_null Binding NULL values.
  27. - You can use cppdb::statement::bind_null() functions.
  28. \n
  29. \code
  30. cppdb::statement st=sql << "INSERT into f(x) values(?)";
  31. st.bind_null(1);
  32. \endcode
  33. - You can use manipulator cppdb::null
  34. \n
  35. \code
  36. sql << "INSERT into f(x) values(?)" << cppdb::null << cppdb::exec;
  37. \endcode
  38. - If you have an information about wether the value is null or not only at run time you can use a cppdb::use() manipulator
  39. which receives a value and a parameter of cppdb::null_tag_type.
  40. \n
  41. \code
  42. cppdb::null_tag_type tag = value_is_null ? cppdb::null_value : cppdb::not_null_value;
  43. sql << "INSERT into f(x) values(?)" << cppdb::use(value,tag) << cppdb::exec;
  44. \endcode
  45. \section stat_exec Executing Statements
  46. Statements are executed by calling cppdb::statement::exec(). Also manipulator cppdb::exec provided
  47. for execution of a statement that allows writing complex queries in one line.
  48. \code
  49. sql << "DELETE FROM users WHERE age<? AND role<>?" << 13 << "moderator" <<cppdb::exec;
  50. \endcode
  51. \section stat_meta Fetching Meta-data
  52. Meta-data about recently executed statement can fetched using following functions:
  53. - cppdb::statement::affected() - the number of rows affected by the statement
  54. - cppdb::statement::last_insert_id() - the last generated row id.
  55. - cppdb::statement::sequence_last() - the last generated sequence number (using named sequences).
  56. \section stat_reset Reusing Statement
  57. The same prepared statement can be reused multiple times. For this purpose after each call of ppdb::statement::exec() or ppdb::statement::query(), ppdb::statement::reset() should be called that would clear all bindings and allow executing it once again:
  58. \code
  59. cppdb::statement st = sql << "INSERT INTO students(id,name) values(?,?)";
  60. for(i=0;i<students.size();i++) {
  61. st.bind(students[i].id);
  62. st.bind(students[i].name);
  63. st.exec();
  64. st.reset();
  65. }
  66. \endcode
  67. */