├── LICENSE ├── README.md ├── binding.gyp ├── build.js ├── examples ├── app1.js └── readme.txt ├── lib └── index.js ├── package.json └── src ├── h ├── connection.h ├── errors.h ├── nodever_cover.h ├── sacapi.h ├── sacapidll.h ├── sqlany_utils.h └── stmt.h ├── sacapidll.cpp ├── sqlanywhere.cpp └── utils.cpp /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-sqlanywhere 2 | This is a Node.js driver written for [SAP SQL Anywhere](https://www.sap.com/products/sql-anywhere.html). 3 | 4 | [![NPM](https://nodei.co/npm/sqlanywhere.png?compact=true)](https://nodei.co/npm/sqlanywhere/) 5 | 6 | ## Install 7 | ``` 8 | npm install sqlanywhere 9 | ``` 10 | #### Prerequisites 11 | This driver communicates with the native SQL Anywhere libraries, and thus requires 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 and a C/C++ tool chain. 13 | 14 | The official version hosted on NPM includes precompiled libraries for Windows (64-bit). 15 | 16 | Versions supported: 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
Driver versionNode.js version
1.0.60.10, 0.12, 4.x, 5.x
1.0.96.x, 7.x
1.0.198.x
1.0.229.x
1.0.2310.x
1.0.24Only 5.x through 10.x (support for 0.10, 0.12, and 4.x is dropped)
1.0.255.x through 12.x
1.0.266.x through 12.x
1.0.276.x through 12.x
30 | 31 | ## Getting Started 32 | 33 | ```js 34 | var sqlanywhere = require('sqlanywhere'); 35 | 36 | var conn = sqlanywhere.createConnection(); 37 | 38 | var conn_params = { 39 | Server : 'demo16', 40 | UserId : 'DBA', 41 | Password: 'sql' 42 | }; 43 | 44 | 45 | conn.connect(conn_params, function(err) { 46 | if (err) throw err; 47 | conn.exec('SELECT Name, Description FROM Products WHERE id = ?', [301], function (err, result) { 48 | if (err) throw err; 49 | 50 | console.log('Name: ', result[0].Name, ', Description: ', result[0].Description); 51 | // output --> Name: Tee Shirt, Description: V-neck 52 | conn.disconnect(); 53 | }) 54 | }); 55 | ``` 56 | 57 | ## Establish a database connection 58 | ### Connecting 59 | 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). 60 | 61 | ##### Example: Connecting over TCP/IP 62 | ```js 63 | conn.connect({ 64 | Host : 'localhost:2638' 65 | UserId : 'DBA', 66 | Password: 'sql' 67 | }); 68 | ``` 69 | 70 | ##### Example: Auto-starting a database on first connection 71 | ```js 72 | conn.connect({ 73 | DatabaseFile: 'demo.db', 74 | AutoStart: 'YES', 75 | UserId: 'DBA', 76 | Password: 'sql', 77 | }); 78 | ``` 79 | 80 | ### Disconnecting 81 | 82 | The `disconnect()` function closes the connection. As of version 1.0.16, you can also use `close()`. 83 | 84 | ```js 85 | conn.disconnect(function(err) { 86 | if (err) throw err; 87 | console.log('Disconnected'); 88 | }); 89 | ``` 90 | 91 | ### Determining whether you are connected 92 | The connected() method was added in version 1.0.16. 93 | ```js 94 | var conn = sqlanywhere.createConnection(); 95 | var connected = conn.connected(); // connected === false 96 | conn.connect({ ... } ); 97 | connected = conn.connected(); // connected === true 98 | conn.disconnect(); 99 | connected = conn.connected(); // connected === false 100 | ``` 101 | 102 | ## Direct Statement Execution 103 | 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. 104 | 105 | #### DDL Statement 106 | 107 | In the case of a successful DDL Statement nothing is returned. 108 | 109 | ```js 110 | conn.exec('CREATE TABLE Test (id INTEGER PRIMARY KEY DEFAULT AUTOINCREMENT, msg LONG VARCHAR)', function (err, result) { 111 | if (err) throw err; 112 | console.log('Table Test created!'); 113 | }); 114 | ``` 115 | 116 | #### DML Statement 117 | 118 | In the case of a DML Statement the number of `affectedRows` is returned. 119 | 120 | ```js 121 | conn.exec("INSERT INTO Test(msg) SELECT 'Hello,' || row_num FROM sa_rowgenerator(1, 10)", function (err, affectedRows) { 122 | if (err) throw err; 123 | console.log('Number of affected rows:', affectedRows); 124 | conn.commit(); 125 | }); 126 | ``` 127 | 128 | #### Query 129 | 130 | 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. 131 | 132 | ```js 133 | conn.exec("SELECT * FROM Test WHERE id < 5", function (err, rows) { 134 | if (err) throw err; 135 | console.log('Rows:', rows); 136 | }); 137 | ``` 138 | 139 | Values in the query can be substitued with JavaScript variables by using `?` placeholders in the query, and passing an array of positional arguments. 140 | 141 | ```js 142 | conn.exec("SELECT * FROM Test WHERE id BETWEEN ? AND ?", [5, 8], function (err, rows) { 143 | if (err) throw err; 144 | console.log('Rows:', rows); 145 | }); 146 | ``` 147 | 148 | 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: 149 | 150 | ```js 151 | conn.exec("INSERT INTO Test VALUES ( ?, ? )", [ [1, 10], [2, 20], [3, 30] ], function (err, rows) { 152 | if (err) throw err; 153 | console.log('Rows:', rows); // should display 3 154 | }); 155 | ``` 156 | 157 | 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. 158 | 159 | ## Prepared Statement Execution 160 | #### Prepare a Statement 161 | The connection returns a `statement` object which can be executed multiple times. 162 | ```js 163 | conn.prepare('SELECT * FROM Test WHERE id = ?', function (err, stmt){ 164 | if (err) throw err; 165 | // do something with the statement 166 | }); 167 | ``` 168 | 169 | #### Execute a Statement 170 | 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. 171 | 172 | ```js 173 | stmt.exec([16], function(err, rows) { 174 | if (err) throw err; 175 | console.log("Rows: ", rows); 176 | }); 177 | ``` 178 | 179 | #### Fetching multiple result sets 180 | 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. 181 | 182 | A simple synchronous example is below. 183 | ```js 184 | stmt = conn.prepare( 'select 1 as a from dummy; select 2 as b, 3 as c from dummy' ); 185 | rs = stmt.exec(); 186 | // rs == [ { a: 1 } ] 187 | rs = stmt.getMoreResults(); 188 | // rs = [ { b: 2, c: 3 } ] 189 | stmt.drop(); 190 | ``` 191 | 192 | #### Drop Statement 193 | ```js 194 | stmt.drop(function(err) { 195 | if (err) throw err; 196 | }); 197 | ``` 198 | 199 | ## Transaction Handling 200 | __Transactions are not automatically commited.__ Executing a statement implicitly starts a new transaction that must be explicitly committed, or rolled back. 201 | 202 | #### Commit a Transaction 203 | 204 | ```js 205 | conn.commit(function(err) { 206 | if (err) throw err; 207 | console.log('Transaction commited.'); 208 | }); 209 | ``` 210 | 211 | #### Rollback a Transaction 212 | ```js 213 | conn.rollback(function(err) { 214 | if (err) throw err; 215 | console.log('Transaction rolled back.'); 216 | }); 217 | ``` 218 | 219 | ## Resources 220 | + [SAP SQL Anywhere Documentation](http://dcx.sap.com/) 221 | + [SAP SQL Anywhere Developer Q&A Forum](http://sqlanywhere-forum.sap.com/) 222 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "sqlanywhere", 5 | "defines": [ '_SACAPI_VERSION=5', 'DRIVER_NAME=sqlanywhere' ], 6 | "sources": [ "src/sqlanywhere.cpp", 7 | "src/utils.cpp", 8 | "src/sacapidll.cpp", ], 9 | 10 | "include_dirs": [ 11 | "src/h", 12 | "=0.10" 12 | }, 13 | "scripts": { 14 | "install": "node build.js" 15 | }, 16 | "dependencies": { 17 | "help": "^3.0.2", 18 | "nan": "2.14.2" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/h/connection.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | using namespace v8; 5 | using namespace node; 6 | 7 | #include "nodever_cover.h" 8 | 9 | /** Represents the connection to the database. 10 | * @class Connection 11 | * 12 | * The following example uses synchronous calls to create a new connection to 13 | * the database server, issue a SQL query against the server, display the 14 | * result set, and then disconnect from the server. 15 | * 16 | *

 17 |  * var sqlanywhere = require( 'sqlanywhere' );
 18 |  * var client = sqlanywhere.createConnection();
 19 |  * client.connect( { ServerName: 'demo17', UserID: 'DBA', Password: 'sql' } )
 20 |  * console.log('Connected');
 21 |  * result = client.exec("SELECT * FROM Customers");
 22 |  * console.log( result );
 23 |  * client.disconnect()
 24 |  * console.log('Disconnected');
 25 |  * 

26 | * 27 | * The following example does essentially the same thing using callbacks 28 | * to perform asynchronous calls. 29 | * Error checking is included. 30 | * 31 | *

 32 |  * var sqlanywhere = require( 'sqlanywhere' );
 33 |  * var client = sqlanywhere.createConnection();
 34 |  * client.connect( "ServerName=demo17;UID=DBA;PWD=sql",
 35 |  *     function( err )
 36 |  *     {
 37 |  *         if( err )
 38 |  *         {
 39 |  *             console.error( "Connect error: ", err );
 40 |  *         }
 41 |  *         else
 42 |  *         {
 43 |  *             console.log( "Connected" )
 44 |  *
 45 |  *             client.exec( "SELECT * FROM Customers",
 46 |  *                 function( err, rows )
 47 |  *                 {
 48 |  *                     if( err )
 49 |  *                     {
 50 |  *                         console.error( "Error: ", err );
 51 |  *                     }
 52 |  *                     else
 53 |  *                     {
 54 |  *                         console.log(rows)
 55 |  *                     }
 56 |  *                 }
 57 |  *             );
 58 |  *
 59 |  *             client.disconnect(
 60 |  *                 function( err )
 61 |  *                 {
 62 |  *                     if( err )
 63 |  *                     {
 64 |  *                         console.error( "Disconnect error: ", err );
 65 |  *                     }
 66 |  *                     else
 67 |  *                     {
 68 |  *                         console.log( "Disconnected" )
 69 |  *                     }
 70 |  *                 }
 71 |  *             );
 72 |  *         }
 73 |  *     }
 74 |  * );
 75 |  * 

76 | * 77 | * The following example also uses callbacks but the functions 78 | * are not inlined and the code is easier to understand. 79 | *

 80 |  * var sqlanywhere = require( 'sqlanywhere' );
 81 |  * var client = sqlanywhere.createConnection();
 82 |  * client.connect( "ServerName=demo17;UID=DBA;PWD=sql", async_connect );
 83 |  *
 84 |  * function async_connect( err )
 85 |  * {
 86 |  *     if( err )
 87 |  *     {
 88 |  * 	console.error( "Connect error: ", err );
 89 |  *     }
 90 |  *     else
 91 |  *     {
 92 |  * 	console.log( "Connected" )
 93 |  *
 94 |  * 	client.exec( "SELECT * FROM Customers", async_results );
 95 |  * 	
 96 |  * 	client.disconnect( async_disco );
 97 |  *     }
 98 |  * }
 99 |  *
100 |  * function async_results( err, rows )
101 |  * {
102 |  *     if( err )
103 |  *     {
104 |  * 	console.error( "Error: ", err );
105 |  *     }
106 |  *     else
107 |  *     {
108 |  * 	console.log(rows)
109 |  *     }
110 |  * }
111 |  *
112 |  * function async_disco( err )
113 |  * {
114 |  *     if( err )
115 |  *     {
116 |  * 	console.error( "Disconnect error: ", err );
117 |  *     }
118 |  *     else
119 |  *     {
120 |  * 	console.log( "Disconnected" )
121 |  *     }
122 |  * }
123 |  * 

124 | * 125 | * You can also pass connection parameters into the createConnection function, 126 | * and those parameters are combined with those in the connect() function call 127 | * to get the connection string used for the connection. You can use a hash of 128 | * connection parameters or a connection string fragment in either call. 129 | *

130 |  * var sqlanywhere = require( 'sqlanywhere' );
131 |  * var client = sqlanywhere.createConnection( { uid: 'dba'; pwd: 'sql' } );
132 |  * client.connect( 'server=MyServer;host=localhost' );
133 |  * // the connection string that will be used is 
134 |  * // "uid=dba;pwd=sql;server=MyServer;host=localhost"
135 |  * 

136 | */ 137 | class Connection : public ObjectWrap 138 | { 139 | public: 140 | /// @internal 141 | #if v010 142 | static void Init(); 143 | #else 144 | static void Init( Isolate * ); 145 | #endif 146 | 147 | /// @internal 148 | static NODE_API_FUNC( NewInstance ); 149 | 150 | private: 151 | /// @internal 152 | #if v010 153 | Connection( const Arguments &args ); 154 | #else 155 | Connection( const FunctionCallbackInfo &args ); 156 | #endif 157 | /// @internal 158 | ~Connection(); 159 | 160 | /// @internal 161 | static Persistent constructor; 162 | 163 | /// @internal 164 | static void noParamAfter( uv_work_t *req ); 165 | /// @internal 166 | static void connectAfter( uv_work_t *req ); 167 | /// @internal 168 | static void connectWork( uv_work_t *req ); 169 | /// @internal 170 | static NODE_API_FUNC( New ); 171 | 172 | /// Connect using an existing connection. 173 | // 174 | // This method connects to the database using an existing connection. 175 | // The DBCAPI connection handle obtained from the JavaScript External 176 | // environment needs to be passed in as a parameter. The disconnect 177 | // method should be called before the end of the program to free 178 | // up resources. 179 | // 180 | // @fn Connection::connect( Number DBCAPI_Handle, Function callback ) 181 | // 182 | // @param DBCAPI_Handle The connection Handle ( type: Number ) 183 | // @param callback The optional callback function. ( type: Function ) 184 | // 185 | //

186 |     // // In a method written for the JavaScript External Environment
187 |     // var sqlanywhere = require( 'sqlanywhere' );
188 |     // var client = sqlanywhere.createConnection;
189 |     // client.connect( sa_dbcapi_handle, callback );
190 |     // 

191 | // 192 | // This method can be either synchronous or asynchronous depending on 193 | // whether or not a callback function is specified. 194 | // The callback function is of the form: 195 | // 196 | //

197 |     // function( err )
198 |     // {
199 |     //
200 |     // };
201 |     // 

202 | // 203 | // @internal 204 | /// 205 | 206 | /** Creates a new connection. 207 | * 208 | * This method creates a new connection using either a connection string 209 | * or a hash of connection parameters passed in as a parameter. Before 210 | * the end of the program, the connection should be disconnected using 211 | * the disconnect method to free up resources. 212 | * 213 | * The CharSet (CS) connection parameter CS=UTF-8 is always appended 214 | * to the end of the connection string by the driver since it is 215 | * required that all strings are sent in that encoding. 216 | * 217 | * This method can be either synchronous or asynchronous depending on 218 | * whether or not a callback function is specified. 219 | * The callback function is of the form: 220 | * 221 | *

222 |      * function( err )
223 |      * {
224 |      *
225 |      * };
226 |      * 

227 | * 228 | * The following synchronous example shows how to use the connect method. 229 | * It is not necessary to specify the CHARSET=UTF-8 connection parameter 230 | * since it is always added automatically. 231 | * 232 | *

233 |      * var sqlanywhere = require( 'sqlanywhere' );
234 |      * var client = sqlanywhere.createConnection();
235 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql;CHARSET=UTF-8" );
236 |      * 

237 | * 238 | * @fn Connection::connect( String conn_string, Function callback ) 239 | * 240 | * @param conn_string A valid connection string ( type: String ) 241 | * @param callback The optional callback function. ( type: Function ) 242 | * 243 | * @see Connection::disconnect 244 | */ 245 | static NODE_API_FUNC( connect ); 246 | 247 | /** Closes the current connection. 248 | * 249 | * This method closes the current connection and should be 250 | * called before the program ends to free up resources. 251 | * 252 | * This method can be either synchronous or asynchronous depending on 253 | * whether or not a callback function is specified. 254 | * The callback function is of the form: 255 | * 256 | *

257 |      * function( err )
258 |      * {
259 |      *
260 |      * };
261 |      * 

262 | * 263 | * The following synchronous example shows how to use the disconnect method. 264 | * 265 | *

266 |      * var sqlanywhere = require( 'sqlanywhere' );
267 |      * var client = sqlanywhere.createConnection();
268 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" );
269 |      * client.disconnect()
270 |      * 

271 | * 272 | * @fn Connection::disconnect( Function callback ) 273 | * 274 | * @param callback The optional callback function. ( type: Function ) 275 | * 276 | * @see Connection::connect 277 | */ 278 | static NODE_API_FUNC( disconnect ); 279 | 280 | /// @internal 281 | static void disconnectWork( uv_work_t *req ); 282 | 283 | /** Executes the specified SQL statement. 284 | * 285 | * This method takes in a SQL statement and an optional array of bind 286 | * parameters to execute. 287 | * 288 | * This method can be either synchronous or asynchronous depending on 289 | * whether or not a callback function is specified. 290 | * The callback function is of the form: 291 | * 292 | *

293 |      * function( err, result )
294 |      * {
295 |      *
296 |      * };
297 |      * 

298 | * 299 | * For queries producing result sets, the result set object is returned 300 | * as the second parameter of the callback. 301 | * For insert, update and delete statements, the number of rows affected 302 | * is returned as the second parameter of the callback. 303 | * For other statements, result is undefined. 304 | * 305 | * The following synchronous example shows how to use the exec method. 306 | * 307 | *

308 |      * var sqlanywhere = require( 'sqlanywhere' );
309 |      * var client = sqlanywhere.createConnection();
310 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" );
311 |      * result = client.exec("SELECT * FROM Customers");
312 |      * console.log( result );
313 |      * client.disconnect()
314 |      * 

315 | * 316 | * The following synchronous example shows how to specify bind parameters. 317 | * 318 | *

319 |      * var sqlanywhere = require( 'sqlanywhere' );
320 |      * var client = sqlanywhere.createConnection();
321 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" );
322 |      * result = client.exec(
323 |      *     "SELECT * FROM Customers WHERE ID >=? AND ID 

328 | * 329 | * @fn Result Connection::exec( String sql, Array params, Function callback ) 330 | * 331 | * @param sql The SQL statement to be executed. ( type: String ) 332 | * @param params Optional array of bind parameters. ( type: Array ) 333 | * @param callback The optional callback function. ( type: Function ) 334 | * 335 | * @return If no callback is specified, the result is returned. 336 | * 337 | */ 338 | static NODE_API_FUNC( exec ); 339 | 340 | /** Prepares the specified SQL statement. 341 | * 342 | * This method prepares a SQL statement and returns a Statement object 343 | * if successful. 344 | * 345 | * This method can be either synchronous or asynchronous depending on 346 | * whether or not a callback function is specified. 347 | * The callback function is of the form: 348 | * 349 | *

350 |      * function( err, Statement )
351 |      * {
352 |      *
353 |      * };
354 |      * 

355 | * 356 | * The following synchronous example shows how to use the prepare method. 357 | * 358 | *

359 |      * var sqlanywhere = require( 'sqlanywhere' );
360 |      * var client = sqlanywhere.createConnection();
361 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
362 |      * stmt = client.prepare( "SELECT * FROM Customers WHERE ID >= ? AND ID < ?" );
363 |      * result = stmt.exec( [200, 300] );
364 |      * console.log( result );
365 |      * client.disconnect();
366 |      * 

367 | * 368 | * @fn Statement Connection::prepare( String sql, Function callback ) 369 | * 370 | * @param sql The SQL statement to be executed. ( type: String ) 371 | * @param callback The optional callback function. ( type: Function ) 372 | * 373 | * @return If no callback is specified, a Statement object is returned. 374 | * 375 | */ 376 | static NODE_API_FUNC( prepare ); 377 | 378 | /// @internal 379 | static void prepareAfter( uv_work_t *req ); 380 | /// @internal 381 | static void prepareWork( uv_work_t *req ); 382 | 383 | /** Performs a commit on the connection. 384 | * 385 | * This method performs a commit on the connection. 386 | * By default, inserts, updates, and deletes are not committed 387 | * upon disconnection from the database server. 388 | * 389 | * This method can be either synchronous or asynchronous depending on 390 | * whether or not a callback function is specified. 391 | * The callback function is of the form: 392 | * 393 | *

394 |      * function( err ) {
395 |      *
396 |      * };
397 |      * 

398 | * 399 | * The following synchronous example shows how to use the commit method. 400 | * 401 | *

402 |      * var sqlanywhere = require( 'sqlanywhere' );
403 |      * var client = sqlanywhere.createConnection();
404 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
405 |      * stmt = client.prepare(
406 |      *     "INSERT INTO Departments "
407 |      *     + "( DepartmentID, DepartmentName, DepartmentHeadID )"
408 |      *     + "VALUES (?,?,?)" );
409 |      * result = stmt.exec( [600, 'Eastern Sales', 902] );
410 |      * result += stmt.exec( [700, 'Western Sales', 902] );
411 |      * stmt.drop();
412 |      * console.log( "Number of rows added: " + result );
413 |      * result = client.exec( "SELECT * FROM Departments" );
414 |      * console.log( result );
415 |      * client.commit();
416 |      * client.disconnect();	
417 |      * 

418 | * 419 | * @fn Connection::commit( Function callback ) 420 | * 421 | * @param callback The optional callback function. ( type: Function ) 422 | * 423 | */ 424 | static NODE_API_FUNC( commit ); 425 | 426 | /// @internal 427 | static void commitWork( uv_work_t *req ); 428 | 429 | /** Performs a rollback on the connection. 430 | * 431 | * This method performs a rollback on the connection. 432 | * 433 | * This method can be either synchronous or asynchronous depending on 434 | * whether or not a callback function is specified. 435 | * The callback function is of the form: 436 | * 437 | *

438 |      * function( err ) {
439 |      *
440 |      * };
441 |      * 

442 | * 443 | * The following synchronous example shows how to use the rollback method. 444 | * 445 | *

446 |      * var sqlanywhere = require( 'sqlanywhere' );
447 |      * var client = sqlanywhere.createConnection();
448 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
449 |      * stmt = client.prepare(
450 |      *     "INSERT INTO Departments "
451 |      *     + "( DepartmentID, DepartmentName, DepartmentHeadID )"
452 |      *     + "VALUES (?,?,?)" );
453 |      * result = stmt.exec( [600, 'Eastern Sales', 902] );
454 |      * result += stmt.exec( [700, 'Western Sales', 902] );
455 |      * stmt.drop();
456 |      * console.log( "Number of rows added: " + result );
457 |      * result = client.exec( "SELECT * FROM Departments" );
458 |      * console.log( result );
459 |      * client.rollback();
460 |      * client.disconnect();	
461 |      * 

462 | * 463 | * @fn Connection::rollback( Function callback ) 464 | * 465 | * @param callback The optional callback function. ( type: Function ) 466 | * 467 | */ 468 | static NODE_API_FUNC( rollback ); 469 | 470 | /// @internal 471 | static void rollbackWork( uv_work_t *req ); 472 | 473 | /** Indicates whether the connection is connected. 474 | * 475 | * This synchronous method returns true if the connection is connected and 476 | * false otherwise. 477 | * 478 | * The following example shows how to use this method. 479 | * 480 | *

481 |      * var sqlanywhere = require( 'sqlanywhere' );
482 |      * var client = sqlanywhere.createConnection();
483 |      * var connected = client.connected(); // false
484 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
485 |      * connected = client.connected(); // true
486 |      * client.disconnect();
487 |      * connected = client.connected(); // false
488 |      * 

489 | * 490 | * @fn Connection::connected() 491 | * 492 | * @return true if the connection is connected, false if not. 493 | * 494 | */ 495 | static NODE_API_FUNC( connected ); 496 | 497 | public: 498 | /// @internal 499 | a_sqlany_connection *conn; 500 | /// @internal 501 | unsigned int max_api_ver; 502 | /// @internal 503 | bool sqlca_connection; 504 | /// @internal 505 | uv_mutex_t conn_mutex; 506 | /// @internal 507 | Persistent _arg; 508 | /// @internal 509 | std::vector statements; 510 | 511 | /// @internal 512 | void removeStmt( class StmtObject *stmt ); 513 | /// @internal 514 | void cleanupStmts( void ); 515 | }; 516 | -------------------------------------------------------------------------------- /src/h/errors.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | #define JS_ERR_INVALID_OBJECT -2001 5 | #define JS_ERR_INVALID_ARGUMENTS -2002 6 | #define JS_ERR_CONNECTION_ALREADY_EXISTS -2003 7 | #define JS_ERR_INITIALIZING_DBCAPI -2004 8 | #define JS_ERR_NOT_CONNECTED -2005 9 | #define JS_ERR_BINDING_PARAMETERS -2006 10 | #define JS_ERR_GENERAL_ERROR -2007 11 | #define JS_ERR_RESULTSET -2008 12 | #define JS_ERR_NO_WIDE_STATEMENTS -2009 13 | -------------------------------------------------------------------------------- /src/h/nodever_cover.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | #include 5 | 6 | #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION == 10 7 | #define v010 1 8 | #define v012 0 9 | #elif NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION == 12 10 | #define v010 0 11 | #define v012 1 12 | #else 13 | #define v010 0 14 | #define v012 0 15 | #endif 16 | 17 | #if v010 18 | #define NODE_API_FUNC( name ) Handle name ( const Arguments &args ) 19 | #else 20 | #define NODE_API_FUNC( name ) void name ( const FunctionCallbackInfo &args ) 21 | #endif 22 | -------------------------------------------------------------------------------- /src/h/sacapi.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // While not a requirement of the license, if you do modify this file, we 19 | // would appreciate hearing about it. Please email 20 | // sqlany_interfaces@sap.com 21 | // 22 | // *************************************************************************** 23 | 24 | #ifndef SACAPI_H 25 | #define SACAPI_H 26 | 27 | /** \mainpage SQL Anywhere C API 28 | * 29 | * \section intro_sec Introduction 30 | * The SQL Anywhere C application programming interface (API) is a data 31 | * access API for the C / C++ languages. The C API specification defines 32 | * a set of functions, variables and conventions that provide a consistent 33 | * database interface independent of the actual database being used. Using 34 | * the SQL Anywhere C API, your C / C++ applications have direct access to 35 | * SQL Anywhere database servers. 36 | * 37 | * The SQL Anywhere C API simplifies the creation of C and C++ wrapper 38 | * drivers for several interpreted programming languages including PHP, 39 | * Perl, Python, and Ruby. The SQL Anywhere C API is layered on top of the 40 | * DBLIB package and it was implemented with Embedded SQL. 41 | * 42 | * Although it is not a replacement for DBLIB, the SQL Anywhere C API 43 | * simplifies the creation of applications using C and C++. You do not need 44 | * an advanced knowledge of embedded SQL to use the SQL Anywhere C API. 45 | * 46 | * \section distribution Distribution of the API 47 | * The API is built as a dynamic link library (DLL) (\b dbcapi.dll) on 48 | * Microsoft Windows systems and as a shared object (\b libdbcapi.so) on 49 | * Unix systems. The DLL is statically linked to the DBLIB package of the 50 | * SQL Anywhere version on which it is built. When the dbcapi.dll file is 51 | * loaded, the corresponding dblibX.dll file is loaded by the operating 52 | * system. Applications using dbcapi.dll can either link directly to it 53 | * or load it dynamically. For more information about dynamic loading, see 54 | * the section "Dynamically Loading the DLL". 55 | * 56 | * Descriptions of the SQL Anywhere C API data types and entry points are 57 | * provided in the main header file (\b sacapi.h). 58 | * 59 | * \section dynamic_loading Dynamically Loading the DLL 60 | * The code to dynamically load the DLL is contained in the sacapidll.c 61 | * source file. Applications must use the sacapidll.h header file and 62 | * include the source code in sacapidll.c. You can use the 63 | * sqlany_initialize_interface method to dynamically load the DLL and 64 | * look up the entry points. Examples are provided with the SQL Anywhere 65 | * installation. 66 | * 67 | * \section threading_support Threading Support 68 | * The C API library is thread-unaware, meaning that the library does not 69 | * perform any tasks that require mutual exclusion. In order to allow the 70 | * library to work in threaded applications, there is only one rule to 71 | * follow: no more than one request is allowed on a single connection . 72 | * With this rule, the application is responsible for doing mutual exclusion 73 | * when accessing any connection-specific resource. This includes 74 | * connection handles, prepared statements, and result set objects. 75 | * 76 | * \version 2.0 77 | */ 78 | 79 | /** \file sacapi.h 80 | * Main API header file. 81 | * This file describes all the data types and entry points of the API. 82 | */ 83 | 84 | /** Version 1 was the initial version of the C/C++ API. 85 | * 86 | * You must define _SACAPI_VERSION as 1 or higher for this functionality. 87 | */ 88 | #define SQLANY_API_VERSION_1 1 89 | 90 | /** Version 2 introduced the "_ex" functions and the ability to cancel requests. 91 | * 92 | * You must define _SACAPI_VERSION as 2 or higher for this functionality. 93 | */ 94 | #define SQLANY_API_VERSION_2 2 95 | 96 | /** Version 3 introduced the "callback" function. 97 | * 98 | * You must define _SACAPI_VERSION as 3 or higher for this functionality. 99 | */ 100 | #define SQLANY_API_VERSION_3 3 101 | 102 | /** Version 4 introduced NCHAR support and wide inserts. 103 | * 104 | * You must define _SACAPI_VERSION as 4 or higher for this functionality. 105 | */ 106 | #define SQLANY_API_VERSION_4 4 107 | 108 | /** Version 5 introduced a way to reset sent data through sqlany_send_param_data() 109 | * and the A_FLOAT data type 110 | * 111 | * You must define _SACAPI_VERSION as 5 or higher for this functionality. 112 | */ 113 | #define SQLANY_API_VERSION_5 5 114 | 115 | /** If the command line does not specify which version to build, 116 | * then build the latest version. 117 | */ 118 | #ifndef _SACAPI_VERSION 119 | #define _SACAPI_VERSION SQLANY_API_VERSION_5 120 | #endif 121 | 122 | /** Returns the minimal error buffer size. 123 | */ 124 | #define SACAPI_ERROR_SIZE 256 125 | 126 | #if defined(__cplusplus) 127 | extern "C" { 128 | #endif 129 | 130 | /** A handle to an interface context 131 | */ 132 | typedef struct a_sqlany_interface_context a_sqlany_interface_context; 133 | 134 | /** A handle to a connection object 135 | */ 136 | typedef struct a_sqlany_connection a_sqlany_connection; 137 | 138 | /** A handle to a statement object 139 | */ 140 | typedef struct a_sqlany_stmt a_sqlany_stmt; 141 | 142 | /** A portable 32-bit signed value */ 143 | typedef signed int sacapi_i32; 144 | /** A portable 32-bit unsigned value */ 145 | typedef unsigned int sacapi_u32; 146 | /** A portable boolean value */ 147 | typedef sacapi_i32 sacapi_bool; 148 | 149 | // TODO:Character set issues 150 | 151 | /** The run-time calling convention in use (Windows only). 152 | */ 153 | #ifdef _WIN32 154 | #define _sacapi_entry_ __stdcall 155 | #endif 156 | #ifndef _sacapi_entry_ 157 | #define _sacapi_entry_ 158 | #endif 159 | 160 | /** Callback function type 161 | */ 162 | #define SQLANY_CALLBACK _sacapi_entry_ 163 | 164 | /** Parameter type for sqlany_register_callback function used to specify the address of the callback routine. 165 | */ 166 | typedef int (SQLANY_CALLBACK *SQLANY_CALLBACK_PARM)(); 167 | 168 | /** Specifies the data type being passed in or retrieved. 169 | */ 170 | typedef enum a_sqlany_data_type 171 | { 172 | /// Invalid data type. 173 | A_INVALID_TYPE, 174 | /// Binary data. Binary data is treated as-is and no character set conversion is performed. 175 | A_BINARY, 176 | /// String data. The data where character set conversion is performed. 177 | A_STRING, 178 | /// Double data. Includes float values. 179 | A_DOUBLE, 180 | /// 64-bit integer. 181 | A_VAL64, 182 | /// 64-bit unsigned integer. 183 | A_UVAL64, 184 | /// 32-bit integer. 185 | A_VAL32, 186 | /// 32-bit unsigned integer. 187 | A_UVAL32, 188 | /// 16-bit integer. 189 | A_VAL16, 190 | /// 16-bit unsigned integer. 191 | A_UVAL16, 192 | /// 8-bit integer. 193 | A_VAL8, 194 | /// 8-bit unsigned integer. 195 | A_UVAL8 196 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_5 197 | , 198 | //// Float precision data. 199 | A_FLOAT 200 | #endif 201 | } a_sqlany_data_type; 202 | 203 | /** Returns a description of the attributes of a data value. 204 | * 205 | * To view examples of the a_sqlany_data_value structure in use, 206 | * see any of the following sample files in the sdk\\dbcapi\\examples directory 207 | * of your SQL Anywhere installation: 208 | * 209 | *
    210 | *
  • dbcapi_isql.cpp 211 | *
  • fetching_a_result_set.cpp 212 | *
  • send_retrieve_full_blob.cpp 213 | *
  • preparing_statements.cpp 214 | *
215 | */ 216 | typedef struct a_sqlany_data_value 217 | { 218 | /// A pointer to user supplied buffer of data. 219 | char * buffer; 220 | /// The size of the buffer. 221 | size_t buffer_size; 222 | /// A pointer to the number of valid bytes in the buffer. This value must be less than buffer_size. 223 | size_t * length; 224 | /// The type of the data 225 | a_sqlany_data_type type; 226 | /// A pointer to indicate whether the last fetched data is NULL. 227 | sacapi_bool * is_null; 228 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 229 | /// Indicates whether the buffer value is an pointer to the actual value. 230 | sacapi_bool is_address; 231 | #endif 232 | } a_sqlany_data_value; 233 | 234 | /** A data direction enumeration. 235 | */ 236 | typedef enum a_sqlany_data_direction 237 | { 238 | /// Invalid data direction. 239 | DD_INVALID = 0x0, 240 | /// Input-only host variables. 241 | DD_INPUT = 0x1, 242 | /// Output-only host variables. 243 | DD_OUTPUT = 0x2, 244 | /// Input and output host variables. 245 | DD_INPUT_OUTPUT = 0x3 246 | } a_sqlany_data_direction; 247 | 248 | /** A bind parameter structure used to bind parameter and prepared statements. 249 | * 250 | * To view examples of the a_sqlany_bind_param structure in use, 251 | * see any of the following sample files in the sdk\\dbcapi\\examples directory 252 | * of your SQL Anywhere installation: 253 | * 254 | *
    255 | *
  • preparing_statements.cpp 256 | *
  • send_retrieve_full_blob.cpp 257 | *
  • send_retrieve_part_blob.cpp 258 | *
259 | * \sa sqlany_execute() 260 | */ 261 | typedef struct a_sqlany_bind_param 262 | { 263 | /// The direction of the data. (input, output, input_output) 264 | a_sqlany_data_direction direction; 265 | /// The actual value of the data. 266 | a_sqlany_data_value value; 267 | /// Name of the bind parameter. This is only used by sqlany_describe_bind_param(). 268 | char *name; 269 | } a_sqlany_bind_param; 270 | 271 | /** An enumeration of the native types of values as described by the server. 272 | * 273 | * The value types correspond to the embedded SQL data types. 274 | * 275 | * \hideinitializers 276 | * \sa sqlany_get_column_info(), a_sqlany_column_info 277 | */ 278 | typedef enum a_sqlany_native_type 279 | { 280 | /// No data type. 281 | DT_NOTYPE = 0, 282 | /// Null-terminated character string that is a valid date. 283 | DT_DATE = 384, 284 | /// Null-terminated character string that is a valid time. 285 | DT_TIME = 388, 286 | /// Null-terminated character string that is a valid timestamp. 287 | DT_TIMESTAMP = 392, 288 | /// Varying length character string, in the CHAR character set, with a two-byte length field. The maximum length is 32765 bytes. When sending data, you must set the length field. When fetching data, the database server sets the length field. The data is not null-terminated or blank-padded. 289 | DT_VARCHAR = 448, 290 | /// Fixed-length blank-padded character string, in the CHAR character set. The maximum length, specified in bytes, is 32767. The data is not null-terminated. 291 | DT_FIXCHAR = 452, 292 | /// Long varying length character string, in the CHAR character set. 293 | DT_LONGVARCHAR = 456, 294 | /// Null-terminated character string, in the CHAR character set. The string is blank-padded if the database is initialized with blank-padded strings. 295 | DT_STRING = 460, 296 | /// 8-byte floating-point number. 297 | DT_DOUBLE = 480, 298 | /// 4-byte floating-point number. 299 | DT_FLOAT = 482, 300 | /// Packed decimal number (proprietary format). 301 | DT_DECIMAL = 484, 302 | /// 32-bit signed integer. 303 | DT_INT = 496, 304 | /// 16-bit signed integer. 305 | DT_SMALLINT = 500, 306 | /// Varying length binary data with a two-byte length field. The maximum length is 32765 bytes. When supplying information to the database server, you must set the length field. When fetching information from the database server, the server sets the length field. 307 | DT_BINARY = 524, 308 | /// Long binary data. 309 | DT_LONGBINARY = 528, 310 | /// 8-bit signed integer. 311 | DT_TINYINT = 604, 312 | /// 64-bit signed integer. 313 | DT_BIGINT = 608, 314 | /// 32-bit unsigned integer. 315 | DT_UNSINT = 612, 316 | /// 16-bit unsigned integer. 317 | DT_UNSSMALLINT = 616, 318 | /// 64-bit unsigned integer. 319 | DT_UNSBIGINT = 620, 320 | /// 8-bit signed integer. 321 | DT_BIT = 624, 322 | /// Null-terminated character string, in the NCHAR character set. The string is blank-padded if the database is initialized with blank-padded strings. 323 | DT_NSTRING = 628, 324 | /// Fixed-length blank-padded character string, in the NCHAR character set. The maximum length, specified in bytes, is 32767. The data is not null-terminated. 325 | DT_NFIXCHAR = 632, 326 | /// Varying length character string, in the NCHAR character set, with a two-byte length field. The maximum length is 32765 bytes. When sending data, you must set the length field. When fetching data, the database server sets the length field. The data is not null-terminated or blank-padded. 327 | DT_NVARCHAR = 636, 328 | /// Long varying length character string, in the NCHAR character set. 329 | DT_LONGNVARCHAR = 640 330 | } a_sqlany_native_type; 331 | 332 | /** Returns column metadata information. 333 | * 334 | * sqlany_get_column_info() can be used to populate this structure. 335 | * 336 | * To view an example of the a_sqlany_column_info structure in use, 337 | * see the following sample file in the sdk\\dbcapi\\examples 338 | * directory of your SQL Anywhere installation. 339 | * 340 | *
    341 | *
  • dbcapi_isql.cpp 342 | *
343 | */ 344 | typedef struct a_sqlany_column_info 345 | { 346 | /// The name of the column (null-terminated). 347 | /// The string can be referenced as long as the result set object is not freed. 348 | char * name; 349 | /// The column data type. 350 | a_sqlany_data_type type; 351 | /// The native type of the column in the database. 352 | a_sqlany_native_type native_type; 353 | /// The precision. 354 | unsigned short precision; 355 | /// The scale. 356 | unsigned short scale; 357 | /// The maximum size a data value in this column can take. 358 | size_t max_size; 359 | /// Indicates whether a value in the column can be null. 360 | sacapi_bool nullable; 361 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 362 | /// The name of the table (null-terminated). 363 | /// The string can be referenced as long as the result set object is not freed. 364 | char * table_name; 365 | /// The name of the owner (null-terminated). 366 | /// The string can be referenced as long as the result set object is not freed. 367 | char * owner_name; 368 | /// Indicates whether the column is bound to a user buffer. 369 | sacapi_bool is_bound; 370 | /// Information about the bound column. 371 | a_sqlany_data_value binding; 372 | #endif 373 | } a_sqlany_column_info; 374 | 375 | /** Gets information about the currently bound parameters. 376 | * 377 | * sqlany_get_bind_param_info() can be used to populate this structure. 378 | * 379 | * To view examples of the a_sqlany_bind_param_info structure in use, 380 | * see any of the following sample files in the sdk\\dbcapi\\examples 381 | * directory of your SQL Anywhere installation. 382 | * 383 | *
    384 | *
  • preparing_statements.cpp 385 | *
  • send_retrieve_full_blob.cpp 386 | *
  • send_retrieve_part_blob.cpp 387 | *
388 | * \sa sqlany_execute() 389 | */ 390 | typedef struct a_sqlany_bind_param_info 391 | { 392 | /// A pointer to the name of the parameter. 393 | char * name; 394 | /// The direction of the parameter. 395 | a_sqlany_data_direction direction; 396 | /// Information about the bound input value. 397 | a_sqlany_data_value input_value; 398 | /// Information about the bound output value. 399 | a_sqlany_data_value output_value; 400 | 401 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 402 | /// The native type of the column in the database. 403 | a_sqlany_native_type native_type; 404 | /// The precision. 405 | unsigned short precision; 406 | /// The scale. 407 | unsigned short scale; 408 | /// The maximum size a data value in this column can take. 409 | size_t max_size; 410 | #endif 411 | } a_sqlany_bind_param_info; 412 | 413 | /** Returns metadata information about a column value in a result set. 414 | * 415 | * sqlany_get_data_info() can be used 416 | * to populate this structure with information about what was last retrieved by a fetch operation. 417 | * 418 | * To view an example of the a_sqlany_data_info structure in use, 419 | * see the following sample file in the sdk\\dbcapi\\examples directory 420 | * of your SQL Anywhere installation: 421 | * 422 | *
    423 | *
  • send_retrieve_part_blob.cpp 424 | *
425 | * \sa sqlany_get_data_info() 426 | */ 427 | typedef struct a_sqlany_data_info 428 | { 429 | /// The type of the data in the column. 430 | a_sqlany_data_type type; 431 | /// Indicates whether the last fetched data is NULL. 432 | /// This field is only valid after a successful fetch operation. 433 | sacapi_bool is_null; 434 | /// The total number of bytes available to be fetched. 435 | /// This field is only valid after a successful fetch operation. 436 | size_t data_size; 437 | } a_sqlany_data_info; 438 | 439 | /** An enumeration of the callback types. 440 | * 441 | * The callback types correspond to the embedded SQL callback types. 442 | * 443 | * \hideinitializers 444 | * \sa sqlany_register_callback() 445 | */ 446 | typedef enum a_sqlany_callback_type { 447 | /// This function is called just before a database request is sent to the server. 448 | /// CALLBACK_START is used only on Windows operating systems. 449 | CALLBACK_START = 0, 450 | /// This function is called repeatedly by the interface library while the database server or client library is busy processing your database request. 451 | CALLBACK_WAIT, 452 | /// This function is called after the response to a database request has been received by the DBLIB interface DLL. 453 | /// CALLBACK_FINISH is used only on Windows operating systems. 454 | CALLBACK_FINISH, 455 | /// This function is called when messages are received from the server during the processing of a request. 456 | /// Messages can be sent to the client application from the database server using the SQL MESSAGE statement. 457 | /// Messages can also be generated by long running database server statements. 458 | CALLBACK_MESSAGE = 7, 459 | /// This function is called when the database server is about to drop a connection because of a liveness timeout, 460 | /// through a DROP CONNECTION statement, or because the database server is being shut down. 461 | /// The connection name conn_name is passed in to allow you to distinguish between connections. 462 | /// If the connection was not named, it has a value of NULL. 463 | CALLBACK_CONN_DROPPED, 464 | /// This function is called once for each debug message and is passed a null-terminated string containing the text of the debug message. 465 | /// A debug message is a message that is logged to the LogFile file. In order for a debug message to be passed to this callback, the LogFile 466 | /// connection parameter must be used. The string normally has a newline character (\n) immediately before the terminating null character. 467 | CALLBACK_DEBUG_MESSAGE, 468 | /// This function is called when a file transfer requires validation. 469 | /// If the client data transfer is being requested during the execution of indirect statements such as from within a stored procedure, 470 | /// the client library will not allow a transfer unless the client application has registered a validation callback and the response from 471 | /// the callback indicates that the transfer may take place. 472 | CALLBACK_VALIDATE_FILE_TRANSFER 473 | } a_sqlany_callback_type; 474 | 475 | /** An enumeration of the message types for the MESSAGE callback. 476 | * 477 | * \hideinitializers 478 | * \sa sqlany_register_callback() 479 | */ 480 | typedef enum a_sqlany_message_type { 481 | /// The message type was INFO. 482 | MESSAGE_TYPE_INFO = 0, 483 | /// The message type was WARNING. 484 | MESSAGE_TYPE_WARNING, 485 | /// The message type was ACTION. 486 | MESSAGE_TYPE_ACTION, 487 | /// The message type was STATUS. 488 | MESSAGE_TYPE_STATUS, 489 | /// The message type was PROGRESS. 490 | /// This type of message is generated by long running database server statements such as BACKUP DATABASE and LOAD TABLE. 491 | MESSAGE_TYPE_PROGRESS 492 | } a_sqlany_message_type; 493 | 494 | /** Initializes the interface. 495 | * 496 | * The following example demonstrates how to initialize the SQL Anywhere C API DLL: 497 | * 498 | *
 499 |  * sacapi_u32 api_version;
 500 |  * if( sqlany_init( "PHP", SQLANY_API_VERSION_1, &api_version ) ) {
 501 |  *     printf( "Interface initialized successfully!\n" );
 502 |  * } else {
 503 |  *     printf( "Failed to initialize the interface! Supported version=%d\n", api_version );
 504 |  * }
 505 |  * 
506 | * 507 | * \param app_name A string that names the application that is using the API. For example, "PHP", "PERL", or "RUBY". 508 | * \param api_version The version of the compiled application. 509 | * \param version_available An optional argument to return the maximum supported API version. 510 | * \return 1 on success, 0 otherwise 511 | * \sa sqlany_fini() 512 | */ 513 | sacapi_bool sqlany_init( const char * app_name, sacapi_u32 api_version, sacapi_u32 * version_available ); 514 | 515 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 516 | /** Initializes the interface using a context. 517 | * 518 | * \param app_name A string that names the API used, for example "PHP", "PERL", or "RUBY". 519 | * \param api_version The current API version that the application is using. 520 | * This should normally be one of the SQLANY_API_VERSION_* macros 521 | * \param version_available An optional argument to return the maximum API version that is supported. 522 | * \return a context object on success and NULL on failure. 523 | * \sa sqlany_fini_ex() 524 | */ 525 | a_sqlany_interface_context *sqlany_init_ex( const char * app_name, sacapi_u32 api_version, sacapi_u32 * version_available ); 526 | #endif 527 | 528 | /** Finalizes the interface. 529 | * 530 | * Frees any resources allocated by the API. 531 | * 532 | * \sa sqlany_init() 533 | */ 534 | void sqlany_fini(); 535 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 536 | /** Finalize the interface that was created using the specified context. 537 | * Frees any resources allocated by the API. 538 | * \param context A context object that was returned from sqlany_init_ex() 539 | * \sa sqlany_init_ex() 540 | */ 541 | void sqlany_fini_ex( a_sqlany_interface_context *context ); 542 | #endif 543 | 544 | /** Creates a connection object. 545 | * 546 | * You must create an API connection object before establishing a database connection. Errors can be retrieved 547 | * from the connection object. Only one request can be processed on a connection at a time. In addition, 548 | * not more than one thread is allowed to access a connection object at a time. Undefined behavior or a failure 549 | * occurs when multiple threads attempt to access a connection object simultaneously. 550 | * 551 | * \return A connection object 552 | * \sa sqlany_connect(), sqlany_disconnect() 553 | */ 554 | a_sqlany_connection * sqlany_new_connection( void ); 555 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 556 | /** Creates a connection object using a context. 557 | * An API connection object needs to be created before a database connection is established. Errors can be retrieved 558 | * from the connection object. Only one request can be processed on a connection at a time. In addition, 559 | * not more than one thread is allowed to access a connection object at a time. If multiple threads attempt 560 | * to access a connection object simultaneously, then undefined behavior/crashes will occur. 561 | * \param context A context object that was returned from sqlany_init_ex() 562 | * \return A connection object 563 | * \sa sqlany_connect(), sqlany_disconnect(), sqlany_init_ex() 564 | */ 565 | a_sqlany_connection *sqlany_new_connection_ex( a_sqlany_interface_context *context ); 566 | #endif 567 | 568 | /** Frees the resources associated with a connection object. 569 | * 570 | * \param sqlany_conn A connection object created with sqlany_new_connection(). 571 | * \sa sqlany_new_connection() 572 | */ 573 | void sqlany_free_connection( a_sqlany_connection *sqlany_conn ); 574 | 575 | /** Creates a connection object based on a supplied DBLIB SQLCA pointer. 576 | * 577 | * \param arg A void * pointer to a DBLIB SQLCA object. 578 | * \return A connection object. 579 | * \sa sqlany_new_connection(), sqlany_execute(), sqlany_execute_direct(), sqlany_execute_immediate(), sqlany_prepare() 580 | */ 581 | a_sqlany_connection * sqlany_make_connection( void * arg ); 582 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 583 | /** Creates a connection object based on a supplied DBLIB SQLCA pointer and context. 584 | * \param context A valid context object that was created by sqlany_init_ex() 585 | * \param arg A void * pointer to a DBLIB SQLCA object. 586 | * \return A connection object. 587 | * \sa sqlany_init_ex(), sqlany_execute(), sqlany_execute_direct(), sqlany_execute_immediate(), sqlany_prepare() 588 | */ 589 | a_sqlany_connection *sqlany_make_connection_ex( a_sqlany_interface_context *context, void *arg ); 590 | #endif 591 | 592 | /** Creates a connection to a SQL Anywhere database server using the supplied connection object and connection string. 593 | * 594 | * The supplied connection object must first be allocated using sqlany_new_connection(). 595 | * 596 | * The following example demonstrates how to retrieve the error code of a failed connection attempt: 597 | * 598 | *
 599 |  * a_sqlany_connection * sqlany_conn;
 600 |  * sqlany_conn = sqlany_new_connection();
 601 |  * if( !sqlany_connect( sqlany_conn, "uid=dba;pwd=passwd" ) ) {
 602 |  *     char reason[SACAPI_ERROR_SIZE];
 603 |  *     sacapi_i32 code;
 604 |  *     code = sqlany_error( sqlany_conn, reason, sizeof(reason) );
 605 |  *     printf( "Connection failed. Code: %d Reason: %s\n", code, reason );
 606 |  * } else {
 607 |  *     printf( "Connected successfully!\n" );
 608 |  *     sqlany_disconnect( sqlany_conn );
 609 |  * }
 610 |  * sqlany_free_connection( sqlany_conn );
 611 |  * 
612 | * 613 | * \param sqlany_conn A connection object created by sqlany_new_connection(). 614 | * \param str A SQL Anywhere connection string. 615 | * \return 1 if the connection is established successfully or 0 when the connection fails. Use sqlany_error() to 616 | * retrieve the error code and message. 617 | * \sa sqlany_new_connection(), sqlany_error() 618 | */ 619 | sacapi_bool sqlany_connect( a_sqlany_connection * sqlany_conn, const char * str ); 620 | 621 | /** Disconnects an already established SQL Anywhere connection. 622 | * 623 | * All uncommitted transactions are rolled back. 624 | * 625 | * \param sqlany_conn A connection object with a connection established using sqlany_connect(). 626 | * \return 1 when successful or 0 when unsuccessful. 627 | * \sa sqlany_connect(), sqlany_new_connection() 628 | */ 629 | sacapi_bool sqlany_disconnect( a_sqlany_connection * sqlany_conn ); 630 | 631 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 632 | /** Cancel an outstanding request on a connection. 633 | * This function can be used to cancel an outstanding request on a specific connection. 634 | * \param sqlany_conn A connection object with a connection established using sqlany_connect(). 635 | */ 636 | void sqlany_cancel( a_sqlany_connection * sqlany_conn ); 637 | #endif 638 | 639 | /** Executes the supplied SQL statement immediately without returning a result set. 640 | * 641 | * This function is useful for SQL statements that do not return a result set. 642 | * 643 | * \param sqlany_conn A connection object with a connection established using sqlany_connect(). 644 | * \param sql A string representing the SQL statement to be executed. 645 | * \return 1 on success or 0 on failure. 646 | */ 647 | sacapi_bool sqlany_execute_immediate( a_sqlany_connection * sqlany_conn, const char * sql ); 648 | 649 | /** Prepares a supplied SQL string. 650 | * 651 | * Execution does not happen until sqlany_execute() is 652 | * called. The returned statement object should be freed using sqlany_free_stmt(). 653 | * 654 | * The following statement demonstrates how to prepare a SELECT SQL string: 655 | * 656 | *
 657 |  * char * str;
 658 |  * a_sqlany_stmt * stmt;
 659 |  *
 660 |  * str = "select * from employees where salary >= ?";
 661 |  * stmt = sqlany_prepare( sqlany_conn, str );
 662 |  * if( stmt == NULL ) {
 663 |  *     // Failed to prepare statement, call sqlany_error() for more info
 664 |  * }
 665 |  * 
666 | * 667 | * \param sqlany_conn A connection object with a connection established using sqlany_connect(). 668 | * \param sql_str The SQL statement to be prepared. 669 | * \return A handle to a SQL Anywhere statement object. The statement object can be used by sqlany_execute() 670 | * to execute the statement. 671 | * \sa sqlany_free_stmt(), sqlany_connect(), sqlany_execute(), sqlany_num_params(), sqlany_describe_bind_param(), sqlany_bind_param() 672 | */ 673 | a_sqlany_stmt * sqlany_prepare( a_sqlany_connection * sqlany_conn, const char * sql_str ); 674 | 675 | /** Frees resources associated with a prepared statement object. 676 | * 677 | * \param sqlany_stmt A statement object returned by the successful execution of sqlany_prepare() or sqlany_execute_direct(). 678 | * \sa sqlany_prepare(), sqlany_execute_direct() 679 | */ 680 | void sqlany_free_stmt( a_sqlany_stmt * sqlany_stmt ); 681 | 682 | /** Returns the number of parameters expected for a prepared statement. 683 | * 684 | * \param sqlany_stmt A statement object returned by the successful execution of sqlany_prepare(). 685 | * \return The expected number of parameters, or -1 if the statement object is not valid. 686 | * \sa sqlany_prepare() 687 | */ 688 | sacapi_i32 sqlany_num_params( a_sqlany_stmt * sqlany_stmt ); 689 | 690 | /** Describes the bind parameters of a prepared statement. 691 | * 692 | * This function allows the caller to determine information about prepared statement parameters. The type of prepared 693 | * statement, stored procedured or a DML, determines the amount of information provided. The direction of the parameters 694 | * (input, output, or input-output) are always provided. 695 | * 696 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 697 | * \param index The index of the parameter. This number must be between 0 and sqlany_num_params() - 1. 698 | * \param param An a_sqlany_bind_param structure that is populated with information. 699 | * \return 1 when successful or 0 when unsuccessful. 700 | * \sa sqlany_bind_param(), sqlany_prepare() 701 | */ 702 | sacapi_bool sqlany_describe_bind_param( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_bind_param * param ); 703 | 704 | /** Bind a user-supplied buffer as a parameter to the prepared statement. 705 | * 706 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 707 | * \param index The index of the parameter. This number must be between 0 and sqlany_num_params() - 1. 708 | * \param param An a_sqlany_bind_param structure description of the parameter to be bound. 709 | * \return 1 on success or 0 on unsuccessful. 710 | * \sa sqlany_describe_bind_param() 711 | */ 712 | sacapi_bool sqlany_bind_param( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_bind_param * param ); 713 | 714 | /** Sends data as part of a bound parameter. 715 | * 716 | * This method can be used to send a large amount of data for a bound parameter in chunks. 717 | * This method can be used only when the batch size is 1. 718 | * 719 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 720 | * \param index The index of the parameter. This should be a number between 0 and sqlany_num_params() - 1. 721 | * \param buffer The data to be sent. 722 | * \param size The number of bytes to send. 723 | * \return 1 on success or 0 on failure. 724 | * \sa sqlany_prepare() 725 | */ 726 | sacapi_bool sqlany_send_param_data( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, char * buffer, size_t size ); 727 | 728 | 729 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_5 730 | /** Clears param data that was previously been set using \sa sqlany_send_param_data() 731 | * 732 | * This method can be used to clear data that was previously been sent using sqlany_send_param_data() 733 | * If no param data was previously sent, nothing is changed. 734 | * 735 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 736 | * \param index The index of the parameter. This should be a number between 0 and sqlany_num_params() - 1. 737 | * \return 1 on success or 0 on failure 738 | * \sa sqlany_prepare(), sqlany_send_param_data() 739 | */ 740 | sacapi_bool sqlany_reset_param_data( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index ); 741 | 742 | /** Retrieves the length of the last error message stored in the connection object 743 | * including the NULL terminator. If there is no error, 0 is returned. 744 | * 745 | * \param sqlany_conn A connection object returned from sqlany_new_connection(). 746 | * \return The length of the last error message including the NULL terminator. 747 | */ 748 | size_t sqlany_error_length( a_sqlany_connection * sqlany_conn ); 749 | #endif 750 | 751 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 752 | /** Sets the size of the row array for a batch execute. 753 | * 754 | * The batch size is used only for an INSERT statement. The default batch size is 1. 755 | * A value greater than 1 indicates a wide insert. 756 | * 757 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 758 | * \param num_rows The number of rows for batch execution. The value must be 1 or greater. 759 | * \return 1 on success or 0 on failure. 760 | * \sa sqlany_bind_param(), sqlany_get_batch_size() 761 | */ 762 | sacapi_bool sqlany_set_batch_size( a_sqlany_stmt * sqlany_stmt, sacapi_u32 num_rows ); 763 | 764 | /** Sets the bind type of parameters. 765 | * 766 | * The default value is 0, which indicates column-wise binding. A non-zero value indicates 767 | * row-wise binding and specifies the byte size of the data structure that stores the row. 768 | * The parameter is bound to the first element in a contiguous array of values. The address 769 | * offset to the next element is computed based on the bind type: 770 | * 771 | *
    772 | *
  • Column-wise binding - the byte size of the parameter type
  • 773 | *
  • Row-wise binding - the row_size
  • 774 | *
775 | * 776 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 777 | * \param row_size The byte size of the row. A value of 0 indicates column-wise binding and a positive value indicates row-wise binding. 778 | * \return 1 on success or 0 on failure. 779 | * \sa sqlany_bind_param() 780 | */ 781 | sacapi_bool sqlany_set_param_bind_type( a_sqlany_stmt * sqlany_stmt, size_t row_size ); 782 | 783 | /** Retrieves the size of the row array for a batch execute. 784 | * 785 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 786 | * \return The size of the row array. 787 | * \sa sqlany_set_batch_size() 788 | */ 789 | sacapi_u32 sqlany_get_batch_size( a_sqlany_stmt * sqlany_stmt ); 790 | 791 | /** Sets the size of the row set to be fetched by the sqlany_fetch_absolute() and sqlany_fetch_next() functions. 792 | * 793 | * The default size of the row set is 1. Specifying num_rows to be a value greater than 1 indicates a wide fetch. 794 | * 795 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 796 | * \param num_rows The size of the row set. The value must be 1 or greater. 797 | * \return 1 on success or 0 on failure. 798 | * \sa sqlany_bind_column(), sqlany_fetch_absolute(), sqlany_fetch_next(), sqlany_get_rowset_size() 799 | */ 800 | sacapi_bool sqlany_set_rowset_size( a_sqlany_stmt * sqlany_stmt, sacapi_u32 num_rows ); 801 | 802 | /** Retrieves the size of the row set to be fetched by the sqlany_fetch_absolute() and sqlany_fetch_next() functions. 803 | * 804 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 805 | * \return The size of the row set, or 0 if the statement does not return a result set. 806 | * \sa sqlany_set_rowset_size(), sqlany_fetch_absolute(), sqlany_fetch_next() 807 | */ 808 | sacapi_u32 sqlany_get_rowset_size( a_sqlany_stmt * sqlany_stmt ); 809 | 810 | /** Sets the bind type of columns. 811 | * 812 | * The default value is 0, which indicates column-wise binding. A non-zero value indicates 813 | * row-wise binding and specifies the byte size of the data structure that stores the row. 814 | * The column is bound to the first element in a contiguous array of values. The address 815 | * offset to the next element is computed based on the bind type: 816 | * 817 | *
    818 | *
  • Column-wise binding - the byte size of the column type
  • 819 | *
  • Row-wise binding - the row_size
  • 820 | *
821 | * 822 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 823 | * \param row_size The byte size of the row. A value of 0 indicates column-wise binding and a positive value indicates row-wise binding. 824 | * \return 1 on success or 0 on failure. 825 | * \sa sqlany_bind_column() 826 | */ 827 | sacapi_bool sqlany_set_column_bind_type( a_sqlany_stmt * sqlany_stmt, sacapi_u32 row_size ); 828 | 829 | /** Binds a user-supplied buffer as a result set column to the prepared statement. 830 | * 831 | * If the size of the fetched row set is greater than 1, the buffer must be large enough to 832 | * hold the data of all of the rows in the row set. This function can also be used to clear the 833 | * binding of a column by specifying value to be NULL. 834 | * 835 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 836 | * \param index The index of the column. This number must be between 0 and sqlany_num_cols() - 1. 837 | * \param value An a_sqlany_data_value structure describing the bound buffers, or NULL to clear previous binding information. 838 | * \return 1 on success or 0 on unsuccessful. 839 | * \sa sqlany_clear_column_bindings(), sqlany_set_rowset_size() 840 | */ 841 | sacapi_bool sqlany_bind_column( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_data_value * value ); 842 | 843 | /** Removes all column bindings defined using sqlany_bind_column(). 844 | * 845 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 846 | * \return 1 on success or 0 on failure. 847 | * \sa sqlany_bind_column() 848 | */ 849 | sacapi_bool sqlany_clear_column_bindings( a_sqlany_stmt * sqlany_stmt ); 850 | 851 | /** Returns the number of rows fetched. 852 | * 853 | * In general, the number of rows fetched is equal to the size specified by the sqlany_set_rowset_size() function. The 854 | * exception is when there are fewer rows from the fetch position to the end of the result set than specified, in which 855 | * case the number of rows fetched is smaller than the specified row set size. The function returns -1 if the last fetch 856 | * was unsuccessful or if the statement has not been executed. The function returns 0 if the statement has been executed 857 | * but no fetching has been done. 858 | * 859 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 860 | * \return The number of rows fetched or -1 on failure. 861 | * \sa sqlany_bind_column(), sqlany_fetch_next(), sqlany_fetch_absolute() 862 | */ 863 | sacapi_i32 sqlany_fetched_rows( a_sqlany_stmt * sqlany_stmt ); 864 | 865 | /** Sets the current row in the fetched row set. 866 | * 867 | * When a sqlany_fetch_absolute() or sqlany_fetch_next() function is executed, a row set 868 | * is created and the current row is set to be the first row in the row set. The functions 869 | * sqlany_get_column(), sqlany_get_data(), sqlany_get_data_info() are used to retrieve data 870 | * at the current row. 871 | * 872 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 873 | * \param row_num The row number within the row set. The valid values are from 0 to sqlany_fetched_rows() - 1. 874 | * \return 1 on success or 0 on failure. 875 | * \sa sqlany_set_rowset_size(), sqlany_get_column(), sqlany_get_data(), sqlany_get_data_info(), sqlany_fetch_absolute(), sqlany_fetch_next() 876 | */ 877 | sacapi_bool sqlany_set_rowset_pos( a_sqlany_stmt * sqlany_stmt, sacapi_u32 row_num ); 878 | #endif 879 | 880 | /** Resets a statement to its prepared state condition. 881 | * 882 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 883 | * \return 1 on success, 0 on failure. 884 | * \sa sqlany_prepare() 885 | */ 886 | sacapi_bool sqlany_reset( a_sqlany_stmt * sqlany_stmt ); 887 | 888 | /** Retrieves information about the parameters that were bound using sqlany_bind_param(). 889 | * 890 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 891 | * \param index The index of the parameter. This number should be between 0 and sqlany_num_params() - 1. 892 | * \param info A sqlany_bind_param_info buffer to be populated with the bound parameter's information. 893 | * \return 1 on success or 0 on failure. 894 | * \sa sqlany_bind_param(), sqlany_describe_bind_param(), sqlany_prepare() 895 | */ 896 | sacapi_bool sqlany_get_bind_param_info( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_bind_param_info * info ); 897 | 898 | /** Executes a prepared statement. 899 | * 900 | * You can use sqlany_num_cols() to verify if the executed statement returned a result set. 901 | * 902 | * The following example shows how to execute a statement that does not return a result set: 903 | * 904 | *
 905 |  * a_sqlany_stmt * 	 stmt;
 906 |  * int		     	 i;
 907 |  * a_sqlany_bind_param   param;
 908 |  *
 909 |  * stmt = sqlany_prepare( sqlany_conn, "insert into moe(id,value) values( ?,? )" );
 910 |  * if( stmt ) {
 911 |  *     sqlany_describe_bind_param( stmt, 0, ¶m );
 912 |  *     param.value.buffer = (char *)\&i;
 913 |  *     param.value.type   = A_VAL32;
 914 |  *     sqlany_bind_param( stmt, 0, ¶m );
 915 |  *
 916 |  *     sqlany_describe_bind_param( stmt, 1, ¶m );
 917 |  *     param.value.buffer = (char *)\&i;
 918 |  *     param.value.type   = A_VAL32;
 919 |  *     sqlany_bind_param( stmt, 1, ¶m );
 920 |  *
 921 |  *     for( i = 0; i < 10; i++ ) {
 922 |  *         if( !sqlany_execute( stmt ) ) {	
 923 |  *	           // call sqlany_error()
 924 |  *	       }
 925 |  *     }
 926 |  *     sqlany_free_stmt( stmt );
 927 |  * }
 928 |  * 
929 | * 930 | * \param sqlany_stmt A statement prepared successfully using sqlany_prepare(). 931 | * \return 1 if the statement is executed successfully or 0 on failure. 932 | * \sa sqlany_prepare() 933 | */ 934 | sacapi_bool sqlany_execute( a_sqlany_stmt * sqlany_stmt ); 935 | 936 | /** Executes the SQL statement specified by the string argument and possibly returns a result set. 937 | * 938 | * Use this method to prepare and execute a statement, 939 | * or instead of calling sqlany_prepare() followed by sqlany_execute(). 940 | * 941 | * The following example shows how to execute a statement that returns a result set: 942 | * 943 | *
 944 |  * a_sqlany_stmt *   stmt;
 945 |  *
 946 |  * stmt = sqlany_execute_direct( sqlany_conn, "select * from employees" );
 947 |  * if( stmt && sqlany_num_cols( stmt ) > 0 ) {
 948 |  *     while( sqlany_fetch_next( stmt ) ) {
 949 |  *         int i;
 950 |  *	       for( i = 0; i < sqlany_num_cols( stmt ); i++ ) {
 951 |  *             // Get column i data 
 952 |  *         }
 953 |  *     }
 954 |  *     sqlany_free_stmt( stmt  );
 955 |  * }
 956 |  * 
957 | * 958 | * Note: This function cannot be used for executing a SQL statement with parameters. 959 | * 960 | * \param sqlany_conn A connection object with a connection established using sqlany_connect(). 961 | * \param sql_str A SQL string. The SQL string should not have parameters such as ?. 962 | * \return A statement handle if the function executes successfully, NULL when the function executes unsuccessfully. 963 | * \sa sqlany_fetch_absolute(), sqlany_fetch_next(), sqlany_num_cols(), sqlany_get_column() 964 | */ 965 | a_sqlany_stmt * sqlany_execute_direct( a_sqlany_connection * sqlany_conn, const char * sql_str ); 966 | 967 | /** Moves the current row in the result set to the specified row number and then fetches 968 | * rows of data starting from the current row. 969 | * 970 | * The number of rows fetched is set using the sqlany_set_rowset_size() function. By default, one row is returned. 971 | * 972 | * \param sqlany_stmt A statement object that was executed by 973 | * sqlany_execute() or sqlany_execute_direct(). 974 | * \param row_num The row number to be fetched. The first row is 1, the last row is -1. 975 | * \return 1 if the fetch was successfully, 0 when the fetch is unsuccessful. 976 | * \sa sqlany_execute_direct(), sqlany_execute(), sqlany_error(), sqlany_fetch_next(), sqlany_set_rowset_size() 977 | */ 978 | sacapi_bool sqlany_fetch_absolute( a_sqlany_stmt * sqlany_stmt, sacapi_i32 row_num ); 979 | 980 | /** Returns the next set of rows from the result set. 981 | * 982 | * When the result object is first created, the current row 983 | * pointer is set to before the first row, that is, row 0. 984 | * This function first advances the row pointer to the next 985 | * unfetched row and then fetches rows of data starting from 986 | * that row. The number of rows fetched is set by the 987 | * sqlany_set_rowset_size() function. By default, one row is returned. 988 | * 989 | * \param sqlany_stmt A statement object that was executed by 990 | * sqlany_execute() or sqlany_execute_direct(). 991 | * \return 1 if the fetch was successfully, 0 when the fetch is unsuccessful. 992 | * \sa sqlany_fetch_absolute(), sqlany_execute_direct(), sqlany_execute(), sqlany_error(), sqlany_set_rowset_size() 993 | */ 994 | sacapi_bool sqlany_fetch_next( a_sqlany_stmt * sqlany_stmt ); 995 | 996 | /** Advances to the next result set in a multiple result set query. 997 | * 998 | * If a query (such as a call to a stored procedure) returns multiple result sets, then this function 999 | * advances from the current result set to the next. 1000 | * 1001 | * The following example demonstrates how to advance to the next result set in a multiple result set query: 1002 | * 1003 | *
1004 |  * stmt = sqlany_execute_direct( sqlany_conn, "call my_multiple_results_procedure()" );
1005 |  * if( result ) {
1006 |  *     do {
1007 |  *         while( sqlany_fetch_next( stmt ) ) {
1008 |  *            // get column data    
1009 |  *         }
1010 |  *     } while( sqlany_get_next_result( stmt ) );
1011 |  *     sqlany_free_stmt( stmt );
1012 |  * }
1013 |  * 
1014 | * 1015 | * \param sqlany_stmt A statement object executed by 1016 | * sqlany_execute() or sqlany_execute_direct(). 1017 | * \return 1 if the statement successfully advances to the next result set, 0 otherwise. 1018 | * \sa sqlany_execute_direct(), sqlany_execute() 1019 | */ 1020 | sacapi_bool sqlany_get_next_result( a_sqlany_stmt * sqlany_stmt ); 1021 | 1022 | /** Returns the number of rows affected by execution of the prepared statement. 1023 | * 1024 | * \param sqlany_stmt A statement that was prepared and executed successfully with no result set returned. 1025 | * For example, an INSERT, UPDATE or DELETE statement was executed. 1026 | * \return The number of rows affected or -1 on failure. 1027 | * \sa sqlany_execute(), sqlany_execute_direct() 1028 | */ 1029 | sacapi_i32 sqlany_affected_rows( a_sqlany_stmt * sqlany_stmt ); 1030 | 1031 | /** Returns number of columns in the result set. 1032 | * 1033 | * \param sqlany_stmt A statement object created by sqlany_prepare() or sqlany_execute_direct(). 1034 | * \return The number of columns in the result set or -1 on a failure. 1035 | * \sa sqlany_execute(), sqlany_execute_direct(), sqlany_prepare() 1036 | */ 1037 | sacapi_i32 sqlany_num_cols( a_sqlany_stmt * sqlany_stmt ); 1038 | 1039 | /** Returns the number of rows in the result set. 1040 | * 1041 | * By default this function only returns an estimate. To return an exact count, set the row_counts option 1042 | * on the connection. 1043 | * 1044 | * \param sqlany_stmt A statement object that was executed by 1045 | * sqlany_execute() or sqlany_execute_direct(). 1046 | * \return The number rows in the result set. If the number of rows is an estimate, the number returned is 1047 | * negative and the estimate is the absolute value of the returned integer. The value returned is positive 1048 | * if the number of rows is exact. 1049 | * \sa sqlany_execute_direct(), sqlany_execute() 1050 | */ 1051 | sacapi_i32 sqlany_num_rows( a_sqlany_stmt * sqlany_stmt ); 1052 | 1053 | /** Fills the supplied buffer with the value fetched for the specified column at the current row. 1054 | * 1055 | * When a sqlany_fetch_absolute() or sqlany_fetch_next() function is executed, a row set 1056 | * is created and the current row is set to be the first row in the row set. The current 1057 | * row is set using the sqlany_set_rowset_pos() function. 1058 | * 1059 | * For A_BINARY and A_STRING * data types, 1060 | * value->buffer points to an internal buffer associated with the result set. 1061 | * Do not rely upon or alter the content of the pointer buffer as it changes when a 1062 | * new row is fetched or when the result set object is freed. Users should copy the 1063 | * data out of those pointers into their own buffers. 1064 | * 1065 | * The value->length field indicates the number of valid characters that 1066 | * value->buffer points to. The data returned in value->buffer is not 1067 | * null-terminated. This function fetches all the returned values from the SQL 1068 | * Anywhere database server. For example, if the column contains 1069 | * a blob, this function attempts to allocate enough memory to hold that value. 1070 | * If you do not want to allocate memory, use sqlany_get_data() instead. 1071 | * 1072 | * \param sqlany_stmt A statement object executed by 1073 | * sqlany_execute() or sqlany_execute_direct(). 1074 | * \param col_index The number of the column to be retrieved. 1075 | * The column number is between 0 and sqlany_num_cols() - 1. 1076 | * \param buffer An a_sqlany_data_value object to be filled with the data fetched for column col_index at the current row in the row set. 1077 | * \return 1 on success or 0 for failure. A failure can happen if any of the parameters are invalid or if there is 1078 | * not enough memory to retrieve the full value from the SQL Anywhere database server. 1079 | * \sa sqlany_execute_direct(), sqlany_execute(), sqlany_fetch_absolute(), sqlany_fetch_next(), sqlany_set_rowset_pos() 1080 | */ 1081 | sacapi_bool sqlany_get_column( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, a_sqlany_data_value * buffer ); 1082 | 1083 | /** Retrieves the data fetched for the specified column at the current row into the supplied buffer memory. 1084 | * 1085 | * When a sqlany_fetch_absolute() or sqlany_fetch_next() function is executed, a row set 1086 | * is created and the current row is set to be the first row in the row set. The current 1087 | * row is set using the sqlany_set_rowset_pos() function. 1088 | * 1089 | * \param sqlany_stmt A statement object executed by 1090 | * sqlany_execute() or sqlany_execute_direct(). 1091 | * \param col_index The number of the column to be retrieved. 1092 | * The column number is between 0 and sqlany_num_cols() - 1. 1093 | * \param offset The starting offset of the data to get. 1094 | * \param buffer A buffer to be filled with the contents of the column at the current row in the row set. The buffer pointer must be aligned correctly 1095 | * for the data type copied into it. 1096 | * \param size The size of the buffer in bytes. The function fails 1097 | * if you specify a size greater than 2^31 - 1. 1098 | * \return The number of bytes successfully copied into the supplied buffer. 1099 | * This number must not exceed 2^31 - 1. 1100 | * 0 indicates that no data remains to be copied. -1 indicates a failure. 1101 | * \sa sqlany_execute(), sqlany_execute_direct(), sqlany_fetch_absolute(), sqlany_fetch_next(), sqlany_set_rowset_pos() 1102 | */ 1103 | sacapi_i32 sqlany_get_data( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, size_t offset, void * buffer, size_t size ); 1104 | 1105 | /** Retrieves information about the fetched data at the current row. 1106 | * 1107 | * When a sqlany_fetch_absolute() or sqlany_fetch_next() function is executed, a row set 1108 | * is created and the current row is set to be the first row in the row set. The current 1109 | * row is set using the sqlany_set_rowset_pos() function. 1110 | * 1111 | * \param sqlany_stmt A statement object executed by 1112 | * sqlany_execute() or sqlany_execute_direct(). 1113 | * \param col_index The column number between 0 and sqlany_num_cols() - 1. 1114 | * \param buffer A data info buffer to be filled with the metadata about the data at the current row in the row set. 1115 | * \return 1 on success, and 0 on failure. Failure is returned when any of the supplied parameters are invalid. 1116 | * \sa sqlany_execute(), sqlany_execute_direct(), sqlany_fetch_absolute(), sqlany_fetch_next(), sqlany_set_rowset_pos() 1117 | */ 1118 | sacapi_bool sqlany_get_data_info( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, a_sqlany_data_info * buffer ); 1119 | 1120 | /** Retrieves column metadata information and fills the a_sqlany_column_info structure with information about the column. 1121 | * 1122 | * \param sqlany_stmt A statement object created by sqlany_prepare() or sqlany_execute_direct(). 1123 | * \param col_index The column number between 0 and sqlany_num_cols() - 1. 1124 | * \param buffer A column info structure to be filled with column information. 1125 | * \return 1 on success or 0 if the column index is out of range, 1126 | * or if the statement does not return a result set. 1127 | * \sa sqlany_execute(), sqlany_execute_direct(), sqlany_prepare() 1128 | */ 1129 | sacapi_bool sqlany_get_column_info( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, a_sqlany_column_info * buffer ); 1130 | 1131 | /** Commits the current transaction. 1132 | * 1133 | * \param sqlany_conn The connection object on which the commit operation is performed. 1134 | * \return 1 when successful or 0 when unsuccessful. 1135 | * \sa sqlany_rollback() 1136 | */ 1137 | sacapi_bool sqlany_commit( a_sqlany_connection * sqlany_conn ); 1138 | 1139 | /** Rolls back the current transaction. 1140 | * 1141 | * \param sqlany_conn The connection object on which the rollback operation is to be performed. 1142 | * \return 1 on success, 0 otherwise. 1143 | * \sa sqlany_commit() 1144 | */ 1145 | sacapi_bool sqlany_rollback( a_sqlany_connection * sqlany_conn ); 1146 | 1147 | /** Returns the current client version. 1148 | * 1149 | * This method fills the buffer passed with the major, minor, patch, and build number of the client library. 1150 | * The buffer will be null-terminated. 1151 | * 1152 | * \param buffer The buffer to be filled with the client version string. 1153 | * \param len The length of the buffer supplied. 1154 | * \return 1 when successful or 0 when unsuccessful. 1155 | */ 1156 | sacapi_bool sqlany_client_version( char * buffer, size_t len ); 1157 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 1158 | /** Returns the current client version. 1159 | * 1160 | * This method fills the buffer passed with the major, minor, patch, and build number of the client library. 1161 | * The buffer will be null-terminated. 1162 | * 1163 | * \param context The object that was created with sqlany_init_ex(). 1164 | * \param buffer The buffer to be filled with the client version string. 1165 | * \param len The length of the buffer supplied. 1166 | * \return 1 when successful or 0 when unsuccessful. 1167 | * \sa sqlany_init_ex() 1168 | */ 1169 | sacapi_bool sqlany_client_version_ex( a_sqlany_interface_context *context, char *buffer, size_t len ); 1170 | #endif 1171 | 1172 | /** Retrieves the last error code and message stored in the connection object. 1173 | * 1174 | * \param sqlany_conn A connection object returned from sqlany_new_connection(). 1175 | * \param buffer A buffer to be filled with the error message. 1176 | * \param size The size of the supplied buffer. 1177 | * \return The last error code. Positive values are warnings, negative values are errors, and 0 indicates success. 1178 | * \sa sqlany_connect() 1179 | */ 1180 | sacapi_i32 sqlany_error( a_sqlany_connection * sqlany_conn, char * buffer, size_t size ); 1181 | 1182 | /** Retrieves the current SQLSTATE. 1183 | * 1184 | * \param sqlany_conn A connection object returned from sqlany_new_connection(). 1185 | * \param buffer A buffer to be filled with the current 5-character SQLSTATE. 1186 | * \param size The buffer size. 1187 | * \return The number of bytes copied into the buffer. 1188 | * \sa sqlany_error() 1189 | */ 1190 | size_t sqlany_sqlstate( a_sqlany_connection * sqlany_conn, char * buffer, size_t size ); 1191 | 1192 | /** Clears the last stored error code 1193 | * 1194 | * \param sqlany_conn A connection object returned from sqlany_new_connection(). 1195 | * \sa sqlany_new_connection() 1196 | */ 1197 | void sqlany_clear_error( a_sqlany_connection * sqlany_conn ); 1198 | 1199 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_3 1200 | /** Register a callback routine. 1201 | * 1202 | * This function can be used to register callback functions. 1203 | * 1204 | * A callback type can be any one of the following: 1205 | *
1206 |      *     CALLBACK_START
1207 |      *     CALLBACK_WAIT
1208 |      *     CALLBACK_FINISH
1209 |      *     CALLBACK_MESSAGE
1210 |      *     CALLBACK_CONN_DROPPED
1211 |      *     CALLBACK_DEBUG_MESSAGE
1212 |      *     CALLBACK_VALIDATE_FILE_TRANSFER
1213 |      * 
1214 | * 1215 | * The following example shows a simple message callback routine and how to register it. 1216 | *
1217 |      * void SQLANY_CALLBACK messages(
1218 |      *     a_sqlany_connection *sqlany_conn,
1219 |      *     a_sqlany_message_type msg_type,
1220 |      *     int sqlcode,
1221 |      *     unsigned short length,
1222 |      *     char *msg )
1223 |      * {
1224 |      *     size_t  mlen;
1225 |      *     char    mbuffer[80];
1226 |      * 
1227 |      *     mlen = __min( length, sizeof(mbuffer) );
1228 |      *     strncpy( mbuffer, msg, mlen );
1229 |      *     mbuffer[mlen] = '\0';
1230 |      *     printf( "Message is \"%s\".\n", mbuffer );
1231 |      *     sqlany_sqlstate( sqlany_conn, mbuffer, sizeof( mbuffer ) );
1232 |      *     printf( "SQLCode(%d) SQLState(\"%s\")\n\n", sqlcode, mbuffer );
1233 |      * }
1234 |      * 
1235 |      * api.sqlany_register_callback( sqlany_conn1, CALLBACK_MESSAGE, (SQLANY_CALLBACK_PARM)messages );
1236 |      * 
1237 | * \param sqlany_conn A connection object with a connection established using sqlany_connect(). 1238 | * \param index Any of the callback types listed below. 1239 | * \param callback Address of the callback routine. 1240 | * \return 1 when successful or 0 when unsuccessful. 1241 | */ 1242 | sacapi_bool sqlany_register_callback( a_sqlany_connection * sqlany_conn, a_sqlany_callback_type index, SQLANY_CALLBACK_PARM callback ); 1243 | #endif 1244 | 1245 | #if defined(__cplusplus) 1246 | } 1247 | #endif 1248 | 1249 | /** \example examples\connecting.cpp 1250 | * This is an example of how to create a connection object and connect with it to SQL Anywhere. 1251 | */ 1252 | 1253 | /** \example examples\fetching_a_result_set.cpp 1254 | * This example shows how to fetch data from a result set. 1255 | */ 1256 | 1257 | /** \example examples\preparing_statements.cpp 1258 | * This example shows how to prepare and execute a statement. 1259 | */ 1260 | 1261 | /** \example examples\fetching_multiple_from_sp.cpp 1262 | * This example shows how to fetch multiple result sets from a stored procedure. 1263 | */ 1264 | 1265 | /** \example examples\send_retrieve_part_blob.cpp 1266 | * This example shows how to insert a blob in chunks and retrieve it in chunks too. 1267 | */ 1268 | 1269 | /** \example examples\send_retrieve_full_blob.cpp 1270 | * This example shows how to insert and retrieve a blob in one chunk . 1271 | */ 1272 | 1273 | /** \example examples\dbcapi_isql.cpp 1274 | * This example shows how to write an ISQL application using dbcapi. 1275 | */ 1276 | 1277 | /** \example examples\callback.cpp 1278 | * This is an example of how to register and use a callback function. 1279 | */ 1280 | 1281 | #endif 1282 | -------------------------------------------------------------------------------- /src/h/sacapidll.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // While not a requirement of the license, if you do modify this file, we 19 | // would appreciate hearing about it. Please email 20 | // sqlany_interfaces@sap.com 21 | // 22 | // *************************************************************************** 23 | 24 | #ifndef SACAPIDLL_H 25 | #define SACAPIDLL_H 26 | 27 | #include "sacapi.h" 28 | 29 | /** \file sacapidll.h 30 | * \brief Header file for stub that can dynamically load the main API DLL. 31 | * The user will need to include sacapidll.h in their source files and compile in sacapidll.c 32 | */ 33 | 34 | 35 | #if defined( __cplusplus ) 36 | extern "C" { 37 | #endif 38 | typedef sacapi_bool (*sqlany_init_func)( const char * app_name, sacapi_u32 api_version, sacapi_u32 * max_version ); 39 | typedef void (*sqlany_fini_func)(); 40 | typedef a_sqlany_connection * (*sqlany_new_connection_func)( ); 41 | typedef void (*sqlany_free_connection_func)( a_sqlany_connection *sqlany_conn ); 42 | typedef a_sqlany_connection * (*sqlany_make_connection_func)( void * arg ); 43 | typedef sacapi_bool (*sqlany_connect_func)( a_sqlany_connection * sqlany_conn, const char * str ); 44 | typedef sacapi_bool (*sqlany_disconnect_func)( a_sqlany_connection * sqlany_conn ); 45 | typedef sacapi_bool (*sqlany_execute_immediate_func)( a_sqlany_connection * sqlany_conn, const char * sql ); 46 | typedef a_sqlany_stmt * (*sqlany_prepare_func)( a_sqlany_connection * sqlany_conn, const char * sql_str ); 47 | typedef void (*sqlany_free_stmt_func)( a_sqlany_stmt * sqlany_stmt ); 48 | typedef sacapi_i32 (*sqlany_num_params_func)( a_sqlany_stmt * sqlany_stmt ); 49 | typedef sacapi_bool (*sqlany_describe_bind_param_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_bind_param * params ); 50 | typedef sacapi_bool (*sqlany_bind_param_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_bind_param * params ); 51 | typedef sacapi_bool (*sqlany_send_param_data_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, char * buffer, size_t size ); 52 | typedef sacapi_bool (*sqlany_reset_func)( a_sqlany_stmt * sqlany_stmt ); 53 | typedef sacapi_bool (*sqlany_get_bind_param_info_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_bind_param_info * info ); 54 | typedef sacapi_bool (*sqlany_execute_func)( a_sqlany_stmt * sqlany_stmt ); 55 | typedef a_sqlany_stmt * (*sqlany_execute_direct_func)( a_sqlany_connection * sqlany_conn, const char * sql_str ); 56 | typedef sacapi_bool (*sqlany_fetch_absolute_func)( a_sqlany_stmt * sqlany_result, sacapi_i32 row_num ); 57 | typedef sacapi_bool (*sqlany_fetch_next_func)( a_sqlany_stmt * sqlany_stmt ); 58 | typedef sacapi_bool (*sqlany_get_next_result_func)( a_sqlany_stmt * sqlany_stmt ); 59 | typedef sacapi_i32 (*sqlany_affected_rows_func)( a_sqlany_stmt * sqlany_stmt ); 60 | typedef sacapi_i32 (*sqlany_num_cols_func)( a_sqlany_stmt * sqlany_stmt ); 61 | typedef sacapi_i32 (*sqlany_num_rows_func)( a_sqlany_stmt * sqlany_stmt ); 62 | typedef sacapi_bool (*sqlany_get_column_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, a_sqlany_data_value * buffer ); 63 | typedef sacapi_i32 (*sqlany_get_data_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, size_t offset, void * buffer, size_t size ); 64 | typedef sacapi_bool (*sqlany_get_data_info_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, a_sqlany_data_info * buffer ); 65 | typedef sacapi_bool (*sqlany_get_column_info_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 col_index, a_sqlany_column_info * buffer ); 66 | typedef sacapi_bool (*sqlany_commit_func)( a_sqlany_connection * sqlany_conn ); 67 | typedef sacapi_bool (*sqlany_rollback_func)( a_sqlany_connection * sqlany_conn ); 68 | typedef sacapi_bool (*sqlany_client_version_func)( char * buffer, size_t len ); 69 | typedef sacapi_i32 (*sqlany_error_func)( a_sqlany_connection * sqlany_conn, char * buffer, size_t size ); 70 | typedef size_t (*sqlany_sqlstate_func)( a_sqlany_connection * sqlany_conn, char * buffer, size_t size ); 71 | typedef void (*sqlany_clear_error_func)( a_sqlany_connection * sqlany_conn ); 72 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 73 | typedef a_sqlany_interface_context *(*sqlany_init_ex_func)( const char *app_name, sacapi_u32 api_version, sacapi_u32 *max_version ); 74 | typedef void (*sqlany_fini_ex_func)( a_sqlany_interface_context *context ); 75 | typedef a_sqlany_connection *(*sqlany_new_connection_ex_func)( a_sqlany_interface_context *context ); 76 | typedef a_sqlany_connection *(*sqlany_make_connection_ex_func)( a_sqlany_interface_context *context, void *arg ); 77 | typedef sacapi_bool (*sqlany_client_version_ex_func)( a_sqlany_interface_context *context, char *buffer, size_t len ); 78 | typedef void (*sqlany_cancel_func)( a_sqlany_connection * sqlany_conn ); 79 | #endif 80 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_3 81 | typedef sacapi_bool (*sqlany_register_callback_func)( a_sqlany_connection * sqlany_conn, a_sqlany_callback_type index, SQLANY_CALLBACK_PARM callback ); 82 | #endif 83 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 84 | typedef sacapi_bool (*sqlany_set_batch_size_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 num_rows ); 85 | typedef sacapi_bool (*sqlany_set_param_bind_type_func)( a_sqlany_stmt * sqlany_stmt, size_t row_size ); 86 | typedef sacapi_u32 (*sqlany_get_batch_size_func)( a_sqlany_stmt * sqlany_stmt ); 87 | typedef sacapi_bool (*sqlany_set_rowset_size_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 num_rows ); 88 | typedef sacapi_u32 (*sqlany_get_rowset_size_func)( a_sqlany_stmt * sqlany_stmt ); 89 | typedef sacapi_bool (*sqlany_set_column_bind_type_func)( a_sqlany_stmt * sqlany_stmt, size_t row_size ); 90 | typedef sacapi_bool (*sqlany_bind_column_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index, a_sqlany_data_value * value ); 91 | typedef sacapi_bool (*sqlany_clear_column_bindings_func)( a_sqlany_stmt * sqlany_stmt ); 92 | typedef sacapi_i32 (*sqlany_fetched_rows_func)( a_sqlany_stmt * sqlany_stmt ); 93 | typedef sacapi_bool (*sqlany_set_rowset_pos_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 row_num ); 94 | #endif 95 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_5 96 | typedef sacapi_bool (*sqlany_reset_param_data_func)( a_sqlany_stmt * sqlany_stmt, sacapi_u32 index ); 97 | typedef size_t (*sqlany_error_length_func)( a_sqlany_connection * conn ); 98 | #endif 99 | 100 | #if defined( __cplusplus ) 101 | } 102 | #endif 103 | 104 | /// @internal 105 | #define function( x ) x ## _func x 106 | 107 | /** The SQL Anywhere C API interface structure. 108 | * 109 | * Only one instance of this structure is required in your application environment. This structure 110 | * is initialized by the sqlany_initialize_interface method. It attempts to load the SQL Anywhere C 111 | * API DLL or shared object dynamically and looks up all the entry points of the DLL. The fields in 112 | * the SQLAnywhereInterface structure is populated to point to the corresponding functions in the DLL. 113 | * \sa sqlany_initialize_interface() 114 | */ 115 | typedef struct SQLAnywhereInterface { 116 | /** DLL handle. 117 | */ 118 | void * dll_handle; 119 | 120 | /** Flag to know if initialized or not. 121 | */ 122 | int initialized; 123 | 124 | /** Pointer to ::sqlany_init() function. 125 | */ 126 | function( sqlany_init ); 127 | 128 | /** Pointer to ::sqlany_fini() function. 129 | */ 130 | function( sqlany_fini ); 131 | 132 | /** Pointer to ::sqlany_new_connection() function. 133 | */ 134 | function( sqlany_new_connection ); 135 | 136 | /** Pointer to ::sqlany_free_connection() function. 137 | */ 138 | function( sqlany_free_connection ); 139 | 140 | /** Pointer to ::sqlany_make_connection() function. 141 | */ 142 | function( sqlany_make_connection ); 143 | 144 | /** Pointer to ::sqlany_connect() function. 145 | */ 146 | function( sqlany_connect ); 147 | 148 | /** Pointer to ::sqlany_disconnect() function. 149 | */ 150 | function( sqlany_disconnect ); 151 | 152 | /** Pointer to ::sqlany_execute_immediate() function. 153 | */ 154 | function( sqlany_execute_immediate ); 155 | 156 | /** Pointer to ::sqlany_prepare() function. 157 | */ 158 | function( sqlany_prepare ); 159 | 160 | /** Pointer to ::sqlany_free_stmt() function. 161 | */ 162 | function( sqlany_free_stmt ); 163 | 164 | /** Pointer to ::sqlany_num_params() function. 165 | */ 166 | function( sqlany_num_params ); 167 | 168 | /** Pointer to ::sqlany_describe_bind_param() function. 169 | */ 170 | function( sqlany_describe_bind_param ); 171 | 172 | /** Pointer to ::sqlany_bind_param() function. 173 | */ 174 | function( sqlany_bind_param ); 175 | 176 | /** Pointer to ::sqlany_send_param_data() function. 177 | */ 178 | function( sqlany_send_param_data ); 179 | 180 | /** Pointer to ::sqlany_reset() function. 181 | */ 182 | function( sqlany_reset ); 183 | 184 | /** Pointer to ::sqlany_get_bind_param_info() function. 185 | */ 186 | function( sqlany_get_bind_param_info ); 187 | 188 | /** Pointer to ::sqlany_execute() function. 189 | */ 190 | function( sqlany_execute ); 191 | 192 | /** Pointer to ::sqlany_execute_direct() function. 193 | */ 194 | function( sqlany_execute_direct ); 195 | 196 | /** Pointer to ::sqlany_fetch_absolute() function. 197 | */ 198 | function( sqlany_fetch_absolute ); 199 | 200 | /** Pointer to ::sqlany_fetch_next() function. 201 | */ 202 | function( sqlany_fetch_next ); 203 | 204 | /** Pointer to ::sqlany_get_next_result() function. 205 | */ 206 | function( sqlany_get_next_result ); 207 | 208 | /** Pointer to ::sqlany_affected_rows() function. 209 | */ 210 | function( sqlany_affected_rows ); 211 | 212 | /** Pointer to ::sqlany_num_cols() function. 213 | */ 214 | function( sqlany_num_cols ); 215 | 216 | /** Pointer to ::sqlany_num_rows() function. 217 | */ 218 | function( sqlany_num_rows ); 219 | 220 | /** Pointer to ::sqlany_get_column() function. 221 | */ 222 | function( sqlany_get_column ); 223 | 224 | /** Pointer to ::sqlany_get_data() function. 225 | */ 226 | function( sqlany_get_data ); 227 | 228 | /** Pointer to ::sqlany_get_data_info() function. 229 | */ 230 | function( sqlany_get_data_info ); 231 | 232 | /** Pointer to ::sqlany_get_column_info() function. 233 | */ 234 | function( sqlany_get_column_info ); 235 | 236 | /** Pointer to ::sqlany_commit() function. 237 | */ 238 | function( sqlany_commit ); 239 | 240 | /** Pointer to ::sqlany_rollback() function. 241 | */ 242 | function( sqlany_rollback ); 243 | 244 | /** Pointer to ::sqlany_client_version() function. 245 | */ 246 | function( sqlany_client_version ); 247 | 248 | /** Pointer to ::sqlany_error() function. 249 | */ 250 | function( sqlany_error ); 251 | 252 | /** Pointer to ::sqlany_sqlstate() function. 253 | */ 254 | function( sqlany_sqlstate ); 255 | 256 | /** Pointer to ::sqlany_clear_error() function. 257 | */ 258 | function( sqlany_clear_error ); 259 | 260 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 261 | /** Pointer to ::sqlany_init_ex() function. 262 | */ 263 | function( sqlany_init_ex ); 264 | 265 | /** Pointer to ::sqlany_fini_ex() function. 266 | */ 267 | function( sqlany_fini_ex ); 268 | 269 | /** Pointer to ::sqlany_new_connection_ex() function. 270 | */ 271 | function( sqlany_new_connection_ex ); 272 | 273 | /** Pointer to ::sqlany_make_connection_ex() function. 274 | */ 275 | function( sqlany_make_connection_ex ); 276 | 277 | /** Pointer to ::sqlany_client_version_ex() function. 278 | */ 279 | function( sqlany_client_version_ex ); 280 | 281 | /** Pointer to ::sqlany_cancel() function. 282 | */ 283 | function( sqlany_cancel ); 284 | #endif 285 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_3 286 | /** Pointer to ::sqlany_register_callback() function. 287 | */ 288 | function( sqlany_register_callback ); 289 | #endif 290 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 291 | /** Pointer to ::sqlany_set_batch_size() function. 292 | */ 293 | function( sqlany_set_batch_size ); 294 | /** Pointer to ::sqlany_set_param_bind_type() function. 295 | */ 296 | function( sqlany_set_param_bind_type ); 297 | /** Pointer to ::sqlany_get_batch_size() function. 298 | */ 299 | function( sqlany_get_batch_size ); 300 | /** Pointer to ::sqlany_set_rowset_size() function. 301 | */ 302 | function( sqlany_set_rowset_size ); 303 | /** Pointer to ::sqlany_get_rowset_size() function. 304 | */ 305 | function( sqlany_get_rowset_size ); 306 | /** Pointer to ::sqlany_set_column_bind_type() function. 307 | */ 308 | function( sqlany_set_column_bind_type ); 309 | /** Pointer to ::sqlany_bind_column() function. 310 | */ 311 | function( sqlany_bind_column ); 312 | /** Pointer to ::sqlany_clear_column_bindings() function. 313 | */ 314 | function( sqlany_clear_column_bindings ); 315 | /** Pointer to ::sqlany_fetched_rows() function. 316 | */ 317 | function( sqlany_fetched_rows ); 318 | /** Pointer to ::sqlany_set_rowset_pos() function. 319 | */ 320 | function( sqlany_set_rowset_pos ); 321 | #endif 322 | 323 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_5 324 | /** Pointer to ::sqlany_reset_param_data() function. 325 | */ 326 | function( sqlany_reset_param_data ); 327 | /** Pointer to ::sqlany_error_length() function. 328 | */ 329 | function( sqlany_error_length ); 330 | #endif 331 | 332 | } SQLAnywhereInterface; 333 | #undef function 334 | 335 | /** Initializes the SQLAnywhereInterface object and loads the DLL dynamically. 336 | * 337 | * Use the following statement to include the function prototype: 338 | * 339 | *
340 |  * \#include "sacapidll.h"
341 |  * 
342 | * 343 | * This function attempts to load the SQL Anywhere C API DLL dynamically and looks up all 344 | * the entry points of the DLL. The fields in the SQLAnywhereInterface structure are 345 | * populated to point to the corresponding functions in the DLL. If the optional path argument 346 | * is NULL, the environment variable SQLANY_API_DLL is checked. If the variable is set, 347 | * the library attempts to load the DLL specified by the environment variable. If that fails, 348 | * the interface attempts to load the DLL directly (this relies on the environment being 349 | * setup correctly). 350 | * 351 | * Examples of how the sqlany_initialize_interface method is used can be found in the 352 | * C API examples in the sdk\\dbcapi\\examples directory of your SQL 353 | * Anywhere installation. 354 | * 355 | * \param api An API structure to initialize. 356 | * \param optional_path_to_dll An optional argument that specifies a path to the SQL Anywhere C API DLL. 357 | * \return 1 on successful initialization, and 0 on failure. 358 | */ 359 | int sqlany_initialize_interface( SQLAnywhereInterface * api, const char * optional_path_to_dll ); 360 | 361 | /** Unloads the C API DLL library and resets the SQLAnywhereInterface structure. 362 | * 363 | * Use the following statement to include the function prototype: 364 | * 365 | *
366 |  * \#include "sacapidll.h"
367 |  * 
368 | * 369 | * Use this method to finalize and free resources associated with the SQL Anywhere C API DLL. 370 | * 371 | * Examples of how the sqlany_finalize_interface method is used can be found in the 372 | * C API examples in the sdk\\dbcapi\\examples directory of your SQL 373 | * Anywhere installation. 374 | * 375 | * \param api An initialized structure to finalize. 376 | */ 377 | 378 | void sqlany_finalize_interface( SQLAnywhereInterface * api ); 379 | 380 | #endif 381 | -------------------------------------------------------------------------------- /src/h/sqlany_utils.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "sacapidll.h" 10 | #include "sacapi.h" 11 | 12 | #pragma warning (disable : 4100) 13 | #pragma warning (disable : 4506) 14 | #pragma warning (disable : 4068) 15 | #pragma warning (disable : 4800) 16 | #pragma GCC diagnostic ignored "-Wunused-parameter" 17 | 18 | #if defined( _MSC_VER ) 19 | // According to the github page for node.js, some warnings generated by 20 | // Visual Studio are "overzealous". They should be fixed in VS 2017 15.6. 21 | // https://github.com/nodejs/node/issues/17114 22 | #pragma warning( disable : 4251 ) 23 | #endif 24 | #include "node.h" 25 | #include "v8.h" 26 | #include "node_buffer.h" 27 | #include "node_object_wrap.h" 28 | #include "uv.h" 29 | 30 | #pragma GCC diagnostic pop 31 | #pragma warning (default : 4100) 32 | #pragma warning (default : 4506) 33 | #pragma warning (default : 4068) 34 | #pragma warning (default : 4800) 35 | 36 | #include "nodever_cover.h" 37 | #include "errors.h" 38 | #include "connection.h" 39 | #include "stmt.h" 40 | 41 | using namespace v8; 42 | 43 | extern SQLAnywhereInterface api; 44 | extern unsigned openConnections; 45 | extern uv_mutex_t api_mutex; 46 | 47 | #define CLEAN_STRINGS( vector ) \ 48 | { \ 49 | for( size_t i = 0; i < vector.size(); i++ ) { \ 50 | delete[] vector[i]; \ 51 | } \ 52 | vector.clear(); \ 53 | } 54 | 55 | #define CLEAN_PTRS( vector ) \ 56 | { \ 57 | for( size_t i = 0; i < vector.size(); i++ ) { \ 58 | delete vector[i]; \ 59 | } \ 60 | vector.clear(); \ 61 | } 62 | 63 | class scoped_lock 64 | { 65 | public: 66 | scoped_lock( uv_mutex_t &mtx ) : _mtx( mtx ) 67 | { 68 | uv_mutex_lock( &_mtx ); 69 | } 70 | ~scoped_lock() 71 | { 72 | 73 | uv_mutex_unlock( &_mtx ); 74 | } 75 | 76 | private: 77 | uv_mutex_t &_mtx; 78 | 79 | }; 80 | 81 | class ExecuteData { 82 | public: 83 | ~ExecuteData() { 84 | clear(); 85 | } 86 | 87 | void clear( void ) { 88 | CLEAN_STRINGS( string_vals ); 89 | CLEAN_STRINGS( string_arr_vals ); 90 | CLEAN_PTRS( int_vals ); 91 | CLEAN_PTRS( num_vals ); 92 | CLEAN_PTRS( len_vals ); 93 | CLEAN_PTRS( null_vals ); 94 | } 95 | void addString( char *str, size_t *len ) { 96 | string_vals.push_back( str ); 97 | len_vals.push_back( len ); 98 | } 99 | void addStrings( char **str, size_t *len ) { 100 | string_arr_vals.push_back( str ); 101 | len_vals.push_back( len ); 102 | } 103 | void addInt( int *val ) { int_vals.push_back( val ); } 104 | void addNum( double *val ) { num_vals.push_back( val ); } 105 | void addNull( sacapi_bool *val ) { null_vals.push_back( val ); } 106 | 107 | char * getString( size_t ind ) { return string_vals[ind]; } 108 | char ** getStrings( size_t ind ) { return string_arr_vals[ind]; } 109 | int getInt( size_t ind ) { return *(int_vals[ind]); } 110 | double getNum( size_t ind ) { return *(num_vals[ind]); } 111 | size_t getLen( size_t ind ) { return *(len_vals[ind]); } 112 | sacapi_bool getNull( size_t ind ) { return *(null_vals[ind]); } 113 | 114 | size_t stringSize( void ) const { return string_vals.size(); } 115 | size_t intSize( void ) const { return int_vals.size(); } 116 | size_t numSize( void ) const { return num_vals.size(); } 117 | size_t lenSize( void ) const { return len_vals.size(); } 118 | size_t nullSize( void ) const { return null_vals.size(); } 119 | 120 | bool stringIsNull( size_t ind ) const { return string_vals[ind] == NULL; } 121 | bool intIsNull( size_t ind ) const { return int_vals[ind] == NULL; } 122 | bool numIsNull( size_t ind ) const { return num_vals[ind] == NULL; } 123 | bool lenIsNull( size_t ind ) const { return string_vals[ind] == NULL; } 124 | bool nullIsNull( size_t ind ) const { return string_vals[ind] == NULL; } 125 | 126 | private: 127 | std::vector string_vals; 128 | std::vector string_arr_vals; 129 | std::vector int_vals; 130 | std::vector num_vals; 131 | std::vector len_vals; 132 | std::vector null_vals; 133 | }; 134 | 135 | bool cleanAPI (); // Finalizes the API and frees up resources 136 | int getError( a_sqlany_connection *conn, char *str, size_t len ); 137 | void getErrorMsg( a_sqlany_connection *conn, std::string &str ); 138 | void getErrorMsg( int code, std::string &str ); 139 | void throwError( a_sqlany_connection *conn ); 140 | void throwError( int code ); 141 | 142 | #if v010 143 | void callBack( std::string * str, 144 | Persistent callback, 145 | Local Result, 146 | bool callback_required = true ); 147 | void callBack( std::string * str, 148 | Local callback, 149 | Local Result, 150 | bool callback_required = true ); 151 | #else 152 | void callBack( std::string * str, 153 | Persistent & callback, 154 | Local & Result, 155 | bool callback_required = true ); 156 | void callBack( std::string * str, 157 | const Local & callback, 158 | Local & Result, 159 | bool callback_required = true ); 160 | void callBack( std::string * str, 161 | Persistent & callback, 162 | Persistent & Result, 163 | bool callback_required = true ); 164 | #endif 165 | 166 | bool getBindParameters( std::vector &execData 167 | , Isolate * isolate 168 | , Local arg 169 | , std::vector ¶ms 170 | , unsigned &num_rows 171 | ); 172 | 173 | #if v010 174 | bool getResultSet( Local &Result 175 | , int &rows_affected 176 | , std::vector &colNames 177 | , ExecuteData *execData 178 | , std::vector &col_types ); 179 | #else 180 | bool getResultSet( Persistent &Result 181 | , int &rows_affected 182 | , std::vector &colNames 183 | , ExecuteData *execData 184 | , std::vector &col_types ); 185 | #endif 186 | 187 | bool fetchResultSet( a_sqlany_stmt *sqlany_stmt 188 | , int &rows_affected 189 | , std::vector &colNames 190 | , ExecuteData *execData 191 | , std::vector &col_types ); 192 | 193 | struct noParamBaton { 194 | Persistent callback; 195 | bool err; 196 | std::string error_msg; 197 | bool callback_required; 198 | 199 | Connection *obj; 200 | 201 | noParamBaton() { 202 | obj = NULL; 203 | err = false; 204 | } 205 | 206 | ~noParamBaton() { 207 | obj = NULL; 208 | } 209 | }; 210 | 211 | void executeAfter( uv_work_t *req ); 212 | void executeWork( uv_work_t *req ); 213 | 214 | #if NODE_MAJOR_VERSION > 0 || NODE_MINOR_VERSION > 10 215 | void HashToString( Isolate *isolate, Local obj, Persistent &ret ); 216 | #else 217 | Persistent HashToString( Local obj ); 218 | Handle CreateConnection( const Arguments &args ); 219 | #endif 220 | -------------------------------------------------------------------------------- /src/h/stmt.h: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | using namespace v8; 5 | 6 | #include "nodever_cover.h" 7 | 8 | /** Represents prepared statement 9 | * @class Statement 10 | * 11 | * The Statement object is for SQL statements that will be executed multiple 12 | * times. 13 | * @see Connection::prepare 14 | */ 15 | class StmtObject : public node::ObjectWrap 16 | { 17 | public: 18 | /// @internal 19 | #if v010 20 | static void Init(); 21 | #else 22 | static void Init( Isolate * ); 23 | #endif 24 | 25 | /// @internal 26 | static NODE_API_FUNC( NewInstance ); 27 | 28 | /// @internal 29 | #if !v010 30 | static void CreateNewInstance( const FunctionCallbackInfo &args, 31 | Persistent &obj ); 32 | #endif 33 | 34 | /// @internal 35 | StmtObject(); 36 | /// @internal 37 | ~StmtObject(); 38 | /// @internal 39 | void cleanup( void ); 40 | void removeConnection( void ); 41 | 42 | private: 43 | /// @internal 44 | static Persistent constructor; 45 | /// @internal 46 | static NODE_API_FUNC( New ); 47 | 48 | /** Executes the prepared SQL statement. 49 | * 50 | * This method optionally takes in an array of bind 51 | * parameters to execute. 52 | * 53 | * This method can be either synchronous or asynchronous depending on 54 | * whether or not a callback function is specified. 55 | * The callback function is of the form: 56 | * 57 | *

 58 |      * function( err, result )
 59 |      * {
 60 |      *
 61 |      * };
 62 |      * 

63 | * 64 | * For queries producing result sets, the result set object is returned 65 | * as the second parameter of the callback. 66 | * For insert, update and delete statements, the number of rows affected 67 | * is returned as the second parameter of the callback. 68 | * For other statements, result is undefined. 69 | * 70 | * The following synchronous example shows how to use the exec method 71 | * on a prepared statement. 72 | * 73 | *

 74 |      * var sqlanywhere = require( 'sqlanywhere' );
 75 |      * var client = sqlanywhere.createConnection();
 76 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
 77 |      * stmt = client.prepare( "SELECT * FROM Customers WHERE ID >= ? AND ID < ?" );
 78 |      * result = stmt.exec( [200, 300] );
 79 |      * stmt.drop();
 80 |      * console.log( result );
 81 |      * client.disconnect();
 82 |      * 

83 | * 84 | * @fn result Statement::exec( Array params, Function callback ) 85 | * 86 | * @param params The optional array of bind parameters. 87 | * @param callback The optional callback function. 88 | * 89 | * @return If no callback is specified, the result is returned. 90 | * 91 | */ 92 | static NODE_API_FUNC( exec ); 93 | 94 | /** Drops the statement. 95 | * 96 | * This method drops the prepared statement and frees up resources. 97 | * 98 | * This method can be either synchronous or asynchronous depending on 99 | * whether or not a callback function is specified. 100 | * The callback function is of the form: 101 | * 102 | *

103 |      * function( err )
104 |      * {
105 |      *
106 |      * };
107 |      * 

108 | * 109 | * The following synchronous example shows how to use the drop method 110 | * on a prepared statement. 111 | * 112 | *

113 |      * var sqlanywhere = require( 'sqlanywhere' );
114 |      * var client = sqlanywhere.createConnection();
115 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
116 |      * stmt = client.prepare( "SELECT * FROM Customers WHERE ID >= ? AND ID < ?" );
117 |      * result = stmt.exec( [200, 300] );
118 |      * stmt.drop();
119 |      * console.log( result );
120 |      * client.disconnect();
121 |      * 

122 | * 123 | * @fn Statement::drop( Function callback ) 124 | * 125 | * @param callback The optional callback function. 126 | * 127 | */ 128 | static NODE_API_FUNC( drop ); 129 | 130 | /// @internal 131 | static void dropAfter( uv_work_t *req ); 132 | /// @internal 133 | static void dropWork( uv_work_t *req ); 134 | 135 | /** Gets the next result set of a multi-result-set query. 136 | * 137 | * This method can be either synchronous or asynchronous depending on 138 | * whether or not a callback function is specified. 139 | * The callback function is of the form: 140 | * 141 | *

142 |      * function( err, res )
143 |      * {
144 |      *
145 |      * };
146 |      * 

147 | * 148 | * The following synchronous example shows how to use the getMoreResults method 149 | * on a prepared statement. 150 | * 151 | *

152 |      * var sqlanywhere = require( 'sqlanywhere' );
153 |      * var client = sqlanywhere.createConnection();
154 |      * client.connect( "ServerName=demo17;UID=DBA;PWD=sql" )
155 |      * stmt = client.prepare( "SELECT * FROM Customers WHERE ID = ?; SELECT * FROM Customers where ID = ?" );
156 |      * result = stmt.exec( [200, 300] );
157 |      * console.log( result ); // first query
158 |      * result = stmt.getMoreResults();
159 |      * console.log( result ); // second query
160 |      * stmt.drop();
161 |      * client.disconnect();
162 |      * 

163 | * 164 | * @fn Statement::getMoreResults( Function callback ) 165 | * 166 | * @param callback The optional callback function. 167 | * 168 | */ 169 | static NODE_API_FUNC( getMoreResults ); 170 | 171 | public: 172 | /// @internal 173 | Connection *connection; 174 | /// @internal 175 | a_sqlany_stmt *sqlany_stmt; 176 | }; 177 | -------------------------------------------------------------------------------- /src/sacapidll.cpp: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // While not a requirement of the license, if you do modify this file, we 19 | // would appreciate hearing about it. Please email 20 | // sqlany_interfaces@sap.com 21 | // 22 | // *************************************************************************** 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #if defined( _WIN32 ) 29 | #include 30 | #define DEFAULT_LIBRARY_NAME "dbcapi.dll" 31 | #else 32 | #include 33 | /* assume we are running on a UNIX platform */ 34 | #if defined( __APPLE__ ) 35 | #define LIB_EXT "dylib" 36 | #else 37 | #define LIB_EXT "so" 38 | #endif 39 | #if defined( _REENTRANT ) || defined( _THREAD_SAFE ) \ 40 | || defined( __USE_REENTRANT ) 41 | /* if building a thread-safe library, we need to load 42 | the thread-safe dbcapi library */ 43 | #define DEFAULT_LIBRARY_NAME "libdbcapi_r." LIB_EXT 44 | #else 45 | #define DEFAULT_LIBRARY_NAME "libdbcapi." LIB_EXT 46 | #endif 47 | #endif 48 | 49 | #include "sacapidll.h" 50 | 51 | static 52 | void * loadLibrary( const char * name ) 53 | /*************************************/ 54 | { 55 | void * handle; 56 | #if defined( _WIN32 ) 57 | handle = LoadLibrary( name ); 58 | #else 59 | handle = dlopen( name, RTLD_LAZY ); 60 | #endif 61 | return handle; 62 | } 63 | 64 | static 65 | void unloadLibrary( void * handle ) 66 | /**********************************/ 67 | { 68 | #if defined( _WIN32 ) 69 | FreeLibrary( (HMODULE)handle ); 70 | #else 71 | dlclose( handle ); 72 | #endif 73 | } 74 | 75 | static 76 | void * findSymbol( void * dll_handle, const char * name ) 77 | /*******************************************************/ 78 | { 79 | #if defined( _WIN32 ) 80 | return GetProcAddress( (HMODULE)dll_handle, name ); 81 | #else 82 | return dlsym( dll_handle, name ); 83 | #endif 84 | } 85 | 86 | #define LookupSymbol( api, sym ) \ 87 | api->sym = (sym ## _func)findSymbol( api->dll_handle, #sym ); 88 | 89 | #define LookupSymbolAndCheck( api, sym ) \ 90 | api->sym = (sym ## _func)findSymbol( api->dll_handle, #sym ); \ 91 | if( api->sym == NULL ) { \ 92 | unloadLibrary( api->dll_handle ); \ 93 | return 0; \ 94 | } 95 | 96 | int sqlany_initialize_interface( SQLAnywhereInterface * api, const char * path ) 97 | /*******************************************************************************/ 98 | { 99 | char * env; 100 | memset( api, 0, sizeof(*api)); 101 | 102 | if( path != NULL ) { 103 | api->dll_handle = loadLibrary( path ); 104 | if( api->dll_handle != NULL ) { 105 | goto loaded; 106 | } 107 | } 108 | env = getenv( "SQLANY_API_DLL" ); 109 | if( env != NULL ) { 110 | api->dll_handle = loadLibrary( env ); 111 | if( api->dll_handle != NULL ) { 112 | goto loaded; 113 | } 114 | } 115 | api->dll_handle = loadLibrary( DEFAULT_LIBRARY_NAME ); 116 | if( api->dll_handle != NULL ) { 117 | goto loaded; 118 | } 119 | return 0; 120 | 121 | loaded: 122 | LookupSymbolAndCheck( api, sqlany_init ); 123 | LookupSymbolAndCheck( api, sqlany_fini ); 124 | LookupSymbolAndCheck( api, sqlany_new_connection ); 125 | LookupSymbolAndCheck( api, sqlany_free_connection ); 126 | LookupSymbolAndCheck( api, sqlany_make_connection ); 127 | LookupSymbolAndCheck( api, sqlany_connect ); 128 | LookupSymbolAndCheck( api, sqlany_disconnect ); 129 | LookupSymbolAndCheck( api, sqlany_execute_immediate ); 130 | LookupSymbolAndCheck( api, sqlany_prepare ); 131 | LookupSymbolAndCheck( api, sqlany_free_stmt ); 132 | LookupSymbolAndCheck( api, sqlany_num_params ); 133 | LookupSymbolAndCheck( api, sqlany_describe_bind_param ); 134 | LookupSymbolAndCheck( api, sqlany_bind_param ); 135 | LookupSymbolAndCheck( api, sqlany_send_param_data ); 136 | LookupSymbolAndCheck( api, sqlany_reset ); 137 | LookupSymbolAndCheck( api, sqlany_get_bind_param_info ); 138 | LookupSymbolAndCheck( api, sqlany_execute ); 139 | LookupSymbolAndCheck( api, sqlany_execute_direct ); 140 | LookupSymbolAndCheck( api, sqlany_fetch_absolute ); 141 | LookupSymbolAndCheck( api, sqlany_fetch_next ); 142 | LookupSymbolAndCheck( api, sqlany_get_next_result ); 143 | LookupSymbolAndCheck( api, sqlany_affected_rows ); 144 | LookupSymbolAndCheck( api, sqlany_num_cols ); 145 | LookupSymbolAndCheck( api, sqlany_num_rows ); 146 | LookupSymbolAndCheck( api, sqlany_get_column ); 147 | LookupSymbolAndCheck( api, sqlany_get_data ); 148 | LookupSymbolAndCheck( api, sqlany_get_data_info ); 149 | LookupSymbolAndCheck( api, sqlany_get_column_info ); 150 | LookupSymbolAndCheck( api, sqlany_commit ); 151 | LookupSymbolAndCheck( api, sqlany_rollback ); 152 | LookupSymbolAndCheck( api, sqlany_client_version ); 153 | LookupSymbolAndCheck( api, sqlany_error ); 154 | LookupSymbolAndCheck( api, sqlany_sqlstate ); 155 | LookupSymbolAndCheck( api, sqlany_clear_error ); 156 | 157 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_2 158 | /* We don't report an error if we don't find the v2 entry points. 159 | That allows the calling app to revert to v1 */ 160 | LookupSymbol( api, sqlany_init_ex ); 161 | LookupSymbol( api, sqlany_fini_ex ); 162 | LookupSymbol( api, sqlany_new_connection_ex ); 163 | LookupSymbol( api, sqlany_make_connection_ex ); 164 | LookupSymbol( api, sqlany_client_version_ex ); 165 | LookupSymbolAndCheck( api, sqlany_cancel ); 166 | #endif 167 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_3 168 | /* We don't report an error if we don't find the v3 entry points. 169 | That allows the calling app to revert to v1 */ 170 | LookupSymbol( api, sqlany_register_callback ); 171 | #endif 172 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_4 173 | /* We don't report an error if we don't find the v4 entry points. 174 | That allows the calling app to revert to v1 */ 175 | LookupSymbol( api, sqlany_set_batch_size ); 176 | LookupSymbol( api, sqlany_set_param_bind_type ); 177 | LookupSymbol( api, sqlany_get_batch_size ); 178 | LookupSymbol( api, sqlany_set_rowset_size ); 179 | LookupSymbol( api, sqlany_get_rowset_size ); 180 | LookupSymbol( api, sqlany_set_column_bind_type ); 181 | LookupSymbol( api, sqlany_bind_column ); 182 | LookupSymbol( api, sqlany_clear_column_bindings ); 183 | LookupSymbol( api, sqlany_fetched_rows ); 184 | LookupSymbol( api, sqlany_set_rowset_pos ); 185 | #endif 186 | #if _SACAPI_VERSION+0 >= SQLANY_API_VERSION_5 187 | LookupSymbol( api, sqlany_reset_param_data ); 188 | LookupSymbol( api, sqlany_error_length ); 189 | #endif 190 | api->initialized = 1; 191 | return 1; 192 | } 193 | #undef LookupSymbolAndCheck 194 | 195 | void sqlany_finalize_interface( SQLAnywhereInterface * api ) 196 | /***********************************************************/ 197 | { 198 | if( !api->initialized ) { 199 | return; 200 | } 201 | unloadLibrary( api->dll_handle ); 202 | memset( api, 0, sizeof(*api)); 203 | } 204 | 205 | 206 | -------------------------------------------------------------------------------- /src/sqlanywhere.cpp: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | #include "nodever_cover.h" 5 | #include "sqlany_utils.h" 6 | 7 | #if !v010 8 | 9 | using namespace v8; 10 | 11 | SQLAnywhereInterface api; 12 | unsigned openConnections = 0; 13 | uv_mutex_t api_mutex; 14 | 15 | struct executeBaton { 16 | Persistent callback; 17 | bool err; 18 | std::string error_msg; 19 | bool callback_required; 20 | unsigned num_rows; 21 | 22 | Connection *obj; 23 | StmtObject *stmt_obj; 24 | 25 | bool free_stmt; 26 | std::string stmt; 27 | std::vector execData; 28 | std::vector params; 29 | 30 | std::vector colNames; 31 | int rows_affected; 32 | std::vector col_types; 33 | 34 | executeBaton() { 35 | err = false; 36 | callback_required = false; 37 | obj = NULL; 38 | stmt_obj = NULL; 39 | rows_affected = -1; 40 | free_stmt = false; 41 | num_rows = 0; 42 | } 43 | 44 | ~executeBaton() { 45 | obj = NULL; 46 | if( stmt_obj != NULL && free_stmt ) { 47 | delete stmt_obj; 48 | stmt_obj = NULL; 49 | } 50 | col_types.clear(); 51 | callback.Reset(); 52 | params.clear(); 53 | CLEAN_STRINGS( colNames ); 54 | CLEAN_PTRS( execData ); 55 | } 56 | }; 57 | 58 | static bool fillResult( executeBaton *baton, Persistent &ResultSet ) 59 | /*************************************************************************/ 60 | { 61 | Isolate *isolate = Isolate::GetCurrent(); 62 | HandleScope scope( isolate ); 63 | Local undef = Local::New( isolate, Undefined( isolate ) ); 64 | 65 | if( baton->err ) { 66 | callBack( &( baton->error_msg ), baton->callback, undef, 67 | baton->callback_required ); 68 | return false; 69 | } 70 | 71 | // We don't support wide fetches 72 | if( !getResultSet( ResultSet, baton->rows_affected, baton->colNames, 73 | baton->execData[0], baton->col_types ) ) { 74 | getErrorMsg( JS_ERR_RESULTSET, baton->error_msg ); 75 | callBack( &( baton->error_msg ), baton->callback, undef, 76 | baton->callback_required ); 77 | return false; 78 | } 79 | if( baton->callback_required ) { 80 | callBack( NULL, baton->callback, ResultSet, baton->callback_required ); 81 | } 82 | return true; 83 | } 84 | 85 | void executeWork( uv_work_t *req ) 86 | /********************************/ 87 | { 88 | int rc = 0; 89 | int sqlcode = 0; 90 | 91 | executeBaton *baton = static_cast(req->data); 92 | scoped_lock lock( baton->obj->conn_mutex ); 93 | 94 | if( baton->obj->conn == NULL ) { 95 | baton->err = true; 96 | getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); 97 | return; 98 | } 99 | 100 | a_sqlany_stmt *sqlany_stmt = NULL; 101 | if( baton->stmt_obj == NULL ) { 102 | baton->stmt_obj = new StmtObject(); 103 | baton->stmt_obj->connection = baton->obj; 104 | baton->obj->statements.push_back( baton->stmt_obj ); 105 | } else { 106 | sqlany_stmt = baton->stmt_obj->sqlany_stmt; 107 | } 108 | 109 | if( sqlany_stmt == NULL && baton->stmt.length() > 0 ) { 110 | sqlany_stmt = api.sqlany_prepare( baton->obj->conn, 111 | baton->stmt.c_str() ); 112 | if( sqlany_stmt == NULL ) { 113 | baton->err = true; 114 | getErrorMsg( baton->obj->conn, baton->error_msg ); 115 | return; 116 | } 117 | baton->stmt_obj->sqlany_stmt = sqlany_stmt; 118 | 119 | } else if( sqlany_stmt == NULL ) { 120 | baton->err = true; 121 | getErrorMsg( JS_ERR_INVALID_OBJECT, baton->error_msg ); 122 | return; 123 | } 124 | 125 | if( !api.sqlany_reset( sqlany_stmt ) ) { 126 | baton->err = true; 127 | getErrorMsg( baton->obj->conn, baton->error_msg ); 128 | return; 129 | } 130 | 131 | for( unsigned int i = 0; i < baton->params.size(); i++ ) { 132 | a_sqlany_bind_param param; 133 | 134 | if( !api.sqlany_describe_bind_param( sqlany_stmt, i, ¶m ) ) { 135 | baton->err = true; 136 | getErrorMsg( baton->obj->conn, baton->error_msg ); 137 | return; 138 | } 139 | 140 | memcpy( ¶m.value, &baton->params[i].value, sizeof( param.value ) ); 141 | 142 | if( !api.sqlany_bind_param( sqlany_stmt, i, ¶m ) ) { 143 | baton->err = true; 144 | getErrorMsg( baton->obj->conn, baton->error_msg ); 145 | return; 146 | } 147 | } 148 | 149 | if( baton->num_rows > 1 ) { 150 | api.sqlany_set_batch_size( sqlany_stmt, baton->num_rows ); 151 | } 152 | 153 | sacapi_bool success_execute = api.sqlany_execute( sqlany_stmt ); 154 | baton->execData[0]->clear(); 155 | 156 | if( !success_execute ) { 157 | baton->err = true; 158 | getErrorMsg( baton->obj->conn, baton->error_msg ); 159 | return; 160 | } 161 | 162 | rc = fetchResultSet( sqlany_stmt, baton->rows_affected, baton->colNames, 163 | baton->execData[0], baton->col_types ); 164 | 165 | if( !rc ) { 166 | sqlcode = getError( baton->obj->conn, NULL, 0 ); 167 | if( (sqlcode != 0) && (sqlcode != 100) ) { 168 | baton->err = true; 169 | getErrorMsg( baton->obj->conn, baton->error_msg ); 170 | return; 171 | } 172 | } 173 | } 174 | 175 | void executeAfter( uv_work_t *req ) 176 | /*********************************/ 177 | { 178 | Isolate *isolate = Isolate::GetCurrent(); 179 | HandleScope scope( isolate ); 180 | executeBaton *baton = static_cast( req->data ); 181 | Persistent ResultSet; 182 | fillResult( baton, ResultSet ); 183 | ResultSet.Reset(); 184 | 185 | delete baton; 186 | delete req; 187 | } 188 | 189 | NODE_API_FUNC( StmtObject::exec ) 190 | /*******************************/ 191 | { 192 | Isolate *isolate = args.GetIsolate(); 193 | HandleScope scope( isolate ); 194 | StmtObject *obj = ObjectWrap::Unwrap( args.This() ); 195 | int num_args = args.Length(); 196 | bool callback_required = false, bind_required = false; 197 | int cbfunc_arg = -1; 198 | Local undef = Local::New( isolate, Undefined( isolate ) ); 199 | 200 | if( num_args == 0 ) { 201 | 202 | } else if( num_args == 1 && args[0]->IsArray() ) { 203 | bind_required = true; 204 | 205 | } else if( num_args == 1 && args[0]->IsFunction() ) { 206 | callback_required = true; 207 | cbfunc_arg = 0; 208 | 209 | } else if( num_args == 2 && args[0]->IsArray() && args[1]->IsFunction() ) { 210 | callback_required = true; 211 | bind_required = true; 212 | cbfunc_arg = 1; 213 | 214 | } else { 215 | throwError( JS_ERR_INVALID_ARGUMENTS ); 216 | args.GetReturnValue().SetUndefined(); 217 | return; 218 | } 219 | 220 | if( obj == NULL || obj->connection == NULL || obj->connection->conn == NULL || 221 | obj->sqlany_stmt == NULL ) { 222 | std::string error_msg; 223 | getErrorMsg( JS_ERR_INVALID_OBJECT, error_msg ); 224 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 225 | args.GetReturnValue().SetUndefined(); 226 | return; 227 | } 228 | 229 | executeBaton *baton = new executeBaton(); 230 | baton->obj = obj->connection; 231 | baton->stmt_obj = obj; 232 | baton->free_stmt = false; 233 | baton->callback_required = callback_required; 234 | 235 | if( bind_required ) { 236 | if( !getBindParameters( baton->execData, isolate, args[0], baton->params, 237 | baton->num_rows ) ) { 238 | delete baton; 239 | std::string error_msg; 240 | getErrorMsg( JS_ERR_BINDING_PARAMETERS, error_msg ); 241 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 242 | args.GetReturnValue().SetUndefined(); 243 | return; 244 | } 245 | if( baton->num_rows > 1 && 246 | baton->obj->max_api_ver < SQLANY_API_VERSION_4 ) { 247 | // can't support wide inserts with older dbcapi 248 | delete baton; 249 | std::string error_msg; 250 | getErrorMsg( JS_ERR_NO_WIDE_STATEMENTS, error_msg ); 251 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 252 | args.GetReturnValue().SetUndefined(); 253 | return; 254 | } 255 | } else { 256 | baton->execData.push_back( new ExecuteData ); 257 | baton->num_rows = 1; 258 | } 259 | 260 | uv_work_t *req = new uv_work_t(); 261 | req->data = baton; 262 | 263 | if( callback_required ) { 264 | Local callback = Local::Cast(args[cbfunc_arg]); 265 | baton->callback.Reset( isolate, callback ); 266 | 267 | int status; 268 | status = uv_queue_work( uv_default_loop(), req, executeWork, 269 | (uv_after_work_cb)executeAfter ); 270 | assert(status == 0); 271 | 272 | args.GetReturnValue().SetUndefined(); 273 | return; 274 | } 275 | 276 | Persistent ResultSet; 277 | 278 | executeWork( req ); 279 | bool success = fillResult( baton, ResultSet ); 280 | delete baton; 281 | delete req; 282 | 283 | if( !success ) { 284 | args.GetReturnValue().SetUndefined(); 285 | return; 286 | } 287 | args.GetReturnValue().Set( ResultSet ); 288 | ResultSet.Reset(); 289 | } 290 | 291 | void getMoreResultsWork( uv_work_t *req ) 292 | /***************************************/ 293 | { 294 | int rc = 0; 295 | int sqlcode = 0; 296 | 297 | executeBaton *baton = static_cast(req->data); 298 | scoped_lock lock( baton->obj->conn_mutex ); 299 | 300 | if( baton->obj->conn == NULL ) { 301 | baton->err = true; 302 | getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); 303 | return; 304 | } 305 | 306 | a_sqlany_stmt *stmt = baton->stmt_obj->sqlany_stmt; 307 | 308 | if( stmt == NULL ) { 309 | baton->err = true; 310 | getErrorMsg( JS_ERR_INVALID_OBJECT, baton->error_msg ); 311 | return; 312 | } 313 | int retval = ( api.sqlany_get_next_result( stmt ) != 0 ); 314 | if( !retval ) { 315 | char buffer[SACAPI_ERROR_SIZE]; 316 | int rc; 317 | rc = api.sqlany_error( baton->obj->conn, buffer, sizeof(buffer) ); 318 | if( rc != 105 ) { 319 | // rc == 105 means "procedure has completed" - i.e. no more result 320 | // sets. Just return null, not an error 321 | baton->err = true; 322 | getErrorMsg( baton->obj->conn, baton->error_msg ); 323 | } 324 | return; 325 | } 326 | 327 | baton->execData[0]->clear(); 328 | rc = fetchResultSet( stmt, baton->rows_affected, baton->colNames, 329 | baton->execData[0], baton->col_types ); 330 | 331 | if( !rc ) { 332 | sqlcode = getError( baton->obj->conn, NULL, 0 ); 333 | if( (sqlcode != 0 ) && (sqlcode != 100) ) { 334 | baton->err = true; 335 | getErrorMsg( baton->obj->conn, baton->error_msg ); 336 | return; 337 | } 338 | } 339 | } 340 | 341 | void getMoreResultsAfter( uv_work_t *req ) 342 | /****************************************/ 343 | { 344 | Isolate *isolate = Isolate::GetCurrent(); 345 | HandleScope scope( isolate ); 346 | executeBaton *baton = static_cast( req->data ); 347 | Persistent ResultSet; 348 | fillResult( baton, ResultSet ); 349 | ResultSet.Reset(); 350 | 351 | delete baton; 352 | delete req; 353 | } 354 | 355 | NODE_API_FUNC( StmtObject::getMoreResults ) 356 | /*****************************************/ 357 | { 358 | Isolate *isolate = args.GetIsolate(); 359 | HandleScope scope( isolate ); 360 | StmtObject *obj = ObjectWrap::Unwrap( args.This() ); 361 | int num_args = args.Length(); 362 | bool callback_required = false; 363 | int cbfunc_arg = -1; 364 | Local undef = Local::New( isolate, Undefined( isolate ) ); 365 | 366 | if( num_args == 0 ) { 367 | 368 | } else if( num_args == 1 && args[0]->IsFunction() ) { 369 | callback_required = true; 370 | cbfunc_arg = 0; 371 | 372 | } else { 373 | throwError( JS_ERR_INVALID_ARGUMENTS ); 374 | args.GetReturnValue().SetUndefined(); 375 | return; 376 | } 377 | 378 | if( obj == NULL || obj->connection == NULL || obj->connection->conn == NULL || 379 | obj->sqlany_stmt == NULL ) { 380 | std::string error_msg; 381 | getErrorMsg( JS_ERR_INVALID_OBJECT, error_msg ); 382 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 383 | args.GetReturnValue().SetUndefined(); 384 | return; 385 | } 386 | 387 | executeBaton *baton = new executeBaton; 388 | baton->obj = obj->connection; 389 | baton->stmt_obj = obj; 390 | baton->free_stmt = false; 391 | baton->callback_required = callback_required; 392 | 393 | baton->execData.push_back( new ExecuteData ); 394 | baton->num_rows = 1; 395 | 396 | uv_work_t *req = new uv_work_t(); 397 | req->data = baton; 398 | 399 | if( callback_required ) { 400 | Local callback = Local::Cast(args[cbfunc_arg]); 401 | baton->callback.Reset( isolate, callback ); 402 | 403 | int status; 404 | status = uv_queue_work( uv_default_loop(), req, getMoreResultsWork, 405 | (uv_after_work_cb)getMoreResultsAfter ); 406 | assert(status == 0); 407 | 408 | args.GetReturnValue().SetUndefined(); 409 | return; 410 | } 411 | 412 | Persistent ResultSet; 413 | 414 | getMoreResultsWork( req ); 415 | bool success = fillResult( baton, ResultSet ); 416 | delete baton; 417 | delete req; 418 | 419 | if( !success ) { 420 | args.GetReturnValue().SetUndefined(); 421 | return; 422 | } 423 | args.GetReturnValue().Set( ResultSet ); 424 | ResultSet.Reset(); 425 | } 426 | 427 | NODE_API_FUNC( Connection::exec ) 428 | /*******************************/ 429 | { 430 | Isolate *isolate = args.GetIsolate(); 431 | Local context = isolate->GetCurrentContext(); 432 | HandleScope scope( isolate ); 433 | Local undef = Local::New( isolate, Undefined( isolate ) ); 434 | 435 | int num_args = args.Length(); 436 | bool callback_required = false, bind_required = false; 437 | int cbfunc_arg = 0; 438 | if( args[0]->IsString() ) { 439 | if( num_args == 1 ) { 440 | 441 | } else if( num_args == 2 && args[1]->IsArray() ) { 442 | bind_required = true; 443 | 444 | } else if( num_args == 2 && args[1]->IsFunction() ) { 445 | callback_required = true; 446 | cbfunc_arg = 1; 447 | 448 | } else if( num_args == 3 && args[1]->IsArray() && args[2]->IsFunction() ) { 449 | callback_required = true; 450 | bind_required = true; 451 | cbfunc_arg = 2; 452 | 453 | } else { 454 | throwError( JS_ERR_INVALID_ARGUMENTS ); 455 | args.GetReturnValue().SetUndefined(); 456 | return; 457 | } 458 | } else { 459 | throwError( JS_ERR_INVALID_ARGUMENTS ); 460 | args.GetReturnValue().SetUndefined(); 461 | return; 462 | } 463 | 464 | Connection *obj = ObjectWrap::Unwrap( args.This() ); 465 | 466 | if( obj == NULL || obj->conn == NULL ) { 467 | std::string error_msg; 468 | getErrorMsg( JS_ERR_INVALID_OBJECT, error_msg ); 469 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 470 | args.GetReturnValue().SetUndefined(); 471 | return; 472 | } 473 | 474 | #if NODE_MAJOR_VERSION >= 12 475 | String::Utf8Value param0( isolate, (args[0]->ToString(context)).ToLocalChecked() ); 476 | #else 477 | String::Utf8Value param0( (args[0]->ToString(context)).ToLocalChecked() ); 478 | #endif 479 | 480 | executeBaton *baton = new executeBaton(); 481 | baton->obj = obj; 482 | baton->callback_required = callback_required; 483 | baton->free_stmt = true; 484 | baton->stmt_obj = NULL; 485 | baton->stmt = std::string(*param0); 486 | 487 | if( bind_required ) { 488 | if( !getBindParameters( baton->execData, isolate, args[1], baton->params, 489 | baton->num_rows ) ) { 490 | delete baton; 491 | std::string error_msg; 492 | getErrorMsg( JS_ERR_BINDING_PARAMETERS, error_msg ); 493 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 494 | args.GetReturnValue().SetUndefined(); 495 | return; 496 | } 497 | if( baton->num_rows > 1 && 498 | baton->obj->max_api_ver < SQLANY_API_VERSION_4 ) { 499 | // can't support wide inserts with older dbcapi 500 | delete baton; 501 | std::string error_msg; 502 | getErrorMsg( JS_ERR_NO_WIDE_STATEMENTS, error_msg ); 503 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 504 | args.GetReturnValue().SetUndefined(); 505 | return; 506 | } 507 | } else { 508 | baton->execData.push_back( new ExecuteData ); 509 | baton->num_rows = 1; 510 | } 511 | 512 | uv_work_t *req = new uv_work_t(); 513 | req->data = baton; 514 | 515 | if( callback_required ) { 516 | Local callback = Local::Cast(args[cbfunc_arg]); 517 | baton->callback.Reset( isolate, callback ); 518 | int status; 519 | status = uv_queue_work( uv_default_loop(), req, executeWork, 520 | (uv_after_work_cb)executeAfter ); 521 | assert(status == 0); 522 | 523 | args.GetReturnValue().SetUndefined(); 524 | return; 525 | } 526 | 527 | Persistent ResultSet; 528 | 529 | executeWork( req ); 530 | bool success = fillResult( baton, ResultSet ); 531 | 532 | delete baton; 533 | delete req; 534 | 535 | if( !success ) { 536 | args.GetReturnValue().SetUndefined(); 537 | return; 538 | } 539 | Local local_result = Local::New( isolate, ResultSet ); 540 | args.GetReturnValue().Set( local_result ); 541 | ResultSet.Reset(); 542 | } 543 | 544 | struct prepareBaton { 545 | Persistent callback; 546 | bool err; 547 | std::string error_msg; 548 | bool callback_required; 549 | 550 | StmtObject *obj; 551 | std::string stmt; 552 | Persistent StmtObj; 553 | 554 | prepareBaton() { 555 | err = false; 556 | callback_required = false; 557 | obj = NULL; 558 | } 559 | 560 | ~prepareBaton() { 561 | obj = NULL; 562 | callback.Reset(); 563 | StmtObj.Reset(); 564 | } 565 | }; 566 | 567 | void Connection::prepareWork( uv_work_t *req ) 568 | /*********************************************/ 569 | { 570 | prepareBaton *baton = static_cast(req->data); 571 | if( baton->obj == NULL || baton->obj->connection == NULL || 572 | baton->obj->connection->conn == NULL ) { 573 | baton->err = true; 574 | getErrorMsg( JS_ERR_INVALID_OBJECT, baton->error_msg ); 575 | return; 576 | } 577 | 578 | scoped_lock lock( baton->obj->connection->conn_mutex ); 579 | 580 | baton->obj->sqlany_stmt = api.sqlany_prepare( baton->obj->connection->conn, 581 | baton->stmt.c_str() ); 582 | if( baton->obj->sqlany_stmt == NULL ) { 583 | baton->err = true; 584 | getErrorMsg( baton->obj->connection->conn, baton->error_msg ); 585 | return; 586 | } 587 | } 588 | 589 | void Connection::prepareAfter( uv_work_t *req ) 590 | /**********************************************/ 591 | { 592 | Isolate *isolate = Isolate::GetCurrent(); 593 | HandleScope scope( isolate ); 594 | prepareBaton *baton = static_cast(req->data); 595 | Local undef = Local::New( isolate, Undefined( isolate ) ); 596 | 597 | if( baton->err ) { 598 | callBack( &( baton->error_msg ), baton->callback, undef, 599 | baton->callback_required ); 600 | delete baton; 601 | delete req; 602 | return; 603 | } 604 | 605 | if( baton->callback_required ) { 606 | Local StmtObj = Local::New( isolate, baton->StmtObj ); 607 | callBack( NULL, baton->callback, StmtObj, baton->callback_required ); 608 | baton->StmtObj.Reset(); 609 | } 610 | 611 | delete baton; 612 | delete req; 613 | } 614 | 615 | NODE_API_FUNC( Connection::prepare ) 616 | /**********************************/ 617 | { 618 | Isolate *isolate = args.GetIsolate(); 619 | Local context = isolate->GetCurrentContext(); 620 | HandleScope scope( isolate ); 621 | bool callback_required = false; 622 | int cbfunc_arg = -1; 623 | Local undef = Local::New( isolate, Undefined( isolate ) ); 624 | 625 | if( args.Length() == 1 && args[0]->IsString() ) { 626 | // do nothing 627 | } else if( args.Length() == 2 && args[0]->IsString() && args[1]->IsFunction() ) { 628 | callback_required = true; 629 | cbfunc_arg = 1; 630 | } else { 631 | throwError( JS_ERR_INVALID_ARGUMENTS ); 632 | args.GetReturnValue().SetUndefined(); 633 | return; 634 | } 635 | 636 | Connection *db = ObjectWrap::Unwrap( args.This() ); 637 | 638 | if( db == NULL || db->conn == NULL ) { 639 | std::string error_msg; 640 | getErrorMsg( JS_ERR_NOT_CONNECTED, error_msg ); 641 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 642 | args.GetReturnValue().SetUndefined(); 643 | return; 644 | } 645 | 646 | Persistent p_stmt; 647 | StmtObject::CreateNewInstance( args, p_stmt ); 648 | Local l_stmt = Local::New( isolate, p_stmt ); 649 | StmtObject *obj = ObjectWrap::Unwrap( l_stmt ); 650 | obj->connection = db; 651 | { 652 | scoped_lock lock( db->conn_mutex ); 653 | db->statements.push_back( obj ); 654 | } 655 | 656 | if( obj == NULL ) { 657 | std::string error_msg; 658 | getErrorMsg( JS_ERR_GENERAL_ERROR, error_msg ); 659 | callBack( &( error_msg ), args[cbfunc_arg], undef, callback_required ); 660 | args.GetReturnValue().SetUndefined(); 661 | p_stmt.Reset(); 662 | return; 663 | } 664 | 665 | #if NODE_MAJOR_VERSION >= 12 666 | String::Utf8Value param0( isolate, (args[0]->ToString(context)).ToLocalChecked() ); 667 | #else 668 | String::Utf8Value param0( (args[0]->ToString(context)).ToLocalChecked() ); 669 | #endif 670 | 671 | prepareBaton *baton = new prepareBaton(); 672 | baton->obj = obj; 673 | baton->callback_required = callback_required; 674 | baton->stmt = std::string(*param0); 675 | 676 | uv_work_t *req = new uv_work_t(); 677 | req->data = baton; 678 | 679 | if( callback_required ) { 680 | Local callback = Local::Cast(args[cbfunc_arg]); 681 | baton->callback.Reset( isolate, callback ); 682 | baton->StmtObj.Reset( isolate, p_stmt ); 683 | int status; 684 | status = uv_queue_work( uv_default_loop(), req, prepareWork, 685 | (uv_after_work_cb)prepareAfter ); 686 | assert(status == 0); 687 | 688 | args.GetReturnValue().SetUndefined(); 689 | p_stmt.Reset(); 690 | return; 691 | } 692 | 693 | prepareWork( req ); 694 | bool err = baton->err; 695 | prepareAfter( req ); 696 | 697 | if( err ) { 698 | args.GetReturnValue().SetUndefined(); 699 | return; 700 | } 701 | args.GetReturnValue().Set( p_stmt ); 702 | p_stmt.Reset(); 703 | } 704 | 705 | 706 | // Connect and disconnect 707 | // Connect Function 708 | struct connectBaton { 709 | Persistent callback; 710 | bool err; 711 | std::string error_msg; 712 | bool callback_required; 713 | 714 | Connection *obj; 715 | bool sqlca_connection; 716 | std::string conn_string; 717 | void *sqlca; 718 | 719 | connectBaton() { 720 | obj = NULL; 721 | sqlca = NULL; 722 | sqlca_connection = false; 723 | err = false; 724 | callback_required = false; 725 | } 726 | 727 | ~connectBaton() { 728 | obj = NULL; 729 | sqlca = NULL; 730 | callback.Reset(); 731 | } 732 | 733 | }; 734 | 735 | void Connection::connectWork( uv_work_t *req ) 736 | /*********************************************/ 737 | { 738 | connectBaton *baton = static_cast(req->data); 739 | scoped_lock api_lock( api_mutex ); 740 | scoped_lock lock( baton->obj->conn_mutex ); 741 | 742 | if( baton->obj->conn != NULL ) { 743 | baton->err = true; 744 | getErrorMsg( JS_ERR_CONNECTION_ALREADY_EXISTS, baton->error_msg ); 745 | return; 746 | } 747 | 748 | if( api.initialized == false) { 749 | 750 | if( !sqlany_initialize_interface( &api, NULL ) ) { 751 | baton->err = true; 752 | getErrorMsg( JS_ERR_INITIALIZING_DBCAPI, baton->error_msg ); 753 | return; 754 | } 755 | 756 | if( !api.sqlany_init( "Node.js", SQLANY_API_VERSION_4, 757 | &(baton->obj->max_api_ver) )) { 758 | // As long as the version is >= 2, we're OK. We just have to disable 759 | // wide inserts 760 | if( baton->obj->max_api_ver >= SQLANY_API_VERSION_2 ) { 761 | if( !api.sqlany_init( "Node.js", baton->obj->max_api_ver, 762 | &(baton->obj->max_api_ver) )) { 763 | baton->err = true; 764 | getErrorMsg( JS_ERR_INITIALIZING_DBCAPI, baton->error_msg ); 765 | return; 766 | } 767 | } else { 768 | baton->err = true; 769 | getErrorMsg( JS_ERR_INITIALIZING_DBCAPI, baton->error_msg ); 770 | return; 771 | } 772 | } 773 | } 774 | 775 | if( !baton->sqlca_connection ) { 776 | baton->obj->conn = api.sqlany_new_connection(); 777 | if( !api.sqlany_connect( baton->obj->conn, baton->conn_string.c_str() ) ) { 778 | getErrorMsg( baton->obj->conn, baton->error_msg ); 779 | baton->err = true; 780 | api.sqlany_free_connection( baton->obj->conn ); 781 | baton->obj->conn = NULL; 782 | cleanAPI(); 783 | return; 784 | } 785 | 786 | } else { 787 | baton->obj->conn = api.sqlany_make_connection( baton->sqlca ); 788 | if( baton->obj->conn == NULL ) { 789 | getErrorMsg( baton->obj->conn, baton->error_msg ); 790 | cleanAPI(); 791 | return; 792 | } 793 | } 794 | 795 | baton->obj->sqlca_connection = baton->sqlca_connection; 796 | openConnections++; 797 | } 798 | 799 | void Connection::connectAfter( uv_work_t *req ) 800 | /**********************************************/ 801 | { 802 | Isolate *isolate = Isolate::GetCurrent(); 803 | HandleScope scope( isolate ); 804 | connectBaton *baton = static_cast(req->data); 805 | Local undef = Local::New( isolate, Undefined( isolate ) ); 806 | 807 | if( baton->err ) { 808 | callBack( &( baton->error_msg ), baton->callback, undef, 809 | baton->callback_required ); 810 | delete baton; 811 | delete req; 812 | return; 813 | } 814 | 815 | callBack( NULL, baton->callback, undef, baton->callback_required ); 816 | 817 | delete baton; 818 | delete req; 819 | } 820 | 821 | NODE_API_FUNC( Connection::connect ) 822 | /**********************************/ 823 | { 824 | Isolate *isolate = args.GetIsolate(); 825 | Local context = isolate->GetCurrentContext(); 826 | HandleScope scope( isolate ); 827 | int num_args = args.Length(); 828 | Connection *obj; 829 | obj = ObjectWrap::Unwrap( args.This() ); 830 | bool sqlca_connection = false; 831 | bool callback_required = false; 832 | int cbfunc_arg = -1; 833 | bool arg_is_string = true; 834 | bool arg_is_object = false; 835 | 836 | if( num_args == 0 ) { 837 | arg_is_string = false; 838 | 839 | } else if( num_args == 1 && args[0]->IsFunction() ) { 840 | callback_required = true; 841 | cbfunc_arg = 0; 842 | arg_is_string = false; 843 | 844 | } else if( num_args == 1 && args[0]->IsNumber() ){ 845 | sqlca_connection = true; 846 | 847 | } else if( num_args == 1 && args[0]->IsString() ) { 848 | sqlca_connection = false; 849 | 850 | } else if( num_args == 1 && args[0]->IsObject() ) { 851 | sqlca_connection = false; 852 | arg_is_string = false; 853 | arg_is_object = true; 854 | 855 | } else if( num_args == 2 && args[0]->IsNumber() && args[1]->IsFunction() ) { 856 | sqlca_connection = true; 857 | callback_required = true; 858 | cbfunc_arg = 1; 859 | 860 | } else if( num_args == 2 && args[0]->IsString() && args[1]->IsFunction() ) { 861 | sqlca_connection = false; 862 | callback_required = true; 863 | cbfunc_arg = 1; 864 | 865 | } else if( num_args == 2 && args[0]->IsObject() && args[1]->IsFunction() ) { 866 | sqlca_connection = false; 867 | callback_required = true; 868 | cbfunc_arg = 1; 869 | arg_is_string = false; 870 | arg_is_object = true; 871 | 872 | } else if( num_args > 1 ) { 873 | throwError( JS_ERR_INVALID_ARGUMENTS ); 874 | args.GetReturnValue().SetUndefined(); 875 | return; 876 | } 877 | 878 | connectBaton *baton = new connectBaton(); 879 | baton->obj = obj; 880 | baton->callback_required = callback_required; 881 | 882 | baton->sqlca_connection = sqlca_connection; 883 | 884 | if( sqlca_connection ) { 885 | baton->sqlca = (void *)(long)(args[0]->NumberValue(context)).FromJust(); 886 | 887 | } else { 888 | Local localArg = Local::New( isolate, obj->_arg ); 889 | if( localArg->Length() > 0 ) { 890 | #if NODE_MAJOR_VERSION >= 12 891 | String::Utf8Value param0( isolate, localArg ); 892 | #else 893 | String::Utf8Value param0( localArg ); 894 | #endif 895 | baton->conn_string = std::string(*param0); 896 | } else { 897 | baton->conn_string = std::string(); 898 | } 899 | if( arg_is_string ) { 900 | #if NODE_MAJOR_VERSION >= 12 901 | String::Utf8Value param0( isolate, (args[0]->ToString(context)).ToLocalChecked()); 902 | #else 903 | String::Utf8Value param0( (args[0]->ToString(context)).ToLocalChecked() ); 904 | #endif 905 | baton->conn_string.append( ";" ); 906 | baton->conn_string.append(*param0); 907 | } else if( arg_is_object ) { 908 | Persistent arg_string; 909 | HashToString( isolate, args[0]->ToObject(isolate), arg_string ); 910 | Local local_arg_string = 911 | Local::New( isolate, arg_string ); 912 | #if NODE_MAJOR_VERSION >= 12 913 | String::Utf8Value param0( isolate, local_arg_string ); 914 | #else 915 | String::Utf8Value param0( local_arg_string ); 916 | #endif 917 | baton->conn_string.append( ";" ); 918 | baton->conn_string.append(*param0); 919 | arg_string.Reset(); 920 | } 921 | baton->conn_string.append( ";CHARSET='UTF-8'" ); 922 | } 923 | 924 | uv_work_t *req = new uv_work_t(); 925 | req->data = baton; 926 | 927 | if( callback_required ) { 928 | Local callback = Local::Cast(args[cbfunc_arg]); 929 | baton->callback.Reset( isolate, callback ); 930 | 931 | int status; 932 | status = uv_queue_work( uv_default_loop(), req, connectWork, 933 | (uv_after_work_cb)connectAfter ); 934 | assert(status == 0); 935 | args.GetReturnValue().SetUndefined(); 936 | return; 937 | } 938 | 939 | connectWork( req ); 940 | connectAfter( req ); 941 | args.GetReturnValue().SetUndefined(); 942 | return; 943 | } 944 | 945 | // Disconnect Function 946 | void Connection::disconnectWork( uv_work_t *req ) 947 | /************************************************/ 948 | { 949 | noParamBaton *baton = static_cast(req->data); 950 | scoped_lock api_lock(api_mutex ); 951 | scoped_lock lock( baton->obj->conn_mutex ); 952 | 953 | if( baton->obj->conn == NULL ) { 954 | getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); 955 | return; 956 | } 957 | 958 | baton->obj->cleanupStmts(); 959 | 960 | if( !baton->obj->sqlca_connection ) { 961 | api.sqlany_disconnect( baton->obj->conn ); 962 | } 963 | // Must free the connection object or there will be a memory leak 964 | api.sqlany_free_connection( baton->obj->conn ); 965 | baton->obj->conn = NULL; 966 | openConnections--; 967 | 968 | if( openConnections <= 0 ) { 969 | openConnections = 0; 970 | cleanAPI(); 971 | } 972 | 973 | return; 974 | } 975 | 976 | NODE_API_FUNC( Connection::disconnect ) 977 | /*************************************/ 978 | { 979 | Isolate *isolate = args.GetIsolate(); 980 | HandleScope scope( isolate ); 981 | int num_args = args.Length(); 982 | bool callback_required = false; 983 | int cbfunc_arg = -1; 984 | 985 | if( num_args == 0 ) { 986 | 987 | } else if( num_args == 1 && args[0]->IsFunction() ) { 988 | callback_required = true; 989 | cbfunc_arg = 0; 990 | 991 | } else { 992 | throwError( JS_ERR_INVALID_ARGUMENTS ); 993 | args.GetReturnValue().SetUndefined(); 994 | return; 995 | } 996 | 997 | Connection *obj = ObjectWrap::Unwrap( args.This() ); 998 | noParamBaton *baton = new noParamBaton(); 999 | 1000 | baton->callback_required = callback_required; 1001 | baton->obj = obj; 1002 | 1003 | uv_work_t *req = new uv_work_t(); 1004 | req->data = baton; 1005 | 1006 | if( callback_required ) { 1007 | Local callback = Local::Cast(args[cbfunc_arg]); 1008 | baton->callback.Reset( isolate, callback ); 1009 | 1010 | int status; 1011 | status = uv_queue_work( uv_default_loop(), req, disconnectWork, 1012 | (uv_after_work_cb)noParamAfter ); 1013 | assert(status == 0); 1014 | 1015 | args.GetReturnValue().SetUndefined(); 1016 | return; 1017 | } 1018 | 1019 | disconnectWork( req ); 1020 | noParamAfter( req ); 1021 | args.GetReturnValue().SetUndefined(); 1022 | return; 1023 | } 1024 | 1025 | void Connection::commitWork( uv_work_t *req ) 1026 | /********************************************/ 1027 | { 1028 | noParamBaton *baton = static_cast(req->data); 1029 | scoped_lock lock( baton->obj->conn_mutex ); 1030 | 1031 | if( baton->obj->conn == NULL ) { 1032 | baton->err = true; 1033 | getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); 1034 | return; 1035 | } 1036 | 1037 | if( !api.sqlany_commit( baton->obj->conn ) ) { 1038 | baton->err = true; 1039 | getErrorMsg( baton->obj->conn, baton->error_msg ); 1040 | return; 1041 | } 1042 | } 1043 | 1044 | NODE_API_FUNC( Connection::commit ) 1045 | /*********************************/ 1046 | { 1047 | Isolate *isolate = args.GetIsolate(); 1048 | HandleScope scope( isolate ); 1049 | int num_args = args.Length(); 1050 | bool callback_required = false; 1051 | int cbfunc_arg = -1; 1052 | 1053 | if( num_args == 0 ) { 1054 | 1055 | } else if( num_args == 1 && args[0]->IsFunction() ) { 1056 | callback_required = true; 1057 | cbfunc_arg = 0; 1058 | 1059 | } else { 1060 | throwError( JS_ERR_INVALID_ARGUMENTS ); 1061 | args.GetReturnValue().SetUndefined(); 1062 | return; 1063 | } 1064 | 1065 | Connection *obj = ObjectWrap::Unwrap( args.This() ); 1066 | 1067 | noParamBaton *baton = new noParamBaton(); 1068 | baton->obj = obj; 1069 | baton->callback_required = callback_required; 1070 | 1071 | uv_work_t *req = new uv_work_t(); 1072 | req->data = baton; 1073 | 1074 | if( callback_required ) { 1075 | Local callback = Local::Cast(args[cbfunc_arg]); 1076 | baton->callback.Reset( isolate, callback ); 1077 | 1078 | int status; 1079 | status = uv_queue_work( uv_default_loop(), req, commitWork, 1080 | (uv_after_work_cb)noParamAfter ); 1081 | assert(status == 0); 1082 | 1083 | args.GetReturnValue().SetUndefined(); 1084 | return; 1085 | } 1086 | 1087 | commitWork( req ); 1088 | noParamAfter( req ); 1089 | args.GetReturnValue().SetUndefined(); 1090 | return; 1091 | } 1092 | 1093 | void Connection::rollbackWork( uv_work_t *req ) 1094 | /**********************************************/ 1095 | { 1096 | noParamBaton *baton = static_cast(req->data); 1097 | scoped_lock lock( baton->obj->conn_mutex ); 1098 | 1099 | if( baton->obj->conn == NULL ) { 1100 | baton->err = true; 1101 | getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); 1102 | return; 1103 | } 1104 | 1105 | if( !api.sqlany_rollback( baton->obj->conn ) ) { 1106 | baton->err = true; 1107 | getErrorMsg( baton->obj->conn, baton->error_msg ); 1108 | return; 1109 | } 1110 | } 1111 | 1112 | NODE_API_FUNC( Connection::rollback ) 1113 | /***********************************/ 1114 | { 1115 | Isolate *isolate = args.GetIsolate(); 1116 | HandleScope scope( isolate ); 1117 | int num_args = args.Length(); 1118 | bool callback_required = false; 1119 | int cbfunc_arg = -1; 1120 | 1121 | if( num_args == 0 ) { 1122 | 1123 | } else if( num_args == 1 && args[0]->IsFunction() ) { 1124 | callback_required = true; 1125 | cbfunc_arg = 0; 1126 | 1127 | } else { 1128 | throwError( JS_ERR_INVALID_ARGUMENTS ); 1129 | args.GetReturnValue().SetUndefined(); 1130 | return; 1131 | } 1132 | 1133 | Connection *obj = ObjectWrap::Unwrap( args.This() ); 1134 | 1135 | noParamBaton *baton = new noParamBaton(); 1136 | baton->obj = obj; 1137 | baton->callback_required = callback_required; 1138 | 1139 | uv_work_t *req = new uv_work_t(); 1140 | req->data = baton; 1141 | 1142 | if( callback_required ) { 1143 | Local callback = Local::Cast(args[cbfunc_arg]); 1144 | baton->callback.Reset( isolate, callback ); 1145 | 1146 | int status; 1147 | status = uv_queue_work( uv_default_loop(), req, rollbackWork, 1148 | (uv_after_work_cb)noParamAfter ); 1149 | assert(status == 0); 1150 | 1151 | args.GetReturnValue().SetUndefined(); 1152 | return; 1153 | } 1154 | 1155 | rollbackWork( req ); 1156 | noParamAfter( req ); 1157 | args.GetReturnValue().SetUndefined(); 1158 | return; 1159 | } 1160 | 1161 | NODE_API_FUNC( Connection::connected ) 1162 | /************************************/ 1163 | { 1164 | Connection *obj = ObjectWrap::Unwrap( args.This() ); 1165 | args.GetReturnValue().Set( obj->conn == NULL ? false : true ); 1166 | } 1167 | 1168 | struct dropBaton { 1169 | Persistent callback; 1170 | bool err; 1171 | std::string error_msg; 1172 | bool callback_required; 1173 | 1174 | StmtObject *obj; 1175 | 1176 | dropBaton() { 1177 | err = false; 1178 | callback_required = false; 1179 | obj = NULL; 1180 | } 1181 | 1182 | ~dropBaton() { 1183 | obj = NULL; 1184 | callback.Reset(); 1185 | } 1186 | }; 1187 | 1188 | void StmtObject::dropAfter( uv_work_t *req ) 1189 | /*******************************************/ 1190 | { 1191 | Isolate *isolate = Isolate::GetCurrent(); 1192 | HandleScope scope( isolate ); 1193 | dropBaton *baton = static_cast(req->data); 1194 | Local undef = Local::New( isolate, Undefined( isolate ) ); 1195 | 1196 | if( baton->err ) { 1197 | callBack( &( baton->error_msg ), baton->callback, undef, 1198 | baton->callback_required ); 1199 | delete baton; 1200 | delete req; 1201 | return; 1202 | } 1203 | 1204 | callBack( NULL, baton->callback, undef, baton->callback_required ); 1205 | 1206 | delete baton; 1207 | delete req; 1208 | } 1209 | 1210 | void StmtObject::dropWork( uv_work_t *req ) 1211 | /******************************************/ 1212 | { 1213 | dropBaton *baton = static_cast(req->data); 1214 | scoped_lock connlock( baton->obj->connection->conn_mutex ); 1215 | 1216 | baton->obj->cleanup(); 1217 | baton->obj->removeConnection(); 1218 | } 1219 | 1220 | NODE_API_FUNC( StmtObject::drop ) 1221 | /*******************************/ 1222 | { 1223 | Isolate *isolate = args.GetIsolate(); 1224 | HandleScope scope( isolate ); 1225 | int num_args = args.Length(); 1226 | bool callback_required = false; 1227 | int cbfunc_arg = -1; 1228 | 1229 | if( num_args == 0 ) { 1230 | 1231 | } else if( num_args == 1 && args[0]->IsFunction() ) { 1232 | callback_required = true; 1233 | cbfunc_arg = 0; 1234 | 1235 | } else { 1236 | throwError( JS_ERR_INVALID_ARGUMENTS ); 1237 | args.GetReturnValue().SetUndefined(); 1238 | return; 1239 | } 1240 | 1241 | StmtObject *obj = ObjectWrap::Unwrap( args.This() ); 1242 | 1243 | dropBaton *baton = new dropBaton(); 1244 | baton->obj = obj; 1245 | baton->callback_required = callback_required; 1246 | 1247 | uv_work_t *req = new uv_work_t(); 1248 | req->data = baton; 1249 | 1250 | if( callback_required ) { 1251 | Local callback = Local::Cast(args[cbfunc_arg]); 1252 | baton->callback.Reset( isolate, callback ); 1253 | 1254 | int status; 1255 | status = uv_queue_work( uv_default_loop(), req, dropWork, 1256 | (uv_after_work_cb)dropAfter ); 1257 | assert(status == 0); 1258 | 1259 | args.GetReturnValue().SetUndefined(); 1260 | return; 1261 | } 1262 | 1263 | dropWork( req ); 1264 | dropAfter( req ); 1265 | args.GetReturnValue().SetUndefined(); 1266 | return; 1267 | } 1268 | 1269 | void init( Local exports ) 1270 | /********************************/ 1271 | { 1272 | uv_mutex_init(&api_mutex); 1273 | #if v012 1274 | Isolate *isolate = Isolate::GetCurrent(); 1275 | #else 1276 | Isolate *isolate = exports->GetIsolate(); 1277 | #endif 1278 | StmtObject::Init( isolate ); 1279 | Connection::Init( isolate ); 1280 | NODE_SET_METHOD( exports, "createConnection", Connection::NewInstance ); 1281 | } 1282 | 1283 | NODE_MODULE( DRIVER_NAME, init ) 1284 | 1285 | #endif // !v010 1286 | -------------------------------------------------------------------------------- /src/utils.cpp: -------------------------------------------------------------------------------- 1 | // *************************************************************************** 2 | // Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. 3 | // *************************************************************************** 4 | #include "nodever_cover.h" 5 | #include "sqlany_utils.h" 6 | #include "nan.h" 7 | 8 | #if !v010 9 | 10 | using namespace v8; 11 | using namespace node; 12 | 13 | int getError( a_sqlany_connection *conn, char *str, size_t len ) 14 | /**************************************************************/ 15 | { 16 | int sqlcode; 17 | sqlcode = api.sqlany_error( conn, str, len ); 18 | return sqlcode; 19 | } 20 | 21 | void getErrorMsg( int code, std::string &str ) 22 | /********************************************/ 23 | { 24 | std::ostringstream message; 25 | message << "Code: "; 26 | message << code; 27 | message << " Msg: "; 28 | 29 | switch( code ) { 30 | case JS_ERR_INVALID_OBJECT: 31 | message << "Invalid Object"; 32 | break; 33 | case JS_ERR_INVALID_ARGUMENTS: 34 | message << "Invalid Arguments"; 35 | break; 36 | case JS_ERR_CONNECTION_ALREADY_EXISTS: 37 | message << "Already Connected"; 38 | break; 39 | case JS_ERR_INITIALIZING_DBCAPI: 40 | message << "Can't initialize DBCAPI"; 41 | break; 42 | case JS_ERR_NOT_CONNECTED: 43 | message << "No Connection Available"; 44 | break; 45 | case JS_ERR_BINDING_PARAMETERS: 46 | message << "Can not bind parameter(s)"; 47 | break; 48 | case JS_ERR_GENERAL_ERROR: 49 | message << "An error occurred"; 50 | break; 51 | case JS_ERR_RESULTSET: 52 | message << "Error making result set Object"; 53 | break; 54 | case JS_ERR_NO_WIDE_STATEMENTS: 55 | message << "The DBCAPI library must be upgraded to support wide statements"; 56 | break; 57 | default: 58 | message << "Unknown Error"; 59 | } 60 | str = message.str(); 61 | 62 | } 63 | 64 | void getErrorMsg( a_sqlany_connection *conn, std::string &str ) 65 | /*************************************************************/ 66 | { 67 | char buffer[SACAPI_ERROR_SIZE]; 68 | int sqlcode; 69 | sqlcode = getError( conn, buffer, sizeof(buffer) ); 70 | std::ostringstream message; 71 | message << "Code: "; 72 | message << sqlcode; 73 | message << " Msg: "; 74 | message << buffer; 75 | str = message.str(); 76 | } 77 | 78 | void throwError( a_sqlany_connection *conn ) 79 | /******************************************/ 80 | { 81 | Isolate *isolate = Isolate::GetCurrent(); 82 | std::string message; 83 | getErrorMsg( conn, message ); 84 | isolate->ThrowException( 85 | Exception::Error( String::NewFromUtf8( isolate, message.c_str() ) ) ); 86 | } 87 | 88 | void throwError( int code ) 89 | /*************************/ 90 | { 91 | Isolate *isolate = Isolate::GetCurrent(); 92 | std::string message; 93 | getErrorMsg( code, message ); 94 | isolate->ThrowException( 95 | Exception::Error( String::NewFromUtf8( isolate, message.c_str() ) ) ); 96 | } 97 | 98 | void callBack( std::string * str, 99 | Persistent & callback, 100 | Local & Result, 101 | bool callback_required ) 102 | /*********************************************************/ 103 | { 104 | Isolate *isolate = Isolate::GetCurrent(); 105 | HandleScope scope( isolate ); 106 | Local local_callback = Local::New( isolate, callback ); 107 | 108 | // If string is NULL, then there is no error 109 | if( callback_required ) { 110 | if( !local_callback->IsFunction() ) { 111 | throwError( JS_ERR_INVALID_ARGUMENTS ); 112 | return; 113 | } 114 | 115 | Local Err; 116 | if( str == NULL ) { 117 | Err = Local::New( isolate, Undefined( isolate ) ); 118 | 119 | } else { 120 | Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ); 121 | } 122 | 123 | int argc = 2; 124 | Local argv[2] = { Err, Result }; 125 | 126 | #if v012 127 | TryCatch try_catch; 128 | #else 129 | TryCatch try_catch( isolate ); 130 | #endif 131 | Nan::Callback *cb = new Nan::Callback( local_callback ); 132 | Nan::Call( *cb, argc, argv ); 133 | if( try_catch.HasCaught()) { 134 | node::FatalException( isolate, try_catch ); 135 | } 136 | } else { 137 | if( str != NULL ) { 138 | isolate->ThrowException( 139 | Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ) ); 140 | } 141 | } 142 | } 143 | 144 | void callBack( std::string * str, 145 | Persistent & callback, 146 | Persistent & Result, 147 | bool callback_required ) 148 | /*********************************************************/ 149 | { 150 | Isolate *isolate = Isolate::GetCurrent(); 151 | HandleScope scope( isolate ); 152 | Local local_result = Local::New( isolate, Result ); 153 | 154 | callBack( str, callback, local_result, callback_required ); 155 | } 156 | 157 | void callBack( std::string * str, 158 | const Local & arg, 159 | Local & Result, 160 | bool callback_required ) 161 | /*********************************************************/ 162 | { 163 | Isolate *isolate = Isolate::GetCurrent(); 164 | HandleScope scope( isolate ); 165 | 166 | // If string is NULL, then there is no error 167 | if( callback_required ) { 168 | if( !arg->IsFunction() ) { 169 | throwError( JS_ERR_INVALID_ARGUMENTS ); 170 | return; 171 | } 172 | Local callback = Local::Cast(arg); 173 | 174 | Local Err; 175 | if( str == NULL ) { 176 | Err = Local::New( isolate, Undefined( isolate ) ); 177 | 178 | } else { 179 | Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ); 180 | } 181 | 182 | int argc = 2; 183 | Local argv[2] = { Err, Result }; 184 | 185 | #if v012 186 | TryCatch try_catch; 187 | #else 188 | TryCatch try_catch( isolate ); 189 | #endif 190 | Nan::Callback *cb = new Nan::Callback( callback ); 191 | Nan::Call( *cb, argc, argv ); 192 | if( try_catch.HasCaught()) { 193 | node::FatalException( isolate, try_catch ); 194 | } 195 | 196 | } else { 197 | if( str != NULL ) { 198 | isolate->ThrowException( 199 | Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ) ); 200 | } 201 | } 202 | } 203 | 204 | static bool getWideBindParameters( std::vector &execData, 205 | Isolate * isolate, 206 | Local arg, 207 | std::vector & params, 208 | unsigned &num_rows ) 209 | /*********************************************************************************/ 210 | { 211 | Local context = isolate->GetCurrentContext(); 212 | Local rows = Local::Cast( arg ); 213 | num_rows = rows->Length(); 214 | 215 | Local row0 = Local::Cast( rows->Get(0) ); 216 | unsigned num_cols = row0->Length(); 217 | unsigned c; 218 | 219 | if( num_cols == 0 ) { 220 | // if an empty array was passed in, we still need ExecuteData 221 | ExecuteData *ex = new ExecuteData; 222 | execData.push_back( ex ); 223 | return true; 224 | } 225 | 226 | // Make sure that each array in the list has the same number and types 227 | // of values 228 | for( unsigned int r = 1; r < num_rows; r++ ) { 229 | Local row = Local::Cast( rows->Get(r) ); 230 | for( c = 0; c < num_cols; c++ ) { 231 | Local val0 = row0->Get(c); 232 | Local val = row->Get(c); 233 | 234 | if( ( val0->IsInt32() || val0->IsNumber() ) && 235 | ( !val->IsInt32() && !val->IsNumber() && !val->IsNull() ) ) { 236 | return false; 237 | } 238 | if( val0->IsString() && 239 | !val->IsString() && !val->IsNull() ) { 240 | return false; 241 | } 242 | if( Buffer::HasInstance( val0 ) && 243 | !Buffer::HasInstance( val ) && !val->IsNull() ) { 244 | return false; 245 | } 246 | } 247 | } 248 | 249 | for( c = 0; c < num_cols; c++ ) { 250 | a_sqlany_bind_param param; 251 | memset( ¶m, 0, sizeof( param ) ); 252 | 253 | ExecuteData *ex = new ExecuteData; 254 | execData.push_back( ex ); 255 | 256 | double * param_double = new double[num_rows]; 257 | ex->addNum( param_double ); 258 | char ** char_arr = new char *[num_rows]; 259 | size_t * len = new size_t[num_rows]; 260 | ex->addStrings( char_arr, len ); 261 | sacapi_bool * is_null = new sacapi_bool[num_rows]; 262 | ex->addNull( is_null ); 263 | param.value.is_null = is_null; 264 | param.value.is_address = false; 265 | 266 | if( row0->Get(c)->IsInt32() || row0->Get(c)->IsNumber() ) { 267 | param.value.type = A_DOUBLE; 268 | param.value.buffer = (char *)( param_double ); 269 | 270 | } else if( row0->Get(c)->IsString() ) { 271 | param.value.type = A_STRING; 272 | param.value.buffer = (char *)char_arr; 273 | param.value.length = len; 274 | param.value.is_address = true; 275 | 276 | } else if( Buffer::HasInstance( row0->Get(c) ) ) { 277 | param.value.type = A_BINARY; 278 | param.value.buffer = (char *)char_arr; 279 | param.value.length = len; 280 | param.value.is_address = true; 281 | 282 | } else if( row0->Get(c)->IsNull() ) { 283 | 284 | } else{ 285 | return false; 286 | } 287 | 288 | for( unsigned int r = 0; r < num_rows; r++ ) { 289 | Local bind_params = Local::Cast( rows->Get(r) ); 290 | 291 | is_null[r] = false; 292 | if( bind_params->Get(c)->IsInt32() || bind_params->Get(c)->IsNumber() ) { 293 | param_double[r] = bind_params->Get(c)->NumberValue(context).FromJust(); 294 | 295 | } else if( bind_params->Get(c)->IsString() ) { 296 | #if NODE_MAJOR_VERSION >= 12 297 | String::Utf8Value paramValue( isolate, (bind_params->Get(c)->ToString(context)).ToLocalChecked() ); 298 | #else 299 | String::Utf8Value paramValue( (bind_params->Get(c)->ToString(context)).ToLocalChecked() ); 300 | #endif 301 | const char* param_string = (*paramValue); 302 | len[r] = (size_t)paramValue.length(); 303 | char *param_char = new char[len[r] + 1]; 304 | char_arr[r] = param_char; 305 | memcpy( param_char, param_string, len[r] + 1 ); 306 | 307 | } else if( Buffer::HasInstance( bind_params->Get(c) ) ) { 308 | len[r] = Buffer::Length( bind_params->Get(c) ); 309 | char *param_char = new char[len[r]]; 310 | char_arr[r] = param_char; 311 | memcpy( param_char, Buffer::Data( bind_params->Get(c) ), len[r] ); 312 | 313 | } else if( bind_params->Get(c)->IsNull() ) { 314 | is_null[r] = true; 315 | } 316 | } 317 | 318 | params.push_back( param ); 319 | } 320 | 321 | return true; 322 | } 323 | 324 | bool getBindParameters( std::vector &execData, 325 | Isolate * isolate, 326 | Local arg, 327 | std::vector & params, 328 | unsigned &num_rows ) 329 | /*************************************************************************/ 330 | { 331 | Local context = isolate->GetCurrentContext(); 332 | Local bind_params = Local::Cast( arg ); 333 | 334 | if( bind_params->Length() == 0 ) { 335 | // if an empty array was passed in, we still need ExecuteData 336 | ExecuteData *ex = new ExecuteData; 337 | execData.push_back( ex ); 338 | return true; 339 | } 340 | 341 | if( bind_params->Get(0)->IsArray() ) { 342 | return getWideBindParameters( execData, isolate, arg, params, num_rows ); 343 | } 344 | num_rows = 1; 345 | 346 | ExecuteData *ex = new ExecuteData; 347 | execData.push_back( ex ); 348 | 349 | for( unsigned int i = 0; i < bind_params->Length(); i++ ) { 350 | a_sqlany_bind_param param; 351 | memset( ¶m, 0, sizeof( param ) ); 352 | 353 | if( bind_params->Get(i)->IsInt32() ) { 354 | int *param_int = new int; 355 | *param_int = bind_params->Get(i)->Int32Value(context).FromJust(); 356 | ex->addInt( param_int ); 357 | param.value.buffer = (char *)( param_int ); 358 | param.value.type = A_VAL32; 359 | 360 | } else if( bind_params->Get(i)->IsNumber() ) { 361 | double *param_double = new double; 362 | *param_double = bind_params->Get(i)->NumberValue(context).FromJust(); // Remove Round off Error 363 | ex->addNum( param_double ); 364 | param.value.buffer = (char *)( param_double ); 365 | param.value.type = A_DOUBLE; 366 | 367 | } else if( bind_params->Get(i)->IsString() ) { 368 | #if NODE_MAJOR_VERSION >= 12 369 | String::Utf8Value paramValue( isolate, (bind_params->Get(i)->ToString(context)).ToLocalChecked() ); 370 | #else 371 | String::Utf8Value paramValue( (bind_params->Get(i)->ToString(context)).ToLocalChecked() ); 372 | #endif 373 | const char* param_string = (*paramValue); 374 | size_t *len = new size_t; 375 | *len = (size_t)paramValue.length(); 376 | 377 | char **char_arr = new char *; 378 | char *param_char = new char[*len + 1]; 379 | *char_arr = param_char; 380 | 381 | memcpy( param_char, param_string, ( *len ) + 1 ); 382 | ex->addStrings( char_arr, len ); 383 | 384 | param.value.type = A_STRING; 385 | param.value.buffer = param_char; 386 | param.value.length = len; 387 | param.value.buffer_size = *len + 1; 388 | 389 | } else if( Buffer::HasInstance( bind_params->Get(i) ) ) { 390 | size_t *len = new size_t; 391 | *len = Buffer::Length( bind_params->Get(i) ); 392 | char **char_arr = new char *; 393 | char *param_char = new char[*len]; 394 | *char_arr = param_char; 395 | 396 | memcpy( param_char, Buffer::Data( bind_params->Get(i) ), *len ); 397 | ex->addStrings( char_arr, len ); 398 | 399 | param.value.type = A_BINARY; 400 | param.value.buffer = param_char; 401 | param.value.length = len; 402 | param.value.buffer_size = sizeof( param_char ); 403 | 404 | } else if( bind_params->Get(i)->IsNull() ) { 405 | param.value.type = A_STRING; 406 | sacapi_bool *is_null = new sacapi_bool; 407 | param.value.is_null = is_null; 408 | ex->addNull( is_null ); 409 | is_null[0] = true; 410 | 411 | } else{ 412 | return false; 413 | } 414 | 415 | params.push_back( param ); 416 | } 417 | 418 | return true; 419 | } 420 | 421 | bool getResultSet( Persistent & Result, 422 | int & rows_affected, 423 | std::vector & colNames, 424 | ExecuteData * execData, 425 | std::vector & col_types ) 426 | /*****************************************************************/ 427 | { 428 | Isolate *isolate = Isolate::GetCurrent(); 429 | HandleScope scope( isolate ); 430 | int num_rows = 0; 431 | size_t num_cols = colNames.size(); 432 | 433 | if( rows_affected >= 0 ) { 434 | Result.Reset( isolate, Integer::New( isolate, rows_affected ) ); 435 | return true; 436 | } 437 | 438 | if( num_cols > 0 ) { 439 | size_t count = 0; 440 | size_t count_int = 0, count_num = 0, count_string = 0; 441 | Local ResultSet = Array::New( isolate ); 442 | while( count_int < execData->intSize() || 443 | count_num < execData->numSize() || 444 | count_string < execData->stringSize() ) { 445 | Local curr_row = Object::New( isolate ); 446 | num_rows++; 447 | for( size_t i = 0; i < num_cols; i++ ) { 448 | switch( col_types[count] ) { 449 | case A_INVALID_TYPE: 450 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 451 | Null( isolate ) ); 452 | break; 453 | 454 | case A_VAL32: 455 | case A_VAL16: 456 | case A_UVAL16: 457 | case A_VAL8: 458 | case A_UVAL8: 459 | if( execData->intIsNull( count_int ) ) { 460 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 461 | Null( isolate ) ); 462 | } else { 463 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 464 | Integer::New( isolate, execData->getInt( count_int ) ) ); 465 | } 466 | count_int++; 467 | break; 468 | 469 | case A_UVAL32: 470 | case A_UVAL64: 471 | case A_VAL64: 472 | case A_DOUBLE: 473 | if( execData->numIsNull( count_num ) ) { 474 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 475 | Null( isolate ) ); 476 | } else { 477 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 478 | Number::New( isolate, execData->getNum( count_num ) ) ); 479 | } 480 | count_num++; 481 | break; 482 | 483 | case A_BINARY: 484 | if( execData->stringIsNull( count_string ) ) { 485 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 486 | Null( isolate ) ); 487 | } else { 488 | #if v012 489 | Local buf = node::Buffer::New( 490 | isolate, execData->getString( count_string ), 491 | execData->getLen( count_string ) ); 492 | curr_row->Set( String::NewFromUtf8( isolate, 493 | colNames[i] ), 494 | buf ); 495 | #else 496 | MaybeLocal mbuf = node::Buffer::Copy( 497 | isolate, execData->getString( count_string ), 498 | execData->getLen( count_string ) ); 499 | Local buf = mbuf.ToLocalChecked(); 500 | #endif 501 | curr_row->Set( String::NewFromUtf8( isolate, 502 | colNames[i] ), 503 | buf ); 504 | } 505 | count_string++; 506 | break; 507 | 508 | case A_STRING: 509 | if( execData->stringIsNull( count_string ) ) { 510 | curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), 511 | Null( isolate ) ); 512 | } else { 513 | curr_row->Set( String::NewFromUtf8( isolate, 514 | colNames[i] ), 515 | #if v012 516 | String::NewFromUtf8( isolate, 517 | execData->getString( count_string ), 518 | String::NewStringType::kNormalString, 519 | (int)execData->getLen( count_string ) ) 520 | #else 521 | String::NewFromUtf8( isolate, 522 | execData->getString( count_string ), 523 | NewStringType::kNormal, 524 | (int)execData->getLen( count_string ) ).ToLocalChecked() 525 | #endif 526 | ); 527 | } 528 | count_string++; 529 | break; 530 | 531 | default: 532 | return false; 533 | } 534 | count++; 535 | } 536 | ResultSet->Set( num_rows - 1, curr_row ); 537 | } 538 | Result.Reset( isolate, ResultSet ); 539 | } else { 540 | Result.Reset( isolate, Local::New( isolate, 541 | Undefined( isolate ) ) ); 542 | } 543 | 544 | return true; 545 | } 546 | 547 | bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, 548 | int & rows_affected, 549 | std::vector & colNames, 550 | ExecuteData * execData, 551 | std::vector & col_types ) 552 | /*****************************************************************/ 553 | { 554 | 555 | a_sqlany_data_value value; 556 | int num_cols = 0; 557 | 558 | rows_affected = api.sqlany_affected_rows( sqlany_stmt ); 559 | num_cols = api.sqlany_num_cols( sqlany_stmt ); 560 | 561 | 562 | if( rows_affected > 0 && num_cols < 1 ) { 563 | return true; 564 | } 565 | 566 | rows_affected = -1; 567 | if( num_cols > 0 ) { 568 | 569 | for( int i = 0; i < num_cols; i++ ) { 570 | a_sqlany_column_info info; 571 | api.sqlany_get_column_info( sqlany_stmt, i, &info ); 572 | size_t size = strlen( info.name ) + 1; 573 | char *name = new char[ size ]; 574 | memcpy( name, info.name, size ); 575 | colNames.push_back( name ); 576 | } 577 | 578 | int count_string = 0, count_num = 0, count_int = 0; 579 | while( true ) { 580 | 581 | if( !api.sqlany_fetch_next( sqlany_stmt ) ) { 582 | return false; 583 | } 584 | 585 | for( int i = 0; i < num_cols; i++ ) { 586 | 587 | if( !api.sqlany_get_column( sqlany_stmt, i, &value ) ) { 588 | return false; 589 | } 590 | 591 | if( *(value.is_null) ) { 592 | col_types.push_back( A_INVALID_TYPE ); 593 | continue; 594 | } 595 | 596 | switch( value.type ) { 597 | case A_BINARY: 598 | { 599 | size_t *size = new size_t; 600 | *size = *(value.length); 601 | char *val = new char[ *size ]; 602 | memcpy( val, value.buffer, *size ); 603 | execData->addString( val, size ); 604 | count_string++; 605 | break; 606 | } 607 | 608 | case A_STRING: 609 | { 610 | size_t *size = new size_t; 611 | *size = (size_t)( (int)*(value.length) ); 612 | char *val = new char[ *size ]; 613 | memcpy( val, (char *)value.buffer, *size ); 614 | execData->addString( val, size ); 615 | count_string++; 616 | break; 617 | } 618 | 619 | case A_VAL64: 620 | { 621 | double *val = new double; 622 | *val = (double)*(long long *)value.buffer; 623 | execData->addNum( val ); 624 | count_num++; 625 | break; 626 | } 627 | 628 | case A_UVAL64: 629 | { 630 | double *val = new double; 631 | *val = (double)*(unsigned long long *)value.buffer; 632 | execData->addNum( val ); 633 | count_num++; 634 | break; 635 | } 636 | 637 | case A_VAL32: 638 | { 639 | int *val = new int; 640 | *val = *(int*)value.buffer; 641 | execData->addInt( val ); 642 | count_int++; 643 | break; 644 | } 645 | 646 | case A_UVAL32: 647 | { 648 | double *val = new double; 649 | *val = (double)*(unsigned int*)value.buffer; 650 | execData->addNum( val ); 651 | count_num++; 652 | break; 653 | } 654 | 655 | case A_VAL16: 656 | { 657 | int *val = new int; 658 | *val = (int)*(short*)value.buffer; 659 | execData->addInt( val ); 660 | count_int++; 661 | break; 662 | } 663 | 664 | case A_UVAL16: 665 | { 666 | int *val = new int; 667 | *val = (int)*(unsigned short*)value.buffer; 668 | execData->addInt( val ); 669 | count_int++; 670 | break; 671 | } 672 | 673 | case A_VAL8: 674 | { 675 | int *val = new int; 676 | *val = (int)*(char *)value.buffer; 677 | execData->addInt( val ); 678 | count_int++; 679 | break; 680 | } 681 | 682 | case A_UVAL8: 683 | { 684 | int *val = new int; 685 | *val = (int)*(unsigned char *)value.buffer; 686 | execData->addInt( val ); 687 | count_int++; 688 | break; 689 | } 690 | 691 | case A_DOUBLE: 692 | { 693 | double *val = new double; 694 | *val = (double)*(double *)value.buffer; 695 | execData->addNum( val ); 696 | count_num++; 697 | break; 698 | } 699 | 700 | default: 701 | return false; 702 | } 703 | col_types.push_back( value.type ); 704 | } 705 | } 706 | } 707 | 708 | return true; 709 | } 710 | 711 | bool cleanAPI() 712 | /*************/ 713 | { 714 | if( openConnections == 0 ) { 715 | if( api.initialized ) { 716 | api.sqlany_fini(); 717 | sqlany_finalize_interface( &api ); 718 | return true; 719 | } 720 | } 721 | return false; 722 | } 723 | 724 | // Generic Baton and Callback (After) Function 725 | // Use this if the function does not have any return values and 726 | // Does not take any parameters. 727 | // Create custom Baton and Callback (After) functions otherwise 728 | 729 | void Connection::noParamAfter( uv_work_t *req ) 730 | /*********************************************/ 731 | { 732 | Isolate *isolate = Isolate::GetCurrent(); 733 | HandleScope scope(isolate); 734 | noParamBaton *baton = static_cast(req->data); 735 | Local undef = Local::New( isolate, Undefined( isolate ) ); 736 | 737 | if( baton->err ) { 738 | callBack( &( baton->error_msg ), baton->callback, undef, 739 | baton->callback_required ); 740 | return; 741 | } 742 | 743 | callBack( NULL, baton->callback, undef, baton->callback_required ); 744 | 745 | delete baton; 746 | delete req; 747 | } 748 | 749 | 750 | // Stmt Object Functions 751 | StmtObject::StmtObject() 752 | /**********************/ 753 | { 754 | connection = NULL; 755 | sqlany_stmt = NULL; 756 | } 757 | 758 | StmtObject::~StmtObject() 759 | /***********************/ 760 | { 761 | uv_mutex_t *mutex = NULL; 762 | if( connection != NULL ) { 763 | mutex = &connection->conn_mutex; 764 | uv_mutex_lock( mutex ); 765 | } 766 | 767 | cleanup(); 768 | removeConnection(); 769 | 770 | if( mutex != NULL ) { 771 | uv_mutex_unlock( mutex ); 772 | } 773 | } 774 | 775 | Persistent StmtObject::constructor; 776 | 777 | void StmtObject::Init( Isolate *isolate ) 778 | /***************************************/ 779 | { 780 | HandleScope scope(isolate); 781 | // Prepare constructor template 782 | Local tpl = FunctionTemplate::New( isolate, New ); 783 | tpl->SetClassName( String::NewFromUtf8( isolate, "StmtObject" ) ); 784 | tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); 785 | 786 | // Prototype 787 | NODE_SET_PROTOTYPE_METHOD( tpl, "exec", exec ); 788 | NODE_SET_PROTOTYPE_METHOD( tpl, "drop", drop ); 789 | NODE_SET_PROTOTYPE_METHOD( tpl, "getMoreResults", getMoreResults ); 790 | Local context = isolate->GetCurrentContext(); 791 | constructor.Reset( isolate, tpl->GetFunction( context ).ToLocalChecked() ); 792 | } 793 | 794 | void StmtObject::New( const FunctionCallbackInfo &args ) 795 | /*************************************************************/ 796 | { 797 | StmtObject* obj = new StmtObject(); 798 | 799 | obj->Wrap( args.This() ); 800 | args.GetReturnValue().Set( args.This() ); 801 | } 802 | 803 | void StmtObject::NewInstance( const FunctionCallbackInfo &args ) 804 | /*********************************************************************/ 805 | { 806 | Persistent obj; 807 | CreateNewInstance( args, obj ); 808 | args.GetReturnValue().Set( obj ); 809 | } 810 | 811 | void StmtObject::CreateNewInstance( const FunctionCallbackInfo & args, 812 | Persistent & obj ) 813 | /***************************************************************************/ 814 | { 815 | Isolate *isolate = args.GetIsolate(); 816 | HandleScope scope(isolate); 817 | const unsigned argc = 1; 818 | Local argv[argc] = { args[0] }; 819 | Localcons = Local::New( isolate, constructor ); 820 | #if NODE_MAJOR_VERSION >= 10 821 | Local env = isolate->GetCurrentContext(); 822 | MaybeLocal mlObj = cons->NewInstance( env, argc, argv ); 823 | Local instance = mlObj.ToLocalChecked(); 824 | 825 | obj.Reset( isolate, instance ); 826 | #else 827 | obj.Reset( isolate, cons->NewInstance( argc, argv ) ); 828 | #endif 829 | } 830 | 831 | void StmtObject::cleanup( void ) 832 | /******************************/ 833 | { 834 | if( sqlany_stmt != NULL ) { 835 | api.sqlany_free_stmt( sqlany_stmt ); 836 | sqlany_stmt = NULL; 837 | } 838 | } 839 | 840 | void StmtObject::removeConnection( void ) 841 | /***************************************/ 842 | { 843 | if( connection != NULL ) { 844 | connection->removeStmt( this ); 845 | connection = NULL; 846 | } 847 | } 848 | 849 | // Connection Functions 850 | 851 | void HashToString( Isolate *isolate, Local obj, Persistent &ret ) 852 | /*************************************************************/ 853 | { 854 | Local context = isolate->GetCurrentContext(); 855 | HandleScope scope(isolate); 856 | Local props = (obj->GetOwnPropertyNames(context)).ToLocalChecked(); 857 | int length = props->Length(); 858 | std::string params = ""; 859 | bool first = true; 860 | for( int i = 0; i < length; i++ ) { 861 | Local key = props->Get(i).As(); 862 | Local val = obj->Get(key).As(); 863 | #if NODE_MAJOR_VERSION >= 12 864 | String::Utf8Value key_utf8( isolate, key ); 865 | String::Utf8Value val_utf8( isolate, val ); 866 | #else 867 | String::Utf8Value key_utf8( key ); 868 | String::Utf8Value val_utf8( val ); 869 | #endif 870 | if( !first ) { 871 | params += ";"; 872 | } 873 | first = false; 874 | params += std::string(*key_utf8); 875 | params += "="; 876 | params += std::string(*val_utf8); 877 | } 878 | ret.Reset( isolate, String::NewFromUtf8( isolate, params.c_str() ) ); 879 | } 880 | 881 | #if 0 882 | // Handy function for determining what type an object is. 883 | static void CheckArgType( Local &obj ) 884 | /*******************************************/ 885 | { 886 | static const char *type = NULL; 887 | if( obj->IsArray() ) { 888 | type = "Array"; 889 | } else if( obj->IsBoolean() ) { 890 | type = "Boolean"; 891 | } else if( obj->IsBooleanObject() ) { 892 | type = "BooleanObject"; 893 | } else if( obj->IsDate() ) { 894 | type = "Date"; 895 | } else if( obj->IsExternal() ) { 896 | type = "External"; 897 | } else if( obj->IsFunction() ) { 898 | type = "Function"; 899 | } else if( obj->IsInt32() ) { 900 | type = "Int32"; 901 | } else if( obj->IsNativeError() ) { 902 | type = "NativeError"; 903 | } else if( obj->IsNull() ) { 904 | type = "Null"; 905 | } else if( obj->IsNumber() ) { 906 | type = "Number"; 907 | } else if( obj->IsNumberObject() ) { 908 | type = "NumberObject"; 909 | } else if( obj->IsObject() ) { 910 | type = "Object"; 911 | } else if( obj->IsRegExp() ) { 912 | type = "RegExp"; 913 | } else if( obj->IsString() ) { 914 | type = "String"; 915 | } else if( obj->IsStringObject() ) { 916 | type = "StringObject"; 917 | } else if( obj->IsUint32() ) { 918 | type = "Uint32"; 919 | } else if( obj->IsUndefined() ) { 920 | type = "Undefined"; 921 | } else { 922 | type = "Unknown"; 923 | } 924 | } 925 | #endif 926 | 927 | Connection::Connection( const FunctionCallbackInfo &args ) 928 | /***************************************************************/ 929 | { 930 | Isolate *isolate = args.GetIsolate(); 931 | HandleScope scope( isolate ); 932 | uv_mutex_init(&conn_mutex); 933 | conn = NULL; 934 | 935 | if( args.Length() > 1 ) { 936 | throwError( JS_ERR_INVALID_ARGUMENTS ); 937 | return; 938 | } 939 | 940 | if( args.Length() == 1 ) { 941 | //CheckArgType( args[0] ); 942 | if( args[0]->IsString() ) { 943 | Local str = args[0]->ToString(isolate); 944 | #if NODE_MAJOR_VERSION >= 12 945 | int string_len = str->Utf8Length(isolate); 946 | char *buf = new char[string_len + 1]; 947 | str->WriteUtf8(isolate, buf); 948 | #else 949 | int string_len = str->Utf8Length(); 950 | char *buf = new char[string_len+1]; 951 | str->WriteUtf8( buf ); 952 | #endif 953 | _arg.Reset( isolate, String::NewFromUtf8( isolate, buf ) ); 954 | delete [] buf; 955 | } else if( args[0]->IsObject() ) { 956 | HashToString( isolate, args[0]->ToObject(isolate), _arg ); 957 | } else if( !args[0]->IsUndefined() && !args[0]->IsNull() ) { 958 | throwError( JS_ERR_INVALID_ARGUMENTS ); 959 | } else { 960 | _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ) ); 961 | } 962 | } else { 963 | _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ) ); 964 | } 965 | } 966 | 967 | 968 | Connection::~Connection() 969 | /***********************/ 970 | { 971 | scoped_lock api_lock( api_mutex ); 972 | scoped_lock lock( conn_mutex ); 973 | 974 | _arg.Reset(); 975 | cleanupStmts(); 976 | if( conn != NULL ) { 977 | api.sqlany_disconnect( conn ); 978 | api.sqlany_free_connection( conn ); 979 | conn = NULL; 980 | openConnections--; 981 | } 982 | 983 | cleanAPI(); 984 | }; 985 | 986 | void Connection::cleanupStmts( void ) 987 | /***********************************/ 988 | { 989 | std::vector::iterator findit; 990 | for( findit = statements.begin(); findit != statements.end(); findit++ ) { 991 | StmtObject *s = reinterpret_cast(*findit); 992 | s->cleanup(); 993 | } 994 | } 995 | 996 | void Connection::removeStmt( class StmtObject *stmt ) 997 | /***************************************************/ 998 | { 999 | // caller must get mutex 1000 | std::vector::iterator findit; 1001 | for( findit = statements.begin(); findit != statements.end(); findit++ ) { 1002 | if( *findit == reinterpret_cast(stmt) ) { 1003 | statements.erase( findit ); 1004 | return; 1005 | } 1006 | } 1007 | } 1008 | 1009 | Persistent Connection::constructor; 1010 | 1011 | void Connection::Init( Isolate *isolate ) 1012 | /***************************************/ 1013 | { 1014 | HandleScope scope( isolate ); 1015 | // Prepare constructor template 1016 | Local tpl = FunctionTemplate::New( isolate, New ); 1017 | tpl->SetClassName( String::NewFromUtf8( isolate, "Connection" ) ); 1018 | tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); 1019 | 1020 | // Prototype 1021 | NODE_SET_PROTOTYPE_METHOD( tpl, "exec", exec ); 1022 | NODE_SET_PROTOTYPE_METHOD( tpl, "prepare", prepare ); 1023 | NODE_SET_PROTOTYPE_METHOD( tpl, "connect", connect ); 1024 | NODE_SET_PROTOTYPE_METHOD( tpl, "disconnect", disconnect ); 1025 | NODE_SET_PROTOTYPE_METHOD( tpl, "close", disconnect ); 1026 | NODE_SET_PROTOTYPE_METHOD( tpl, "commit", commit ); 1027 | NODE_SET_PROTOTYPE_METHOD( tpl, "rollback", rollback ); 1028 | NODE_SET_PROTOTYPE_METHOD( tpl, "connected", connected ); 1029 | 1030 | Local context = isolate->GetCurrentContext(); 1031 | constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked()); 1032 | } 1033 | 1034 | void Connection::New( const FunctionCallbackInfo &args ) 1035 | /*************************************************************/ 1036 | { 1037 | Isolate *isolate = args.GetIsolate(); 1038 | HandleScope scope( isolate ); 1039 | 1040 | if( args.IsConstructCall() ) { 1041 | Connection *obj = new Connection( args ); 1042 | obj->Wrap( args.This() ); 1043 | args.GetReturnValue().Set( args.This() ); 1044 | } else { 1045 | const int argc = 1; 1046 | Local argv[argc] = { args[0] }; 1047 | Local cons = Local::New( isolate, constructor ); 1048 | #if NODE_MAJOR_VERSION >= 10 1049 | Local env = isolate->GetCurrentContext(); 1050 | MaybeLocal mlObj = cons->NewInstance( env, argc, argv ); 1051 | const Local obj = mlObj.ToLocalChecked(); 1052 | args.GetReturnValue().Set( obj ); 1053 | #else 1054 | args.GetReturnValue().Set( cons->NewInstance( argc, argv ) ); 1055 | #endif 1056 | } 1057 | } 1058 | 1059 | void Connection::NewInstance( const FunctionCallbackInfo &args ) 1060 | /*********************************************************************/ 1061 | { 1062 | Isolate *isolate = args.GetIsolate(); 1063 | HandleScope scope( isolate ); 1064 | const unsigned argc = 1; 1065 | Local argv[argc] = { args[0] }; 1066 | 1067 | Local cons = Local::New( isolate, constructor ); 1068 | #if NODE_MAJOR_VERSION >= 10 1069 | Local env = isolate->GetCurrentContext(); 1070 | MaybeLocal mlObj = cons->NewInstance( env, argc, argv ); 1071 | Local instance = mlObj.ToLocalChecked(); 1072 | #else 1073 | Local instance = cons->NewInstance( argc, argv ); 1074 | #endif 1075 | args.GetReturnValue().Set( instance ); 1076 | } 1077 | 1078 | #endif // !v010 1079 | --------------------------------------------------------------------------------