B
Bryan Castillo
I was looking to put all of the SQL for a web application into an XML
file for a project I am working on. It is a struts 1.1 app running on
Tomcat. I am not using EJB's nor am I using any O/R tools. Anyway, I
thought it would be nice to store all of the SQL in one place, so
DBA's and other programmers can review the SQL very easily, as it is
not embedded in any java files. It is also conceivable that the SQL
can be changed out withought chaning the code using parameters ?'s. I
could even change to stored procedures for some operations. (I've
already been storing my SQL in property files for some time now).
Anyway I had a basic SQL factory class that reads the XML and gives
back the statement bases on the name of the statement.
Here is a basic example of the xml (which will be expanded - perhaps
to include documentation strings).
<sql-statements>
<statement name="update.terminals">
UPDATE terminals
SET strategy_id = ?
WHERE merid = ?
</statement>
</sql-statements>
This is all fine and dandy given that I want to use
PreparedStatements, however I see that it would be nice to use some
type of MACRO expansion of the SQL. We have one database system where
each customer has its own table with exactly the same structure as
each other customer. Some people will of course say that it should
all be 1 table with a customer ID column added, however it was created
long ago and there are reasons to leave it the way it is for now. So
I might have an SQL statement to query or update one of the customer
tables, but I can't use a ? mark for a table name as a means of giving
a parameter to a PreparedStatement.
Here is an example of a statement where I might want to expand some
variable
customer_id as part of a table name.
<sql-statements>
<statement name="update.terminals">
UPDATE cust_txns_${customer_id}
SET status = ?
WHERE txid = ?
</statement>
</sql-statements>
I would like to use something similar to the way Ant expands variables
or perhaps something like the messages used by Struts which use ${0},
${1}, etc...
I looked through jakarta-commons and tried to see if there was a
library for expanding MACROS similar to what Ant does. There doesn't
seem to be. It should be simple enough to write my own, or pull the
source from Ant.
Question 1: Is there a standalone java library typically used for
MACRO expansion?
I was also considering using Velocity to create the SQL statements. I
think Velocity would give me a lot of flexibility, but I was a little
hesitant to introduce another language/technology many people on my
team do not know. One positive thing I could gain from using
velocity, is that I could embed conditional logic in the SQL statement
where I am building a fairly dynamic query. I have one query right
now that is built a little differently depending on what parameters
are being searched upon (it brings in some outer joins in some cases).
Anyone else out there use some strategies for pulling your SQL out of
the java code? Do you use any macro expansions on SQL?
Thanks.
file for a project I am working on. It is a struts 1.1 app running on
Tomcat. I am not using EJB's nor am I using any O/R tools. Anyway, I
thought it would be nice to store all of the SQL in one place, so
DBA's and other programmers can review the SQL very easily, as it is
not embedded in any java files. It is also conceivable that the SQL
can be changed out withought chaning the code using parameters ?'s. I
could even change to stored procedures for some operations. (I've
already been storing my SQL in property files for some time now).
Anyway I had a basic SQL factory class that reads the XML and gives
back the statement bases on the name of the statement.
Here is a basic example of the xml (which will be expanded - perhaps
to include documentation strings).
<sql-statements>
<statement name="update.terminals">
UPDATE terminals
SET strategy_id = ?
WHERE merid = ?
</statement>
</sql-statements>
This is all fine and dandy given that I want to use
PreparedStatements, however I see that it would be nice to use some
type of MACRO expansion of the SQL. We have one database system where
each customer has its own table with exactly the same structure as
each other customer. Some people will of course say that it should
all be 1 table with a customer ID column added, however it was created
long ago and there are reasons to leave it the way it is for now. So
I might have an SQL statement to query or update one of the customer
tables, but I can't use a ? mark for a table name as a means of giving
a parameter to a PreparedStatement.
Here is an example of a statement where I might want to expand some
variable
customer_id as part of a table name.
<sql-statements>
<statement name="update.terminals">
UPDATE cust_txns_${customer_id}
SET status = ?
WHERE txid = ?
</statement>
</sql-statements>
I would like to use something similar to the way Ant expands variables
or perhaps something like the messages used by Struts which use ${0},
${1}, etc...
I looked through jakarta-commons and tried to see if there was a
library for expanding MACROS similar to what Ant does. There doesn't
seem to be. It should be simple enough to write my own, or pull the
source from Ant.
Question 1: Is there a standalone java library typically used for
MACRO expansion?
I was also considering using Velocity to create the SQL statements. I
think Velocity would give me a lot of flexibility, but I was a little
hesitant to introduce another language/technology many people on my
team do not know. One positive thing I could gain from using
velocity, is that I could embed conditional logic in the SQL statement
where I am building a fairly dynamic query. I have one query right
now that is built a little differently depending on what parameters
are being searched upon (it brings in some outer joins in some cases).
Anyone else out there use some strategies for pulling your SQL out of
the java code? Do you use any macro expansions on SQL?
Thanks.