1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
7
<year>1999</year><year>2009</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
25
<prepared>Ingela Anderton Andin</prepared>
26
<responsible></responsible>
32
<modulesummary>Erlang ODBC application</modulesummary>
34
<p>This application provides an Erlang interface to communicate
35
with relational SQL-databases. It is built on top of Microsofts
36
ODBC interface and therefore requires that you have an ODBC driver
37
to the database that you want to connect to.</p>
39
<p>The functions <c>first/[1,2]</c>, <c>last/[1,2]</c>,
40
<c>next/[1,2]</c>, <c>prev[1,2]</c> and <c>select/[3,4]</c>
41
assumes there is a result set associated with the connection to
42
work on. Calling the function <c>select_count/[2,3]</c>
43
associates such a result set with the connection. Calling
44
select_count again will remove the current result set
45
association and create a new one. Calling a function which dose
46
not operate on an associated result sets, such as
47
<c>sql_query/[2,3]</c>, will remove the current result set
49
<p>Alas some drivers only support sequential traversal of the
50
result set, e.i. they do not support what in the ODBC world is
51
known as scrollable cursors. This will have the effect that
52
functions such as <c>first/[1,2]</c>, <c>last/[1,2]</c>,
53
<c>prev[1,2]</c>, etc will return <c>{error, driver_does_not_support_function}</c></p>
58
<title>COMMON DATA TYPES </title>
59
<p>Here follows type definitions that are used by more than one
60
function in the ODBC API. </p>
62
<p>The type <c>TimeOut</c> has the default value
63
<c>infinity</c>, so for instance: <br></br>
65
commit(Ref, CommitMode) is the same as
66
commit(Ref, CommitMode, infinity). If the
67
timeout expires the client will exit with the reason
71
connection_reference() - as returned by connect/2 </code>
73
time_out() = milliseconds() | infinity </code>
75
milliseconds() = integer() >= 0 </code>
77
common_reason() = connection_closed | term() - some kind of
78
explanation of what went wrong </code>
80
string() = list of ASCII characters </code>
82
col_name() = string() - Name of column in the result set </code>
84
col_names() - [col_name()] - e.g. a list of the names of the
85
selected columns in the result set. </code>
87
row() = {value()} - Tuple of column values e.g. one row of the
90
value() = null | term() - A column value. </code>
92
rows() = [row()] - A list of rows from the result set. </code>
95
{updated, n_rows()} | {selected, col_names(), rows()} </code>
97
n_rows() = integer() - The number of affected rows for UPDATE,
98
INSERT, or DELETE queries. For other query types the value
99
is driver defined, and hence should be ignored. </code>
101
odbc_data_type() = sql_integer | sql_smallint | sql_tinyint |
102
{sql_decimal, precison(), scale()} |
103
{sql_numeric, precison(), scale()} |
104
{sql_char, size()} | {sql_varchar, size()} | {sql_float, precision()} |
105
{sql_float, precision()} | sql_real | sql_double | sql_bit | atom()
108
precision() = integer() </code>
110
scale() = integer() </code>
112
size() = integer() </code>
116
<title>ERROR HANDLING </title>
117
<p>The error handling strategy and possible errors sources are
118
described in the Erlang ODBC <seealso marker="error_handling">User's Guide.</seealso></p>
122
<name>commit(Ref, CommitMode) -></name>
123
<name>commit(Ref, CommitMode, TimeOut) -> ok | {error, Reason} </name>
124
<fsummary>Commits or rollbacks a transaction. </fsummary>
126
<v>Ref = connection_reference() </v>
127
<v>CommitMode = commit | rollback</v>
128
<v>TimeOut = time_out()</v>
129
<v>Reason = not_an_explicit_commit_connection | process_not_owner_of_odbc_connection | common_reason()</v>
132
<p>Commits or rollbacks a transaction. Needed on connections
133
where automatic commit is turned off.</p>
137
<name>connect(ConnectStr, Options) -> {ok, Ref} | {error, Reason} </name>
138
<fsummary>Opens a connection to the database. </fsummary>
140
<v>ConnectStr = string()</v>
141
<d>An example of a connection string:<c>"DSN=sql-server;UID=alladin;PWD=sesame"</c>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<c>"DSN=Oracle8;DBQ=gandalf;UID=alladin;PWD=sesame"</c>where DBQ is your TNSNAMES.ORA entry name e.g. some Oracle specific configuration attribute.</d>
142
<v>Options = [] | [option()]</v>
143
<d>All options has default values. </d>
144
<v>option() = {auto_commit, auto_commit_mode()} | {timeout, milliseconds()} | {tuple_row, tuple_mode()} | {scrollable_cursors, use_srollable_cursors()} | {trace_driver, trace_mode()} </v>
145
<d>The default timeout is infinity </d>
146
<v>auto_commit_mode() = on | off </v>
147
<d>Default is on.</d>
148
<v>tuple_mode() = on | off </v>
149
<d>Default is on. The option is deprecated and should not be used in new code.</d>
150
<v>use_srollable_cursors() = on | off </v>
151
<d>Default is on.</d>
152
<v>trace_mode() = on | off </v>
153
<d>Default is off.</d>
154
<v>Ref = connection_reference() - should be used to acess the connection. </v>
155
<v>Reason = port_program_executable_not_found | common_reason()</v>
158
<p>Opens a connection to the database. The connection is
159
associated with the process that created it and can only be
160
accessed through it. This funtion may spawn new processes
161
to handle the connection. These processes will terminate if
162
the process that created the connection dies or if you call
164
<p>If automatic commit mode is turned on, each query will be
165
considered as an individual transaction and will be
166
automaticly commited after it has been executed. If you want
167
more than one query to be part of the same transaction the automatic
168
commit mode should be turned off. Then you will have to call
169
commit/3 explicitly to end a transaction. </p>
170
<p>As default result sets are returned as a lists of
171
tuples. The <c>TupleMode</c> option still exists to keep some
172
degree of backwards compatiblity. If the option is set to
173
off, result sets will be returned as a lists of lists
174
instead of a lists of tuples.</p>
175
<p>Scrollable cursors are nice but causes some overhead. For
176
some connections speed might be more important than flexible
177
data access and then you can disable scrollable cursor for a
178
connection, limiting the API but gaining speed</p>
179
<p>If trace mode is turned on this tells the ODBC driver to
180
write a trace log to the file SQL.LOG that is placed in the
181
current directory of the erlang emulator. This information
182
may be useful if you suspect there might be a bug in the
183
erlang ODBC application, and it might be relevant for you to
184
send this file to our support. Otherwise you will probably
185
not have much use of this.</p>
187
<p>For more information about the <c>ConnectStr</c> see
188
description of the function SQLDriverConnect in [1].</p>
193
<name>disconnect(Ref) -> ok | {error, Reason} </name>
194
<fsummary>Closes a connection to a database. </fsummary>
196
<v>Ref = connection_reference()</v>
197
<v>Reason = process_not_owner_of_odbc_connection</v>
200
<p>Closes a connection to a database. This will also
201
terminate all processes that may have been spawned
202
when the connection was opened. This call will always succeed.
203
If the connection can not be disconnected gracefully it will
204
be brutally killed. However you may receive an error message
205
as result if you try to disconnect a connection started by another
207
<marker id="describe_table"></marker>
212
<name>describe_table(Ref, Table) -> </name>
213
<name>describe_table(Ref, Table, Timeout) -> {ok, Description} | {error, Reason} </name>
214
<fsummary>Queries the database to find out the data types of the columns of the table <c>Table</c>. </fsummary>
216
<v>Ref = connection_reference()</v>
217
<v>Table = string() - Name of databas table.</v>
218
<v>TimeOut = time_out()</v>
219
<v>Description = [{col_name(), odbc_data_type()}]</v>
220
<v>Reason = common_reason()</v>
223
<p>Queries the database to find out the ODBC data types of the
224
columns of the table <c>Table</c>. </p>
228
<name>first(Ref) -></name>
229
<name>first(Ref, Timeout) -> {selected, ColNames, Rows} | {error, Reason} </name>
230
<fsummary>Returns the first row of the result set and positions a cursor at this row.</fsummary>
232
<v>Ref = connection_reference()</v>
233
<v>TimeOut = time_out()</v>
234
<v>ColNames = col_names() </v>
236
<v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
239
<p>Returns the first row of the result set and positions a
240
cursor at this row.</p>
244
<name>last(Ref) -></name>
245
<name>last(Ref, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
246
<fsummary>Returns the last row of the result set and positions a cursor at this row. </fsummary>
248
<v>Ref = connection_reference()</v>
249
<v>TimeOut = time_out()</v>
250
<v>ColNames = col_names() </v>
252
<v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
255
<p>Returns the last row of the result set and positions a
256
cursor at this row.</p>
260
<name>next(Ref) -> </name>
261
<name>next(Ref, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
262
<fsummary>Returns the next row of the result set relative the current cursor position and positions the cursor at this row. </fsummary>
264
<v>Ref = connection_reference()</v>
265
<v>TimeOut = time_out()</v>
266
<v>ColNames = col_names() </v>
268
<v>Reason = result_set_does_not_exist | process_not_owner_of_odbc_connection | common_reason() </v>
271
<p>Returns the next row of the result set relative the
272
current cursor position and positions the cursor at this
273
row. If the cursor is positioned at the last row of the
274
result set when this function is called the returned value
275
will be <c>{selected, ColNames,[]}</c> e.i. the list of row
276
values is empty indicating that there is no more data to fetch.
277
<marker id="param_query"></marker>
282
<name>param_query(Ref, SQLQuery, Params) -> </name>
283
<name>param_query(Ref, SQLQuery, Params, TimeOut) -> ResultTuple | {error, Reason} </name>
284
<fsummary>Executes a parameterized SQL query.</fsummary>
286
<v>Ref = connection_reference()</v>
287
<v>SQLQuery = string() - a SQL query with parameter markers/place holders in form of question marks.</v>
288
<v>Params = [{odbc_data_type(), [value()]}] |[{odbc_data_type(), in_or_out(), [value()]}] </v>
289
<v>in_or_out = in | out | inout</v>
290
<d>Defines IN, OUT, and IN OUT Parameter Modes for stored procedures.</d>
291
<v>TimeOut = time_out()</v>
292
<v>Values = term() - Must be consistent with the Erlang data type that corresponds to the ODBC data type ODBCDataType</v>
295
<p>Executes a parameterized SQL query. For an
296
example see the <seealso marker="getting_started#param_query">"Using the Erlang API"</seealso> in the Erlang ODBC
299
<p>Use the function describe_table/[2,3] to find out which
300
ODBC data type that is expected for each column of that
301
table. If a column has a data type that is described with
302
capital letters, alas it is not currently supported by the
303
param_query function. Too know which Erlang data type
304
corresponds to an ODBC data type see the Erlang to ODBC
305
data type<seealso marker="databases#type">mapping</seealso> in the User's Guide.</p>
310
<name>prev(Ref) -> </name>
311
<name>prev(ConnectionReference, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
312
<fsummary>Returns the previous row of the result set relative the current cursor position and positions the cursor at this row. </fsummary>
314
<v>Ref = connection_reference()</v>
315
<v>TimeOut = time_out()</v>
316
<v>ColNames = col_names() </v>
318
<v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
321
<p>Returns the previous row of the result set relative the
322
current cursor position and positions the
323
cursor at this row.</p>
328
<name>start() -> </name>
329
<name>start(Type) -> ok | {error, Reason}</name>
330
<fsummary>Starts the odb application. </fsummary>
333
<v>Type = permanent | transient | temporary
338
<p> Starts the odbc application. Default type
340
<seealso marker="kernel:application">See application(3)</seealso>
346
<name>stop() -> ok </name>
347
<fsummary> Stops the odbc application.</fsummary>
350
<p> Stops the odbc application.
351
<seealso marker="kernel:application">See application(3)</seealso>
357
<name>sql_query(Ref, SQLQuery) -> </name>
358
<name>sql_query(Ref, SQLQuery, TimeOut) -> ResultTuple | [ResultTuple] |{error, Reason}</name>
359
<fsummary>Executes a SQL query or a batch of SQL queries. If it is a SELECT query the result set is returned, on the format<c>{selected, ColNames, Rows}</c>. For other query types the tuple <c>{updated, NRows}</c>is returned, and for batched queries, if the driver supports them, this function can also return a list of result tuples.</fsummary>
361
<v>Ref = connection_reference()</v>
362
<v>SQLQuery = string() - The string may be composed by several SQL-queries separated by a ";", this is called a batch. </v>
363
<v>TimeOut = time_out()</v>
364
<v>ResultTuple = result_tuple() </v>
365
<v>Reason = process_not_owner_of_odbc_connection | common_reason() </v>
368
<p>Executes a SQL query or a batch of SQL queries. If it
369
is a SELECT query the result set is returned, on the format
370
<c>{selected, ColNames, Rows}</c>. For other query types the
371
tuple <c>{updated, NRows}</c> is returned, and for batched
372
queries, if the driver supports them, this function can also
373
return a list of result tuples.</p>
375
<p>Some drivers may not have the information of the number
376
of affected rows available and then the return value may
377
be <c>{updated, undefined} </c>. </p>
378
<p>The list of column names is ordered in the same way as the
379
list of values of a row, e.g. the first <c>ColName</c> is
380
associated with the first <c>Value</c> in a <c>Row</c>.</p>
386
<name>select_count(Ref, SelectQuery) -> </name>
387
<name>select_count(Ref, SelectQuery, TimeOut) -> {ok, NrRows} | {error, Reason} </name>
388
<fsummary>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 <c>{ok, NrRows}</c>is returned. </fsummary>
390
<v>Ref = connection_reference()</v>
391
<v>SelectQuery = string()</v>
392
<d>SQL SELECT query.</d>
393
<v>TimeOut = time_out()</v>
394
<v>NrRows = n_rows()</v>
395
<v>Reason = process_not_owner_of_odbc_connection | common_reason() </v>
398
<p>Executes a SQL SELECT query and associates the result set
399
with the connection. A cursor is positioned before the first
400
row in the result set and the tuple <c>{ok, NrRows}</c> is
403
<p>Some drivers may not have the information of the number of
404
rows in the result set, then <c>NrRows</c> will have the value
405
<c>undefined</c>. </p>
410
<name>select(Ref, Position, N) -></name>
411
<name>select(Ref, Position, N, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
412
<fsummary>Selects <c>N</c>consecutive rows of the result set.</fsummary>
414
<v>Ref = connection_reference()</v>
415
<v>Position = next | {relative, Pos} | {absolute, Pos} </v>
416
<d>Selection strategy, determines at which row in the result set to start the selection.</d>
417
<v>Pos = integer() </v>
418
<d>Should indicate a row number in the result set. When used together with the option <c>relative</c>it will be used as an offset from the current cursor position, when used together with the option <c>absolute</c>it will be interpreted as a row number.</d>
419
<v>N = integer() </v>
420
<v>TimeOut = time_out()</v>
421
<v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
424
<p>Selects <c>N</c> consecutive rows of the result set. If
425
<c>Position</c> is <c>next</c> it is semanticly equivalent
426
of calling <c>next/[1,2]</c><c>N</c> times. If
427
<c>Position</c> is <c>{relative, Pos}</c>, <c>Pos</c> will be
428
used as an offset from the current cursor position to
429
determine the first selected row. If <c>Position</c> is
430
<c>{absolute, Pos}</c>, <c>Pos</c> will be the number of the
431
first row selected. After this function has returned the
432
cursor is positioned at the last selected row. If there is
433
less then <c>N</c> rows left of the result set the length of
434
<c>Rows</c> will be less than <c>N</c>. If the first row to
435
select happens to be beyond the last row of the result set,
436
the returned value will be <c>{selected, ColNames,[]}</c>
437
e.i. the list of row values is empty indicating that there
438
is no more data to fetch.</p>
444
<title>REFERENCES</title>
445
<p>[1]: Microsoft ODBC 3.0, Programmer's Reference and SDK Guide <br></br>
447
See also http://msdn.microsoft.com/</p>