You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New features:
1. A new function on the connection object called connected()
which returns true if the connection is connected to a
server and false otherwise.
2. Passing an array of arrays rather than an array of values
into stmt.exec or conn.exec will execute a wide insert /
update / delete.
3. You can prepare multiple select statements at once and then
once the first result set is fetched, use stmt.getMoreResults()
to get the next result set.
Change-Id: Ie928c72c3983d7ad6a8bf0d3fd50d361c579b34e
This driver communicates with the native SQL Anywhere libraries, and thus requires
12
-
native compilation. Native compilation is managed by
13
-
[`node-gyp`](https://github.com/TooTallNate/node-gyp/). Please see that project
14
-
for additional prerequisites including Python 2.7, and C/C++ tool chain.
12
+
native compilation. Native compilation is managed by [`node-gyp`](https://github.com/TooTallNate/node-gyp/). Please see that project for additional prerequisites including Python 2.7, and C/C++ tool chain.
15
13
16
-
The official version hosted on NPM includes precompiled libraries for Windows
17
-
(64-bit).
14
+
The official version hosted on NPM includes precompiled libraries for Windows (64-bit).
18
15
19
16
As of version 1.0.6, the node-sqlanywhere driver supports node.js v0.10, 0.12, 4.x, and 5.x. As of version 1.0.9, it also supports node.js v6.x and v7.x.
A database connection object is created by calling `createConnection`. The
50
-
connection is established by calling the connection object's `connect` method,
51
-
and passing in an object representing connection parameters. The object can
52
-
contain most valid [connection properties](http://dcx.sybase.com/index.html#sa160/en/dbadmin/da-conparm.html).
44
+
## Establish a database connection
45
+
### Connecting
46
+
A database connection object is created by calling `createConnection`. The connection is established by calling the connection object's `connect` method, and passing in an object representing connection parameters. The object can contain most valid [connection properties](http://dcx.sybase.com/index.html#sa160/en/dbadmin/da-conparm.html).
53
47
54
-
#####Example: Connecting over TCP/IP
48
+
#####Example: Connecting over TCP/IP
55
49
```js
56
50
conn.connect({
57
51
Host :'localhost:2638'
@@ -60,7 +54,7 @@ conn.connect({
60
54
});
61
55
```
62
56
63
-
#####Example: Auto-starting a database on first connection
57
+
#####Example: Auto-starting a database on first connection
64
58
```js
65
59
conn.connect({
66
60
DatabaseFile:'demo.db',
@@ -70,21 +64,32 @@ conn.connect({
70
64
});
71
65
```
72
66
73
-
###Disconnecting
67
+
### Disconnecting
68
+
69
+
The `disconnect()` function closes the connection. As of version 1.0.16, you can also use `close()`.
74
70
75
71
```js
76
72
conn.disconnect(function(err) {
77
73
if (err) throw err;
78
74
console.log('Disconnected');
79
75
});
80
76
```
81
-
##Direct Statement Execution
82
-
Direct statement execution is the simplest way to execute SQL statements. The
83
-
inputs are the SQL command to be executed, and an optional array of positional
84
-
arguments. The result is returned using callbacks. The type of returned result
85
-
depends on the kind of statement.
86
77
87
-
####DDL Statement
78
+
### Determining whether you are connected
79
+
The connected() method was added in version 1.0.16.
80
+
```js
81
+
var conn =sqlanywhere.createConnection();
82
+
var connected =conn.connected(); // connected === false
Direct statement execution is the simplest way to execute SQL statements. The inputs are the SQL command to be executed, and an optional array of positional arguments. The result is returned using callbacks. The type of returned result depends on the kind of statement.
91
+
92
+
#### DDL Statement
88
93
89
94
In the case of a successful DDL Statement nothing is returned.
90
95
@@ -95,7 +100,7 @@ conn.exec('CREATE TABLE Test (id INTEGER PRIMARY KEY DEFAULT AUTOINCREMENT, msg
95
100
});
96
101
```
97
102
98
-
####DML Statement
103
+
####DML Statement
99
104
100
105
In the case of a DML Statement the number of `affectedRows` is returned.
101
106
@@ -107,10 +112,9 @@ conn.exec("INSERT INTO Test(msg) SELECT 'Hello,' || row_num FROM sa_rowgenerator
107
112
});
108
113
```
109
114
110
-
####Query
115
+
####Query
111
116
112
-
The `exec` function is a convenient way to completely retrieve the result of a
113
-
query. In this case all selected rows are fetched and returned in the callback.
117
+
The `exec` function is a convenient way to completely retrieve the result of a query. In this case all selected rows are fetched and returned in the callback.
114
118
115
119
```js
116
120
conn.exec("SELECT * FROM Test WHERE id < 5", function (err, rows) {
@@ -119,8 +123,7 @@ conn.exec("SELECT * FROM Test WHERE id < 5", function (err, rows) {
119
123
});
120
124
```
121
125
122
-
Values in the query can be substitued with JavaScript variables by using `?`
123
-
placeholders in the query, and passing an array of positional arguments.
126
+
Values in the query can be substitued with JavaScript variables by using `?` placeholders in the query, and passing an array of positional arguments.
124
127
125
128
```js
126
129
conn.exec("SELECT * FROM Test WHERE id BETWEEN ? AND ?", [5, 8], function (err, rows) {
@@ -129,8 +132,19 @@ conn.exec("SELECT * FROM Test WHERE id BETWEEN ? AND ?", [5, 8], function (err,
129
132
});
130
133
```
131
134
132
-
##Prepared Statement Execution
133
-
####Prepare a Statement
135
+
As of version 1.0.16, wide inserts, deletes, and updates are possible by passing in an array of arrays, one per row. For example, the following statement inserts three rows rather than just one:
136
+
137
+
```js
138
+
conn.exec("INSERT INTO Test VALUES ( ?, ? )", [ [1, 10], [2, 20], [3, 30] ], function (err, rows) {
139
+
if (err) throw err;
140
+
console.log('Rows:', rows); // should display 3
141
+
});
142
+
```
143
+
144
+
When using wide statements, each array must have the same number of elements and the type of the values must be the same in each row.
145
+
146
+
## Prepared Statement Execution
147
+
#### Prepare a Statement
134
148
The connection returns a `statement` object which can be executed multiple times.
135
149
```js
136
150
conn.prepare('SELECT * FROM Test WHERE id = ?', function (err, stmt){
@@ -139,28 +153,40 @@ conn.prepare('SELECT * FROM Test WHERE id = ?', function (err, stmt){
139
153
});
140
154
```
141
155
142
-
####Execute a Statement
143
-
The execution of a prepared statement is similar to the direct statement execution.
144
-
The first parameter of `exec` function is an array with positional parameters.
156
+
####Execute a Statement
157
+
The execution of a prepared statement is similar to the direct statement execution. The first parameter of `exec` function is an array with positional parameters. With version 1.0.16, wide statements (except SELECT) are supported here as well.
158
+
145
159
```js
146
160
stmt.exec([16], function(err, rows) {
147
161
if (err) throw err;
148
162
console.log("Rows: ", rows);
149
163
});
150
164
```
151
165
152
-
####Drop Statement
166
+
#### Fetching multiple result sets
167
+
As of version 1.0.16, you can prepare and execute a batch containing multiple select statements. To do this, you would prepare the multiple select statements and use `stmt.exec()` to fetch the first result set. To fetch the next result set, call `stmt.getMoreResults()`. getMoreResults takes an optional callback function (which takes the same arguments as `exec`), making it asynchronous. The `getMoreResults()` function returns `undefined` (or passes it to the callback function) after the last result set.
168
+
169
+
A simple synchronous example is below.
170
+
```js
171
+
stmt =conn.prepare( 'select 1 as a from dummy; select 2 as b, 3 as c from dummy' );
172
+
rs =stmt.exec();
173
+
// rs == [ { a: 1 } ]
174
+
rs =stmt.getMoreResults();
175
+
// rs = [ { b: 2, c: 3 } ]
176
+
stmt.drop();
177
+
```
178
+
179
+
#### Drop Statement
153
180
```js
154
181
stmt.drop(function(err) {
155
182
if (err) throw err;
156
183
});
157
184
```
158
185
159
-
##Transaction Handling
160
-
__Transactions are not automatically commited.__ Executing a statement implicitly
161
-
starts a new transaction that must be explicitly committed, or rolled back.
186
+
## Transaction Handling
187
+
__Transactions are not automatically commited.__ Executing a statement implicitly starts a new transaction that must be explicitly committed, or rolled back.
0 commit comments