From 0359a82e6ef538316e550e7fa7c6dee8db72a225 Mon Sep 17 00:00:00 2001 From: Nico Vertriest Date: Tue, 28 Jan 2020 12:41:16 +0100 Subject: Doc: make Qt Sql snippets compilable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-number: QTBUG-81496 Change-Id: Id6206e9179c2e8157c99e777a3de35bd83d49e34 Reviewed-by: Topi Reiniƶ Reviewed-by: Paul Wicking --- src/sql/doc/snippets/code/doc_src_sql-driver.cpp | 61 ++++++++++-------- .../snippets/code/doc_src_sql-driver_snippet.cpp | 66 +++++++++++++++++++ .../snippets/code/src_sql_kernel_qsqldatabase.cpp | 73 +++++++++------------- .../code/src_sql_kernel_qsqldatabase_snippet.cpp | 69 ++++++++++++++++++++ .../snippets/code/src_sql_kernel_qsqldriver.cpp | 26 ++++++-- .../doc/snippets/code/src_sql_kernel_qsqlerror.cpp | 8 +++ .../doc/snippets/code/src_sql_kernel_qsqlquery.cpp | 22 +++---- .../code/src_sql_kernel_qsqlquery_snippet.cpp | 58 +++++++++++++++++ .../snippets/code/src_sql_kernel_qsqlresult.cpp | 33 +++++----- .../code/src_sql_kernel_qsqlresult_snippet.cpp | 65 +++++++++++++++++++ .../code/src_sql_models_qsqlquerymodel.cpp | 10 ++- 11 files changed, 385 insertions(+), 106 deletions(-) create mode 100644 src/sql/doc/snippets/code/doc_src_sql-driver_snippet.cpp create mode 100644 src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase_snippet.cpp create mode 100644 src/sql/doc/snippets/code/src_sql_kernel_qsqlquery_snippet.cpp create mode 100644 src/sql/doc/snippets/code/src_sql_kernel_qsqlresult_snippet.cpp (limited to 'src/sql/doc/snippets/code') diff --git a/src/sql/doc/snippets/code/doc_src_sql-driver.cpp b/src/sql/doc/snippets/code/doc_src_sql-driver.cpp index 54576733bf..076a3367dc 100644 --- a/src/sql/doc/snippets/code/doc_src_sql-driver.cpp +++ b/src/sql/doc/snippets/code/doc_src_sql-driver.cpp @@ -47,7 +47,15 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - +#include +#include +#include +#include +#include +#include + +void testProc() +{ //! [2] QSqlQuery q; q.exec("call qtestproc (@outval1, @outval2)"); @@ -55,17 +63,22 @@ q.exec("select @outval1, @outval2"); q.next(); qDebug() << q.value(0) << q.value(1); // outputs "42" and "43" //! [2] +} - +void callStoredProc() +{ //! [10] // STORED_PROC uses the return statement or returns multiple result sets QSqlQuery query; query.setForwardOnly(true); query.exec("{call STORED_PROC}"); //! [10] +} - +void setHost() +{ //! [24] +QSqlDatabase db; db.setHostName("MyServer"); db.setDatabaseName("C:\\test.gdb"); //! [24] @@ -76,8 +89,10 @@ db.setDatabaseName("C:\\test.gdb"); db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1"); db.open(); //! [25] +} - +void exProc() +{ //! [26] QSqlQuery q; q.exec("execute procedure my_procedure"); @@ -85,50 +100,43 @@ q.next(); qDebug() << q.value(0); // outputs the first RETURN/OUT value //! [26] - +qDebug( \ //! [31] -QSqlDatabase: QMYSQL driver not loaded -QSqlDatabase: available drivers: QMYSQL +"QSqlDatabase: QMYSQL driver not loaded \ +QSqlDatabase: available drivers: QMYSQL" \ //! [31] +); - +/* Commented because the following line is not compilable //! [34] column.contains(QRegularExpression("pattern")); //! [34] - - -//! [36] -QSqlQuery query(db); -query.setForwardOnly(true); -query.exec("SELECT * FROM table"); -while (query.next()) { - // Handle changes in every iteration of the loop - QVariant v = query.result()->handle(); - if (qstrcmp(v.typeName(), "PGresult*") == 0) { - PGresult *handle = *static_cast(v.data()); - if (handle) { - // Do something... - } - } +*/ } -//! [36] + +void updTable2() +{ +QSqlDatabase db; //! [37] int value; -QSqlQuery query1(db); +QSqlQuery query1; query1.setForwardOnly(true); query1.exec("select * FROM table1"); while (query1.next()) { value = query1.value(0).toInt(); if (value == 1) { - QSqlQuery query2(db); + QSqlQuery query2; query2.exec("update table2 set col=2"); // WRONG: This will discard all results of } // query1, and cause the loop to quit } //! [37] +} +void setConnString() +{ //! [39] QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3"); QString connectString = QStringLiteral( @@ -139,3 +147,4 @@ QString connectString = QStringLiteral( "SCROLLABLERESULT=true"); db.setDatabaseName(connectString); //! [39] +} diff --git a/src/sql/doc/snippets/code/doc_src_sql-driver_snippet.cpp b/src/sql/doc/snippets/code/doc_src_sql-driver_snippet.cpp new file mode 100644 index 0000000000..7cffe58ff1 --- /dev/null +++ b/src/sql/doc/snippets/code/doc_src_sql-driver_snippet.cpp @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//! [36] +QSqlQuery query; +QVariant v; +query.setForwardOnly(true); +query.exec("SELECT * FROM table"); +while (query.next()) { + // Handle changes in every iteration of the loop + v = query.result()->handle(); + + if (qstrcmp(v.typeName(), "PGresult*") == 0) { + PGresult *handle = *static_cast(v.data()); + if (handle) { + // Do something... + } + } +} +//! [36] diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase.cpp index f09315435e..c7ceb847da 100644 --- a/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase.cpp +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase.cpp @@ -47,18 +47,25 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include +void openDatabase() +{ //! [0] // WRONG QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); QSqlDatabase::removeDatabase("sales"); // will output a warning - // "db" is now a dangling invalid database connection, // "query" contains an invalid result set //! [0] +} - +void removeDatabase() +{ //! [1] { QSqlDatabase db = QSqlDatabase::database("sales"); @@ -67,72 +74,51 @@ QSqlDatabase::removeDatabase("sales"); // will output a warning // Both "db" and "query" are destroyed because they are out of scope QSqlDatabase::removeDatabase("sales"); // correct //! [1] +} - -//! [2] -QSqlDatabase::registerSqlDriver("MYDRIVER", - new QSqlDriverCreator); -QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER"); -//! [2] - - +void setmyDatabase() +{ //! [3] -... -db = QSqlDatabase::addDatabase("QODBC"); +// ... +QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb"); if (db.open()) { // success! } -... +// ... //! [3] +} - -//! [4] -... +// ... // MySQL connection +void dbConnect() +{ +QSqlDatabase db; +//! [4] db.setConnectOptions("SSL_KEY=client-key.pem;SSL_CERT=client-cert.pem;SSL_CA=ca-cert.pem;CLIENT_IGNORE_SPACE=1"); // use an SSL connection to the server if (!db.open()) { db.setConnectOptions(); // clears the connect option string - ... + // ... } -... +// ... // PostgreSQL connection db.setConnectOptions("requiressl=1"); // enable PostgreSQL SSL connections if (!db.open()) { db.setConnectOptions(); // clear options - ... + // ... } -... +// ... // ODBC connection db.setConnectOptions("SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"); // set ODBC options if (!db.open()) { db.setConnectOptions(); // don't try to set this option - ... + // ... +} } //! [4] - -//! [5] -#include "qtdir/src/sql/drivers/psql/qsql_psql.cpp" -//! [5] - - -//! [6] -PGconn *con = PQconnectdb("host=server user=bart password=simpson dbname=springfield"); -QPSQLDriver *drv = new QPSQLDriver(con); -QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection -QSqlQuery query; -query.exec("SELECT NAME, ID FROM STAFF"); -... -//! [6] - - -//! [7] -unix:LIBS += -lpq -win32:LIBS += libpqdll.lib -//! [7] - - +void dbQdebug() +{ //! [8] QSqlDatabase db; qDebug() << db.isValid(); // Returns false @@ -143,3 +129,4 @@ qDebug() << db.isValid(); // Returns \c true if "sales" connection exists QSqlDatabase::removeDatabase("sales"); qDebug() << db.isValid(); // Returns false //! [8] +} diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase_snippet.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase_snippet.cpp new file mode 100644 index 0000000000..a53880fee1 --- /dev/null +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqldatabase_snippet.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [2] +QSqlDatabase::registerSqlDriver("MYDRIVER", new QSqlDriverCreator); +QVERIFY(QSqlDatabase::drivers().contains("MYDRIVER")); +QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER"); +QVERIFY(db.isValid()); +//! [2] +//! [6] +PGconn *con = PQconnectdb("host=server user=bart password=simpson dbname=springfield"); +QPSQLDriver *drv = new QPSQLDriver(con); +QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection +QSqlQuery query; +query.exec("SELECT NAME, ID FROM STAFF"); +//! [6] + +//! [7] +unix:LIBS += -lpq +win32:LIBS += libpqdll.lib +//! [7] + diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqldriver.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqldriver.cpp index a13cf86d3f..47e8701149 100644 --- a/src/sql/doc/snippets/code/src_sql_kernel_qsqldriver.cpp +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqldriver.cpp @@ -47,28 +47,42 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include +void checkHandle() +{ +//dummy definitions +typedef void sqlite3; +typedef void PGconn; +typedef void MYSQL; //! [0] -QSqlDatabase db = ...; +QSqlDatabase db = QSqlDatabase::database(); QVariant v = db.driver()->handle(); -if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) { +if (v.isValid() && (qstrcmp(v.typeName(), "sqlite3*") == 0)) { // v.data() returns a pointer to the handle sqlite3 *handle = *static_cast(v.data()); if (handle) { - ... + // ... } } //! [0] - //! [1] if (qstrcmp(v.typeName(), "PGconn*") == 0) { PGconn *handle = *static_cast(v.data()); - if (handle) ... + if (handle) { + // ... + } } if (qstrcmp(v.typeName(), "MYSQL*") == 0) { MYSQL *handle = *static_cast(v.data()); - if (handle) ... + if (handle) { + // ... + } } //! [1] +} diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqlerror.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqlerror.cpp index 9f6c5da57c..d442768fe2 100644 --- a/src/sql/doc/snippets/code/src_sql_kernel_qsqlerror.cpp +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqlerror.cpp @@ -47,10 +47,18 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include +#include +void checkSqlQueryModel() +{ //! [0] QSqlQueryModel model; model.setQuery("select * from myTable"); if (model.lastError().isValid()) qDebug() << model.lastError(); //! [0] +} diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery.cpp index b99745c749..496c971621 100644 --- a/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery.cpp +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery.cpp @@ -47,12 +47,13 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include -//! [0] -SELECT forename, surname FROM people; -//! [0] - - +void selectEmployees() +{ //! [1] QSqlQuery q("select * from employees"); QSqlRecord rec = q.record(); @@ -63,8 +64,6 @@ int nameCol = rec.indexOf("name"); // index of the field "name" while (q.next()) qDebug() << q.value(nameCol).toString(); // output all names //! [1] - - //! [2] QSqlQuery q; q.prepare("insert into myTable values (?, ?)"); @@ -80,11 +79,4 @@ q.addBindValue(names); if (!q.execBatch()) qDebug() << q.lastError(); //! [2] - - -//! [3] -1 Harald -2 Boris -3 Trond -4 NULL -//! [3] +} diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery_snippet.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery_snippet.cpp new file mode 100644 index 0000000000..d7d2a14d56 --- /dev/null +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqlquery_snippet.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//! [0] +SELECT forename, surname FROM people; +//! [0] +//! [3] +1 Harald +2 Boris +3 Trond +4 NULL +//! [3] diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult.cpp index 8ab2baf2a1..606b6d19fa 100644 --- a/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult.cpp +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult.cpp @@ -47,7 +47,18 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include +#include +#include +// dummy typedef +typedef void *sqlite3_stmt; + +void insertVariants() +{ //! [0] QSqlQuery q; q.prepare("insert into test (i1, i2, s) values (?, ?, ?)"); @@ -67,29 +78,21 @@ q.bindValue(2, col3); if (!q.execBatch()) qDebug() << q.lastError(); //! [0] +} - +void querySqlite() +{ //! [1] -QSqlQuery query = ... +QSqlDatabase db = QSqlDatabase::database("sales"); +QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); + QVariant v = query.result()->handle(); if (v.isValid() && qstrcmp(v.typeName(), "sqlite3_stmt*") == 0) { // v.data() returns a pointer to the handle sqlite3_stmt *handle = *static_cast(v.data()); if (handle) { - ... + // ... } } //! [1] - - -//! [2] -if (qstrcmp(v.typeName(), "PGresult*") == 0) { - PGresult *handle = *static_cast(v.data()); - if (handle) ... -} - -if (qstrcmp(v.typeName(), "MYSQL_STMT*") == 0) { - MYSQL_STMT *handle = *static_cast(v.data()); - if (handle) ... } -//! [2] diff --git a/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult_snippet.cpp b/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult_snippet.cpp new file mode 100644 index 0000000000..2b1891f6c1 --- /dev/null +++ b/src/sql/doc/snippets/code/src_sql_kernel_qsqlresult_snippet.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [2] +if (qstrcmp(v.typeName(), "PGresult*") == 0) { + PGresult *handle = *static_cast(v.data()); + if (handle) { + // ... + } +} + +if (qstrcmp(v.typeName(), "MYSQL_STMT*") == 0) { + MYSQL_STMT *handle = *static_cast(v.data()); + if (handle) { + // ... + } + } +//! [2] diff --git a/src/sql/doc/snippets/code/src_sql_models_qsqlquerymodel.cpp b/src/sql/doc/snippets/code/src_sql_models_qsqlquerymodel.cpp index b3a43537a1..cb2bde6c7d 100644 --- a/src/sql/doc/snippets/code/src_sql_models_qsqlquerymodel.cpp +++ b/src/sql/doc/snippets/code/src_sql_models_qsqlquerymodel.cpp @@ -47,16 +47,24 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include +#include +#include +#include +#include "../sqldatabase/sqldatabase.cpp" +void MyModel::fetchModel() +{ +MyModel *myModel = new MyModel; //! [0] while (myModel->canFetchMore()) myModel->fetchMore(); //! [0] - //! [1] QSqlQueryModel model; model.setQuery("select * from MyTable"); if (model.lastError().isValid()) qDebug() << model.lastError(); //! [1] +} -- cgit v1.2.3 From 3f3e200aef80ac33e6bf116c656ebc28d5918836 Mon Sep 17 00:00:00 2001 From: Paul Wicking Date: Mon, 23 Mar 2020 12:56:02 +0100 Subject: Doc: Fix coverity warnings in SQL snippets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: QTBUG-83008 Change-Id: I126bc04719cd221a3d80ae825fca44e63aeec934 Reviewed-by: Topi Reiniƶ --- src/sql/doc/snippets/code/doc_src_sql-driver.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/sql/doc/snippets/code') diff --git a/src/sql/doc/snippets/code/doc_src_sql-driver.cpp b/src/sql/doc/snippets/code/doc_src_sql-driver.cpp index 076a3367dc..6b5a5e0335 100644 --- a/src/sql/doc/snippets/code/doc_src_sql-driver.cpp +++ b/src/sql/doc/snippets/code/doc_src_sql-driver.cpp @@ -60,8 +60,8 @@ void testProc() QSqlQuery q; q.exec("call qtestproc (@outval1, @outval2)"); q.exec("select @outval1, @outval2"); -q.next(); -qDebug() << q.value(0) << q.value(1); // outputs "42" and "43" +if (q.next()) + qDebug() << q.value(0) << q.value(1); // outputs "42" and "43" //! [2] } @@ -87,7 +87,8 @@ db.setDatabaseName("C:\\test.gdb"); //! [25] // connect to database using the Latin-1 character set db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1"); -db.open(); +if (db.open()) + qDebug("The database connection is open."); //! [25] } @@ -96,8 +97,8 @@ void exProc() //! [26] QSqlQuery q; q.exec("execute procedure my_procedure"); -q.next(); -qDebug() << q.value(0); // outputs the first RETURN/OUT value +if (q.next()) + qDebug() << q.value(0); // outputs the first RETURN/OUT value //! [26] qDebug( \ -- cgit v1.2.3