tools/assistant/lib/fulltextsearch/qqueryparser.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team.
       
     4 ** All rights reserved.
       
     5 **
       
     6 ** Portion Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     7 ** All rights reserved.
       
     8 **
       
     9 ** This file may be used under the terms of the GNU Lesser General Public
       
    10 ** License version 2.1 as published by the Free Software Foundation and
       
    11 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    12 ** Please review the following information to ensure the GNU Lesser General
       
    13 ** Public License version 2.1 requirements will be met:
       
    14 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    15 **
       
    16 ****************************************************************************/
       
    17 
       
    18 #include "qqueryparser_p.h"
       
    19 #include "qquery_p.h"
       
    20 #include "qclucene_global_p.h"
       
    21 
       
    22 #include <CLucene.h>
       
    23 #include <CLucene/queryParser/QueryParser.h>
       
    24 
       
    25 QT_BEGIN_NAMESPACE
       
    26 
       
    27 QCLuceneQueryParserPrivate::QCLuceneQueryParserPrivate()
       
    28     : QSharedData()
       
    29 {
       
    30     queryParser = 0;
       
    31     deleteCLuceneQueryParser = true;
       
    32 }
       
    33 
       
    34 QCLuceneQueryParserPrivate::QCLuceneQueryParserPrivate(const QCLuceneQueryParserPrivate &other)
       
    35     : QSharedData()
       
    36 {
       
    37     queryParser = _CL_POINTER(other.queryParser);
       
    38     deleteCLuceneQueryParser = other.deleteCLuceneQueryParser;
       
    39 }
       
    40 
       
    41 QCLuceneQueryParserPrivate::~QCLuceneQueryParserPrivate()
       
    42 {
       
    43     if (deleteCLuceneQueryParser)
       
    44         _CLDECDELETE(queryParser);
       
    45 }
       
    46 
       
    47 
       
    48 QCLuceneQueryParser::QCLuceneQueryParser(const QString &field,
       
    49                                          QCLuceneAnalyzer &analyzer)
       
    50     : d(new QCLuceneQueryParserPrivate())
       
    51     , field(field)
       
    52     , analyzer(analyzer)
       
    53 {
       
    54     TCHAR *fieldName = QStringToTChar(field);
       
    55 
       
    56     d->queryParser = new lucene::queryParser::QueryParser(fieldName,
       
    57         analyzer.d->analyzer);
       
    58 
       
    59     delete [] fieldName;
       
    60 }
       
    61 
       
    62 QCLuceneQueryParser::~QCLuceneQueryParser()
       
    63 {
       
    64     // nothing todo
       
    65 }
       
    66 
       
    67 QCLuceneQuery* QCLuceneQueryParser::parse(const QString &query)
       
    68 {
       
    69     TCHAR *string = QStringToTChar(query);
       
    70 
       
    71     QCLuceneQuery *retValue = 0;
       
    72     lucene::search::Query* q = d->queryParser->parse(string);
       
    73     if (q) {
       
    74         retValue = new QCLuceneQuery();
       
    75         retValue->d->query = q;
       
    76     }
       
    77 
       
    78     delete [] string;
       
    79     return retValue;
       
    80 }
       
    81 
       
    82 QCLuceneQuery* QCLuceneQueryParser::parse(QCLuceneReader &reader)
       
    83 {
       
    84     QCLuceneQuery *retValue = 0;
       
    85     lucene::search::Query* q = d->queryParser->parse(reader.d->reader);
       
    86     if (q) {
       
    87         retValue = new QCLuceneQuery();
       
    88         retValue->d->query = q;
       
    89     }
       
    90 
       
    91     return retValue;
       
    92 }
       
    93 
       
    94 QCLuceneQuery* QCLuceneQueryParser::parse(const QString &query, const QString &field,
       
    95                                           QCLuceneAnalyzer &analyzer)
       
    96 {
       
    97     QCLuceneQueryParser parser(field, analyzer);
       
    98     return parser.parse(query);
       
    99 }
       
   100 
       
   101 QCLuceneAnalyzer QCLuceneQueryParser::getAnalyzer()
       
   102 {
       
   103     return analyzer;
       
   104 }
       
   105 
       
   106 QString QCLuceneQueryParser::getField()
       
   107 {
       
   108     return field;
       
   109 }
       
   110 
       
   111 
       
   112 QCLuceneMultiFieldQueryParser::QCLuceneMultiFieldQueryParser(
       
   113     const QStringList &fieldList, QCLuceneAnalyzer &analyzer)
       
   114     : QCLuceneQueryParser(QLatin1String(""), analyzer)
       
   115 {
       
   116     Q_UNUSED(fieldList)
       
   117 }
       
   118 
       
   119 QCLuceneMultiFieldQueryParser::~QCLuceneMultiFieldQueryParser()
       
   120 {
       
   121     // nothing todo
       
   122 }
       
   123 
       
   124 QCLuceneQuery* QCLuceneMultiFieldQueryParser::parse(const QString &query,
       
   125                                                     const QStringList &fieldList,
       
   126                                                     QCLuceneAnalyzer &analyzer)
       
   127 {
       
   128     QCLuceneBooleanQuery *retValue = new QCLuceneBooleanQuery();
       
   129     foreach (const QString &field, fieldList) {
       
   130         QCLuceneQuery *q = QCLuceneQueryParser::parse(query, field, analyzer);
       
   131         if (!q) {
       
   132             delete retValue;
       
   133             retValue = 0; break;
       
   134         } else {
       
   135             retValue->add(q, true, false, false);
       
   136         }
       
   137     }
       
   138 
       
   139     return retValue;
       
   140 }
       
   141 
       
   142 QCLuceneQuery* QCLuceneMultiFieldQueryParser::parse(const QString &query,
       
   143                                                     const QStringList &fieldList,
       
   144                                                     QList<FieldFlags> flags, 
       
   145                                                     QCLuceneAnalyzer &analyzer)
       
   146 {
       
   147     QCLuceneBooleanQuery *retValue = new QCLuceneBooleanQuery();
       
   148     qint32 i = 0;
       
   149     foreach (const QString &field, fieldList) {
       
   150 		QCLuceneQuery *q = QCLuceneQueryParser::parse(query, field, analyzer);
       
   151         if (q) {
       
   152             qint32 flag = flags.at(i);
       
   153             switch (flag) {
       
   154                 case QCLuceneMultiFieldQueryParser::REQUIRED_FIELD: {
       
   155                     retValue->add(q, true, true, false);
       
   156                 }   break;
       
   157 
       
   158                 case QCLuceneMultiFieldQueryParser::PROHIBITED_FIELD: {
       
   159                     retValue->add(q, true, false, true);
       
   160                 }   break;
       
   161 
       
   162                 default: {
       
   163                     retValue->add(q, true, false, false);
       
   164                 }   break;
       
   165             }
       
   166 
       
   167             ++i;
       
   168         } else {
       
   169             delete retValue;
       
   170             retValue = 0; break;
       
   171         }
       
   172     }
       
   173     return retValue;
       
   174 }
       
   175 
       
   176 QT_END_NAMESPACE