• Modules
    • By category
    • By name
    • Most popular
    • Most downloaded
    • Repository
  • Register
  • Log in
  • Help
    • Start using Ceylon Herd
    • Publish your first module
    • Module publishing guidelines
    • All about Ceylon
    • Keyboard Shortcuts

    • s Focus search module bar
      ? Open this information panel
      j Move selection down
      k Move selection up
      enter Open current selection
Module info
Name
FroMage / ceylon.dbc
Ceylon Database Platform Module
Description

This module offers some components for JDBC-based database connectivity. The main component is the class Sql, an instance of which may be obtained for any given [[javax.sql::DataSource]].

value sql = Sql(newConnectionFromDataSource(dataSource));

You can easily get a query result as a Sequence where each row is a Map:

value rows = sql.Select("select * from mytable").execute();
for (row in rows) {
    assert (is String name = rows["name"]);
    assert (is Integer count = rows["count"]);
    ...
}

You can define parameters to the query, using the ? notation:

value rows = sql.Select("select * from mytable where col1=? and col2=?")
                .execute(arg1, arg2);

The Sql.Select.Results class lets results be iterated lazily:

try (results = sql.Select("select * from mytable where date>?")
                  .Results(date)) {
    results.limit = 50;
    for (row in results) {
        ...
    }
}

Alternatively, Sql.Select.forEachRow is a little less verbose:

sql.Select("select * from mytable where date>?")
   .forEachRow(date)((row) {
    ...
});

A Sql.Select is reusable:

value query = sql.Select("select * from mytable where col=?");
value result1 = query.execute(value1);
value result2 = query.execute(value2);

And of course you can execute update and insert statements, using Sql.Update and Sql.Insert:

sql.Update("update table SET col=? where key=?")
   .execute(newValue, key);

sql.Insert("insert into table (key,col) values (?, ?)")
   .execute(key, initialValue);

If you need to perform several operations within a single transaction, you can pass a function to the method Sql.transaction. All statements of the function will be executed within a transaction, using the same JDBC connection, and finally the transaction is committed iff the function returns true:

sql.transaction {
    function do() {
        sql.Insert("insert ... ").execute();
        sql.Update("update ... ").execute();
        sql.Update("delete ... ").execute();
        //return true to commit the transaction
        //return false or throw to roll it back
        return true;
    }
};

To pass a null value as an argument, use a SqlNull with the right SQL type (defined by the JDBC class [[java.sql::Types]]):

sql.Update("update table set col=? where key=?")
   .execute(SqlNull(Types.integer));

If a column is null on a result row, it will be represented as a SqlNull instance under the column's name.

Category SDK

The Ceylon SDK

Last Published Aug 21, 2017
Stats Downloads (JVM): 7058
Downloads (JS): 0
Source downloads: 7275
Module links Home
Code repository
Issue tracker
Imported By
Browse
List of published versions
1.3.3 JVM JVM: 1.2.x, 1.3.x (latest) Published Aug 21, 2017 Browse
View docs
1.3.2 JVM JVM: 1.2.x, 1.3.x (latest) Published Mar 1, 2017 Browse
View docs
1.3.1 JVM JVM: 1.2.x, 1.3.x (latest) Published Nov 21, 2016 Browse
View docs
1.3.0 JVM JVM: 1.2.x, 1.3.x (latest) Published Sep 15, 2016 Browse
View docs
1.2.2 JVM JVM: 1.2.x, 1.3.x (latest) Published Mar 11, 2016 Browse
View docs
1.2.1 JVM JVM: 1.2.x, 1.3.x (latest) Published Feb 11, 2016 Browse
View docs
1.2.0 JVM JVM: 1.2.x, 1.3.x (latest) Published Oct 28, 2015 Browse
View docs
1.1.0 JVM JVM: 1.1.0 (outdated) Published Oct 8, 2014 Browse
View docs
1.0.0 JVM JVM: 1.0.0 (outdated) Published Nov 11, 2013 Browse
View docs
0.6.1 JVM JVM: M6 (outdated) Published Sep 24, 2013 Browse
View docs
0.6 JVM JVM: M6 (outdated) Published Sep 20, 2013 Browse
View docs
0.5 JVM JVM: M5 (outdated) Published Mar 14, 2013 Browse
View docs
You must be logged in to comment.

Ceylon Herd v1.24 Copyright 2012-2023 Red Hat. About