[QGIS Commit] r13754 - in trunk/qgis: python/core src/app src/app/attributetable src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Jun 20 11:52:54 EDT 2010


Author: wonder
Date: 2010-06-20 15:52:54 +0000 (Sun, 20 Jun 2010)
New Revision: 13754

Modified:
   trunk/qgis/python/core/qgssearchtreenode.sip
   trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp
   trunk/qgis/src/app/qgssearchquerybuilder.cpp
   trunk/qgis/src/core/qgssearchstringlexer.ll
   trunk/qgis/src/core/qgssearchtreenode.cpp
   trunk/qgis/src/core/qgssearchtreenode.h
Log:
Fixed #2346 - allow quoting of column references using double quotes.
Quoting is done in both search query builder and search from attribute table.


Modified: trunk/qgis/python/core/qgssearchtreenode.sip
===================================================================
--- trunk/qgis/python/core/qgssearchtreenode.sip	2010-06-20 15:21:04 UTC (rev 13753)
+++ trunk/qgis/python/core/qgssearchtreenode.sip	2010-06-20 15:52:54 UTC (rev 13754)
@@ -115,6 +115,10 @@
     //! @note added in 1.5
     bool needsGeometry();
 
+    //! return quoted column reference (in double quotes)
+    //! @note added in 1.5
+    static QString quotedColumnRef( QString name );
+
   protected:
 
 

Modified: trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp	2010-06-20 15:21:04 UTC (rev 13753)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp	2010-06-20 15:52:54 UTC (rev 13754)
@@ -615,21 +615,17 @@
 void QgsAttributeTableDialog::search()
 {
 
-  QString str = mColumnBox->currentText();
-
+  QString fieldName = mColumnBox->currentText();
   const QgsFieldMap& flds = mLayer->pendingFields();
-  int fldIndex = mLayer->fieldNameIndex( str );
+  int fldIndex = mLayer->fieldNameIndex( fieldName );
   QVariant::Type fldType = flds[fldIndex].type();
   bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
 
-  if ( numeric )
-    str += " = '";
-  else
-    str += " ~ '";
+  QString str = QString( "%1 %2 '%3'" )
+                .arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
+                .arg( numeric ? "=" : "~" )
+                .arg( mQuery->displayText().replace( "'", "''" ) ); // escape quotes
 
-  str += mQuery->displayText().replace( "'", "''" ); // escape quotes
-  str += "'";
-
   doSearch( str );
 }
 

Modified: trunk/qgis/src/app/qgssearchquerybuilder.cpp
===================================================================
--- trunk/qgis/src/app/qgssearchquerybuilder.cpp	2010-06-20 15:21:04 UTC (rev 13753)
+++ trunk/qgis/src/app/qgssearchquerybuilder.cpp	2010-06-20 15:52:54 UTC (rev 13754)
@@ -80,11 +80,14 @@
 void QgsSearchQueryBuilder::populateFields()
 {
   QgsDebugMsg( "entering." );
+  QRegExp reQuote( "[A-Za-z_][A-Za-z0-9_]*" );
   const QgsFieldMap& fields = mLayer->pendingFields();
   for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
   {
     QString fieldName = it->name();
     mFieldMap[fieldName] = it.key();
+    if ( !reQuote.exactMatch( fieldName ) ) // quote if necessary
+      fieldName = QgsSearchTreeNode::quotedColumnRef( fieldName );
     QStandardItem *myItem = new QStandardItem( fieldName );
     myItem->setEditable( false );
     mModelFields->insertRow( mModelFields->rowCount(), myItem );

Modified: trunk/qgis/src/core/qgssearchstringlexer.ll
===================================================================
--- trunk/qgis/src/core/qgssearchstringlexer.ll	2010-06-20 15:21:04 UTC (rev 13753)
+++ trunk/qgis/src/core/qgssearchstringlexer.ll	2010-06-20 15:52:54 UTC (rev 13754)
@@ -49,6 +49,9 @@
 col_next     [A-Za-z0-9_]|{non_ascii}
 column_ref  {col_first}{col_next}*
 
+col_str_char  "\"\""|[^\"]
+column_ref_quoted  "\""{col_str_char}*"\""
+
 dig     [0-9]
 num1    {dig}+\.?([eE][-+]?{dig}+)?
 num2    {dig}*\.{dig}+([eE][-+]?{dig}+)?
@@ -101,6 +104,7 @@
 "$length" { return LENGTH; }
 
 {column_ref}   { return COLUMN_REF; }
+{column_ref_quoted}  { return COLUMN_REF; }
 
 {white}    /* skip blanks and tabs */
 

Modified: trunk/qgis/src/core/qgssearchtreenode.cpp
===================================================================
--- trunk/qgis/src/core/qgssearchtreenode.cpp	2010-06-20 15:21:04 UTC (rev 13753)
+++ trunk/qgis/src/core/qgssearchtreenode.cpp	2010-06-20 15:52:54 UTC (rev 13754)
@@ -70,6 +70,11 @@
   {
     mType = tColumnRef;
     mText = text;
+    if ( text.at( 0 ) == '\"' )
+    {
+      // column reference is quoted
+      stripColRef();
+    }
   }
   else
   {
@@ -162,6 +167,21 @@
 
 }
 
+void QgsSearchTreeNode::stripColRef()
+{
+  // strip double quotes on start,end
+  mText = mText.mid( 1, mText.length() - 2 );
+
+  // make single "double quotes" from double "double quotes"
+  mText.replace( QRegExp( "\"\"" ), "\"" );
+}
+
+QString QgsSearchTreeNode::quotedColumnRef( QString name )
+{
+  return QString( "\"%1\"" ).arg( name.replace( "\"", "\"\"" ) );
+}
+
+
 QString QgsSearchTreeNode::makeSearchString()
 {
   QString str;

Modified: trunk/qgis/src/core/qgssearchtreenode.h
===================================================================
--- trunk/qgis/src/core/qgssearchtreenode.h	2010-06-20 15:21:04 UTC (rev 13753)
+++ trunk/qgis/src/core/qgssearchtreenode.h	2010-06-20 15:52:54 UTC (rev 13754)
@@ -153,6 +153,10 @@
     //! @note added in 1.5
     bool needsGeometry();
 
+    //! return quoted column reference (in double quotes)
+    //! @note added in 1.5
+    static QString quotedColumnRef( QString name );
+
   protected:
 
 
@@ -162,6 +166,9 @@
     //! strips mText when node is of string type
     void stripText();
 
+    //! strip mText when column reference is quoted
+    void stripColRef();
+
     //! initialize node's internals
     void init();
 



More information about the QGIS-commit mailing list