Skip to content

Commit e48766e

Browse files
author
wonder
committedJun 20, 2010
Fixed #2346 - allow quoting of column references using double quotes.
Quoting is done in both search query builder and search from attribute table. git-svn-id: http://svn.osgeo.org/qgis/trunk@13754 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0490af7 commit e48766e

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed
 

‎python/core/qgssearchtreenode.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class QgsSearchTreeNode
115115
//! @note added in 1.5
116116
bool needsGeometry();
117117

118+
//! return quoted column reference (in double quotes)
119+
//! @note added in 1.5
120+
static QString quotedColumnRef( QString name );
121+
118122
protected:
119123

120124

‎src/app/attributetable/qgsattributetabledialog.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -615,20 +615,16 @@ void QgsAttributeTableDialog::doSearch( QString searchString )
615615
void QgsAttributeTableDialog::search()
616616
{
617617

618-
QString str = mColumnBox->currentText();
619-
618+
QString fieldName = mColumnBox->currentText();
620619
const QgsFieldMap& flds = mLayer->pendingFields();
621-
int fldIndex = mLayer->fieldNameIndex( str );
620+
int fldIndex = mLayer->fieldNameIndex( fieldName );
622621
QVariant::Type fldType = flds[fldIndex].type();
623622
bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
624623

625-
if ( numeric )
626-
str += " = '";
627-
else
628-
str += " ~ '";
629-
630-
str += mQuery->displayText().replace( "'", "''" ); // escape quotes
631-
str += "'";
624+
QString str = QString( "%1 %2 '%3'" )
625+
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
626+
.arg( numeric ? "=" : "~" )
627+
.arg( mQuery->displayText().replace( "'", "''" ) ); // escape quotes
632628

633629
doSearch( str );
634630
}

‎src/app/qgssearchquerybuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,14 @@ QgsSearchQueryBuilder::~QgsSearchQueryBuilder()
8080
void QgsSearchQueryBuilder::populateFields()
8181
{
8282
QgsDebugMsg( "entering." );
83+
QRegExp reQuote( "[A-Za-z_][A-Za-z0-9_]*" );
8384
const QgsFieldMap& fields = mLayer->pendingFields();
8485
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
8586
{
8687
QString fieldName = it->name();
8788
mFieldMap[fieldName] = it.key();
89+
if ( !reQuote.exactMatch( fieldName ) ) // quote if necessary
90+
fieldName = QgsSearchTreeNode::quotedColumnRef( fieldName );
8891
QStandardItem *myItem = new QStandardItem( fieldName );
8992
myItem->setEditable( false );
9093
mModelFields->insertRow( mModelFields->rowCount(), myItem );

‎src/core/qgssearchstringlexer.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ col_first [A-Za-z_]|{non_ascii}
4949
col_next [A-Za-z0-9_]|{non_ascii}
5050
column_ref {col_first}{col_next}*
5151

52+
col_str_char "\"\""|[^\"]
53+
column_ref_quoted "\""{col_str_char}*"\""
54+
5255
dig [0-9]
5356
num1 {dig}+\.?([eE][-+]?{dig}+)?
5457
num2 {dig}*\.{dig}+([eE][-+]?{dig}+)?
@@ -101,6 +104,7 @@ string "'"{str_char}*"'"
101104
"$length" { return LENGTH; }
102105
103106
{column_ref} { return COLUMN_REF; }
107+
{column_ref_quoted} { return COLUMN_REF; }
104108
105109
{white} /* skip blanks and tabs */
106110

‎src/core/qgssearchtreenode.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ QgsSearchTreeNode::QgsSearchTreeNode( QString text, bool isColumnRef )
7070
{
7171
mType = tColumnRef;
7272
mText = text;
73+
if ( text.at( 0 ) == '\"' )
74+
{
75+
// column reference is quoted
76+
stripColRef();
77+
}
7378
}
7479
else
7580
{
@@ -162,6 +167,21 @@ void QgsSearchTreeNode::stripText()
162167

163168
}
164169

170+
void QgsSearchTreeNode::stripColRef()
171+
{
172+
// strip double quotes on start,end
173+
mText = mText.mid( 1, mText.length() - 2 );
174+
175+
// make single "double quotes" from double "double quotes"
176+
mText.replace( QRegExp( "\"\"" ), "\"" );
177+
}
178+
179+
QString QgsSearchTreeNode::quotedColumnRef( QString name )
180+
{
181+
return QString( "\"%1\"" ).arg( name.replace( "\"", "\"\"" ) );
182+
}
183+
184+
165185
QString QgsSearchTreeNode::makeSearchString()
166186
{
167187
QString str;

‎src/core/qgssearchtreenode.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ class CORE_EXPORT QgsSearchTreeNode
153153
//! @note added in 1.5
154154
bool needsGeometry();
155155

156+
//! return quoted column reference (in double quotes)
157+
//! @note added in 1.5
158+
static QString quotedColumnRef( QString name );
159+
156160
protected:
157161

158162

@@ -162,6 +166,9 @@ class CORE_EXPORT QgsSearchTreeNode
162166
//! strips mText when node is of string type
163167
void stripText();
164168

169+
//! strip mText when column reference is quoted
170+
void stripColRef();
171+
165172
//! initialize node's internals
166173
void init();
167174

0 commit comments

Comments
 (0)
Please sign in to comment.