All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vertx-pg-client-js.pg_cursor.js Maven / Gradle / Ivy

/*
 * 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-pg-client-js/pg_cursor */
var utils = require('vertx-js/util/utils');
var Row = require('vertx-pg-client-js/row');
var PgResult = require('vertx-pg-client-js/pg_result');

var io = Packages.io;
var JsonObject = io.vertx.core.json.JsonObject;
var JPgCursor = Java.type('com.julienviet.pgclient.PgCursor');

/**
 A cursor that reads progressively the rows from Postgres, it is usefull for reading very large result.

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

  var j_pgCursor = j_val;
  var that = this;

  /**
   Read rows from the cursor, the result is provided asynchronously to the handler.

   @public
   @param count {number} the amount of rows to read 
   @param handler {function} the handler for the result 
   */
  this.read = function(count, handler) {
    var __args = arguments;
    if (__args.length === 2 && typeof __args[0] ==='number' && typeof __args[1] === 'function') {
      j_pgCursor["read(int,io.vertx.core.Handler)"](count, function(ar) {
      if (ar.succeeded()) {
        handler(utils.convReturnVertxGen(PgResult, ar.result(), Row._jtype), null);
      } else {
        handler(null, ar.cause());
      }
    });
    } else throw new TypeError('function invoked with invalid arguments');
  };

  /**
   Returns true when the cursor has results in progress and the  should be called to retrieve
   them.

   @public

   @return {boolean} whether the cursor has more results,
   */
  this.hasMore = function() {
    var __args = arguments;
    if (__args.length === 0) {
      return j_pgCursor["hasMore()"]();
    } else throw new TypeError('function invoked with invalid arguments');
  };

  /**
   Like {@link PgCursor#close} but with a completionHandler called when the cursor has been released.

   @public
   @param completionHandler {function} 
   */
  this.close = function() {
    var __args = arguments;
    if (__args.length === 0) {
      j_pgCursor["close()"]();
    }  else if (__args.length === 1 && typeof __args[0] === 'function') {
      j_pgCursor["close(io.vertx.core.Handler)"](function(ar) {
      if (ar.succeeded()) {
        __args[0](null, null);
      } else {
        __args[0](null, ar.cause());
      }
    });
    } 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_pgCursor;
};

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy