Results 1 to 7 of 7

Thread: SQL Query Organization

Threaded View

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

    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.

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

    dk (10th January 2014)

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
  •  
Qt is a trademark of The Qt Company.