Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt parsing sql statement
Forum Updated to NodeBB v4.3 + New Features

Qt parsing sql statement

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hanses
    wrote on 3 Aug 2018, 13:03 last edited by
    #1

    Hi everyone,

    I'm relatively new to Qt and sql. Been building my own Qt driver/plugin for a relatively unknown database. Everything is working and going all fine. However, I have run into a weird problem.

    Qt parses my statements. This meaning when I prepare the statement:

    QVERIFY(query.prepare("insert into tab_char values(?,?,?)"));

    The actual statement being sent to the database is:

    "insert into tab_char values(:a,:bb,:bc)"

    That is usually fine, however can sometimes be problematic.

    This would not be stange if it wasn't for the fact that in the "hasFeature" function for the driver both NamedPlaceholders and PositionalPlaceholders are set to be true and if you look at whats written on, http://doc.qt.io/qt-5/sql-sqlstatements.html, is says the following:

    "Qt supports two placeholder syntaxes: named binding and positional binding. Here's an example of named binding. Both syntaxes work with all database drivers provided by Qt. If the database supports the syntax natively, Qt simply forwards the query to the DBMS; otherwise, Qt simulates the placeholder syntax by preprocessing the query."

    I'm therefore a bit confused why Qt seem to be, per default, using namedPlaceholders. When I'm using "null indicators" it is problomatic that Qt parses my statements.

    I'm aware of the hasFeature as i said before, so i know I can turn off namedPlaceholders etc. However i want both of them and I just don't understand why Qt parses the statement, the essential part of hasFeature looks like this:

    bool QMSQLDriver::hasFeature(DriverFeature f) const {
    switch (f) {
    case NamedPlaceholdercans:
    case PositionalPlaceholders:
    return true;
    }
    }

    Is there any other way to set the options for Qt to parse the sql statements? Would be very grateful for some help, and if the situation isn't clear just ask questions and I will explain it further.

    Regards

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Aug 2018, 13:14 last edited by
      #2

      Hi and welcome to devnet,

      You should also return false for all the other cases.

      By the way, what about PreparedQueries ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hanses
        wrote on 3 Aug 2018, 14:36 last edited by Hanses 8 Mar 2018, 14:36
        #3

        Hi and thanks for replying SGaist.

        Didn't post the whole function, just part of it. The whole hasFeature function looks like this:

        bool QMSQLDriver::hasFeature(DriverFeature f) const {
        switch (f) {
        case LastInsertId:
        case EventNotifications:
        return false;
        case Transactions:
        case QuerySize:
        case BLOB:
        case Unicode:
        case PreparedQueries:
        case PositionalPlaceholders:
        case NamedPlaceholders:
        case BatchOperations:
        case SimpleLocking:
        case LowPrecisionNumbers:
        case FinishQuery:
        case MultipleResultSets:
        case CancelQuery:
        return true;
        default:
        return false;
        }
        }

        Where all three of PreparedQueries, PositionalPlaceholders, NamedPlaceholders are true, which i thought they should be since the database has those feature. Most other cases are also true since they will be implemented in the database, also assumed that they wouldn't matter for the problem at hand.

        Should I be setting any of the cases to false to be able to achieve no parsing of my statement?

        Thanks again for replying I really appreciate you taking the time :)

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 3 Aug 2018, 15:58 last edited by VRonin 8 Mar 2018, 19:54
          #4

          What I don't understand is: is this causing you any problem?

          For what I understand Qt uniforms all the named bindings into positional bindings and then converts them to named binding if hasFeature(NamedPlaceholders)==true. This allows users to mix the two up query.prepare("insert into tab_char values(?,:namedVar,?)"). The fact that the driver receives only named binding is expected.

          Do you have a test case where this normalisation causes a break in functionality?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3
          • H Offline
            H Offline
            Hanses
            wrote on 6 Aug 2018, 07:20 last edited by
            #5

            Hi and thanks for replying,

            Well yes this is causing me problem.. When you bind using "?" and Qt parses the statement to ":aa" or something similar, this has an impact on how the database interprets the sql statement. For my datebase you need to use the syntax ":aa:bb" in the binding if you want to be able to set a variable to null, (using a null indicator). When then trying to set a value to null but the statement the database retrieves doesn't contain a null indicator name for that specific field it results in an error.

            Ex on statements that are acceptable:

            (1)
            create table TAB (C1 integer);
            insert into TAB (values ?);

            (2)
            create table TAB (C1 integer);
            insert into TAB (values :a:i1);

            Ex. of statements that are not acceptable:

            (3)
            create table TAB (C1 integer not null);
            insert into TAB (values ?);

            (4)
            create table TAB (C1 integer primary key); -- primary key implicates not null
            insert into TAB (values ?);

            (5)
            create table TAB (C1 integer);
            insert into TAB (values :a);

            (And then continuing by setting/binding the value to null.. )

            Nr. 5 is the one causing me problem...
            When you write a statement like nr 1, the database receives nr 5 and then when you try to bind the variable to null you get error cause the statement doesn't contain a null indicator name.

            If it is expected that the driver receives only named binding, there is't much to do i guess... The problem is practically solved by turning off namedPlaceholders --> the database receives "?" which is better cause the database doesn't have as much restrictions for that case.

            I just figured that there should be a way to make Qt not parse/ do anything with my sql statement.

            But really appreciate you taking the time to answer :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 6 Aug 2018, 20:40 last edited by
              #6

              Which version of Qt are you using ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Hanses
                wrote on 7 Aug 2018, 07:18 last edited by
                #7

                Using Qt version 5.9.5

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 7 Aug 2018, 18:48 last edited by
                  #8

                  I’d check with a more recent version to see if you still have the same behavior.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  1/8

                  3 Aug 2018, 13:03

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved