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.

65 lines
2.7 KiB

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