Trying to PARSE text in a SQL query between SELECT and FROM
-
All I want to do is select the text in between SELECT and FROM in the SQL query below.
I did a little reading on QRegExp but have still been unsuccessful at finding a solution.
@
SELECT- users.id,
users.username AS username,
users.email AS email,
users.first_name AS firstName,
users.last_name AS lastName,
users_groups.user_id,
groups.name AS groupName*
FROM
users
INNER JOIN
users_groups
ON
(
users.id =users_groups.user_id)
INNER JOIN
groups
ON
(
users_groups.group_id =groups.id) ;
@
- users.id,
-
Hi and welcome to devnet,
You can use a combination of QString's "indexOf":http://qt-project.org/doc/qt-4.8/qstring.html#indexOf combined with "mid":http://qt-project.org/doc/qt-4.8/qstring.html#mid to achieve this
-
Sounds easy? Just look for the location of SELECT and of FROM in the string, and use QString::mid to get the part of the string you're interested in. Works as long as you don't have complicated queries with subqueries. If you have that, you're going to need real lexing and parsing anyway, and regexps won't help you either.
-
That worked perfectly. The last thing I am trying to do is get the name of each column.
Remaining SQL to get Columns From
users.id AS id,
users.username AS username,
users.email AS email,
users.first_name AS firstName,
users.last_name AS lastName,
users_groups.user_id AS user_id,
groups.name AS groupNameSo I'm searching in between AS and ,
qDebug() << betweenSelectAndFrom.indexOf("AS");
qDebug() << betweenSelectAndFrom.indexOf(",");This code works great with QString::mid, however it only gets the first occurrence. How do I get all occurrences?
-
I'd first split the fields using QString::split(), using the comma as the separator. Then for each string of the result of that operation, look for the AS clause.
Might I ask what your real purpose is? So far, it seems to me that there is an easier way do get the field names that result from your query...
-
I'm still fairly new to Qt so there may be a better solution.
What I'm creating is a PHP code (CRUD) Generator. The specific purpose of this function is to generate the variables used in a PHP class. Sometimes my JOIN statement can contain 5+ different tables and I don't need every column in each table I only want the columns that are in the query itself.
This is a simple example of the php code I'm trying to generate of what I'm trying to generate
@
class UserModel extends CActiveModel
{-
private $userId; private $username;*
}
@edit: please use @ tags around code sections; Andre
-