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
This commit is contained in:
wonder 2010-06-20 15:52:54 +00:00
parent 0490af72b3
commit e48766e5ec
6 changed files with 44 additions and 10 deletions

View File

@ -115,6 +115,10 @@ class QgsSearchTreeNode
//! @note added in 1.5 //! @note added in 1.5
bool needsGeometry(); bool needsGeometry();
//! return quoted column reference (in double quotes)
//! @note added in 1.5
static QString quotedColumnRef( QString name );
protected: protected:

View File

@ -615,20 +615,16 @@ void QgsAttributeTableDialog::doSearch( QString searchString )
void QgsAttributeTableDialog::search() void QgsAttributeTableDialog::search()
{ {
QString str = mColumnBox->currentText(); QString fieldName = mColumnBox->currentText();
const QgsFieldMap& flds = mLayer->pendingFields(); const QgsFieldMap& flds = mLayer->pendingFields();
int fldIndex = mLayer->fieldNameIndex( str ); int fldIndex = mLayer->fieldNameIndex( fieldName );
QVariant::Type fldType = flds[fldIndex].type(); QVariant::Type fldType = flds[fldIndex].type();
bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double ); bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
if ( numeric ) QString str = QString( "%1 %2 '%3'" )
str += " = '"; .arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
else .arg( numeric ? "=" : "~" )
str += " ~ '"; .arg( mQuery->displayText().replace( "'", "''" ) ); // escape quotes
str += mQuery->displayText().replace( "'", "''" ); // escape quotes
str += "'";
doSearch( str ); doSearch( str );
} }

View File

@ -80,11 +80,14 @@ QgsSearchQueryBuilder::~QgsSearchQueryBuilder()
void QgsSearchQueryBuilder::populateFields() void QgsSearchQueryBuilder::populateFields()
{ {
QgsDebugMsg( "entering." ); QgsDebugMsg( "entering." );
QRegExp reQuote( "[A-Za-z_][A-Za-z0-9_]*" );
const QgsFieldMap& fields = mLayer->pendingFields(); const QgsFieldMap& fields = mLayer->pendingFields();
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it ) for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
{ {
QString fieldName = it->name(); QString fieldName = it->name();
mFieldMap[fieldName] = it.key(); mFieldMap[fieldName] = it.key();
if ( !reQuote.exactMatch( fieldName ) ) // quote if necessary
fieldName = QgsSearchTreeNode::quotedColumnRef( fieldName );
QStandardItem *myItem = new QStandardItem( fieldName ); QStandardItem *myItem = new QStandardItem( fieldName );
myItem->setEditable( false ); myItem->setEditable( false );
mModelFields->insertRow( mModelFields->rowCount(), myItem ); mModelFields->insertRow( mModelFields->rowCount(), myItem );

View File

@ -49,6 +49,9 @@ col_first [A-Za-z_]|{non_ascii}
col_next [A-Za-z0-9_]|{non_ascii} col_next [A-Za-z0-9_]|{non_ascii}
column_ref {col_first}{col_next}* column_ref {col_first}{col_next}*
col_str_char "\"\""|[^\"]
column_ref_quoted "\""{col_str_char}*"\""
dig [0-9] dig [0-9]
num1 {dig}+\.?([eE][-+]?{dig}+)? num1 {dig}+\.?([eE][-+]?{dig}+)?
num2 {dig}*\.{dig}+([eE][-+]?{dig}+)? num2 {dig}*\.{dig}+([eE][-+]?{dig}+)?
@ -101,6 +104,7 @@ string "'"{str_char}*"'"
"$length" { return LENGTH; } "$length" { return LENGTH; }
{column_ref} { return COLUMN_REF; } {column_ref} { return COLUMN_REF; }
{column_ref_quoted} { return COLUMN_REF; }
{white} /* skip blanks and tabs */ {white} /* skip blanks and tabs */

View File

@ -70,6 +70,11 @@ QgsSearchTreeNode::QgsSearchTreeNode( QString text, bool isColumnRef )
{ {
mType = tColumnRef; mType = tColumnRef;
mText = text; mText = text;
if ( text.at( 0 ) == '\"' )
{
// column reference is quoted
stripColRef();
}
} }
else else
{ {
@ -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 QgsSearchTreeNode::makeSearchString()
{ {
QString str; QString str;

View File

@ -153,6 +153,10 @@ class CORE_EXPORT QgsSearchTreeNode
//! @note added in 1.5 //! @note added in 1.5
bool needsGeometry(); bool needsGeometry();
//! return quoted column reference (in double quotes)
//! @note added in 1.5
static QString quotedColumnRef( QString name );
protected: protected:
@ -162,6 +166,9 @@ class CORE_EXPORT QgsSearchTreeNode
//! strips mText when node is of string type //! strips mText when node is of string type
void stripText(); void stripText();
//! strip mText when column reference is quoted
void stripColRef();
//! initialize node's internals //! initialize node's internals
void init(); void init();