3

I was wondering if there is something I can use in C++ similar to "sqlparse" module in Python to format my query. Do you know what can I use?

I'm sorry for didn't provide an example before. I want that something like this:

SELECT MEMB.NAME, MEMB.AGE, AGE.GROUP FROM MEMB, AGE WHERE MEMB.AGE = AGE.AGE

Become this:

SELECT MEMB.NAME,
       MEMB.AGE,
       AGE.GROUP
FROM   MEMB,
       AGE
WHERE  MEMB.AGE = AGE.AGE

Thanks a lot.

3
  • 3
    For those of us who don't know what "sqlparse" is - format it in what way? Please provide an example. Commented May 9, 2011 at 16:15
  • 1
    Hmm, why not just write it that way then? Why do you need a tool? I would think the enter key and the space bar would be all the tools you need. I guess I'm missing the point of the question. Commented May 9, 2011 at 16:25
  • Well, I don't want to write any function that does that. First because the code is not mine and second because I don't code in c++. I just need a function that does that, like the format method in the module that I've mentioned before. Commented May 9, 2011 at 16:28

1 Answer 1

3

You can write your own pretty printer. In that case, it won't be any hard. Just replace things like the following:

"FROM" -> "\nFROM"
"WHERE" -> "\nWHERE"
"," -> ",\n\t"
"AND" -> "AND\n\t"
"OR" -> "OR\n\t"

etc.

Edit: as you don't code, here's a little version of this functionality.

#include <string>
using std::string; /* put these lines in the top of your file */

string replace(string a, string b, string c) {
    unsigned x;
    for(x = a.find(b); x != string::npos;) {
        a.erase(x, b.length());
    a.insert(x, c);
    }
    return a;
}




string formatSQL(string sql) {

    replace(sql, "FROM", "\nFROM");
    replace(sql, "WHERE", "\nWHERE");
    replace(sql, "," , ",\n\t");
    replace(sql, "AND", "AND\n\t");
    replace(sql, "OR", "OR\n\t");
}

So calling formatSql("SELECT MEMB.NAME, MEMB.AGE, AGE.GROUP FROM MEMB, AGE WHERE MEMB.AGE = AGE.AGE") gives you the desired result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.