+ All Categories
Home > Documents > Telosys DAL v1.8

Telosys DAL v1.8

Date post: 09-Apr-2018
Category:
Upload: laurent-guerin
View: 252 times
Download: 6 times
Share this document with a friend

of 27

Transcript
  • 8/8/2019 Telosys DAL v1.8

    1/27

    Telosys 1

    Data Access Layer

    ( Telosys-DAL )

    Laurent Gurin / V 1.8 / 2008 November

    ( for Telosys 1.0.0 and + )

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 2

    Telosys-DAL : The big principles

    Telosys-DAL goals : Provide an easy way to write and use standard CRUD

    DAOs ( for single Value Objects and Lists )

    Let the programmer see the SQL code and control allthe operations

    Keep the SQL code and the O/R mapping in the Javaclasses (when the code runs, it cannot be altered byexternal files)

    Increase the productivity by providing a set of classes to encapsulate all the JDBC complexity

    external tools to generate the specific Java code required toaccess a table

    you dont have to write code to create DAO and ValueObjects !

  • 8/8/2019 Telosys DAL v1.8

    2/27

    Telosys 2

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 3

    Telosys-DAL : Overview

    Data Access Layer

    Value Objects

    ConnectionManager

    SqlConnection

    SqlConnection

    SqlConnection

    SqlConnectionPool

    SqlConnectionPool

    SqlConnectionFactory

    SqlConnectionProvider

    AgencyVO

    EmployeeVO

    EmployeeVOList

    DAO

    AgencyDAO

    EmployeeDAO

    Application

    init()

    getConnection()

    AgencyVOList

    SQLDataSourceDataSource

    Telosys

    J2EE Server

    dbconfig.xml

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 4

    Configuration file dbconfig.xml

    dbconfig.xml example :

  • 8/8/2019 Telosys DAL v1.8

    3/27

    Telosys 3

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 5

    Telosys-DAL : Initialization

    Telosys-DAL stand-alone :

    Telosys-DAL with Telosys framework : Define the dbconfig file in the telosys.properties

    Init Telosys framework, then Telosys-DAL

    //--- Telosys DAL initialization with dbconfig file path

    private static final String DB_CONFIG = CONF_DIR + "dbconfig.xml" ;

    TelosysDAL.init(DB_CONFIG);

    //--- 1) Telosys global initialization

    Telosys.init(..., ...);

    //--- 2) Telosys DAL initialization

    TelosysDAL.init();

    DbConfFile = C:/Eclipse301/workspace/Telosys/conf/dbconfig.xml

    DbConfFile = ./dbconfig.xml

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 6

    Telosys-DAL : Connections management

    SqlConnectionProvider

    . getConnection()

    . getName()

    ConnectionProvider>

    *

    . init ( String dbconfigFile )

    . getDefaultDatabase()

    . getNumberOfDatabases()

    . getConnection()

    . getConnection(int base)

    . getSession()

    . getSession(int base)

    ConnectionManager>

    SqlConnectionFactory

    SqlConnectionPool

    SqlDataSource

    javax.sql.DataSource

    SqlConnection>

    java.sql.Connection

    >

    Pool( Vector )

    getConnection :create a newconnection

    getConnection :get from pool

    getConnection :get from DataSource

    *

    1

  • 8/8/2019 Telosys DAL v1.8

    4/27

    Telosys 4

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 7

    Telosys-DAL : Connections management

    How to get a Connection :

    How to release a Connection :

    if the connection comes from

    a pool

    it is recycled ( in the Telosys pool ) a factory it is closed

    a datasource managed by the DataSource

    //--- Connection fromDefault Database

    Connection c1 = ConnectionManager.getConnection();

    //--- Connection fromDatabase 1

    Connection c2 = ConnectionManager.getConnection(1);

    Connection c1 = ConnectionManager.getConnection();

    c1.close();

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 8

    DAL V.O. ( Value Objects )

    The data exchange between the Data AccessLayer and the other layers is done withVO ( Value Object ), also knownas DTO ( Data Transfer Object )

    For each VO, 2 Java classes are required :

    TheVO itself A specific list of this type of VO (with strong type

    checkingmethods )

    Example : to managed Employees we need EmployeeVO.java ( the VO )

    EmployeeVOList.java ( the list of EmployeeVO )

  • 8/8/2019 Telosys DAL v1.8

    5/27

    Telosys 5

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 9

    DAL V.O. ( Value Objects )

    . getXxxx()

    . setXxxx(). isXxxx()

    . toString()

    EmployeeVO

    . EmployeeVO add()

    . add( EmployeeVO ). insert ( index, EmployeeVO )

    . replace ( index, EmployeeVO )

    . remove ( index )

    . remove (EmployeeVO )

    . EmployeeVO get( index )

    . EmployeeVO getFirst()

    . EmployeeVO getNext()

    EmployeeVOList

    . getList()

    . size()

    . isEmpty()

    . clear()

    GenericVOList

    LinkedList

    List of EmployeeVO

    Value Object

    for entity Employee

    Employee example :

    no inheritance

    Standard DataList providedby Telosys

    extends GenericVOList

    Specific mthods toencapsulate the

    generic methods ofDataList

    Standard template,but no interfaceimplementationbecause of the strongtype methods

    Standard accessors,for each attribute

    T

    e

    los

    y

    s

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 10

    DAL D.A.O. ( Data Access Objects )

    The DAO provides methods to manipulate A single entity : theValue Object

    A list of entity corresponding to a specific Query TheValue Objects List

    The DAO works with other classes :

    1 Value Object ( VO ) 1 Value Objects List ( VOList )

    0 .. N Query (providing aVOList )

  • 8/8/2019 Telosys DAL v1.8

    6/27

    Telosys 6

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 11

    DAL D.A.O. ( Data Access Objects )

    The DAO operations to manipulate a single V.O.

    load (try to load a VO using its primary key)

    save (save a VO by SQL insert or update )

    delete (delete a VO using its primary key )

    insert ( try to insert the VO )

    insertKeyGen ( insert with key generation )

    update ( try to update the VO )

    exists ( check if the VO exists in the database )

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 12

    DAL D.A.O. ( Data Access Objects )

    The DAO operations to manipulate SQL QueriesandV.O. lists createQuery ( create a SQL query to be used afterwards )

    loadList ( load a list as the result of a Query )

    saveList ( save a list by a SQL delete using the Query criteria& n SQL insert for all the items of the list)

    deleteList ( delete the list using the Query criteria )

    count ( do a select count(*) .. using the Query criteria )

    List elements management ( without Query ) : updateList ( update all the items of the list )

    insertList ( insert all the items of the list )

    deleteList ( delete all the items of the list )

    VOListor

    List

  • 8/8/2019 Telosys DAL v1.8

    7/27

    Telosys 7

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 13

    DAL D.A.O. ( Data Access Objects )

    DAO with or without Connections ?Each operation of a DAO has 4 signaturesExamples :

    dao.save( myVO );works with the default database ID ( cfdbconfig.xml )

    dao.save( myVO, base_id );works with the given database ID ( int )

    dao.save( myVO, connection );

    works with the given standard JDBC Connection dao.save( myVO, database_session );

    works with the given DatabaseSession object( a DatabaseSession is a Telosys object which encapsulate aconnection, useful for Screen Managers )

    DB

    id

    Connectio

    n

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 14

    DAL D.A.O. ( Data Access Objects )

    With logical DB id ( without Connection ) The DAO uses the ConnectionManager

    to get a connection ( ConnectionManager.getConnection(); )

    Each insert, update or delete is commited automatically

    The connection is closed (recycled if it belongs to apool ) after each operation

    The programmer never see the Connection object

    With Connection or DatabaseSession The connection management is the programmers

    responsibility

    The DAO doesnt commit anything

    And doesnt close the connection

  • 8/8/2019 Telosys DAL v1.8

    8/27

    Telosys 8

    How to use D.A.O.

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 16

    How to get a DAO instance

    A DAO is thread-safe=> its instance can be shared an reused

    1/ Using the default DAO constructor

    2/ Using a DAO provider if any (since v 1.0.0)Employee bean = new Employee();

    DAOProvider provider = TelosysDAL.getDAOProvider();

    EmployeeDAO dao ;

    dao = (EmployeeDAO)provider.getDAO(bean); // by bean instance

    dao = (EmployeeDAO)provider.getDAO(bean,3); // same but for DB 3

    dao = (EmployeeDAO)provider.getDAO(Employee.class); // by bean class

    dao = (EmployeeDAO)provider.getDAO(Employee.class,3); // same for DB 3

    ( the dynamic DAO registries can be defined in thefiles dbconfig.xml and telosys.properties )

    EmployeeDAO dao = new EmployeeDAO();

  • 8/8/2019 Telosys DAL v1.8

    9/27

    Telosys 9

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 17

    DAO management

    DAO description : String s = dao.describe()

    The returned string contains the DAO Java Class

    the DAO target TABLE

    the set of SQL requests for standard operations :SELECT, INSERT, UPDATE, DELETE

    Other methods String dao.getTableName()

    Class dao.getEntityBeanClass()

    SqlRequests dao.getSqlRequests()

    DAO & Entities (VO)

  • 8/8/2019 Telosys DAL v1.8

    10/27

    Telosys 10

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 19

    DAO : load an entity by PK

    Set the Primary Key vo.setId(IdValue);

    Try to load the entity dao.load(vo);

    SQL request : Select from where

    Return code ( int) : 1 Found and loaded ( VO attributes are populated )

    0 Not found ( no change on the VO )

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 20

    DAO : load with lock

    Lock the record with a SELECT FOR UPDATE : dao.load(vo,FOR UPDATE);

    NB: Database specific => any string can be used !

    Use string constants !

  • 8/8/2019 Telosys DAL v1.8

    11/27

    Telosys 11

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 21

    DAO : check the existence of an entity

    Set the Primary Key vo.setId(IdValue);

    Check existence if ( dao.exists(vo) )

    boolean b = dao.exists(vo)

    SQL request : Select count where

    Return code ( boolean) True : the entity exists

    False : the entity doesnt exist

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 22

    DAO : update an entity

    Set the VO attributes vo.setId(IdValue);

    vo.setOther();

    Update dao.update(vo);

    SQL request : Update table set where

    Return code ( int) The standard JDBC return value (the number of rows

    affected). For an entity ( with a unique Key ) : 1 Entity updated ( entity found and updated )

    0 Entity not updated ( entity not found )

  • 8/8/2019 Telosys DAL v1.8

    12/27

    Telosys 12

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 23

    DAO : insert an entity

    Set the VO attributes vo.setId(IdValue);

    vo.setOther();

    Insert dao.insert(vo);

    SQL request : Insert into () values ()

    Return code ( int) The standard JDBC return value (the number of rows

    affected). For an entity ( with a unique Key ) : 1 Successful insert ( entity inserted )

    else : never happen Exception

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 24

    DAO : insert with key generation

    Set the VO attributes vo.setAttrib1(..);

    vo.setAttrib2(..);

    Insert Long key = dao.insertKeyGen(vo);

    Set the Key attribute in the VO instance vo.setId( key.intValue() );

    SQL request : Insert into () values ()

    Return code : The generated key wrapped in a Long object

    ( or null if none )

    Keyattributeis useless

  • 8/8/2019 Telosys DAL v1.8

    13/27

    Telosys 13

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 25

    DAO : delete an entity

    Set the Primary Key vo.setId(IdValue);

    Delete dao.delete(vo);

    SQL request : Delete from where

    Return code ( int) The standard JDBC return value (the number of rows

    affected). For an entity ( with a unique Key ) : 1 Entity deleted ( entity found and deleted )

    0 Entity not deleted ( entity not found )

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 26

    DAO : save an entity

    Set the VO attributes vo.setId(IdValue);

    vo.setOther();

    Save dao.save(vo);

    SQL requests : Selectcount from where

    if the enity exists

    Update

    else

    Insert

  • 8/8/2019 Telosys DAL v1.8

    14/27

    Telosys 14

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 27

    DAO : save an entity

    Return code ( int) The standard JDBC return value (the number of rows

    affected). For an entity ( with a unique Key ) : Always 1 :

    entity inserted 1 row affected

    or

    entity updated 1 row affected

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 28

    DAL Exceptions

    TelosysException Use a standardtry catch block

    What can happen ? Duplicate key on insert

    Other standard JDBC exceptions : the table has changed => the DAO is not up to date

    an integrity rule is violated

    etc

    try

    {

    // DAO operations

    }

    catch ( TelosysException ex )

    {

    ...

    }

  • 8/8/2019 Telosys DAL v1.8

    15/27

    Telosys 15

    D.A.O. & Lists (VOList)

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 30

    DAO and ListQueries

    A VOList is loaded by using a DAO and aListQuery

    A ListQuery holds 3 SQL requests with the same WHERE clause :

    Select from WHERE

    Select count form WHERE Delete form WHERE

    The parameters to set in the WHERE clause

    Lifecycle : 1/ created by the DAO

    2/ customized with the specific parameters

    3/ passed to the DAO methods to managed VOLists

  • 8/8/2019 Telosys DAL v1.8

    16/27

    Telosys 16

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 31

    DAO and ListQuery model

    . setParameter(int,int)

    . setParameter(int,long)

    . setParameter(int,short)

    . setParameter(int,Object)

    . getSqlCount()

    . getSqlSelect()

    . getSqlDelete()

    ListQuery>

    . createQuery()

    . createQuery()

    . createQuery()

    . createQuery()

    . createQueryAll()

    . createQueryAll()

    DAO

    creates

    uses

    .

    Criteria

    .

    Criterion

    *

    uses

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 32

    DAO : Query for all the entities of a table

    How to load ALL the entities of a table//--- Create the DAO

    EmployeDAO dao = new EmployeDAO();

    //--- Create the VOList

    EmployeVOList list = new EmployeVOList();

    //--- Define a query

    ListQuery query = dao.createQueryAll(); // ALL (no WHERE clause)

    //--- Load the list using the query

    dao.loadList(query, list);

    //--- Print the list size

    System.out.println("List size = " + list.size());

    //--- Print all the VO of the list

    Iterator iter = list.iterator() ;

    int i = 0 ;

    while ( iter.hasNext() )

    {

    System.out.println(" . " + (i++) + " : " + iter.next() );

    }

    ListQuery query = dao.createQueryAll( order by name );

  • 8/8/2019 Telosys DAL v1.8

    17/27

    Telosys 17

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 33

    DAO : Query with static criteria

    Query with static criteria without parameters :

    Query with static criteria with parameters :

    ListQuery query = dao.createQuery( "Matricule > 7000" );

    ListQuery query = dao.createQuery( "Matricule > 7000" ,

    "order by matricule" );

    //--- Load the list using the query

    dao.loadList(query, list);

    ListQuery query = dao.createQuery( "Matricule > ? and Matricule < ? " );

    ListQuery query = dao.createQuery( "Matricule > ? and Matricule < ? ",

    "order by matricule" );

    query.setParameter( 1, 100 ); // First parameter value

    query.setParameter( 2, 200 ); // Second parameter value

    //--- Load the list using the query

    dao.loadList(query, list);

    Param. index :from 1 to N

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 34

    DAO : Query with dynamic criteria

    Define the list of criteria :

    At request time, choose the criteria to use :

    Then, create the query :

    Criterion [] SQL_CRITERIA =

    {

    new Criterion( "nom like ?" , ParamType.STRING ), /* 1 */

    new Criterion( "prenom like ?" , ParamType.STRING ), /* 2 */

    new Criterion( "Matricule > ?" , ParamType.INTEGER ), /* 3 */

    new Criterion( "Matricule < ?" , ParamType.INTEGER ) /* 4 */

    };

    Criteria crit = new Criteria (SQL_CRITERIA, "and" );

    crit.doNotUse(1);

    crit.doNotUse(2);

    crit.useWithValue(3, "10");

    crit.useWithValue(4, "900");

    ListQuery query = dao.createQuery(crit, "order by Matricule");

  • 8/8/2019 Telosys DAL v1.8

    18/27

    Telosys 18

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 35

    DAO & ListQuery : limitations

    A DAO works with only one table

    DAO + ListQuery can only retrieve data from asingle table ( a list of VO/records load form thetable )

    To retrieve data with complexes requests see DataSet

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 36

    DAO : List persistence

    The DAO supports global list persistence,based on a query criteria :

    dao.deleteList( query ) Delete all the occurrences corresponding to the query

    criteria

    dao.saveList( query, list) Save the new list content by removing all the existing

    items then inserting the new list items : Delete all the occurrences corresponding to the query criteria

    Insert all the occurrences of the list parameter

    NB : be sure that theres no risk of integrity violation when

    using a global delete for a list

  • 8/8/2019 Telosys DAL v1.8

    19/27

    Telosys 19

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 37

    DAO : List persistence

    Bulk insert/update/delete :

    dao.insertList(list)

    dao.updateList(list)

    dao.deleteList(list)

    Insert, update or delete all the elements of thegiven list ( VOList or standard java.util.List )

    D.A.O. & Sequences

  • 8/8/2019 Telosys DAL v1.8

    20/27

    Telosys 20

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 39

    Sequence DAO

    A Sequence DAO is a specific type of DAOdedicated to the SEQUENCES

    It provides the standard NEXT VALand CURR VAL operations

    Sequence DAO are available for Oracle

    PostgreSQL

    MySQL ( sequence simulation with tables )

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 40

    Sequence DAO

    How to create and use a Sequence DAO

    SequenceDAO seq = new SequenceDAO( "request_id_seq", SequenceDAO.POSTGRESQL);

    //--- With the default DataBase

    long val = seq.nextVal();

    //--- With a specific DataBase ID

    long val = seq.nextVal(DbId);

    //--- With a connection

    long val = seq.nextVal(c);

    long val = seq.currVal(c);

    //--- With a DB Session

    long val = seq.nextVal(s);

    long val = seq.currVal(s);

    NB :currVal only witha connection

    SequenceDAO.POSTGRESQLSequenceDAO.ORACLESequenceDAO.MYSQL

  • 8/8/2019 Telosys DAL v1.8

    21/27

    Telosys 21

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 41

    Sequence DAO

    Best practice : create a class for each sequence

    public class EmpIdSeqDAO extends SequenceDAO

    {

    public EmpIdSeqDAO()

    {

    super( "EmpId" , ORACLE);

    }

    }

    //--- Use an existing Sequence DAOEmpIdSeqDAO empIdSeq = new EmpIdSeqDAO();

    System.out.println("nextVal : " + empIdSeq.nextVal() );

    DataSet

  • 8/8/2019 Telosys DAL v1.8

    22/27

    Telosys 22

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 43

    DataSet

    A DataSet is a set of DataRow ( 0..N rows )

    A DataRow is a set of Object ( 0..N cells )

    => a DataSet is like a TABLEwith ROWSand COLUMNS

    A DataSet is abstract and its content can beloaded from different sources.

    The source of the DataSet is defined in aDataSet definition

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 44

    DataSet

    Principle

    Step 1 : define it ( constructor + definition ) DataSet ds = new XXXXXX();

    Step 2 : load or reload it ds.load()

    ds.load ( origin )

    Step 3 : use it

    ds.load();

    for ( int i = 1 ; i

  • 8/8/2019 Telosys DAL v1.8

    23/27

    Telosys 23

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 45

    DataSet : how to fetch data

    for ( int i = 1 ; i

  • 8/8/2019 Telosys DAL v1.8

    24/27

    Telosys 24

    SQLDataSet

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 48

    SQLDataSet

    A SQLDataSet is an extension (subclass)of "DataSet"

    It can be loaded from a relational database,using a definition based on a SQL request

    The definition for a SQLDataSet is held by an

    instance ofSQLDataSetDefinition

  • 8/8/2019 Telosys DAL v1.8

    25/27

    Telosys 25

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 49

    SQLDataSet and relational DB

    Use case 1 : Basic SQLDataSet (no parameter)//--- DataSet without SQL parameters

    SQLDataSetDefinition def = new SQLDataSetDefinition(

    "select name, manager, agency",

    "from employee" ,

    null ,

    "order by name" );

    SQLDataSetDefinition def2 = new SQLDataSetDefinition(

    "select name, manager, agency from employee where agency > 10" );

    //--- Set a specific Database ID

    def.setDatabaseId(2);

    //--- Create the DataSet with the definition

    SQLDataSet ds = new SQLDataSet( def );

    //--- Load the DataSet using the Database specified in the definition

    ds.load() ;

    //--- Examples with the load(Object)method :

    ds.load(new Integer(1)); // load using the given Database ID

    ds.load(connection); // load using the given JDBC Connection

    ds.load(dbSession); // load using the given DatabaseSession

    No parameter in criteria :no "where clause"or static "where clause"

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 50

    SQLDataSet and relational DB

    Use case 2 :SQLDataSet with parameters ( static criteria )//--- DataSet with one INTEGER parameter

    int []paramTypes = { ParamType.INTEGER } ; // One param ( INTEGER )

    SQLDataSetDefinition def = new SQLDataSetDefinition(

    "select name, manager, agency",

    "from employee",

    "where agency = ? ","order by name",

    paramTypes ); // Array of param types

    SQLDataSetDefinition def2 = new SQLDataSetDefinition(

    "select name, manager, agency from employee where agency > ?",

    paramTypes );

    //--- Set the parameter values

    StringparamValues[] = { "1" }; // Array of param value(s)

    //--- Create the DataSet with the Definition

    SQLDataSet ds = new SQLDataSet( def,paramValues );

    1 .. N fixed parametersin the "where clause"

  • 8/8/2019 Telosys DAL v1.8

    26/27

    Telosys 26

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 51

    SQLDataSet and relational DB

    Use case 3 : SQLDataSet with dynamic criteria

    SQLDataSetDefinition def = new SQLDataSetDefinition(

    "select name, manager, agency",

    "from employee",

    null,

    "order by name");

    //--- Define the dynamic criteria

    Criterion[] SQL_CRITERIA = {

    new Criterion("name like ?", ParamType.STRING), /* 1 */

    new Criterion("manager like ?", ParamType.STRING), /* 2 */

    new Criterion("agency > ?", ParamType.INTEGER), /* 3 */

    new Criterion("agency < ?", ParamType.INTEGER) /* 4 */

    };

    Criteria crit = new Criteria(SQL_CRITERIA, "and");

    //--- Set dynamically the useful criteriacrit.useWithValue(1, "ZO%");

    crit.doNotUse(2);

    crit.doNotUse(3);

    crit.useWithValue(4, "7000");

    //--- Create the DataSet with the Definition

    SQLDataSet ds = new SQLDataSet( def, crit );

    No "where clause" ( dynamic )If "where clause", dynamic criteria will be added

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 52

    SQLDataSet use cases

    DataSet are useful for

    Complex SQL request :the SQLDataSetDefinition support Joins between different tables

    Additional SQL clauses :

    Order by Group by

    Search services :complex criteria combination with DynamicCriteria

  • 8/8/2019 Telosys DAL v1.8

    27/27

    Telosys framework - D.A.L. ( Laurent Gurin / ver 1.8 ) 53

    Other SQLDataSet capabilities

    Use the SQLDataSet definition for "count" int n = ds.count() ;

    Execute a "select count(*)" with the dataset definition criteria

    Load a "page" (a subset of the request result) Define the number of lines per page :

    def.setLinesPerPage(100); // default = 20

    Load a page : ds.loadPage(2);

    The SQL request is executed, only the expected rows areretrieved from the JDBC ResultSet

    boolean b = ds.endOfResultSet();To know if the last row of the ResultSet has been loaded

    Retrieve the SQL request : String sql = ds.getSqlRequest();

    T H E E N D


Recommended