Пример #1
0
 test('uses defaults', function() {
   var input = "/var/run/postgresql";
   var output = utils.normalizeConnectionInfo(input);
   assert.equal(output.user, process.env.USER);
   assert.equal(output.host, '/var/run/postgresql');
   assert.equal(output.database, process.env.USER);
   assert.equal(output.port, 5432);
 });
Пример #2
0
 test('ignores defaults if string contains them all', function() {
   var input = "tcp://*****:*****@host3:3333/databaseName";
   var output = utils.normalizeConnectionInfo(input);
   assert.equal(output.user, 'user1');
   assert.equal(output.database, 'databaseName');
   assert.equal(output.port, 3333);
   assert.equal(output.host, 'host3');
   assert.equal(output.password, 'pass2');
 })
Пример #3
0
 test('uses defaults', function() {
   var input = "";
   var output = utils.normalizeConnectionInfo(input);
   assert.equal(output.user, defaults.user);
   assert.equal(output.database, defaults.database);
   assert.equal(output.port, defaults.port);
   assert.equal(output.host, defaults.host);
   assert.equal(output.password, defaults.password);
 });
Пример #4
0
 test('full object ignores defaults', function() {
   var input = {
     user: '******',
     database: 'test2',
     port: 'test3',
     host: 'test4',
     password: '******'
   };
   assert.equal(utils.normalizeConnectionInfo(input), input);
 });
Пример #5
0
var clientBuilder = function(config) {
  config = config || {};
  var connection = new Connection();
  connection._queryQueue = [];
  connection._namedQueries = {};
  connection._activeQuery = null;
  connection._config = utils.normalizeConnectionInfo(config);
  //attach properties to normalize interface with pure js client
  connection.user = connection._config.user;
  connection.password = connection._config.password;
  connection.database = connection._config.database;
  connection.host = connection._config.host;
  connection.port = connection._config.port;
  connection.on('connect', function() {
    connection._connected = true;
    connection._pulseQueryQueue(true);
  });

  //proxy some events to active query
  connection.on('_row', function(row) {
    connection._activeQuery.handleRow(row);
  });

  //TODO: emit more native error properties (make it match js error)
  connection.on('_error', function(err) {
    //create Error object from object literal
    var error = new Error(err.message || "Unknown native driver error");
    for(var key in err) {
      error[key] = err[key];
    }

    //give up on trying to wait for named query prepare
    this._namedQuery = false;
    if(connection._activeQuery) {
      connection._activeQuery.handleError(error);
    } else {
      connection.emit('error', error);
    }
  });

  connection.on('_readyForQuery', function() {
    var q = this._activeQuery;
    //a named query finished being prepared
    if(this._namedQuery) {
      this._namedQuery = false;
      this._sendQueryPrepared(q.name, q.values||[]);
    } else {
      connection._activeQuery.handleReadyForQuery();
      connection._activeQuery = null;
      connection._pulseQueryQueue();
    }
  });

  return connection;
};
Пример #6
0
 test('uses overridden defaults', function() {
   defaults.host = "/var/run/postgresql";
   defaults.user = "******";
   defaults.password = "******";
   defaults.port = 1234;
   var output = utils.normalizeConnectionInfo("asdf");
   assert.equal(output.user, "boom");
   assert.equal(output.password, "yeah");
   assert.equal(output.port, 1234);
   assert.equal(output.host, "/var/run/postgresql");
 })
Пример #7
0
var Client = function(config) {
  EventEmitter.call(this);
  if(typeof config === 'string') {
    config = utils.normalizeConnectionInfo(config)
  }
  config = config || {};
  this.user = config.user || defaults.user;
  this.database = config.database || defaults.database;
  this.port = config.port || defaults.port;
  this.host = config.host || defaults.host;
  this.connection = config.connection || new Connection({stream: config.stream});
  this.queryQueue = [];
  this.password = config.password || defaults.password;
  this.encoding = 'utf8';
  this.processID = null;
  this.secretKey = null;
  var self = this;
};
Пример #8
0
var Client = function(config) {
  EventEmitter.call(this);
  if(typeof config === 'string') {
    config = utils.normalizeConnectionInfo(config)
  }
  config = config || {};
  this.user = config.user || defaults.user;
  this.database = config.database || defaults.database;
  this.port = config.port || defaults.port;
  this.host = config.host || defaults.host;
  this.queryQueue = [];
  this.connection = config.connection || new Connection({stream: config.stream});
  this.queryQueue = [];
  this.password = config.password || defaults.password;
  this.encoding = 'utf8';
  var self = this;
  this.connection.on('notify', function(msg) {
    self.emit('notify', msg);
  })
};
Пример #9
0
var ctor = function(config) {
  config = config || {};
  var connection = new Connection();
  connection._queryQueue = [];
  connection._namedQueries = {};
  connection._activeQuery = null;
  connection._config = utils.normalizeConnectionInfo(config);
  connection.on('connect', function() {
    connection._connected = true;
    connection._pulseQueryQueue();
  });

  //proxy some events to active query
  connection.on('_row', function(row) {
    connection._activeQuery.handleRow(row);
  })
  connection.on('_error', function(err) {
    //give up on trying to wait for named query prepare
    this._namedQuery = false;
    if(connection._activeQuery) {
      connection._activeQuery.handleError(err);
    } else {
      connection.emit('error', err);
    }
  })
  connection.on('_readyForQuery', function() {
    var q = this._activeQuery;
    //a named query finished being prepared
    if(this._namedQuery) {
      this._namedQuery = false;
      this._sendQueryPrepared(q.name, q.values||[]);
    } else {
      connection._activeQuery.handleReadyForQuery();
      connection._activeQuery = null;
      connection._pulseQueryQueue();
    }
  });
  return connection;
};
Пример #10
0
var clientBuilder = function(config) {
  config = config || {};
  var connection = new Connection();
  connection._queryQueue = [];
  connection._namedQueries = {};
  connection._activeQuery = null;
  connection._config = utils.normalizeConnectionInfo(config);
  //attach properties to normalize interface with pure js client
  connection.user = connection._config.user;
  connection.password = connection._config.password;
  connection.database = connection._config.database;
  connection.host = connection._config.host;
  connection.port = connection._config.port;
  connection.on('connect', function() {
    connection._connected = true;
    connection._pulseQueryQueue(true);
  });

  //proxy some events to active query
  connection.on('_row', function(row) {
    connection._activeQuery.handleRow(row);
  });

  connection.on('_cmdStatus', function(status) {
    //set this here so we can pass it to the query
    //when the query completes
    connection._lastMeta = status;
  });

  //TODO: emit more native error properties (make it match js error)
  connection.on('_error', function(err) {
    //create Error object from object literal
    var error = new Error(err.message || "Unknown native driver error");
    for(var key in err) {
      error[key] = err[key];
    }

    //give up on trying to wait for named query prepare
    this._namedQuery = false;
    if(connection._activeQuery) {
      connection._activeQuery.handleError(error);
    } else {
      connection.emit('error', error);
    }
  });

  connection.on('_readyForQuery', function() {
    var q = this._activeQuery;
    //a named query finished being prepared
    if(this._namedQuery) {
      this._namedQuery = false;
      this._sendQueryPrepared(q.name, q.values||[]);
    } else {
      connection._activeQuery.handleReadyForQuery(connection._lastMeta);
      connection._activeQuery = null;
      connection._pulseQueryQueue();
    }
  });
  connection.on('copyInResponse', function () {
    //connection is ready to accept chunks
    //start to send data from stream 
    connection._activeQuery.streamData(connection);
  });
  connection.on('copyOutResponse', function(msg) {
    if (connection._activeQuery.stream  === undefined) {
      connection._activeQuery._canceledDueToError = new Error('No destination stream defined');
      (new clientBuilder({port: connection.port, host: connection.host})).cancel(connection, connection._activeQuery);
   }
  });
  connection.on('copyData', function (chunk) {
    //recieve chunk from connection
    //move it to stream
    connection._activeQuery.handleCopyFromChunk(chunk);
  });
  return connection;
};