|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | |||||||||
See:
Description
| Interface Summary | |
|---|---|
| IReader | Specifies the behavior of objects that provide data from ResultSets to IRow implementers. |
| IRecord | Specifies the behavior of objects that represent a row of a table. |
| IRow | Specifies the behavior of objects that represent a row of a ResultSet. |
| UpdateListener | Specifies the behavior of objects that receive notification of update activity performed through a Database. |
| Class Summary | |
|---|---|
| BaseRecord | Provides an IRecord adapter, and BaseRow enhancer. |
| BaseRow | Provides an IRow adapter. |
| ChangedColumn | Returned from IRecord.getChanges. |
| ColDef | Is a container for column metadata. |
| Database | Provides access to persistence services. |
| XmlMapper | Used to externalize SQL statements. |
Since jdbc drivers provide metadata and java provides reflection, all we need on the bottom tier is to map columns to fields.
public class CustomerRec extends BaseRecord
{
private long customer_id = 0;
private String customer_name="";
private boolean is_active=false;
//property gets and sets...
}
//
public class SaleRec extends BaseRecord
{
private long customer_id = 0;
private String customer_po="";
private BigDecimal sale_amount=new BigDecimal(0);
private String sale_desc="";
private long sale_id = 0;
//property gets and sets...
}
//
public class SaleRow extends BaseRow
{
private long customer_id = 0;
private long sale_id = 0;
private String sale_desc="";
private String customer_name="";
//property gets and sets...
}
//
CustomerRec theCust = new CustomerRec();
//
//select by passed keys
//
if (theCust.select(someValueReceivedFromElsewhere))
System.out.println("Found customer " + someValueReceivedFromElsewhere);
//
//select embodied
//
theCust.setCustomerId(someValueReceivedFromElsewhere);
if (theCust.select())
System.out.println("Found customer " + theCust.getCustomerId());
//
if (theCust.exists())
{
theCust.update();
}
else
{
theCust.insert();
}
//
// write does the same as exists() ? update() : insert()
//
theCust.write();
//
theCust.delete();
<?xml version="1.0" encoding="UTF-8"?>
<sql>
<CustomerRec>
<selectWholesale><![CDATA[
select * from customer where type_code='WH'
]]></selectWholesale>
</CustomerRec>
<SaleRec>
<isCustomerPoUsed><![CDATA[
select 1 from sale where customer_po=? and sale<>?
]]></isCustomerPoUsed>
<selectByCustomerPo><![CDATA[
select * from sale where customer_po=? order by sale_id desc
]]></selectByCustomerPo>
<selectForCustomer><![CDATA[
select * from sale where customer_id=? order by sale_id desc
]]></selectForCustomer>
<updateCustomerPo><![CDATA[
update sale set customer_po=? where sale_id=?
]]></updateCustomerPo>
</SaleRec>
<SaleRow>
<selectLotsOfSales><![CDATA[
select s.*,c.customer_name from sale s inner join customer c on s.customer_id=c.customer_id
order by customer_name,sale_id desc
]]></selectLotsOfSales>
</SaleRow>
</sql>
The advantages of SQL externalization are:
public void updateArbitrary() throws Exception
{
getDatabase().execute("ThisObjectClass.updateArbitrary",parm1,parm2);
}
To select a record by alternate key, use Database.selectFirst:
public static SaleRec selectByCustomerPO(String po) throws Exception
{
SaleRec r = new SaleRec();
if (r.getDatabase().selectFirst("SaleRec.selectByCustomerPO",r,po))
return r;
return null;
}
For relatively small result sets, implement set selections as static methods and return strongly
typed sets to consuming code:
public static List<SaleRec> selectForCustomer(long cust) throws Exception
{
SaleRec returnTypeTemplate = new SaleRec();
return r.getDatabase().selectRows("SaleRec.selectForCustomer",returnTypeTemplate,-1,cust);
}
Related persistence objects can consume each other's methods for relation-traversal
with easily readable code:
//(instance method on CustomerRec)
public List<SaleRec> selectSales() throws Exception
{
return SaleRec.selectForCustomer(this.customer_id);
}
For potentially large result sets, use Database.selectReader in conjunction with
an IRow/IRecord implementer:
public static IReader selectLots() throws Exception
{
SaleRow r = new SaleRow();
return r.getDatabase().selectReader("SaleRow.selectLots",-1);
}
//
// somewhere above the data access tier...
//
IReader rdr = null;
try
{
rdr = SaleRow.selectLots();
SaleRow row = new SaleRow();
while (rdr.next(row))
{//do stuff
}
}
finally
{
if (rdr != null)
rdr.close();
}
For scalar selections, see Database.selectScalar and Database.selectScalarList. If you want to
retrieve a relatively small set of objects and you don't want to bother defining an
IRow, see Database.selectMatrix. Last, if you want to do something directly
in jdbc, you can use Database.getConnection and roll your own.
If you don't want to use select *, or you want to use field aliases, and you need to select the same fields in more than one way, you can combine multiple sql elements to build a statement:
...
<LegacyAddressRow>
<selectFrom><![CDATA[
select psacn# as control_number,psast# as street_number,
psasdr as street_direction,psas#t as street_number_to,psastr as street_name,
d.stades as city_name,psasda as municipality,psapx1 as exchange
from lgaddr s inner join lgcity d on s.psadtc=d.stasab
]]></selectFrom>
<whereCustomerEquals><![CDATA[
INNER JOIN lgcadd a ON a.atacl#=s.psacn# and atexpd>? and a.atapls=1
where a.atcus#=?
]]></whereCustomerEquals>
<wherePostalCodeEquals><![CDATA[
where upper(psazip)=?
]]></wherePostalCodeEquals>
<whereSuffix><![CDATA[
and psasts<>'D' order by psacn#
]]></whereSuffix>
</LegacyAddressRow>
...
public class LegacyAddressRow extends BaseRow
{
...
public static List<LegacyAddressRow> selectByPostalCode(String pc) throws Exception
{
LegacyAddressRow returnTemplate = new LegacyAddressRow();
Database db = returnTemplate.getDatabase();
String sql = db.combineNamedStatements(
"LegacyAddressRow.selectFrom", "LegacyAddressRow.wherePostalCodeEquals",
"LegacyAddressRow.whereSuffix");
return db.selectRows(sql,returnTemplate,1000, pc);
}
public static List<LegacyAddressRow> selectForCustomer(long cust) throws Exception
{
LegacyAddressRow returnTemplate = new LegacyAddressRow();
Database db = returnTemplate.getDatabase();
String sql = db.combineNamedStatements(
"LegacyAddressRow.selectFrom", "LegacyAddressRow.whereCustomerEquals",
"LegacyAddressRow.whereSuffix");
return db.selectRows(sql,r,1000,todayYmdLongObj,cust);
}
...
}
public void updateCustomerPo() throws Exception
{
getDatabase().execute("SaleRec.updateCustomerPo",sale,customer_po);
}
When generating data objects, ThinkInSQL uses java property names the same as column names except as follows:
public static void copyToCache(List<SaleRec> lst) throws Exception
{
CacheDb cdb = CacheDb.open();
try
{
for (SaleRec r : lst)
cdb.insert(r);
}
finally
{
cdb.close();
}
}
|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | |||||||||