QString mysqlQuery::stripBinds(const QString &in) { BindParams.clear(); QString retval; std::auto_ptr <SQLLexer::Lexer> lexer = LexerFactTwoParmSing::Instance().create("mySQLGuiLexer", "", "toCustomLexer"); lexer->setStatement(in); SQLLexer::Lexer::token_const_iterator start = lexer->begin(); while (start->getTokenType() != SQLLexer::Token::X_EOF) { switch (start->getTokenType()) { case SQLLexer::Token::L_BIND_VAR: retval += start->getText(); BindParams << start->getText(); break; case SQLLexer::Token::L_BIND_VAR_WITH_PARAMS: { QString l1 = start->getText(); QString l2 = l1.left( l1.indexOf('<')); QString l3 = l2.leftJustified( l1.length(), ' '); BindParams << l2; retval += l3; } break; default: retval += start->getText(); //no break here } start++; } return retval; }
void toLexerOracleAPIs::updateAutoCompletionList(const QStringList &context, QStringList &list) { list << "AAA" << "BBB"; QsciScintilla* editor = lexer()->editor(); if (editor == NULL) return; QmlJS::PersistentTrie::Trie trie; QString text = editor->text(); int len = editor->length(); char* bufferText = new char[len+1]; editor->SendScintilla(QsciScintillaBase::SCI_GETTEXT, len, bufferText); bufferText[len] = '\0'; std::auto_ptr <SQLLexer::Lexer> lexer = LexerFactTwoParmSing::Instance().create("OracleGuiLexer", "", "toCustomLexer"); lexer->setStatement(bufferText, len); SQLLexer::Lexer::token_const_iterator it = lexer->begin(); while (it != lexer->end()) { SQLLexer::Token::TokenType t = it->getTokenType(); if (it->getTokenType() == SQLLexer::Token::L_IDENTIFIER) trie.insert(it->getText()); it++; } list << trie.complete(context.last()); }
bool toSqlText::showToolTip(toSqlText::ToolTipData const& t) { int start_pos = SendScintilla(QsciScintilla::SCI_POSITIONFROMLINE, t.line); int end_pos = SendScintilla(QsciScintilla::SCI_GETLINEENDPOSITION, t.line); int word_len = end_pos - start_pos; if (word_len <= 0) return false; char *buf = new char[word_len + 1]; SendScintilla(SCI_GETTEXTRANGE, start_pos, end_pos, buf); QString word = convertTextS2Q(buf); std::unique_ptr <SQLLexer::Lexer> lexer(LexerFactTwoParmSing::Instance().create("OracleGuiLexer", "", "toSqlText::showToolTip")); lexer->setStatement(buf, word_len + 1); SQLLexer::Lexer::token_const_iterator i = lexer->begin(); QString toolTipText; long offset = t.textPosition - start_pos; do { if (i->getPosition().getLinePos() > offset) break; toolTipText = i->getText(); toolTipText += i->getPosition().toString(); toolTipText += '(' + i->getTokenTypeName() + '/' + i->_mOrigTypeText + ')'; i++; } while(i != lexer->end()); QToolTip::showText(t.globalPos, toolTipText, viewport(), t.rect); delete[] buf; return true; }
toQueryParams toParamGet::getParam(toConnection &conn, QWidget *parent, QString &str) { const QString def = QString::fromLatin1("<char[4000]>"); toQueryParams ret; std::unique_ptr <SQLLexer::Lexer> lexer; if (conn.providerIs("Oracle")) lexer = LexerFactTwoParmSing::Instance().create("OracleGuiLexer", str, "toParamGet::getParam"); else if(conn.providerIs("QMYSQL")) lexer = LexerFactTwoParmSing::Instance().create("MySQLGuiLexer", str, "toParamGet::getParam"); else if(conn.providerIs("QPSQL")) lexer = LexerFactTwoParmSing::Instance().create("PostreSQLGuiLexer", str, "toParamGet::getParam"); else throw QString("Unsupported sql dialect: %1").arg(conn.provider()); QString retvalsql; QStringList BindParamList; // Keep order of BindParam occurrences SQLLexer::Lexer::token_const_iterator iter = lexer->begin(); while (iter->getTokenType() != SQLLexer::Token::X_EOF) { switch (iter->getTokenType()) { case SQLLexer::Token::L_BIND_VAR: { QString name = iter->getText(); retvalsql += name; if (conn.providerIs("Oracle")) retvalsql+=def; if (!BindParamList.contains(name)) BindParamList << name; } break; case SQLLexer::Token::L_BIND_VAR_WITH_PARAMS: { QString l1 = iter->getText(); QString name = l1.left( l1.indexOf('<')); if (!BindParamList.contains(name)) BindParamList << name; retvalsql += l1; } break; default: retvalsql += iter->getText(); //no break here } iter++; } // No bind parameters in the SQL query if (BindParamList.empty()) return ret; toParamGet *widget = new toParamGet(parent); int num = 0; foreach(QString BindParam, BindParamList) { widget->createWidgetRow(BindParam, num); num++; }
toSyntaxAnalyzer::statement toSyntaxAnalyzerOracle::getStatementAt(unsigned line, unsigned linePos) { toSyntaxAnalyzer::statement retval; toScintilla *editor = qobject_cast<toScintilla *>(parent()); std::string str(editor->text().toStdString()); try { std::unique_ptr <SQLLexer::Lexer> lexer = LexerFactTwoParmSing::Instance().create("OracleGuiLexer", "", "toCustomLexer"); lexer->setStatement(str.c_str(), (int)str.length()); SQLLexer::Lexer::token_const_iterator start = lexer->begin(); start = lexer->findStartToken(start); while (start->getTokenType() != SQLLexer::Token::X_EOF) { SQLLexer::Lexer::token_const_iterator end = lexer->findEndToken(start); SQLLexer::Lexer::token_const_iterator nextStart = lexer->findStartToken(end); if (end->getPosition().getLine() < line) { start = nextStart; continue; } // The statement was found - setup retval toScintilla *editor = qobject_cast<toScintilla *>(parent()); retval = statement( start->getPosition().getLine(), end->getPosition().getLine()); retval.firstWord = start->getText(); retval.posFrom = editor->positionFromLineIndex(start->getPosition().getLine() , start->getPosition().getLinePos()); retval.posTo = editor->positionFromLineIndex(end->getPosition().getLine() , end->getPosition().getLinePos() + ( end->getTokenType() == SQLLexer::Token::X_EOL ? 0 : end->getLength())); switch (start->getTokenType()) { case SQLLexer::Token::L_LPAREN: case SQLLexer::Token::L_DML_INTRODUCER: // INSERT/UPDATE/DELETE/MERGE retval.statementType = DML; break; case SQLLexer::Token::L_SELECT_INTRODUCER: retval.statementType = SELECT; break; case SQLLexer::Token::L_PL_INTRODUCER: retval.statementType = PLSQL; break; case SQLLexer::Token::L_OTHER_INTRODUCER: retval.statementType = OTHER; break; case SQLLexer::Token::X_ONE_LINE: retval.statementType = SQLPLUS; break; default: // DDL, // CREATE // OTHER, // ALTER SESSION ..., ANALYZE, SET ROLE, EXPLAIN // SQLPLUS // sqlplus command retval.statementType = UNKNOWN; break; } return retval; } } catch (std::exception const &e) { std::string s(e.what()); std::cout << s << std::endl; } catch (QString const& e) { qDebug() << e; } catch (...) { qDebug() << __FUNCTION__ ; } return retval; }
QStringList mysqlQuery::queryParam(const QString &in, toQueryParams ¶ms) { bool useBinds = toConfigurationNewSingle::Instance().option(MySQL::UseBindsBool).toBool(); QString sql, allDatabases; toQueryParams::const_iterator cpar = params.constBegin(); toQueryParams filteredParams; std::unique_ptr <SQLLexer::Lexer> lexer = LexerFactTwoParmSing::Instance().create("MySQLGuiLexer", "", "toCustomLexer"); lexer->setStatement(in); SQLLexer::Lexer::token_const_iterator start = lexer->begin(); while (start->getTokenType() != SQLLexer::Token::X_EOF) { QString str = start->getText(); switch (start->getTokenType()) { case SQLLexer::Token::L_BIND_VAR: if (useBinds) { BindParams << str; filteredParams << *cpar; } else { str = toQMySqlTraits::quoteVarcharStatic(*cpar); } cpar++; break; case SQLLexer::Token::L_BIND_VAR_WITH_PARAMS: { QString text = start->getText(); // ":f1<alldatabases>", ":f1<varchar,noquote>" or ":f1<int>" QString name = text.left(text.indexOf('<')); // ":f1" QString bindname = name.mid(1); // "f1" QString option = text.mid(text.indexOf('<') + 1); option.chop(1); // "alldatabases" or "varchar,noquote" QStringList options = option.split(','); QString name2 = name.leftJustified(text.length(), ' '); // ":f1 " space padded if (options.contains("alldatabases")) { allDatabases = name; str = name2; } else if (options.contains("noquote")) { str = *cpar; cpar++; } else if (options.contains("backquote")) { str = '`' + *cpar + '`'; cpar++; } else if (useBinds) { BindParams << name; str = name2; filteredParams << *cpar; cpar++; } else { if (cpar == params.end()) throw toConnection::exception("Not all bind variables supplied"); if ((*cpar).isNull()) { str = QString::fromLatin1("NULL"); } else if ((*cpar).isInt() || (*cpar).isDouble()) { str = QString(*cpar); } else str = toQMySqlTraits::quoteVarcharStatic(*cpar); cpar++; } break; } } start++; sql.append(str); } QStringList ret; if (allDatabases.isEmpty()) { ret << sql; } else { QStringList databases = query()->connection().getCache().userList(toCache::DATABASES); Q_FOREACH(QString database, databases) { QString new_sql(sql); new_sql.replace(allDatabases, database); ret << new_sql; } }