Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Working with a database"

(New page: '''Defining a record''' <source lang="java"> record CUSTOMER type Entity{@table{name = "CUSTOMER"}} // record is bound to the CUSTOMER table (or view) CUSTID string{@id}; ...)
 
(Removing all content from page)
 
Line 1: Line 1:
'''Defining a record'''
 
  
<source lang="java">
 
record CUSTOMER type Entity{@table{name = "CUSTOMER"}}  // record is bound to the CUSTOMER table (or view)
 
    CUSTID string{@id};                                  // primary key field
 
    NAME string;       
 
    COUNTRY string;
 
    STATE string?;                                      // nullable field 
 
end
 
</source>
 
 
'''Getting a record'''
 
 
<source lang="java">
 
function getCustomer(someId String in} returns (Customer)
 
    ds SQLDataSource?{@resource {}};                      // declares a data source that will use binding name "ds" (since a name is not specified)
 
 
    aCust Customer;                                        // declares and empty customer record
 
    get aCust from ds using(someId);                      // gets the record in the table that has a key value of someID and populates the customer record
 
 
    get aCust from ds using(someId) with #sql{            // alternative approach for getting a single record (allows for customizing the SQL)
 
SELECT *
 
FROM CUSTOMER
 
WHERE id = ?
 
    };
 
 
    vals Dictionary;                                      // declares a new dictionary
 
    get vals from ds using(someId) with #sql{              // alternative approach for getting a single record (does not require a Record definition)
 
SELECT *
 
FROM CUSTOMER
 
WHERE id = ?
 
    };                                                    // creates a key/value pair in the dictionary for each column in the result set
 
 
    return (aCust);
 
end
 
</source>
 
 
'''Getting multiple records''' <source lang="java">
 
function getCustomer(} returns (Customer[])
 
    ds SQLDataSource?{@resource {}};                      // declares a data source that will use binding name "ds" (since a name is not specified)
 
 
    custs Customer[];                                      // declares a new dynamic array of customer records
 
    get custs from ds;                                    // populates the array with a Customer record for each row in the result set
 
 
    get custs from ds with #sql {
 
      select * from customer where state = 'CO'
 
    };                                                    // populates the array, but with a limited set of Customer records
 
 
    state String = "CO";
 
    get custs from ds using(state) with #sql {
 
      select * from customer where state = ?
 
    };                                                    // parameterized version of the previous example
 
 
 
end
 
</source>
 
 
'''Inserting a record'''
 
 
<source lang="java">
 
function addCustomer{customer Customer in}
 
    ds SQLDataSource?{@resource {}};
 
    add customer to ds;
 
end
 
</source>
 
 
'''Looping through a SQL result set'''
 
 
<source lang="java">
 
function loopCust()
 
    ds SQLDataSource?{@resource{uri = "binding:myDB" }};      // declares a new data source
 
    rs SQLResultSet?;                                          // declares a new result set
 
 
    open rs from ds with #sql{                                // opens a result set using the specified SQL query
 
SELECT * FROM CUSTOMER
 
    };
 
 
    myCust Customer;
 
 
    //Loop through results and write out customer name
 
    while(rs.getNext())
 
        get myCust from rs;
 
        Syslib.writeStdOut ("Customer name: " + myCust.name);
 
    end
 
 
end
 
</source>
 

Latest revision as of 12:02, 10 February 2012

Back to the top