Results 1 to 7 of 7

Thread: SQL Query Organization

  1. #1
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question SQL Query Organization

    Hi there,
    I'm about to start a project with an mysql database. now comes the question how to organize all the queries. Do you have any tips?
    My plan would be to put all the queries in one object and return them to the caller. Then the queries would look more or less like this:

    Qt Code:
    1. QSqlQuery myClass::GetUser(int userID)
    2. {
    3. QString s("SELECT * FROM User WHERE User.ID = userID;");
    4.  
    5. q.prepare(s);
    6.  
    7. return q;
    8. }
    To copy to clipboard, switch view to plain text mode 

    My App will have maybe 100 - 200 queries, so, some organization might be a good idea.

    any advice in very appreciated.
    Thanks a lot in advance

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: SQL Query Organization

    I don't think returning query objects is a good idea. Imagine that you have 10 client classes using this code and someday you need to change the database structure - now you have to go through all the client classes code and update the query access.
    Better option is to directly return the data from the database, without exposing internal database structure:
    Qt Code:
    1. class User{
    2. public:
    3. User(QString name, QString address, ...) :_name(name), _address(address) ... {
    4. }
    5. QString name() const{ return _name; }
    6. QString address() const{ return _address; }
    7. // .. other access methods
    8. private:
    9. QString _name;
    10. QString _address;
    11. QString _whatever;
    12. ...
    13. };
    14.  
    15. User myClass::GetUser(int userId)
    16. {
    17. q.prepare("SELECT name,address,whatever FROM User WHERE User.ID = :userId;");
    18. q.bindValue(":id",userId);
    19. if (q.next()) {
    20. return User(q.value("name").toString(), q.value("address").toString(), ...);
    21. }
    22. throw NoSuchUserException; //.. or another way of reporting an error
    23. }
    To copy to clipboard, switch view to plain text mode 
    This way database access code and db structure are encapsulated and separated from the rest of the program.

  3. The following user says thank you to stampede for this useful post:

    dk (10th January 2014)

  4. #3
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL Query Organization

    Thanks a million my friend.
    The main idea behind returning the query is: i could use the model / view architecture, and simply pass the executed query to a qTableView.
    So, it is better to write a custom model for each view or just use regular widgets e.g. qTableWidget to present the data?

    Thanks again for the help

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: SQL Query Organization

    So, it is better to write a custom model for each view or just use regular widgets e.g. qTableWidget to present the data?
    I don't know, it depends on the application. Maybe for your needs it will be just fine to use a simple table widget. I'm not a specialist on table view/model things, so maybe someone else will have some hints for you.
    My point is, if you hardcode db access with queries you may find it inconvenient in some cases. For example, your version of "GetUser" method could be uncomfortable if we have to present user details in a labels or set of line edits for editing:
    Qt Code:
    1. // here you can see 'GetUser' method name is not very self-explanatory
    2. QSqlQuery query = db.GetUser(id);
    3. ui.label_name->setText(query.value("name").toString());
    4. ui.label_address->setText(query.value("address").toString());
    5.  
    6. vs.
    7.  
    8. User user = db.GetUser(id);
    9. ui.label_name->setText(user.name());
    10. ui.label_address->setText(user.address());
    11. // etc
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to stampede for this useful post:

    dk (11th January 2014)

  7. #5
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL Query Organization

    True, you are right! def much more readable. But i do have an other question.
    I can see the massive benefit of arranging the queries the way you explained. But how about inner joins? or a query from multible tables?

    For example i designed the database in the way that a user has a user_status. so i created a table called user_status_tbl. with an ID and a Description
    The user_tbl contains one column called user_status which referes to the user_status_tbl.

    How can i implement that without having to programm the SQLQuery in C++ and also take advantave of the simplicity and speed of an SQLQuery?

    Thanks again my friend, ou are so helpful!

  8. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: SQL Query Organization

    Great, i'm glad I can help
    But how about inner joins? or a query from multible tables?

    For example i designed the database in the way that a user has a user_status. so i created a table called user_status_tbl. with an ID and a Description
    The user_tbl contains one column called user_status which referes to the user_status_tbl.

    How can i implement that without having to programm the SQLQuery in C++ and also take advantave of the simplicity and speed of an SQLQuery?
    You can still use SQL code to do cross-table selections and return this data in a data structure, for example:
    Qt Code:
    1. // extended version of User class
    2. class UserExt{
    3. public:
    4. UserExt(QString name, QString address, QString description...)
    5. :_name(name)
    6. ,_address(address)
    7. ,_description(description) ... {
    8. }
    9. QString name() const{ return _name; }
    10. QString address() const{ return _address; }
    11. QString description() const{ return _description; }
    12. // .. other access methods
    13. private:
    14. QString _name;
    15. QString _address;
    16. QString _description;
    17. ...
    18. };
    19.  
    20. // it is just a pseudocode but i think you get the idea
    21. User myClass::GetUser(int userId)
    22. {
    23. q.prepare("SELECT u.name,u.address,st.description FROM User as u, user_status_tbl as st, othertable as ot, ... WHERE u.ID = :userId AND u.id=st.id AND ot.id=st.id, AND ...;");
    24. q.bindValue(":id",userId);
    25. if (q.next()) {
    26. return UserExt(q.value("name").toString(), q.value("address").toString(), q.value("description").toString()...);
    27. }
    28. throw new NoSuchUserException; //.. or another way of reporting an error
    29. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to stampede for this useful post:

    dk (14th January 2014)

  10. #7
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL Query Organization

    Hello stampede,

    very good idea with putting each query in an object. Much easier for further processing! Especialy the more complex queryies become more unified towards the actual application, fantastic!
    Thanks you very much for all your help and effort, i wish i could pay a beer for you my friend

Similar Threads

  1. Qt Creator Subfolder Organization
    By hiead in forum Newbie
    Replies: 3
    Last Post: 21st October 2013, 00:50
  2. Replies: 2
    Last Post: 13th January 2011, 18:36
  3. About tree organization...
    By Patrick Sorcery in forum Newbie
    Replies: 2
    Last Post: 3rd September 2010, 07:43
  4. SQL query
    By JD2000 in forum Newbie
    Replies: 4
    Last Post: 1st December 2009, 14:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.