Skip to content

Commit

Permalink
Fixed #2346 - allow quoting of column references using double quotes.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wonder committed Jun 20, 2010
1 parent 0490af7 commit e48766e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions python/core/qgssearchtreenode.sip
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class QgsSearchTreeNode
//! @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:


Expand Down
16 changes: 6 additions & 10 deletions src/app/attributetable/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,20 +615,16 @@ void QgsAttributeTableDialog::doSearch( QString searchString )
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 += " ~ '";

str += mQuery->displayText().replace( "'", "''" ); // escape quotes
str += "'";
QString str = QString( "%1 %2 '%3'" )
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
.arg( numeric ? "=" : "~" )
.arg( mQuery->displayText().replace( "'", "''" ) ); // escape quotes

doSearch( str );
}
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgssearchquerybuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ QgsSearchQueryBuilder::~QgsSearchQueryBuilder()
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 );
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgssearchstringlexer.ll
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ col_first [A-Za-z_]|{non_ascii}
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}+)?
Expand Down Expand Up @@ -101,6 +104,7 @@ string "'"{str_char}*"'"
"$length" { return LENGTH; }
{column_ref} { return COLUMN_REF; }
{column_ref_quoted} { return COLUMN_REF; }
{white} /* skip blanks and tabs */
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgssearchtreenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ QgsSearchTreeNode::QgsSearchTreeNode( QString text, bool isColumnRef )
{
mType = tColumnRef;
mText = text;
if ( text.at( 0 ) == '\"' )
{
// column reference is quoted
stripColRef();
}
}
else
{
Expand Down Expand Up @@ -162,6 +167,21 @@ void QgsSearchTreeNode::stripText()

}

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;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgssearchtreenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class CORE_EXPORT QgsSearchTreeNode
//! @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:


Expand All @@ -162,6 +166,9 @@ class CORE_EXPORT QgsSearchTreeNode
//! 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();

Expand Down

0 comments on commit e48766e

Please sign in to comment.