QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
20 #include <QRegularExpression>
36 "=",
"<>",
"<=",
">=",
"<",
">",
"LIKE",
"NOT LIKE",
"ILIKE",
"NOT ILIKE",
"IS",
"IS NOT",
37 "+",
"-",
"*",
"/",
"//",
"%",
"^",
50 "",
"LEFT",
"LEFT OUTER",
"RIGHT",
"RIGHT OUTER",
"CROSS",
"INNER",
"FULL"
66 return tr(
"(no root)" );
73 return QStringLiteral(
"\"%1\"" ).arg( name.replace(
'\"', QLatin1String(
"\"\"" ) ) );
79 static const char *
const RESERVED_KEYWORDS[] =
81 "AND",
"OR",
"NOT",
"LIKE",
"IN",
"IS",
"BETWEEN",
"NULL",
"SELECT",
"ALL",
"DISTINCT",
"CAST",
"AS",
82 "FROM",
"JOIN",
"ON",
"USING",
"WHERE",
"ORDER",
"BY",
"ASC",
"DESC",
83 "LEFT",
"RIGHT",
"INNER",
"OUTER",
"CROSS",
"FULL",
"NATURAL",
"UNION",
84 "OFFSET",
"LIMIT",
"GROUP",
"HAVING"
87 for (
size_t i = 0; i <
sizeof( RESERVED_KEYWORDS ) /
sizeof( RESERVED_KEYWORDS[0] ); ++i )
89 if ( name.compare( QString( RESERVED_KEYWORDS[i] ), Qt::CaseInsensitive ) == 0 )
94 const thread_local QRegularExpression IDENTIFIER_RE(
"^[A-Za-z_\x80-\xff][A-Za-z0-9_\x80-\xff]*$" );
95 return IDENTIFIER_RE.match( name ).hasMatch() ? name :
quotedIdentifier( name );
100 if ( text.length() >= 2 && text[0] ==
'"' && text[text.length() - 1] ==
'"' )
103 text = text.mid( 1, text.length() - 2 );
106 text.replace( QLatin1String(
"\"\"" ), QLatin1String(
"\"" ) );
113 if ( text.length() >= 2 && text[0] ==
'[' && text[text.length() - 1] ==
']' )
116 text = text.mid( 1, text.length() - 2 );
123 text.replace(
'\'', QLatin1String(
"''" ) );
124 text.replace(
'\\', QLatin1String(
"\\\\" ) );
125 text.replace(
'\n', QLatin1String(
"\\n" ) );
126 text.replace(
'\t', QLatin1String(
"\\t" ) );
127 return QStringLiteral(
"'%1'" ).arg( text );
136 : mAllowFragments( allowFragments )
150 if ( &other !=
this )
182 const auto constTables = n.
tables();
185 table->accept( *
this );
187 const auto constColumns = n.
columns();
190 column->accept( *
this );
192 const auto constJoins = n.
joins();
195 join->accept( *
this );
200 const auto constOrderBy = n.
orderBy();
203 column->accept( *
this );
255 errorMsgOut = tr(
"No root node" );
265 if ( !errorMsgOut.isEmpty() )
266 errorMsgOut += QLatin1Char(
' ' );
267 errorMsgOut += tr(
"Table %1 is referenced by column %2, but not selected in FROM / JOIN." ).arg( pair.first, pair.second );
271 return errorMsgOut.isEmpty();
288 const auto constMList = mList;
289 for (
Node *node : constMList )
291 nl->
mList.append( node->clone() );
301 const auto constMList = mList;
302 for (
Node *n : constMList )
304 if ( !first ) msg += QLatin1String(
", " );
367 Q_ASSERT(
false &&
"unexpected binary operator" );
402 Q_ASSERT(
false &&
"unexpected binary operator" );
412 QString rdump( mOpRight->dump() );
417 rdump.prepend(
'(' ).append(
')' );
421 if ( leftAssociative() )
423 fmt += lOp && ( lOp->
precedence() < precedence() ) ?
"(%1)" :
"%1";
424 fmt += QLatin1String(
" %2 " );
425 fmt += rOp && ( rOp->
precedence() <= precedence() ) ?
"(%3)" :
"%3";
429 fmt += lOp && ( lOp->
precedence() <= precedence() ) ?
"(%1)" :
"%1";
430 fmt += QLatin1String(
" %2 " );
431 fmt += rOp && ( rOp->
precedence() < precedence() ) ?
"(%3)" :
"%3";
446 return QStringLiteral(
"%1 %2IN (%3)" ).arg( mNode->dump(), mNotIn ?
"NOT " :
"", mList->dump() );
451 return new NodeInOperator( mNode->clone(), mList->clone(), mNotIn );
458 return QStringLiteral(
"%1 %2BETWEEN %3 AND %4" ).arg( mNode->dump(), mNotBetween ?
"NOT " :
"", mMinVal->dump(), mMaxVal->dump() );
463 return new NodeBetweenOperator( mNode->clone(), mMinVal->clone(), mMaxVal->clone(), mNotBetween );
470 return QStringLiteral(
"%1(%2)" ).arg( mName, mArgs ? mArgs->dump() : QString() );
475 return new NodeFunction( mName, mArgs ? mArgs->clone() :
nullptr );
482 if ( mValue.isNull() )
483 return QStringLiteral(
"NULL" );
485 switch ( mValue.type() )
488 return QString::number( mValue.toInt() );
489 case QVariant::LongLong:
490 return QString::number( mValue.toLongLong() );
491 case QVariant::Double:
492 return QString::number( mValue.toDouble() );
493 case QVariant::String:
496 return mValue.toBool() ?
"TRUE" :
"FALSE";
498 return tr(
"[unsupported type: %1; value: %2]" ).arg( mValue.typeName(), mValue.toString() );
513 ret += QLatin1String(
"DISTINCT " );
514 if ( !mTableName.isEmpty() )
540 ret += mColumnNode->dump();
541 if ( !mAlias.isEmpty() )
543 ret += QLatin1String(
" AS " );
565 if ( !mSchema.isEmpty() )
566 ret += mSchema +
'.';
569 if ( !mAlias.isEmpty() )
571 ret += QLatin1String(
" AS " );
591 qDeleteAll( mTableList );
592 qDeleteAll( mColumns );
593 qDeleteAll( mJoins );
595 qDeleteAll( mOrderBy );
600 QString ret = QStringLiteral(
"SELECT " );
602 ret += QLatin1String(
"DISTINCT " );
603 bool bFirstColumn =
true;
604 const auto constMColumns = mColumns;
608 ret += QLatin1String(
", " );
609 bFirstColumn =
false;
610 ret += column->dump();
612 ret += QLatin1String(
" FROM " );
613 bool bFirstTable =
true;
614 const auto constMTableList = mTableList;
618 ret += QLatin1String(
", " );
620 ret += table->dump();
622 const auto constMJoins = mJoins;
630 ret += QLatin1String(
" WHERE " );
631 ret += mWhere->dump();
633 if ( !mOrderBy.isEmpty() )
635 ret += QLatin1String(
" ORDER BY " );
637 const auto constMOrderBy = mOrderBy;
641 ret += QLatin1String(
", " );
643 ret += orderBy->dump();
651 QList<QgsSQLStatement::NodeSelectedColumn *> newColumnList;
652 const auto constMColumns = mColumns;
655 newColumnList.push_back( column->cloneThis() );
657 QList<QgsSQLStatement::NodeTableDef *> newTableList;
658 const auto constMTableList = mTableList;
661 newTableList.push_back( table->cloneThis() );
664 const auto constMJoins = mJoins;
671 newSelect->
setWhere( mWhere->clone() );
673 QList<QgsSQLStatement::NodeColumnSorted *> newOrderByList;
674 const auto constMOrderBy = mOrderBy;
677 newOrderByList.push_back( columnSorted->cloneThis() );
691 ret += QLatin1Char(
' ' );
693 ret += QLatin1String(
"JOIN " );
694 ret += mTableDef->dump();
697 ret += QLatin1String(
" ON " );
698 ret += mOnExpr->dump();
702 ret += QLatin1String(
" USING (" );
704 const auto constMUsingColumns = mUsingColumns;
705 for ( QString column : constMUsingColumns )
708 ret += QLatin1String(
", " );
712 ret += QLatin1Char(
')' );
725 return new NodeJoin( mTableDef->cloneThis(), mOnExpr->clone(), mType );
727 return new NodeJoin( mTableDef->cloneThis(), mUsingColumns, mType );
735 ret = mColumn->dump();
737 ret += QLatin1String(
" DESC" );
755 QString ret( QStringLiteral(
"CAST(" ) );
756 ret += mNode->dump();
757 ret += QLatin1String(
" AS " );
765 return new NodeCast( mNode->clone(), mType );
QString tableName() const
The name of the table. May be empty.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::Node * mRootNode
static QString stripQuotedIdentifier(QString text)
Remove double quotes from an identifier.
QgsSQLStatement(const QString &statement)
Creates a new statement based on the provided string.
QString dump() const override
Abstract virtual dump method.
QPair< QString, QString > TableColumnPair
QString name() const
Table name.
bool leftAssociative() const
Is left associative ?
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
void setOrderBy(const QList< QgsSQLStatement::NodeColumnSorted * > &orderBy)
Sets order by columns.
void setWhere(QgsSQLStatement::Node *where)
Sets where clause.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QgsSQLStatement::NodeColumnRef * cloneThis() const
Clone with same type return.
static const char * JOIN_TYPE_TEXT[]
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
Function with a name and arguments node.
void acceptVisitor(QgsSQLStatement::Visitor &v) const
Entry function for the visitor pattern.
QString dump() const
Returns the statement string, constructed from the internal abstract syntax tree.
QgsSQLStatement::Node * where() const
Returns the where clause.
QgsSQLStatement::NodeTableDef * cloneThis() const
Clone with same type return.
void setDistinct(bool distinct=true)
Sets whether this is prefixed by DISTINCT.
void visit(const QgsSQLStatement::NodeColumnRef &n) override
Visit NodeColumnRef.
QString dump() const override
Abstract virtual dump method.
Unary logicial/arithmetical operator ( NOT, - )
void appendJoin(QgsSQLStatement::NodeJoin *join)
Append a join.
QgsSQLStatement::NodeList * clone() const
Creates a deep copy of this list. Ownership is transferred to the caller.
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QgsSQLStatementFragment(const QString &fragment)
Constructor for QgsSQLStatementFragment of the specified fragment.
QList< QgsSQLStatement::NodeSelectedColumn * > columns() const
Returns the list of columns.
QString schema() const
Returns the schema name.
QgsSQLStatement::NodeTableDef * tableDef() const
Table definition.
Support for visitor pattern - algorithms dealing with the statement may be implemented without modify...
QString dump() const override
Abstract virtual dump method.
static QString stripMsQuotedIdentifier(QString text)
Remove double quotes from an Microsoft style identifier.
QString statement() const
Returns the original, unmodified statement string.
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes)
const QgsSQLStatement::Node * rootNode() const
Returns the root node of the statement.
QgsSQLStatement::NodeJoin * cloneThis() const
Clone with same type return.
bool hasParserError() const
Returns true if an error occurred when parsing the input statement.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QgsSQLStatement::UnaryOperator op() const
Operator.
QString alias() const
Table alias.
QSet< TableColumnPair > tableNamesReferenced
QgsSQLStatement::NodeColumnSorted * cloneThis() const
Clone with same type return.
QString mParserErrorString
static const char * BINARY_OPERATOR_TEXT[]
Class for parsing SQL statements.
int precedence() const
Precedence.
QList< QgsSQLStatement::NodeTableDef * > tables() const
Returns the list of tables.
void setAlias(const QString &alias)
Sets alias name.
virtual QString dump() const =0
Abstract virtual dump method.
QgsSQLStatement & operator=(const QgsSQLStatement &other)
Create a copy of this statement.
QString dump() const override
Abstract virtual dump method.
QString dump() const override
Abstract virtual dump method.
QString dump() const override
Abstract virtual dump method.
bool doBasicValidationChecks(QString &errorMsgOut) const
Performs basic validity checks.
Binary logical/arithmetical operator (AND, OR, =, +, ...)
QList< QgsSQLStatement::NodeColumnSorted * > orderBy() const
Returns the list of order by columns.
static const char * UNARY_OPERATOR_TEXT[]
virtual void accept(QgsSQLStatement::Visitor &v) const =0
Support the visitor pattern.
QList< QgsSQLStatement::NodeJoin * > joins() const
Returns the list of joins.
QString dump() const override
Abstract virtual dump method.
void visit(const QgsSQLStatement::NodeUnaryOperator &n) override
Visit NodeUnaryOperator.
virtual QString dump() const
Dump list.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QgsSQLStatement::NodeSelectedColumn * cloneThis() const
Clone with same type return.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
static QString quotedIdentifier(QString name)
Returns a quoted column reference (in double quotes)
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QSet< QString > tableNamesDeclared
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
A visitor that recursively explores all children.
void accept(QgsSQLStatement::Visitor &v) const
Accept visitor.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QString name() const
The name of the column.
QString dump() const override
Abstract virtual dump method.
'X BETWEEN y and z' operator
static QString quotedIdentifierIfNeeded(const QString &name)
Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.
QgsSQLStatement::Node * onExpr() const
On expression. Will be nullptr if usingColumns() is not empty.
virtual ~QgsSQLStatement()
QString dump() const override
Abstract virtual dump method.
QString parserErrorString() const
Returns parser error.
QgsSQLStatement::Node * parse(const QString &str, QString &parserErrorMsg, bool allowFragments)
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
Literal value (integer, integer64, double, string)