aboutsummaryrefslogtreecommitdiffstats
path: root/lib/odbc/doc/src/databases.xml
blob: 614c327a46e560e2eb425add8db2d1306872a93f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE chapter SYSTEM "chapter.dtd">

<chapter>
  <header>
    <copyright>
      <year>2002</year><year>2016</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    </legalnotice>

    <title>Databases</title>
    <prepared>Ingela Anderton</prepared>
    <responsible></responsible>
    <docno></docno>
    <approved></approved>
    <checked></checked>
    <date></date>
    <rev></rev>
    <file>databases.xml</file>
  </header>

  <section>
    <title>Databases</title>
    <p>If you need to access a relational database such as
      <c>sqlserver</c>, <c>mysql</c>, <c>postgres</c>, <c>oracle</c>,
      <c>cybase</c> etc. from your erlang application using the Erlang
      ODBC interface is a good way to go about it.</p>
    <p></p>
    <p>The Erlang ODBC application should work for any relational
      database that has an ODBC driver. But currently it is only
      regularly tested for <c>sqlserver</c> and <c>postgres</c>.</p>
  </section>

  <section>
    <title>Database independence </title>
    <p>The Erlang ODBC interface is in principal database
      independent, e.i. an erlang program using the interface could be
      run without changes towards different databases. But as SQL is
      used it is alas possible to write database dependent
      programs. Even though SQL is an ANSI-standard meant to be
      database independent, different databases have proprietary
      extensions to SQL defining their own data types. If you keep to
      the ANSI data types you will minimize the problem. But
      unfortunately there is no guarantee that all databases actually
      treats the ANSI data types equivalently. For instance an
      installation of <c>Oracle Enterprise release 8.0.5.0.0 for unix</c> will accept that you create a table column with the
      ANSI data type <c>integer</c>, but when retrieving values from
      this column the driver reports that it is of type
      <c>SQL_DECIMAL(0, 38)</c> and not <c>SQL_INTEGER</c> as you may have
      expected. </p>
    <p>Another obstacle is that some drivers do not support scrollable
      cursors which has the effect that the only way to traverse the
      result set is sequentially, with next, from the first row to the
      last, and once you pass a row you cannot go back. This means
      that some functions in the interface will not work together with
      certain drivers. A similar problem is that not all drivers
      support "row count" for select queries, hence resulting in that
      the function <c>select_count/[3,4]</c> will return <c>{ok, undefined}</c> instead of <c>{ok, NrRows}</c> where
      <c>NrRows</c> is the number of rows in the result set.</p>
  </section>

  <section>
    <title>Data types </title>
    <p>The following is a list of the ANSI data types. For details
      turn to the ANSI standard documentation. Usage of other data types
      is of course possible, but you should be aware that this makes your
      application dependent on the database you are using at the moment.</p>
    <list type="bulleted">
      <item>CHARACTER (size), CHAR (size)</item>
      <item>NUMERIC (precision, scale), DECIMAL (precision, scale), DEC
       (precision, scale ) precision - total number of digits, scale
       - total number of decimal places</item>
      <item>INTEGER, INT, SMALLINT</item>
      <item>FLOAT (precision)</item>
      <item>REAL</item>
      <item>DOUBLE PRECISION</item>
      <item>CHARACTER VARYING(size), CHAR VARYING(size)</item>
    </list>
    <p>When inputting data using sql_query/[2,3] the values will
      always be in string format as they are part of an SQL-query.
      Example:</p>
    <code type="none">
      odbc:sql_query(Ref, "INSERT INTO TEST VALUES(1, 2, 3)").
    </code>
    <note>
      <p>Note that when the value of the data to input is a string, it
        has to be quoted with <c>'</c>. Example: </p>
      <code type="none">
odbc:sql_query(Ref, "INSERT INTO EMPLOYEE VALUES(1, 'Jane', 'Doe', 'F')").
      </code>
    </note>
    <p>You may also input data using <seealso marker="odbc#param_query">param_query/[3,4]</seealso> and then
      the input data will have the Erlang type corresponding to the
      ODBC type of the column.<seealso marker="#type">See ODBC to Erlang mapping</seealso></p>
    <p>      <marker id="type"></marker>
 When selecting data from a table, all data
      types are returned from the database to the ODBC driver as an
      ODBC data type. The tables below shows the mapping between those
      data types and what is returned by the Erlang API.</p>
    <table>
      <row>
        <cell align="left" valign="middle">ODBC Data Type </cell>
        <cell align="left" valign="middle">Erlang Data Type </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_CHAR(size)</cell>
        <cell align="left" valign="middle">String | Binary (configurable)</cell>
      </row>
      <row>
	<cell align="left" valign="middle">SQL_WCHAR(size) </cell>
	<cell align="left" valign="middle">Unicode binary encoded as UTF16 little endian.</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_NUMERIC(p,s)         <br></br>
when (p >= 0 and p &lt;= 9 and s == 0) </cell>
        <cell align="left" valign="middle">Integer </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_NUMERIC(p,s)         <br></br>
when (p >= 10 and p &lt;= 15 and s == 0) or (s &lt;= 15 and s > 0)</cell>
        <cell align="left" valign="middle">Float </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_NUMERIC(p,s)        <br></br>
when p >= 16 </cell>
        <cell align="left" valign="middle">String </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_DECIMAL(p,s)        <br></br>
when (p >= 0 and p &lt;= 9 and s == 0) </cell>
        <cell align="left" valign="middle">Integer </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_DECIMAL(p,s)         <br></br>
when (p >= 10 and p &lt;= 15 and s == 0) or (s &lt;= 15 and s > 0)</cell>
        <cell align="left" valign="middle">Float </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_DECIMAL(p,s)        <br></br>
when p >= 16 </cell>
        <cell align="left" valign="middle">String </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_INTEGER </cell>
        <cell align="left" valign="middle">Integer </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_SMALLINT </cell>
        <cell align="left" valign="middle">Integer </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_FLOAT </cell>
        <cell align="left" valign="middle">Float </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_REAL </cell>
        <cell align="left" valign="middle">Float </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_DOUBLE</cell>
        <cell align="left" valign="middle">Float</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_VARCHAR(size) </cell>
	<cell align="left" valign="middle">String | Binary (configurable)</cell>
      </row>
      <row>
	<cell align="left" valign="middle">SQL_WVARCHAR(size) </cell>
        <cell align="left" valign="middle">Unicode binary encoded as UTF16 little endian.</cell>
      </row>
      <tcaption>Mapping of ODBC data types to the Erlang data types returned to the Erlang application.</tcaption>
    </table>
    <table>
      <row>
        <cell align="left" valign="middle">ODBC Data Type </cell>
        <cell align="left" valign="middle">Erlang Data Type </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_TYPE_DATE </cell>
        <cell align="left" valign="middle">String </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_TYPE_TIME </cell>
        <cell align="left" valign="middle">String </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_TYPE_TIMESTAMP </cell>
        <cell align="left" valign="middle"> {{YY, MM, DD}, {HH, MM, SS}} </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_LONGVARCHAR </cell>
	<cell align="left" valign="middle">String | Binary (configurable)</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_WLONGVARCHAR(size) </cell>
        <cell align="left" valign="middle">Unicode binary encoded as UTF16 little endian.</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_BINARY</cell>
	<cell align="left" valign="middle">String | Binary (configurable)</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_VARBINARY</cell>
	<cell align="left" valign="middle">String | Binary (configurable)</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_LONGVARBINARY</cell>
	<cell align="left" valign="middle">String | Binary (configurable)</cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_TINYINT </cell>
        <cell align="left" valign="middle">Integer </cell>
      </row>
      <row>
        <cell align="left" valign="middle">SQL_BIT</cell>
        <cell align="left" valign="middle">Boolean </cell>
      </row>
      <tcaption>Mapping of extended ODBC data types to the Erlang data types returned to the Erlang application.</tcaption>
    </table>
    <note>
      <p>To find out which data types will be returned for the
        columns in a table use the function <seealso marker="odbc#describe_table">describe_table/[2,3]</seealso></p>
    </note>
  </section>

  <section>
    <title>Batch handling</title>
    <p>Grouping of SQL queries can be desirable in order to reduce
      network traffic. Another benefit can be that the data source
      sometimes can optimize execution of a batch of SQL queries.</p>
    <p>Explicit batches an procedures described below will result
      in multiple results being returned from sql_query/[2,3].
      while with parameterized queries only one result will be returned
      from param_query/[2,3].</p>

    <section>
      <title>Explicit batches</title>
      <p>The most basic form of a batch is created by semicolons
        separated SQL queries, for example:</p>
      <code type="none">
"SELECT * FROM FOO; SELECT * FROM BAR" or
"INSERT INTO FOO VALUES(1,'bar'); SELECT * FROM FOO"
      </code>
    </section>

    <section>
      <title>Procedures </title>
      <p>Different databases may also support creating of procedures
        that contains more than one SQL query. For example, the
        following SQLServer-specific statement creates a procedure that
        returns a result set containing information about employees
        that work at the department and a result set listing the
        customers of that department. </p>
      <code type="none">
 CREATE PROCEDURE DepartmentInfo (@DepartmentID INT) AS
 SELECT * FROM Employee WHERE department = @DepartmentID
 SELECT * FROM Customers WHERE department = @DepartmentID
      </code>
    </section>

    <section>
      <title>Parameterized queries</title>
      <p>To effectively perform a batch of similar queries, you can use
        parameterized queries. This means that you in your SQL query
        string will mark the places that usually would contain values
        with question marks and then provide lists of values for each
        parameter. For instance you can use this to insert multiple
        rows into the <c>EMPLOYEE</c> table while executing only a
        single SQL statement, for example code see <seealso marker="getting_started#param_query">"Using the Erlang API"</seealso> section in the "Getting Started" chapter.</p>
    </section>
  </section>
</chapter>