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.
Difference between revisions of "EDT:Code snippets"
m (→Database access) |
|||
Line 1: | Line 1: | ||
Please [[#Share_a_code_snippet|share your code snippets]]! <br> | Please [[#Share_a_code_snippet|share your code snippets]]! <br> | ||
− | == | + | == Declaring data <br> == |
− | ''' | + | '''Variables''' |
<source lang="java"> | <source lang="java"> | ||
− | + | // defaults to 0 | |
− | someVal int | + | someVal int; |
− | + | ||
− | + | ||
− | + | // defaults to null. | |
− | + | someVal int?; | |
− | + | ||
− | + | // initializes value to 15 (future value can be null). | |
− | + | someVal int? = 15; | |
− | + | ||
− | + | ||
− | + | // initializes value to 23. | |
− | + | someVal int = 23; | |
− | + | // defaults to "". | |
+ | firstName string; | ||
− | + | // defaults to null. | |
+ | firstName string?; | ||
− | '''Constants''' | + | // initializes value to "John". |
+ | firstName string = "John"; | ||
+ | |||
+ | // defaults to 0. | ||
+ | largeVal bigint; | ||
+ | |||
+ | // number with 5 total digits (2 after the decimal point). Defaults to 000.00. | ||
+ | amount decimal(5,2); | ||
+ | |||
+ | // defaults to 0.0. | ||
+ | coord float; | ||
+ | |||
+ | // defaults to false | ||
+ | toggle boolean; | ||
+ | |||
+ | // defaults to today's date. | ||
+ | dob date; | ||
+ | |||
+ | // defaults to the current timestamp. | ||
+ | ts timestamp?; | ||
+ | |||
+ | // value can be set to any value (primitive, object, etc). See the casting examples later. | ||
+ | x any?; | ||
+ | </source> | ||
+ | |||
+ | '''Constants''' | ||
<source lang="java"> | <source lang="java"> | ||
+ | // value is unchangeable. | ||
const NUMBEROFDAYS int = 7; | const NUMBEROFDAYS int = 7; | ||
− | |||
− | '''Arrays''' | + | // values are changeable, but name cannot refer to another list. |
+ | const | ||
+ | </source> | ||
+ | |||
+ | '''Arrays''' | ||
<source lang="java"> | <source lang="java"> | ||
Line 55: | Line 81: | ||
<br> | <br> | ||
− | <br> | + | <br> |
== Rich UI == | == Rich UI == | ||
Line 61: | Line 87: | ||
== Services == | == Services == | ||
− | '''Calling a REST service''' | + | '''Calling a REST service''' |
<source lang="java"> | <source lang="java"> | ||
Line 69: | Line 95: | ||
</source> | </source> | ||
− | '''Accessing HTTP request headers''' | + | '''Accessing HTTP request headers''' |
<source lang="java"> | <source lang="java"> | ||
Line 78: | Line 104: | ||
</source> | </source> | ||
− | '''HTTP request and response''' | + | '''HTTP request and response''' |
<source lang="java"> | <source lang="java"> | ||
Line 92: | Line 118: | ||
== Database access == | == Database access == | ||
− | '''Defining a record''' | + | '''Defining a record''' |
<source lang="java"> | <source lang="java"> | ||
Line 103: | Line 129: | ||
</source> | </source> | ||
− | '''Getting a record''' | + | '''Getting a record''' |
<source lang="java"> | <source lang="java"> | ||
Line 127: | Line 153: | ||
return (aCust); | return (aCust); | ||
end | end | ||
− | </source> | + | </source> |
− | '''Getting multiple records''' | + | '''Getting multiple records''' <source lang="java"> |
− | <source lang="java"> | + | |
function getCustomer(} returns (Customer[]) | function getCustomer(} returns (Customer[]) | ||
ds SQLDataSource?{@resource {}}; // declares a data source that will use binding name "ds" (since a name is not specified) | ds SQLDataSource?{@resource {}}; // declares a data source that will use binding name "ds" (since a name is not specified) | ||
Line 148: | Line 173: | ||
end | end | ||
− | </source> | + | </source> |
− | '''Inserting a record''' | + | '''Inserting a record''' |
<source lang="java"> | <source lang="java"> | ||
Line 159: | Line 184: | ||
</source> | </source> | ||
− | '''Looping through a SQL result set''' | + | '''Looping through a SQL result set''' |
<source lang="java"> | <source lang="java"> | ||
Line 181: | Line 206: | ||
</source> | </source> | ||
− | <br> | + | <br> |
== Share a code snippet == | == Share a code snippet == |
Revision as of 14:49, 7 February 2012
Please share your code snippets!
Declaring data
Variables
// defaults to 0 someVal int; // defaults to null. someVal int?; // initializes value to 15 (future value can be null). someVal int? = 15; // initializes value to 23. someVal int = 23; // defaults to "". firstName string; // defaults to null. firstName string?; // initializes value to "John". firstName string = "John"; // defaults to 0. largeVal bigint; // number with 5 total digits (2 after the decimal point). Defaults to 000.00. amount decimal(5,2); // defaults to 0.0. coord float; // defaults to false toggle boolean; // defaults to today's date. dob date; // defaults to the current timestamp. ts timestamp?; // value can be set to any value (primitive, object, etc). See the casting examples later. x any?;
Constants
// value is unchangeable. const NUMBEROFDAYS int = 7; // values are changeable, but name cannot refer to another list. const
Arrays
vals int[]; // new dynamic array of size 0 vals int[]?; // dynamic array, but not instantiated (vals must be instantiated before it can be accessed) vals int[] = new int[4]; // new dynamic array of size 4; all values set to their default value (0 in this example) names string[] = [ "Paul", "John", "George", "Ringo" ]; // dynamic array, initialized with 4 values names[1] = "Bob"; // assigns the first index in the array to "Bob" names[names.getSize()] = "Ken"; // assigns the last index in the array to "Ken" cities string[]; // new dynamic array of size 0 cities.appendElement("Delta"); // appends a new value to the end of the array cities.appendAll([ "Denver", "Pueblo" ]); // appends two new values to the end of the array cities.removeElement(2); // removes the second value in the array cities.removeAll(); // removes all values from the array
Rich UI
Services
Calling a REST service
http HttpRest{request.uri="http:host\\myService"}; srvc IRest?; srvc = ServiceLib.completeBind(srvc, http);
Accessing HTTP request headers
http HttpRest{}; http.request.headers = new Dictionary { param1 = "a value to pass to my service" }; srvc IRest?{@Resource {}}; srvc = ServiceLib.completeBind(srvc, http);
HTTP request and response
function invokeDoSomething() call srvc.doSomething() returning to serviceCallback; end function serviceCallback(returnValueOne String, callbackHttp IHTTP in) // process callback request or response end
Database access
Defining a record
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
Getting a record
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
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
Inserting a record
function addCustomer{customer Customer in} ds SQLDataSource?{@resource {}}; add customer to ds; end
Looping through a SQL result set
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
Edit this page by logging in to eclipse.org and clicking the Edit tab. Use the Wikitext editor to add a new snippet. Add the title using Heading 4. Use the source lang="java" tag to provide a background for your snippet and some syntax highlighting.