Code coverage report for Node-DBI\lib\dbAdapterAbstract.js

Statements: 30.91% (17 / 55)      Branches: 0% (0 / 18)      Functions: 0% (0 / 15)      Lines: 34% (17 / 50)      Ignored: none     

All files » Node-DBI\lib\ » dbAdapterAbstract.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244                1                       1                                               1   1                 1                   1                             1                         1                     1                       1                     1                     1                       1                   1                           1                                                               1                                                   1                      
/**
 * @class DBAdapterAbstract
 * @author Dr. Benton - github.com/DrBenton
 * @see https://github.com/DrBenton/Node-DBI
 */
 
// ------------------------------------- modules
 
var _ = require('lodash')
  , util = require('util')
  , EventEmitter = require('events').EventEmitter
  , DBSelect = require('./dbSelect').DBSelect
  , DBWrapper = require('./dbWrapper').DBWrapper;
 
// ------------------------------------- constructor
 
/**
 * @param {DBWrapper} dbWrapper
 * @param {Object} connectionParams
 */
function DBAdapterAbstract( dbWrapper, connectionParams )
{
  
  //if( ! (dbWrapper instanceof DBWrapper) )
  //  throw new Error('dbWrapper param must be a DBWrapper instance !');
  //console.dir(dbWrapper);
  
  /**
   * @type DBWrapper
   */
  this._dbWrapper = dbWrapper;
  
  /**
   * @type Object
   */
  this._dbClient = null;
  
  /**
   * @type Object
   */
  this._connectionParams = connectionParams;
  
}
 
util.inherits(DBAdapterAbstract, EventEmitter);
 
module.exports.DBAdapterAbstract = DBAdapterAbstract; 
 
// ------------------------------------- public methods
 
 
/**
 * Abstract method : DB Adapter implementations must override it. 
 * @api public
 */
DBAdapterAbstract.prototype.connect = function()
{
  throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.connect() !');
};
 
 
/**
 * Abstract method : DB Adapter implementations must override it. 
 * @api public
 */
DBAdapterAbstract.prototype.close = function()
{
  throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.close() !');
};
 
 
/**
 * Abstract method : DB Adapter implementations must override it. 
 * The callback function will receive the casual "err" as first param,
 * and the number of affected fields as the second one.
 * @param {String}  sql
 * @param {Array|null}  bind
 * @param {Function|null}  callback
 * @api public
 */
DBAdapterAbstract.prototype.query = function( sql, bind, callback )
{
  throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.query() !');
};
 
 
/**
 * Abstract method : DB Adapter implementations must override it. 
 * @param {String}  sql
 * @param {Array|null}  bind
 * @param {Function|null}  callback
 * @api public
 */
DBAdapterAbstract.prototype.fetchAll = function( sql, bind, callback )
{
  throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.fetchAll() !');
};
 
 
/**
 * Abstract method : DB Adapter implementations must override it. 
 * @rturn {Integer|null}  the last inserted Id
 * @api public
 */
DBAdapterAbstract.prototype.getLastInsertId = function()
{
  throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.getLastInsertId() !');
};
 
 
/**
 * Abstract method : DB Adapter implementations must override it. 
 * @param {String}  value
 * @rturn {String}  the escaped value
 * @api public
 */
DBAdapterAbstract.prototype.escape = function( value )
{
  throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.escape() !');
};
 
/**
 * escape a tableName appropriately - MySQL uses backticks some others don't escape at all.
 * @param {String}  value
 * @rturn {String}  the escaped value
 * @api public
 */
DBAdapterAbstract.prototype.escapeTable = function( value )
{
  return '`' + value + '`';
};
 
/**
 * escape a fieldName appropriately - MySQL uses backticks some others don't escape at all.
 * @param {String}  value
 * @rturn {String}  the escaped value
 * @api public
 */
DBAdapterAbstract.prototype.escapeField = function( value )
{
  return '`' + value + '`';
};
 
 
/**
 * You can override this implementation if your Adapter needs a custom DBSelect subclass.
 * 
 * @returns {DBSelect}  Returns a new DBSelect instance
 * @api public
 */
DBAdapterAbstract.prototype.getSelect = function()
{
  return new DBSelect( this );
};
 
 
/**
 * @returns {DBWrapper}  Returns the associated DBWrapper
 * @api public
 */
DBAdapterAbstract.prototype.getDBWrapper = function()
{
  return this._dbWrapper;
};
 
// ------------------------------------- protected methods
 
/**
 * Retuns a single String with "?" replaced by escaped values of the "bind" Array. 
 * @param {String}  sql
 * @param {Array|null}  bind
 * @returns {String}  
 * @api protected
 */
DBAdapterAbstract.prototype._format = function( sql, bind )
{
  
  // Code copied from "mysql" Node module's Client class.
  // @see https://github.com/felixge/node-mysql/blob/master/lib/mysql/client.js
  
  if( this._debug )
    console.log('DBAdapterAbstract._format('+sql+', '+JSON.stringify(bind)+')');
  
  var escape = _.bind( this.escape, this );
  
  sql = sql.replace(/\?/g, function() {
    
    if (bind.length == 0)
      throw new Error('too few parameters given');
    
    return escape( bind.shift() );
    
  });
  
  if (bind.length > 0 )
    throw new Error('too many parameters given');
  
  return sql;
  
};
 
/**
 * Converts a Date in a "YYYY-MM-DD H:i:s" String.
 * 
 * @param err {Error|null}
 */
DBAdapterAbstract.prototype._stringifyDate = function( javascriptDate )
{
  
  var year = javascriptDate.getFullYear()
    , month = ( javascriptDate.getUTCMonth() + 1 )
    , day = javascriptDate.getUTCDate()
    , hours = javascriptDate.getUTCHours()
    , minutes = javascriptDate.getUTCMinutes()
    , seconds = javascriptDate.getUTCSeconds();
  
  if( month.toString().length==1 ) month = '0' + month ;
  if( day.toString().length==1 ) day = '0' + day ;
  if( hours.toString().length==1 ) hours = '0' + hours ;
  if( minutes.toString().length==1 ) minutes = '0' + minutes ;
  if( seconds.toString().length==1 ) seconds = '0' + seconds ;
  
  return year + '-' + month + '-' + day +' ' + hours + ':' + minutes + ':' + seconds;
  
};
 
 
/**
 * Triggers the initialization callback function and emits a "connection" Event.
 * 
 * @param err {Error|null}
 */
DBAdapterAbstract.prototype._onConnectionInitialization = function( err )
{
  
  if( this._debug )
    console.log('_onConnectionInitialization('+err+')');
  
  this.emit( 'connection', err );
  
};
 
// vim: sw=2 ts=2 et