Working Effectively with Legacy Code

This book covers the adoption of Unit Testing and TDD into existing (Legacy) applications.
The author defines Legacy code to be any code not covered by unit tests (which is rather strict).
He also insists that individual unit tests that take 0.1 seconds are slow.
This prevents unit tests from relying upon databases.

I have a slight problem with implementing that – the main projects that I work on have a fair amount
of stored procedure code. These need unit test coverage (plus the interfaces with the rest of the code also needs to be tested).

The book also provides strategies to get Legacy code under test.

Referenced Book:

Rober C Martin’s Agile Software Development : Principles, Patterns and Practices (Pearson Education, 2002).

Oracle vs SQL Server

I have been using SQL Server for the last few years.
It is very user friendly and has a gentle learning curve.

Investigating a port to Oracle has been a real culture shock.
Out of the box Oracle only has a command-line interface.
To get the equivalent of Query Analyser in Oracle you need to
use a third party tool such as TOAD or PL/Developer.

SQL Server is also more flexible when dealing with stored procedures – you can directly return recordsets. Oracle is very strict when dealing with stored procedures all output must be via parameters.

To Oracle’s benefit they have much stronger error handling. SQL Server stored procedures cannot be used to catch constraint validation errors. This is rather annoying and forces a look before you leap approach.

Useful SQLPlus Commands

The SQLPLUS utility is the Oracle equivalent of iSQL.

It includes a few shortcuts to make life easier:

@FILENAME

This will excecute FILENAME (or FILENAME.SQL)

SHOW ERRORS

This will show the detailed errors for the last command.

L

This lists the last command entered.

Beware that stored procedures or functions cannot include blank lines (as far as i can tell – I got so many errors that I removed them).

Without some of these commands it is as user-friendly as a cornered rat.

Recordsets from Oracle Stored procedures

This is link to an article that explains how to Oracle stored procedures to return recordsets.

The main trick is to add:

//<![CDATA[
<!–
document.write('’);
document.write(‘ .scripthide { display: none; }’);
document.write(‘ .scriptinline { display: inline; }’);
document.write(‘ .scriptblock { display: block; }’);
document.write(”);
//–>
//]]>
<style type=”text/css”><!– .noscripthide { display:none; } .noscriptinline { display:inline; } .noscriptblock { display:block; } //–></style>
//<![CDATA[
<!–
if (document.getElementById) {
document.write('’);
document.write(‘ .script12hide { display: none; }’);
document.write(‘ .script12inline { display: inline; }’);
document.write(‘ .script12block { display: block; }’);
document.write(”);
}
//–>
//]]>

//<![CDATA[
<!–
if (document.getElementById) {
document.write('’);
document.write(‘ .lnav { position: absolute; }’);
document.write(‘ .lnavch { margin-left:23.0ex;}’);
document.write(”);
}
//–>
//]]>

//<![CDATA[

//]]>

//<![CDATA[
<!–
// ———————————-
// visibility functions
function tog() {
// tog: toggle the visibility of html elements (arguments[1..]) from none to
// arguments[0]. Return what should be returned in a javascript onevent().
display = arguments[0];
for( var i=1; i
//]]>

//<![CDATA[

//]]>

//<![CDATA[

//]]>

//<![CDATA[

//]]>
Extended Properties=”PLSQLRSet=1″ to your connection string.

The cursor itself must be defined as a ref cursor :

CREATE OR REPLACE
PACKAGE Types AS
  TYPE cursor_type IS REF CURSOR
END;

Oracle 9i introduced the SYS_REFCURSOR type for this purpose.

Here is a valid example:

CREATE OR REPLACE
PROCEDURE USP_GET_UNIT(p_recordset OUT SYS_REFCURSOR) AS
BEGIN
    OPEN p_precordset FOR
       SELECT UNIT, DESCRIPTION FROM UNIT;
END;

On the delphi side don’t add the cursor as a parameter.