Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Object Oriented Generation of SQL statements


Message #1 by Feduke Cntr Charles R <FedukeCR@m...> on Wed, 29 May 2002 13:47:30 -0400
Guys,

	I just completed a rather nice series of classes that generate
Oracle-specific SQL statements in an OOP-way.  Rather than having to
intermingle your SQL with your source code, these classes help you break it
out.  For example:

---
Command cmd = new Command("ADDRESS");
Parameters data = new Parameters();
Parameters cond = new Parameters();
// add the data parameters
data.Add(new CommandParameter("CITY", this.city.Value));
data.Add(new CommandParameter("STATE", this.state.Value));
data.Add(new CommandParameter("COUNTRY", this.country.Value));
data.Add(new CommandParameter("ADDRESS_LINE1", this.addressLine1.Value));
data.Add(new CommandParameter("ADDRESS_LINE2", this.addressLine2.Value));
data.Add(new CommandParameter("PO_BOX", this.poBox.Value));
data.Add(new CommandParameter("POSTAL_CODE", this.postalCode.Value));
// determine if we are inserting or updating
if (this.addressId.Value.Length == 0)
{
	cmd.Type = CommandType.Insert;
}
else
{
	cmd.Type = CommandType.Update;
	cond.Add(new ConditionalParameter("ADDRESS_ID",
Int32.Parse(this.addressId.Value)));
}
// create the SQL
cmd.ValueParameters = data;
cmd.ConditionalParameters = cond;
Page.Trace.Write("Generated SQL", cmd.ToString());
---

	The cmd.ToString() method generates a full blown SQL statement to
handle either Insert, Update, or Delete commands.  Its a rather nice
alternative to typing out all that SQL and appending logic when you need to
build a statement dynamically.  There is also a Select class that you can
use to build simple select statements (currently doesn't support
paranthesis, IN, or sub queries, but it will).

	Anyhow I figure there are other developers out there who could use
this, so I'll release the code freely to anyone who'd like to see or use it.
Its Oracle-specific, but if someone wants to make it support multiple
vendors it wouldn't be too difficult to do (and I wouldn't mind seeing the
end result!).

	If you'd like the code, contact me at fedukecr@m...

	And, oh, yes, I am aware of all the .NET-stuff that is supposed to
do this automatically.

- Chuck

  Return to Index