Source: vertx-sql-client-js/prepared_query.js

/*
 * Copyright 2014 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/** @module vertx-sql-client-js/prepared_query */
var utils = require('vertx-js/util/utils');
var RowStream = require('vertx-sql-client-js/row_stream');
var Row = require('vertx-sql-client-js/row');
var RowSet = require('vertx-sql-client-js/row_set');
var Cursor = require('vertx-sql-client-js/cursor');
var Tuple = require('vertx-sql-client-js/tuple');
var Promise = require('vertx-js/promise');

var io = Packages.io;
var JsonObject = io.vertx.core.json.JsonObject;
var JsonArray = io.vertx.core.json.JsonArray;
var JPreparedQuery = Java.type('io.vertx.sqlclient.PreparedQuery');

/**
 A prepared query.

 @class
*/
var PreparedQuery = function(j_val) {

  var j_preparedQuery = j_val;
  var that = this;

  var __super_execute = this.execute;
  var __super_execute = this.execute;
  var __super_cursor = this.cursor;
  var __super_cursor = this.cursor;
  var __super_createStream = this.createStream;
  var __super_batch = this.batch;
  var __super_close = this.close;
  var __super_close = this.close;
  /**
   Create a cursor with the provided <code>arguments</code>.

   @public
   @param args {Tuple} the list of arguments 
   @param handler {function} 
   @return {PreparedQuery} the query
   */
  this.execute =  function() {
    var __args = arguments;
    if (__args.length === 1 && typeof __args[0] === 'function') {
      j_preparedQuery["execute(io.vertx.core.Handler)"](function(ar) {
        if (ar.succeeded()) {
          __args[0](utils.convReturnVertxGen(RowSet, ar.result()), null);
        } else {
          __args[0](null, ar.cause());
        }
      }) ;
      return that;
    } else if (__args.length === 0) {
      var __prom = Promise.promise();
      var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
      j_preparedQuery["execute(io.vertx.core.Handler)"](function(ar) {
        if (ar.succeeded()) {
          __prom_completer_handler(utils.convReturnVertxGen(RowSet, ar.result()), null);
        } else {
          __prom_completer_handler(null, ar.cause());
        }
      });
      return __prom.future();
    } else if (__args.length === 2 && typeof __args[0] === 'object' && __args[0]._jdel && typeof __args[1] === 'function') {
      j_preparedQuery["execute(io.vertx.sqlclient.Tuple,io.vertx.core.Handler)"](__args[0]._jdel, function(ar) {
        if (ar.succeeded()) {
          __args[1](utils.convReturnVertxGen(RowSet, ar.result()), null);
        } else {
          __args[1](null, ar.cause());
        }
      }) ;
      return that;
    } else if (__args.length === 1 && typeof __args[0] === 'object' && __args[0]._jdel) {
      var __prom = Promise.promise();
      var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
      j_preparedQuery["execute(io.vertx.sqlclient.Tuple,io.vertx.core.Handler)"](__args[0]._jdel, function(ar) {
        if (ar.succeeded()) {
          __prom_completer_handler(utils.convReturnVertxGen(RowSet, ar.result()), null);
        } else {
          __prom_completer_handler(null, ar.cause());
        }
      });
      return __prom.future();
    } else if (typeof __super_execute != 'undefined') {
      return __super_execute.apply(this, __args);
    }
    else throw new TypeError('function invoked with invalid arguments');
  };

  /**
   Create a cursor with the provided <code>arguments</code>.

   @public
   @param args {Tuple} the list of arguments 
   @return {Cursor} the query
   */
  this.cursor =  function() {
    var __args = arguments;
    if (__args.length === 0) {
      return utils.convReturnVertxGen(Cursor, j_preparedQuery["cursor()"]()) ;
    } else if (__args.length === 1 && typeof __args[0] === 'object' && __args[0]._jdel) {
      return utils.convReturnVertxGen(Cursor, j_preparedQuery["cursor(io.vertx.sqlclient.Tuple)"](__args[0]._jdel)) ;
    } else if (typeof __super_cursor != 'undefined') {
      return __super_cursor.apply(this, __args);
    }
    else throw new TypeError('function invoked with invalid arguments');
  };

  /**
   Execute the prepared query with a cursor and createStream the result. The createStream opens a cursor
   with a <code>fetch</code> size to fetch the results.
   <p/>
   Note: this requires to be in a transaction, since cursors require it.

   @public
   @param fetch {number} the cursor fetch size 
   @param args {Tuple} the prepared query arguments 
   @return {RowStream} the createStream
   */
  this.createStream =  function(fetch, args) {
    var __args = arguments;
    if (__args.length === 2 && typeof __args[0] ==='number' && typeof __args[1] === 'object' && __args[1]._jdel) {
      return utils.convReturnVertxGen(RowStream, j_preparedQuery["createStream(int,io.vertx.sqlclient.Tuple)"](fetch, args._jdel), Row._jtype) ;
    } else if (typeof __super_createStream != 'undefined') {
      return __super_createStream.apply(this, __args);
    }
    else throw new TypeError('function invoked with invalid arguments');
  };

  /**
   Execute a batch.

   @public
   @param argsList {Array.<Tuple>} the list of tuple for the batch 
   @param handler {function} 
   @return {PreparedQuery} the createBatch
   */
  this.batch =  function(argsList, handler) {
    var __args = arguments;
    if (__args.length === 2 && typeof __args[0] === 'object' && __args[0] instanceof Array && typeof __args[1] === 'function') {
      j_preparedQuery["batch(java.util.List,io.vertx.core.Handler)"](utils.convParamListVertxGen(argsList), function(ar) {
        if (ar.succeeded()) {
          handler(utils.convReturnVertxGen(RowSet, ar.result()), null);
        } else {
          handler(null, ar.cause());
        }
      }) ;
      return that;
    } else if (__args.length === 1 && typeof __args[0] === 'object' && __args[0] instanceof Array) {
      var __prom = Promise.promise();
      var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
      j_preparedQuery["batch(java.util.List,io.vertx.core.Handler)"](utils.convParamListVertxGen(argsList), function(ar) {
        if (ar.succeeded()) {
          __prom_completer_handler(utils.convReturnVertxGen(RowSet, ar.result()), null);
        } else {
          __prom_completer_handler(null, ar.cause());
        }
      });
      return __prom.future();
    } else if (typeof __super_batch != 'undefined') {
      return __super_batch.apply(this, __args);
    }
    else throw new TypeError('function invoked with invalid arguments');
  };

  /**
   Like {@link PreparedQuery#close} but notifies the <code>completionHandler</code> when it's closed.

   @public
   @param completionHandler {function} 
   */
  this.close =  function() {
    var __args = arguments;
    if (__args.length === 0) {
      j_preparedQuery["close()"]();
    } else if (__args.length === 1 && typeof __args[0] === 'function') {
      j_preparedQuery["close(io.vertx.core.Handler)"](function(ar) {
        if (ar.succeeded()) {
          __args[0](null, null);
        } else {
          __args[0](null, ar.cause());
        }
      });
    } else if (__args.length === 0) {
      var __prom = Promise.promise();
      var __prom_completer_handler = function (result, cause) { if (cause === null) { __prom.complete(result); } else { __prom.fail(cause); } };
      j_preparedQuery["close(io.vertx.core.Handler)"](function(ar) {
        if (ar.succeeded()) {
          __prom_completer_handler(null, null);
        } else {
          __prom_completer_handler(null, ar.cause());
        }
      });
      return __prom.future();
    } else if (typeof __super_close != 'undefined') {
      return __super_close.apply(this, __args);
    }
    else throw new TypeError('function invoked with invalid arguments');
  };

  // A reference to the underlying Java delegate
  // NOTE! This is an internal API and must not be used in user code.
  // If you rely on this property your code is likely to break if we change it / remove it without warning.
  this._jdel = j_preparedQuery;
};

PreparedQuery._jclass = utils.getJavaClass("io.vertx.sqlclient.PreparedQuery");
PreparedQuery._jtype = {accept: function(obj) {
    return PreparedQuery._jclass.isInstance(obj._jdel);
  },wrap: function(jdel) {
    var obj = Object.create(PreparedQuery.prototype, {});
    PreparedQuery.apply(obj, arguments);
    return obj;
  },
  unwrap: function(obj) {
    return obj._jdel;
  }
};
PreparedQuery._create = function(jdel) {var obj = Object.create(PreparedQuery.prototype, {});
  PreparedQuery.apply(obj, arguments);
  return obj;
}
module.exports = PreparedQuery;