This application provides an Erlang interface to communicate with relational SQL-databases. It is built on top of Microsofts ODBC interface and therefore requires that you have an ODBC driver to the database that you want to connect to.
Here follows type definitions that are used by more than one function in the ODBC API.
The error handling strategy and possible errors sources are described in the Erlang ODBC User's Guide.
commit(Ref, CommitMode) ->
commit(Ref, CommitMode, TimeOut) -> ok | {error, Reason}
Types
Commits or rollbacks a transaction. Needed on connections where automatic commit is turned off.
connect(ConnectStr, Options) -> {ok, Ref} | {error, Reason}
Types
An example of a connection string:
"DSN=sql-server;UID=aladdin;PWD=sesame"
where DSN is your ODBC Data Source Name, UID is a database user id and PWD is the password for that user. These are usually the attributes required in the connection string, but some drivers have other driver specific attributes, for example
"DSN=Oracle8;DBQ=gandalf;UID=aladdin;PWD=sesame"
where DBQ is your TNSNAMES.ORA entry name e.g. some Oracle specific configuration attribute.
All options has default values.
Opens a connection to the database. The connection is associated with the process that created it and can only be accessed through it. This function may spawn new processes to handle the connection. These processes will terminate if the process that created the connection dies or if you call disconnect/1.
If automatic commit mode is turned on, each query will be considered as an individual transaction and will be automatically committed after it has been executed. If you want more than one query to be part of the same transaction the automatic commit mode should be turned off. Then you will have to call commit/3 explicitly to end a transaction.
The default timeout is infinity
>If the option binary_strings is turned on all strings will be returned as binaries and strings inputed to param_query will be expected to be binaries. The user needs to ensure that the binary is in an encoding that the database expects. By default this option is turned off.
As default result sets are returned as a lists of tuples. The TupleMode
option still exists to keep some degree of backwards compatibility. If the option is set to off, result sets will be returned as a lists of lists instead of a lists of tuples.
Scrollable cursors are nice but causes some overhead. For some connections speed might be more important than flexible data access and then you can disable scrollable cursor for a connection, limiting the API but gaining speed.
Note
Turning the scrollable_cursors option off is noted to make old odbc-drivers able to connect that will otherwhise fail.
If trace mode is turned on this tells the ODBC driver to write a trace log to the file SQL.LOG that is placed in the current directory of the erlang emulator. This information may be useful if you suspect there might be a bug in the erlang ODBC application, and it might be relevant for you to send this file to our support. Otherwise you will probably not have much use of this.
Note
For more information about the ConnectStr
see description of the function SQLDriverConnect in [1].
The extended_errors
option enables extended ODBC error information when an operation fails. Rather than returning {error, Reason}
, the failing function will reutrn {error, {ODBCErrorCode, NativeErrorCode, Reason}}
. Note that this information is probably of little use when writing database-independent code, but can be of assistance in providing more sophisticated error handling when dealing with a known underlying database.
-
ODBCErrorCode
is the ODBC error string returned by the ODBC driver. -
NativeErrorCode
is the numberic error code returned by the underlying database. The possible values and their meanings are dependent on the database being used. -
Reason
is as per the Reason
field when extended errors are not enabled.
Note
The current implementation spawns a port programm written in C that utilizes the actual ODBC driver. There is a default timeout of 5000 msec for this port programm to connect to the Erlang ODBC application. This timeout can be changed by setting an application specific environment variable 'port_timeout' with the number of milliseconds for the ODBC application. E.g.: [{odbc, [{port_timeout, 60000}]}] to set it to 60 seconds.
disconnect(Ref) -> ok | {error, Reason}
Types
Closes a connection to a database. This will also terminate all processes that may have been spawned when the connection was opened. This call will always succeed. If the connection can not be disconnected gracefully it will be brutally killed. However you may receive an error message as result if you try to disconnect a connection started by another process.
describe_table(Ref, Table) ->
describe_table(Ref, Table, Timeout) -> {ok, Description} | {error, Reason}
Types
Queries the database to find out the ODBC data types of the columns of the table Table
.
first(Ref) ->
first(Ref, Timeout) -> {selected, ColNames, Rows} | {error, Reason}
Types
Returns the first row of the result set and positions a cursor at this row.
last(Ref) ->
last(Ref, TimeOut) -> {selected, ColNames, Rows} | {error, Reason}
Types
Returns the last row of the result set and positions a cursor at this row.
next(Ref) ->
next(Ref, TimeOut) -> {selected, ColNames, Rows} | {error, Reason}
Types
Returns the next row of the result set relative the current cursor position and positions the cursor at this row. If the cursor is positioned at the last row of the result set when this function is called the returned value will be {selected, ColNames,[]}
e.i. the list of row values is empty indicating that there is no more data to fetch.
param_query(Ref, SQLQuery, Params) ->
param_query(Ref, SQLQuery, Params, TimeOut) -> ResultTuple | {error, Reason}
Types
Defines IN, OUT, and IN OUT Parameter Modes for stored procedures.
Executes a parameterized SQL query. For an example see the "Using the Erlang API"
in the Erlang ODBC User's Guide.
Note
Use the function describe_table/[2,3] to find out which ODBC data type that is expected for each column of that table. If a column has a data type that is described with capital letters, alas it is not currently supported by the param_query function. Too know which Erlang data type corresponds to an ODBC data type see the Erlang to ODBC data type mapping
in the User's Guide.
prev(Ref) ->
prev(ConnectionReference, TimeOut) -> {selected, ColNames, Rows} | {error, Reason}
Types
Returns the previous row of the result set relative the current cursor position and positions the cursor at this row.
start() ->
start(Type) -> ok | {error, Reason}
Types
Starts the odbc application. Default type is temporary. See application(3)
stop() -> ok
Stops the odbc application. See application(3)
sql_query(Ref, SQLQuery) ->
sql_query(Ref, SQLQuery, TimeOut) -> ResultTuple | [ResultTuple] |{error, Reason}
Types
Executes a SQL query or a batch of SQL queries. If it is a SELECT query the result set is returned, on the format {selected, ColNames, Rows}
. For other query types the tuple {updated, NRows}
is returned, and for batched queries, if the driver supports them, this function can also return a list of result tuples.
Note
Some drivers may not have the information of the number of affected rows available and then the return value may be {updated, undefined}
.
The list of column names is ordered in the same way as the list of values of a row, e.g. the first ColName
is associated with the first Value
in a Row
.
select_count(Ref, SelectQuery) ->
select_count(Ref, SelectQuery, TimeOut) -> {ok, NrRows} | {error, Reason}
Types
SQL SELECT query.
Executes a SQL SELECT query and associates the result set with the connection. A cursor is positioned before the first row in the result set and the tuple {ok, NrRows}
is returned.
Note
Some drivers may not have the information of the number of rows in the result set, then NrRows
will have the value undefined
.
select(Ref, Position, N) ->
select(Ref, Position, N, TimeOut) -> {selected, ColNames, Rows} | {error, Reason}
Types
Selection strategy, determines at which row in the result set to start the selection.
Should indicate a row number in the result set. When used together with the option
relative
it will be used as an offset from the current cursor position, when used together with the option
absolute
it will be interpreted as a row number.
Selects N
consecutive rows of the result set. If Position
is next
it is semantically equivalent of calling next/[1,2]
N
times. If Position
is {relative, Pos}
, Pos
will be used as an offset from the current cursor position to determine the first selected row. If Position
is {absolute, Pos}
, Pos
will be the number of the first row selected. After this function has returned the cursor is positioned at the last selected row. If there is less then N
rows left of the result set the length of Rows
will be less than N
. If the first row to select happens to be beyond the last row of the result set, the returned value will be {selected, ColNames,[]}
e.i. the list of row values is empty indicating that there is no more data to fetch.