views:

3469

answers:

27

I have a function, parseQuery, that parses a SQL query into an abstract representation of that query.

I'm about to write a function that takes an abstract representation of a query and returns a SQL query string.

What should I call the second function?

+5  A: 

generateQuery, possibly? createQuery?

Dre
Good, simple answer!
fastcodejava
+2  A: 

Possibly Format(). or ToSQL() in your instance?

Omar Kooheji
+5  A: 

I would call it constructQuery.

Sec
+9  A: 

ToQueryString()

plinth
+78  A: 

I think the verb you want is 'compose'.

Joel Coehoorn
Thanks for validating my (independently derived) answer.
Mike Brown
Would someone please tell me why this answer got upvoted 43 times? I mean I know it's right, but 40+ votes??
Mitch Wheat
@Mitch Wheat: sadly that's how SO works. It doesn't matter how good your answer is (or how much time you put into it). All it matters is how many people like it. I used to spend too much time writing valid answers to obscure questions and getting no feedback at all, while other's "Yes/No"s got +100.
efotinis
If it makes you feel any better, I think I only earned maybe 40 rep for this rather than 430, as I had already hit the cap that day.
Joel Coehoorn
-1 for bah humbug - Now you only got 30! (Only joking) :)
Patrick McDonald
Wow, where'd all the votes for this come from today?
Joel Coehoorn
@Joel : http://twitter.com/shanselman/status/5024768993
Daniel Schaffer
I mean, coming back a year later I'd even answer 'assemble' as a better opposite, or 'build' as a better function name.
Joel Coehoorn
Oh wow, I didn't check the dates on this... SO question necromancy!
Daniel Schaffer
err.. why not ToString() ? Seems to be the standard set by the likes of Int32, etc
Joseph Kingry
made my previous comment before seeing that the question was language-agnostic. ToString() seems to be the accepted standard by .NET
Joseph Kingry
+4  A: 

Maybe prettyPrintQuery?

fhe
+2  A: 

generate or emit, possibly.

DGentry
+5  A: 

The antonym of 'analyze' is 'synthesize'.

synthesize. good choice.
MikeJ
+2  A: 

unParse()? Just kidding, I would go with toQueryString()

Ben Hoffstein
DeParse is what I've used
Mike Dunlavey
+21  A: 

Compose? When parsing a query you break it into its constituent parts (tokens, etc.), the reverse would be composing the parts into a string query.

Mike Brown
+5  A: 

I think "serialize" is probably the word you want. It means to produce a textual representation of data that can be exported (and imported) from the program.

Kyle Cronin
Serialize can just as easily mean a binary representation.
Ben Hoffstein
True. Parsimg is all about fading in external data, and serialization is all about producing data for external uses. The format produced isn't required to be text, but often is.
Kyle Cronin
Apparently my iPod's keyboard is getting the better of me. That's supposed to be "parsing" and "reading".
Kyle Cronin
+10  A: 

I would use one of these:

  • ToString()
  • ToSQL()
  • Render()
Sklivvz
+1  A: 

flatten?

The parsed query object perhaps represents a condition hierarchy, which you are "flattening" back into a 1 dimensional string.

But given that you're going from object to string, really just use toString or toSQL() or something like that. Besides, if you designed it well and are using the right app, you can rename it later and just stick stuff in the comments on what it does.

Josh
+4  A: 

Just to add some stuff.

Surely parse is a two way word.

You can parse an abstract into a query.

You can parse a query into an abstract.

The question should be, what do you name the latter part of the method, and because in this instance you're parsing an abstract to make a query you'd call it parseAbstract.

To answer the question, parsing has no opposite.

PintSizedCat
+3  A: 

compose, construct, generate, render,condense, reduce, toSQL, toString depending on the nature of the class and its related operators

MikeJ
A: 

+1 for ToQuery() or ToSQL()

+1  A: 

A traditional compiler has two parts: a parser and a code generator.

So you could call it "Generate". Of course, it's a little bit different here because the compiler isn't writing source code. (unless it's a precompiler).

Walter Mitty
+1  A: 

+1 for Generate, but tack on what you're generating, i.e. GenerateSQL()

Tom Lahti
A: 

writeQuery. Parse is the act of reading it from a string and creating the object (let's say 'actual') representation. The opposite would be writing the object into a string.

helios
A: 

I voted for 'compose' but if you don't like that I would also suggest 'build'

Guy
+1  A: 

I'd say serialize and deserialize, instead of parse and ...

Christophe Herreman
+3  A: 

The opposite of parse is serialize

Yuval A
+5  A: 

Definitely Render.

David Mitchell
+5  A: 

In compiler terminology, the opposite is "unparse". Specifically, parsing turns a stream of tokens into abstract syntax trees, while unparsing turns abstract syntax trees into a stream of tokens.

Barry Kelly
+2  A: 

I would go for ToString(), since you can usually chain-nest them (opposite functions, that let you pass from Class1 to Class2 and vice-versa)

DateTime.Parse( DateTime.Parse( myDate.ToString() ).ToString() );

Serialize() looks like a nice choice, but it already has an opposite in Deserialize().

In your specific scenario, as other pointed out, ToSql() is another good choice.

Filini
A: 

I believe the answer you're looking for is: "Don't parse SQL or assemble SQL in the first place. Use an Object/Relational Mapper and stop wasting your employer's money by solving problems that have already been solved for quite some time."

chadmyers
-1. You have no idea what he's doing. He might have a perfectly good reason for doing it. Not all applications are the same.
erikkallen
I must agree, I had recently a perfectly good reason to parse SQL that had nothing to do with ORM's (which I used in the project as well).
Craig
I'd love to hear this 'perfectly good reason'. No, actually I wouldn't. I could imagine if you were building a SQL IDE or something, but even then -- why are you building a SQL IDE when they are some heavy hitters in the market already?
chadmyers
Reason was basically we had to turn a query 'SELECT CustomerId FROM Customer' into something like 'SELECT <<field:25>> FROM <<table:76>>'. This is easy for basic select, very difficult for complex queries.
Craig
Please tell me how nHibernate would help me to do this?
Craig
Please tell me why you'd be doing that in the first place.
chadmyers