Forum OpenACS Development: Code duplication?

Collapse
Posted by Friedrich Dominicus on
I'm wondering if it's still ok to have duplicateted code
in .tcl files and .xql files. What is the current policy? Should the queries be put in the .xql files or is the swing back to let them show up in .tcl files?

I wonder because I found this in ecommerce

checkout-2.tcl
[db_0or1row get_order_id_and_order_owner "
    select order_id, user_id as order_owner
    from ec_orders
    where user_session_id = :user_session_id
    and order_state='in_basket' "]

<fullquery name="get_order_id_and_order_owner">
    <querytext>
      select order_id,
      user_id as order_owner
      from ec_orders
      where user_session_id=:user_session_id
      and order_state='in_basket'
    </querytext>
  </fullquery>

but it's getting worse the same code can be found also in another .xql file
<fullquery name="get_order_id_and_order_owner">
    <querytext>
      select order_id, shipping_address as address_id, user_id as order_owner
      from ec_orders
      where user_session_id=:user_session_id
      and order_state='in_basket'
    </querytext>
  </fullquery>
but this time the same query name is used but the query is
different.

For me it seems either
- ecommerce is stuck (means it's not maintained any longer)
- this are left overs and can be graded as bug?

Does anyone can explain that to me?

Regards
Friedrich

Collapse
2: Re: Code duplication? (response to 1)
Posted by Nick Carroll on
Friedrich,

Queries should be separated out of the TCL files and placed in XQL files. Generic SQL code that works on both Oracle and PostgreSQL can be placed in file.xql, whereas database specific SQL should be placed in file-dbname.xql (where dbname is either oracle or postgresql).

OpenACS first looks for a query in file-dbname.xql, if that file doesn't exist, then it looks for file.xql. If that doesn't exist, it will then run the query in the TCL file. Again it is preferable for queries to be placed in the XQL files. This makes the TCL code look cleaner, and makes maintaining queries and TCL code simpler.

Cheers,
Nick.

Collapse
3: Re: Code duplication? (response to 1)
Posted by Friedrich Dominicus on
Thanks, I found that also but am wondering about the massive code duplication and the naming which gives the same name to different queries. I read about tcl and xql in a thread dated back to 2002, but the point is that the code still is there. It's in the .xql file and in the .tcl file. This is by many means a bad thing.

Just an example you change  query in the .xql file (let's say postgres) but do not change it for Oracle also, and you still keep another query in the .tcl file. You can't keep that in sync. So if there is a query in the .xql file the same should not be in the .tcl file.

Regards
Friedrich