From: APTX Date: Fri, 12 Apr 2013 11:41:21 +0000 (+0200) Subject: Make search-gui used a prepared statement for search. X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=7616d67ac08906644e5a2a0f2f538222b10388d4;p=localmylist.git Make search-gui used a prepared statement for search. --- diff --git a/search-gui/mainwindow.cpp b/search-gui/mainwindow.cpp index 7445415..08f40c3 100644 --- a/search-gui/mainwindow.cpp +++ b/search-gui/mainwindow.cpp @@ -11,8 +11,8 @@ using namespace LocalMyList; MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) + QMainWindow(parent), + ui(new Ui::MainWindow) { ui->setupUi(this); MyList::instance()->loadLocalSettings(); @@ -32,13 +32,31 @@ MainWindow::~MainWindow() void MainWindow::on_search_textChanged(const QString &text) { - QSqlQuery q( - "SELECT a.aid, b.title AS MainTitle, a.title, a.language, a.type FROM anime_title a" - " LEFT JOIN anime_title b on b.aid = a.aid" - " WHERE lower(a.title) LIKE '%" + text + "%'" - " AND b.type = 1" - " ORDER BY a.title ASC, a.aid ASC", MyList::instance()->database()->connection()); -qDebug() << q.lastError(); + QString query; + + if (text.isEmpty()) + { + model->setQuery(QSqlQuery()); + return; + } + + if (text.length() > 3) + query = "%" + text + "%"; + else + query = text + "%"; + + QSqlQuery &q = MyList::instance()->database()->prepare( + "SELECT a.aid, b.title AS main_title, a.title, a.language, a.type, a.title <-> :word AS distance FROM anime_title a " + " LEFT JOIN anime_title b on b.aid = a.aid " + " WHERE a.title ILIKE :query " + " AND b.type = 1 " + " ORDER BY distance ASC, a.title ASC, a.aid ASC " + " LIMIT 100"); + q.bindValue(":word", text); + q.bindValue(":query", query); + + MyList::instance()->database()->exec(q); model->setQuery(q); + ui->resultView->resizeColumnsToContents(); }