logo

class

sql::Connection

sys::Obj
  sql::Connection

Connection manages a logical connection to an SQL database.

Refer to the Sql docs for more details.

Slots

autoCommit

Bool autoCommit

If auto commit is true, then each statement is executed immediately. Otherwise statements are executed inside a transaction which is terminated by a call to commit or rollback. Auto commit defaults to true.

close

Bool close()

Close the connection. This method is guaranteed to never throw an Err. Return true if the connection was closed successfully or false if closed abnormally.

commit

Void commit()

Commit all the changes made inside the current transaction.

execute

Int execute(Str sql)

Execute a SQL statement and if applicable return the number of rows modified.

isClosed

Bool isClosed()

Return if this connection has been closed.

open

static Connection open(Str database, Str username, Str password)

Open a connection to a SQL database. Throw Err on failure.

In a Java platform, the connection is opened using JDBC. The database Str specifies the java.sql.Connection URL. The JDBC driver class should be preloaded - the simplest mechanism is to add it to "sys.props".

prepare

Statement prepare(Str sql)

Prepare a statement for use later with this connection.

query

Obj[] query(Str sql, Type of := def)

Execute an SQL query which returns a relational table as a result. If of is null, the result is returned as a List of Rows. The Cols are available from List.of.fields or on type.fields of each row instance. If of is specified, then the result is returned as a List of that type. Columns are mapped to the type's fields. If a column cannot be mapped to a field, then an SqlErr is thrown.

queryEach

Void queryEach(Str sql, Type of, |Obj| each)

Execute an SQL query. For each row in the result, invoke the specified function c. If of is null, the Obj passed to the each function will be of type Row. Otherwise the of type specifies the row type passed to the each function.

rollback

Void rollback()

Undo any changes made inside the current transaction.

tableRow

Obj tableRow(Str tableName)

Get a default row instance for the specified table. The result has a field for each table column.

tables

Str[] tables()

List the tables in the database. Returns a list of the table names.