Example #1
0
    client.query(deleteQuery, function(errDelete, deleteResult) {

      if (errDelete) {
        logger.logError('DELETE : An error occurred while deleting row : ' + deleteQuery);
        callback(errDelete);
        return;
      }

      for ( var i = 0; i < m2mTemp.values.length; i++) {

        var query = 'INSERT INTO ' + m2mTemp.joinTableName;
        query += ' ("' + m2mTemp.thisId + '", "' + m2mTemp.valueId + '") ';
        query += 'VALUES (' + id + ', ' + m2mTemp.values[i].id + ') ';

        logger.logDebug('INSERT query : ' + query);
        client.query(query, function(err, result) {

          if (err) {
            logger.logError('INSERT : An error occurred while inserting row : ' + query);
            callback(err);
            return;
          }

          // All the m2m query are performed in parallele.
          // We need to be sure every query ended, and ended without error before calling the callback.
          NumberInserted++;
          if (NumberInserted >= totalInsert) {
            NumberInserted = 0;
            callback();
          }
        });
      }
    });
Example #2
0
  pg.connect(db_conString, function(errConnect, client, done) {

    if (errConnect) {
      logger.logError('Error while connecting to Database.');
      logger.logError('Error : ' + errConnect);
      callback(errConnect);
      return;
    }

    logger.logDebug('SELECT query : ' + query);

    client.query(query, objectToArray(paramsArray), function(errQuery, entityResult) {

      // err treatment.
      if (errQuery) {
        logger.logError('SELECT : An error occurred while executing query : ' + query);
        logger.logError('Error : ' + errQuery);
        done();
        callback(errQuery, null);
        return;
      }

      if (entityResult.rows.length === 0) {
        done();
        callback(null, entityResult.rows);
        return;
      }

      logger.logInfo('SELECT o2ms query');
      performOneToManySelect(client, entityResult.rows, entity, function(errO2M, o2mResult) {

        if (errO2M) {
          logger.logError('SELECT : An error occurred while Selecting o2ms rows.');
          logger.logError('Error : ' + errO2M);
          done();
          callback(errO2M);
          return;
        }

        logger.logInfo('SELECT m2ms query');
        performManyToManySelect(client, o2mResult, entity, function(errM2M, m2mResult) {

          if (errM2M) {
            logger.logError('Select : An error occurred while Selecting m2ms rows.');
            logger.logError('Error : ' + errM2M);
            done();
            callback(errM2M);
            return;
          }

          // release connection whatever happened.
          done();

          // normal case.
          callback(null, m2mResult);
        });
      });
    });
  });
Example #3
0
      process.nextTick(function() {

        // First Insert Statement
        logger.logDebug('UPDATE query : ' + query);
        client.query(query, objectToArray(model.fields), function(errQuery, insertedResult) {

          // err treatment.
          if (errQuery) {
            logger.logError('UPDATE : An error occurred while updating row : ' + query);
            logger.logError('Error : ' + errQuery);
            performRollback(client, done);
            callback(errQuery);
            return;
          }

          logger.logInfo('UPDATE o2ms query');
          performOneToManyUpdate(client, insertedResult.rows[0].id, model, function(errO2M) {

            if (errO2M) {
              logger.logError('UPDATE : An error occurred while inserting o2ms rows.');
              logger.logError('Error : ' + errO2M);
              performRollback(client, done);
              callback(errO2M);
              return;
            }

            logger.logInfo('UPDATE m2ms query');
            performManyToManyUpdate(client, insertedResult.rows[0].id, model, function(errM2M) {

              if (errM2M) {
                logger.logError('UPDATE : An error occurred while inserting m2ms rows.');
                logger.logError('Error : ' + errM2M);
                performRollback(client, done);
                callback(errM2M);
                return;
              }

              // COMMIT the transaction
              logger.logInfo('COMMIT Transaction');
              client.query('COMMIT', function() {

                done();

                // Get the inserted Element with all its objects
                find(model, { id : insertedResult.rows[0].id }, function(errSelect, element) {

                  if (errSelect) {
                    logger.logError('SELECT : An error occurred while Re finding the Updated rows.');
                    logger.logError('Error : ' + errSelect);
                  }

                  callback(errSelect, element[0]);
                });
              });
            });
          });
        });
      });
Example #4
0
/**
 * Update into database the values of the oneToMany elements of the given model
 * 
 * @param client
 *          the postgres client
 * @param id
 *          the id of the model
 * @param model
 *          the model containing the onetoManies elements
 * @param callback
 *          the cb executed after all insert statements
 */
function performOneToManyUpdate(client, id, model, callback) {

  var o2ms = model.oneToMany;
  var NumberInserted = 0;
  var totalInsert = 0;

  if (!o2ms) {
    callback();
    return;
  }

  for ( var o2m in o2ms) {
    if (!o2ms[o2m].values) {
      callback();
      return;
    }
    totalInsert += o2ms[o2m].values.length;
  }

  for ( var o2m in o2ms) {

    var o2mTemp = o2ms[o2m];
    for ( var i = 0; i < o2mTemp.values.length; i++) {

      var element = o2mTemp.values[i];
      element[o2mTemp.thisId] = id;
      var query = buildUpdateStatement(o2mTemp.tableName, element, element.id);

      logger.logDebug('UPDATE query : ' + query);
      client.query(query, objectToArray(element), function(err, result) {

        if (err) {
          logger.logError('UPDATE : An error occurred while inserting row : ' + query);
          callback(err);
          return;
        }

        // All the o2m query are performed in parallele.
        // We need to be sure every query ended, and ended without error before calling the callback.
        NumberInserted++;
        if (NumberInserted >= totalInsert) {
          NumberInserted = 0;
          callback();
        }
      });
    }
  }
}
Example #5
0
/**
 * Insert into database the values of the manyToMany elements of the given model
 * 
 * @param client
 *          the postgres client
 * @param id
 *          the id of the model
 * @param model
 *          the model containing the manyToManies elements
 * @param callback
 *          the cb executed after all insert statements
 */
function performManyToManyInserts(client, id, model, callback) {

  var m2ms = model.manyToMany;
  var NumberInserted = 0;
  var totalInsert = 0;

  if (!m2ms) {
    callback();
    return;
  }

  for ( var m2m in m2ms) {
    if (!m2ms[m2m].values) {
      callback();
      return;
    }
    totalInsert += m2ms[m2m].values.length;
  }

  for ( var m2m in m2ms) {

    var m2mTemp = m2ms[m2m];
    for ( var i = 0; i < m2mTemp.values.length; i++) {

      var query = 'INSERT INTO ' + m2mTemp.joinTableName;
      query += ' ("' + m2mTemp.thisId + '", "' + m2mTemp.valueId + '") ';
      query += 'VALUES (' + id + ', ' + m2mTemp.values[i].id + ') ';

      logger.logDebug('INSERT query : ' + query);
      client.query(query, function(err, result) {

        if (err) {
          logger.logError('INSERT : An error occurred while inserting row : ' + query);
          callback(err);
          return;
        }

        // All the m2m query are performed in parallele.
        // We need to be sure every query ended, and ended without error before calling the callback.
        NumberInserted++;
        if (NumberInserted >= totalInsert) {
          NumberInserted = 0;
          callback();
        }
      });
    }
  }
}