<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1999</year><year>2011</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.

      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.

    </legalnotice>

    <title>odbc</title>
    <prepared>Ingela Anderton Andin</prepared>
    <responsible></responsible>
    <docno></docno>
    <date></date>
    <rev></rev>
  </header>
  <module>odbc</module>
  <modulesummary>Erlang ODBC application</modulesummary>
  <description>
    <p>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.</p>
    <note>
      <p>The functions <c>first/[1,2]</c>, <c>last/[1,2]</c>,
        <c>next/[1,2]</c>, <c>prev[1,2]</c> and <c>select/[3,4]</c>
        assumes there is a result set associated with the connection to
        work on. Calling the function <c>select_count/[2,3]</c>
        associates such a result set with the connection. Calling
        select_count again will remove the current result set
        association and create a new one. Calling a function which dose
        not operate on an associated result sets, such as
        <c>sql_query/[2,3]</c>, will remove the current result set
        association.</p>
      <p>Alas some drivers only support sequential traversal of the
        result set, e.i. they do not support what in the ODBC world is
        known as scrollable cursors. This will have the effect that
        functions such as <c>first/[1,2]</c>, <c>last/[1,2]</c>,
        <c>prev[1,2]</c>, etc will return <c>{error, driver_does_not_support_function}</c></p>
    </note>
  </description>

  <section>
    <title>COMMON DATA TYPES </title>
    <p>Here follows type definitions that are used by more than one
      function in the ODBC API. </p>
    <note>
      <p>The type <c>TimeOut</c> has the default value
        <c>infinity</c>, so for instance:        <br></br>

        commit(Ref, CommitMode) is the same as
        commit(Ref, CommitMode, infinity). If the
        timeout expires the client will exit with the reason
        timeout. </p>
    </note>
    <code type="none">
 connection_reference() - as returned by connect/2    </code>
    <code type="none">
 time_out() = milliseconds() | infinity    </code>
    <code type="none">
 milliseconds() = integer() >= 0     </code>
    <code type="none">
 common_reason() = connection_closed | term() - some kind of
                 explanation of what went wrong    </code>
    <code type="none">
 string() = list of ASCII characters    </code>
    <code type="none">
 col_name() = string() - Name of column in the result set    </code>
    <code type="none">
 col_names() - [col_name()] - e.g. a list of the names of the
           selected columns in the result set.     </code>
    <code type="none">
 row() = {value()} - Tuple of column values e.g. one row of the
           result set.     </code>
    <code type="none">
 value() = null | term() - A column value.     </code>
    <code type="none">
 rows() = [row()] - A list of rows from the result set.    </code>
    <code type="none">
 result_tuple() =
      {updated, n_rows()} | {selected, col_names(), rows()}    </code>
    <code type="none">
 n_rows() = integer() - The number of affected rows for UPDATE,
           INSERT, or DELETE queries. For other query types the value
           is driver defined, and hence should be ignored.    </code>
    <code type="none">
 odbc_data_type() = sql_integer | sql_smallint | sql_tinyint |
      {sql_decimal, precision(), scale()} |
      {sql_numeric, precision(), scale()} |
      {sql_char, size()} | {sql_wchar, size()} | {sql_varchar, size()} | {sql_wvarchar, size()}| {sql_float, precision()} |
      {sql_wlongvarchar, size()} | {sql_float, precision()} | sql_real | sql_double | sql_bit | atom()
    </code>
    <code type="none">
 precision() = integer()    </code>
    <code type="none">
 scale() = integer()    </code>
    <code type="none">
 size() = integer()    </code>
  </section>

  <section>
    <title>ERROR HANDLING </title>
    <p>The error handling strategy and possible errors sources are
      described in the Erlang ODBC <seealso marker="error_handling">User's Guide.</seealso></p>
  </section>
  <funcs>
    <func>
      <name>commit(Ref, CommitMode) -></name>
      <name>commit(Ref, CommitMode, TimeOut) -> ok | {error, Reason} </name>
      <fsummary>Commits or rollbacks a transaction. </fsummary>
      <type>
        <v>Ref = connection_reference() </v>
        <v>CommitMode = commit | rollback</v>
        <v>TimeOut = time_out()</v>
        <v>Reason = not_an_explicit_commit_connection | process_not_owner_of_odbc_connection | common_reason()</v>
      </type>
      <desc>
        <p>Commits or rollbacks a transaction. Needed on connections
          where automatic commit is turned off.</p>
      </desc>
    </func>
    <func>
      <name>connect(ConnectStr, Options) -> {ok, Ref} | {error, Reason} </name>
      <fsummary>Opens a connection to the database. </fsummary>
      <type>
        <v>ConnectStr = string()</v>
        <d>An example of a connection string: <c>"DSN=sql-server;UID=aladdin;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=aladdin;PWD=sesame"</c> where DBQ is your TNSNAMES.ORA entry name e.g. some Oracle specific configuration attribute.</d>
        <v>Options = [] | [option()]</v>
        <d>All options has default values. </d>
        <v>option() = {auto_commit, on | off} | {timeout, milliseconds()}
	| {binary_strings, on | off} | {tuple_row, on | off} | {scrollable_cursors, on | off} |
	{trace_driver, on | off} </v>
        <v>Ref = connection_reference() - should be used to access the connection. </v>
        <v>Reason = port_program_executable_not_found | common_reason()</v>
      </type>
      <desc>
        <p>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.</p>
	    
	  <p>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. </p>

	  <p>The default timeout is infinity </p>
	  
	  <p> >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.</p>
	  
        <p>As default result sets are returned as a lists of
          tuples. The <c>TupleMode</c> 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.</p>
	  
        <p>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.</p>

	  <note><p>Turning the scrollable_cursors option off is noted
	  to make old odbc-drivers able to connect that will otherwhise fail.</p></note>
	  
        <p>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.</p>
	  
        <note>
          <p>For more information about the <c>ConnectStr</c> see
            description of the function SQLDriverConnect in [1].</p>
        </note>
      </desc>
    </func>
    <func>
      <name>disconnect(Ref) -> ok | {error, Reason} </name>
      <fsummary>Closes a connection to a database. </fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>Reason = process_not_owner_of_odbc_connection</v>
      </type>
      <desc>
        <p>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.
                    <marker id="describe_table"></marker>
</p>
      </desc>
    </func>
    <func>
      <name>describe_table(Ref, Table) -> </name>
      <name>describe_table(Ref, Table, Timeout) -> {ok, Description} | {error, Reason} </name>
      <fsummary>Queries the database to find out the data types of the columns of the table <c>Table</c>. </fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>Table = string() - Name of databas table.</v>
        <v>TimeOut = time_out()</v>
        <v>Description = [{col_name(), odbc_data_type()}]</v>
        <v>Reason = common_reason()</v>
      </type>
      <desc>
        <p>Queries the database to find out the ODBC data types of the
          columns of the table <c>Table</c>. </p>
      </desc>
    </func>
    <func>
      <name>first(Ref) -></name>
      <name>first(Ref, Timeout) -> {selected, ColNames, Rows} | {error, Reason} </name>
      <fsummary>Returns the first row of the result set and positions a cursor at this row.</fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>TimeOut = time_out()</v>
        <v>ColNames = col_names() </v>
        <v>Rows = rows()</v>
        <v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>Returns the first row of the result set and positions a
          cursor at this row.</p>
      </desc>
    </func>
    <func>
      <name>last(Ref) -></name>
      <name>last(Ref, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
      <fsummary>Returns the last row of the result set and positions a cursor at this row. </fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>TimeOut = time_out()</v>
        <v>ColNames = col_names() </v>
        <v>Rows = rows()</v>
        <v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>Returns the last row of the result set and positions a
          cursor at this row.</p>
      </desc>
    </func>
    <func>
      <name>next(Ref) -> </name>
      <name>next(Ref, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
      <fsummary>Returns the next row of the result set relative the current cursor position and positions the cursor at this row. </fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>TimeOut = time_out()</v>
        <v>ColNames = col_names() </v>
        <v>Rows = rows()</v>
        <v>Reason = result_set_does_not_exist | process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>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 <c>{selected, ColNames,[]}</c> e.i. the list of row
          values is empty indicating that there is no more data to fetch.
                    <marker id="param_query"></marker>
</p>
      </desc>
    </func>
    <func>
      <name>param_query(Ref, SQLQuery, Params) -> </name>
      <name>param_query(Ref, SQLQuery, Params, TimeOut) -> ResultTuple | {error, Reason} </name>
      <fsummary>Executes a parameterized SQL query.</fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>SQLQuery = string() - a SQL query with parameter markers/place holders in form of question marks.</v>
        <v>Params = [{odbc_data_type(), [value()]}] |[{odbc_data_type(), in_or_out(), [value()]}]  </v>
	<v>in_or_out = in | out | inout</v>
	<d>Defines IN, OUT, and IN OUT Parameter Modes for stored procedures.</d>
	<v>TimeOut = time_out()</v>
        <v>Values = term() - Must be consistent with the Erlang data type that corresponds to the ODBC data type ODBCDataType</v>
      </type>
      <desc>
        <p>Executes a parameterized SQL query. For an
          example see the <seealso marker="getting_started#param_query">"Using the Erlang API"</seealso> in the Erlang ODBC 
          User's Guide.</p>
        <note>
          <p>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 <seealso marker="databases#type">mapping</seealso> in the User's Guide.</p>
        </note>
      </desc>
    </func>
    <func>
      <name>prev(Ref) -> </name>
      <name>prev(ConnectionReference, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
      <fsummary>Returns the previous row of the result set relative the current cursor position and positions the cursor at this row. </fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>TimeOut = time_out()</v>
        <v>ColNames = col_names() </v>
        <v>Rows = rows()</v>
        <v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>Returns the previous row of the result set relative the
          current cursor position and positions the
          cursor at this row.</p>
      </desc>
    </func>

    <func>
      <name>start() -> </name>
      <name>start(Type) -> ok | {error, Reason}</name>
      <fsummary>Starts the odb application. </fsummary>
      
      <type>
	<v>Type =  permanent | transient | temporary
	</v>
      </type>
      
      <desc>
	<p> Starts the odbc application. Default type
	  is temporary.
	  <seealso marker="kernel:application">See application(3)</seealso>
	</p>
      </desc>
    </func>

    <func>
      <name>stop() -> ok </name>
      <fsummary> Stops the odbc application.</fsummary>
      
      <desc>
	<p> Stops the odbc application.
	  <seealso marker="kernel:application">See application(3)</seealso>
	</p>
      </desc>
    </func>
    
    <func>
      <name>sql_query(Ref, SQLQuery) -> </name>
      <name>sql_query(Ref, SQLQuery, TimeOut) -> ResultTuple | [ResultTuple] |{error, Reason}</name>
      <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>
      <type>
        <v>Ref = connection_reference()</v>
        <v>SQLQuery = string() - The string may be composed by several SQL-queries separated by a ";", this is called a batch. </v>
        <v>TimeOut = time_out()</v>
        <v>ResultTuple = result_tuple() </v>
        <v>Reason = process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>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.</p>
        <note>
          <p>Some drivers may not have the information of the number
            of affected rows available and then the return value may
            be <c>{updated, undefined} </c>. </p>
          <p>The list of column names is ordered in the same way as the
            list of values of a row, e.g. the first <c>ColName</c> is
            associated with the first <c>Value</c> in a <c>Row</c>.</p>
        </note>
        <br></br>
      </desc>
    </func>
    <func>
      <name>select_count(Ref, SelectQuery) -> </name>
      <name>select_count(Ref, SelectQuery, TimeOut) -> {ok, NrRows} | {error, Reason} </name>
      <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>
      <type>
        <v>Ref = connection_reference()</v>
        <v>SelectQuery = string()</v>
        <d>SQL SELECT query.</d>
        <v>TimeOut = time_out()</v>
        <v>NrRows = n_rows()</v>
        <v>Reason = process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>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. </p>
        <note>
          <p>Some drivers may not have the information of the number of
            rows in the result set, then <c>NrRows</c> will have the value
            <c>undefined</c>. </p>
        </note>
      </desc>
    </func>
    <func>
      <name>select(Ref, Position, N) -></name>
      <name>select(Ref, Position, N, TimeOut) -> {selected, ColNames, Rows} | {error, Reason} </name>
      <fsummary>Selects <c>N</c>consecutive rows of the result set.</fsummary>
      <type>
        <v>Ref = connection_reference()</v>
        <v>Position = next | {relative, Pos} | {absolute, Pos} </v>
        <d>Selection strategy, determines at which row in the result set to start the selection.</d>
        <v>Pos = integer() </v>
        <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>
        <v>N = integer() </v>
        <v>TimeOut = time_out()</v>
        <v>Reason = result_set_does_not_exist | driver_does_not_support_function | scrollable_cursors_disabled | process_not_owner_of_odbc_connection | common_reason() </v>
      </type>
      <desc>
        <p>Selects <c>N</c> consecutive rows of the result set. If
          <c>Position</c> is <c>next</c> it is semantically equivalent
          of calling <c>next/[1,2]</c> <c>N</c> times. If
          <c>Position</c> is <c>{relative, Pos}</c>, <c>Pos</c> will be
          used as an offset from the current cursor position to
          determine the first selected row. If <c>Position</c> is
          <c>{absolute, Pos}</c>, <c>Pos</c> 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 <c>N</c> rows left of the result set the length of
          <c>Rows</c> will be less than <c>N</c>. If the first row to
          select happens to be beyond the last row of the result set,
          the returned value will be <c>{selected, ColNames,[]}</c>
          e.i. the list of row values is empty indicating that there
          is no more data to fetch.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>REFERENCES</title>
    <p>[1]: Microsoft ODBC 3.0, Programmer's Reference and SDK Guide      <br></br>

      See also http://msdn.microsoft.com/</p>
  </section>
  
</erlref>