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 "EDT:Code snippets"

m (Variable declarations)
Line 1: Line 1:
Real code for real people!
 
 
 
Please [[#Share_a_code_snippet|share your code snippets]]! <br>  
 
Please [[#Share_a_code_snippet|share your code snippets]]! <br>  
  
Line 29: Line 27:
 
</source>
 
</source>
  
==== Constant declaration  ====
+
'''Constants'''
  
 
<source lang="java">
 
<source lang="java">
const NUMBEROFDAYS INT = 7;
+
const NUMBEROFDAYS int = 7;
 
</source>
 
</source>
  
==== Arrays ====
+
'''Arrays'''
  
 
<source lang="java">
 
<source lang="java">
Line 63: Line 61:
 
== Services  ==
 
== Services  ==
  
==== Dynamic URI  ====
+
'''Calling a REST service'''
  
 
<source lang="java">
 
<source lang="java">
Line 71: Line 69:
 
</source>  
 
</source>  
  
==== HTTP Request Headers  ====
+
'''Accessing HTTP request headers'''
  
 
<source lang="java">
 
<source lang="java">
 
http HttpRest{};
 
http HttpRest{};
http.request.headers = new Dictionary{key1="a value to pass to my service"};
+
http.request.headers = new Dictionary { param1 = "a value to pass to my service" };
 
srvc IRest?{@Resource {}};
 
srvc IRest?{@Resource {}};
 
srvc = ServiceLib.completeBind(srvc, http);
 
srvc = ServiceLib.completeBind(srvc, http);
 
</source>  
 
</source>  
  
==== HTTP Request and Response  ====
+
'''HTTP request and response'''
  
 
<source lang="java">
 
<source lang="java">
Line 94: Line 92:
 
== Database access  ==
 
== Database access  ==
  
==== Record definition  ====
+
'''Defining a record'''
  
 
<source lang="java">
 
<source lang="java">
record CUSTOMER type Entity{@table{name = "CUSTOMER"}}
+
record CUSTOMER type Entity{@table{name = "CUSTOMER"}}
    NAME string
+
    NAME string;       
    COUNTRY string{@id};
+
    COUNTRY string{@id};                                   // primary key field
    STATE string?;
+
    STATE string?;                                         // nullable string field
    CUSTID string;
+
    CUSTID string;
end
+
end
 
</source>  
 
</source>  
  
==== Get a record ====
+
'''Getting a single record'''
  
 
<source lang="java">
 
<source lang="java">
  function getCust{id String in} returns (Customer)
+
  function getCustomer{someId String in} returns (Customer)
    ds SQLDataSource?{@resource {}};
+
    ds SQLDataSource?{@resource {}};                       // declares a data source that will use binding name "ds" (since a name is not specified)
    aCust Customer;
+
 
    get aCust from ds using(id);
+
    aCust Customer;                                       // declares and empty customer record
    return (aCust);
+
    get aCust from ds using(someId);                       // gets the record in the table that has a key value of someID and populates the customer record
end
+
 
 +
    return (aCust);
 +
end
 
</source>  
 
</source>  
  
==== Add a record ====
+
'''Inserting a record'''
  
 
<source lang="java">
 
<source lang="java">
function addCust{customer Customer in}
+
function addCustomer{customer Customer in}
    ds SQLDataSource?{@resource {}};
+
    ds SQLDataSource?{@resource {}};
    add customer to ds;
+
    add customer to ds;
end
+
end
 
</source>  
 
</source>  
  
==== Loop through a SQL result set ====
+
'''Looping through a SQL result set'''
  
 
<source lang="java">
 
<source lang="java">
 
  function loopCust()
 
  function loopCust()
     ds SQLDataSource?{@resource{bindingkey = "MyDB"}};
+
     ds SQLDataSource?{@resource{uri = "binding:myDB" }};       // declares a new data source
     rs SQLResultSet?;
+
     rs SQLResultSet?;                                         // declares a new result set
  
     open rs from ds with #sql{
+
     open rs from ds with #sql{                                 // opens a result set using the specified SQL query
 
SELECT * FROM CUSTOMER
 
SELECT * FROM CUSTOMER
 
     };
 
     };

Revision as of 16:27, 4 February 2012

Please share your code snippets!

General

Variable Declarations

someVal int;                // defaults to 0
someVal int?;               // defaults to null
someVal int? = 15;          // initializes value to 15 (future value can be null)
someVal int = 23;           // initializes value to 23
 
firstName string;           // defaults to ""
firstName string?;          // defaults to null
firstName string = "John";  // initializes value to "John"
 
largeVal bigint;            // defaults to 0
amount decimal(5,2);        // number with 5 total digits (2 after the decimal point). Defaults to 000.00;
coord float;                // defaults to 0.0
toggle boolean;             // defaults to false
 
dob date;                   // defaults to today's date
ts timestamp?;
 
x any?;                     // value can be set to any value (primitive, object, etc). See casting examples below.

Constants

const NUMBEROFDAYS int = 7;

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"}}
    NAME string;        
    COUNTRY string{@id};                                   // primary key field
    STATE string?;                                         // nullable string field
    CUSTID string;
end

Getting a single 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
 
    return (aCust);
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


Share a code snippet

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.

Back to the top