A C++ DAL / ORM code generation framework
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.

85 lines
2.5 KiB

13 years ago
13 years ago
13 years ago
  1. #include "../src/sql/wsqldatabase.h"
  2. #include <iostream>
  3. int main()
  4. {
  5. WSql::WSqlDatabase db ( WSql::WMYSQL );
  6. db.setDatabaseName ( std::string ( "sakila" ) );
  7. db.setUserName ( "root" );
  8. if ( !db.open() )
  9. {
  10. std::cerr << "Failed to open: " << db.error().text() << std::endl;
  11. return 1;
  12. }
  13. std::vector<std::string>tables = db.tableNames();
  14. if ( db.hasError() )
  15. std::cout << "error: " << db.error().text() << std::endl;
  16. std::vector<std::string>::iterator it = tables.begin();
  17. WSql::WSqlTable stbl;
  18. int numflds = 0;
  19. int row = 0;
  20. while ( it != tables.end() )
  21. {
  22. std::vector<std::string> column_names;
  23. stbl = db.tableMetaData ( *it );
  24. numflds = stbl.count();
  25. std::cout << " ======== Table name: " << stbl.name()
  26. << " =========" << std::endl;
  27. for ( int i = 0; i < numflds; ++i )
  28. {
  29. WSql::WSqlColumn clm = stbl.column ( i );
  30. column_names.push_back ( clm.columnName() );
  31. std::cout << "Column " << i << " = " << clm.columnName() << std::endl;
  32. std::cout << " * Data type: " << WSql::WSqlDataType::toString ( clm.dataType() ) << std::endl;
  33. std::cout << " * Max Length: " << clm.maxLength() << std::endl;
  34. std::cout << " * Unsigned: " << ( clm.isUnsigned() ? "true" : "false" ) << std::endl;
  35. std::cout << " * Can be null: " << ( clm.canBeNull() ? "true" : "false" ) << std::endl;
  36. std::cout << " * Primary key: " << ( clm.isPrimaryKey() ? "true" : "false" ) << std::endl;
  37. std::cout << " * Autoincrement: " << ( clm.isAutoIncremented() ? "true" : "false" ) << std::endl;
  38. std::cout << " * default value: " << clm.defaultValue<std::string>() << std::endl;
  39. }
  40. std::string sql = "select * from " + *it + " limit 2;";
  41. if ( !db.doQuery ( sql ) )
  42. {
  43. std::cout << "Query Failed: " << db.error().text() << std::endl;
  44. it++;
  45. continue;
  46. }
  47. else
  48. {
  49. WSql::WSqlResult *result = db.getResult();
  50. WSql::WSqlRecord record = result->fetchFirst();
  51. while ( !record.empty() )
  52. {
  53. std::cout << "Record Size: " << record.size() << std::endl;
  54. WSql::WSqlField fld;
  55. int pt = 0;
  56. for ( ; pt < record.size(); pt++ )
  57. {
  58. // std::cout << "data for " << column_names[pt] << ": " << record[column_names[pt]] << std::endl;
  59. //no danger - if there is nothing there we still get an empty field object
  60. fld = record.field ( pt );
  61. std::cout << "Field " << fld.name() << ", Origin Column " << fld.columnName() << ": "
  62. << fld.data<std::string>() << std::endl;
  63. }
  64. record = result->fetchNext();
  65. }
  66. }
  67. it++;
  68. }
  69. }