<commit_before>/*---------------------------------------------------------------
  :: sails-postgresql
  -> adapter
---------------------------------------------------------------*/

// Dependencies
var pg = require('pg'),
    _ = require('underscore'),
    async = require('async'),
    Query = require('./query'),
    utils = require('./utils');

module.exports = (function() {

  // Keep track of all the dbs used by the app
  var dbs = {};

  var adapter = {
    identity: 'sails-postgresql',

    syncable: true,

    commitLog: {},

    defaults: {
      host: 'localhost',
      port: 5432,
      pool: false
    },

    /*************************************************************************/
    /* Public Methods for Sails/Waterline Adapter Compatibility              */
    /*************************************************************************/

    // Register a new DB Collection
    registerCollection: function(collection, cb) {
      dbs[collection.identity] = _.find(dbs, function(db) {
        return collection.host === db.host && collection.database === db.database;
      });

      if(!dbs[collection.identity]) {
        dbs[collection.identity] = utils.marshalConfig(collection);
        return cb();
      } else return cb();
    },

    // Teardown
    teardown: function(cb) {
      cb();
    },

    // Raw Query Interface
    query: function(table, query, data, cb) {

      if (_.isFunction(data)) {
        cb = data;
        data = null;
      }

      spawnConnection(function __QUERY__(client, cb) {

        // Run query
        if (data) client.query(query, data, cb);
        else client.query(query, cb);

      }, dbs[table], cb);
    },

    // Describe a table
    describe: function(table, cb) {
      spawnConnection(function __DESCRIBE__(client, cb) {

        // Build Query
        var query = "SELECT column_name, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = '" + table + "';";

        // Run Query
        client.query(query, function(err, result) {
          if(err) return cb();
          if(result.rows.length === 0) return cb();
          cb(null, utils.normalizeSchema(result.rows));
        });

      }, dbs[table], cb);
    },

    // Create a new table
    define: function(table, definition, cb) {
      spawnConnection(function __DEFINE__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Iterate through each attribute, building a query string
        var schema = utils.buildSchema(definition.attributes);

        // Build Query
        var query = 'CREATE TABLE ' + table + ' (' + schema + ')';

        // Run Query
        client.query(query, function __DEFINE__(err, result) {
          if(err) return cb(err);
          cb(null, result);
        });

      }, dbs[table], cb);
    },

    // Drop a table
    drop: function(table, cb) {
      spawnConnection(function __DROP__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Build Query
        var query = 'DROP TABLE ' + table + ';';

        // Run Query
        client.query(query, function __DROP__(err, result) {
          if(err) result = null;
          cb(null, result);
        });

      }, dbs[table], cb);
    },

    // Add a column to a table
    addAttribute: function(table, attrName, attrDef, cb) {
      spawnConnection(function __ADD_ATTRIBUTE__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Setup a Schema Definition
        var attrs = {};
        attrs[attrName] = attrDef;

        var schema = utils.buildSchema(attrs);

        // Build Query
        var query = 'ALTER TABLE ' + table + ' ADD COLUMN ' + schema;

        // Run Query
        client.query(query, function __ADD_ATTRIBUTE__(err, result) {
          if(err) return cb(err);
          cb(null, result.rows);
        });

      }, dbs[table], cb);
    },

    // Remove a column from a table
    removeAttribute: function (table, attrName, cb) {
      spawnConnection(function __REMOVE_ATTRIBUTE__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Build Query
        var query = 'ALTER TABLE ' + table + ' DROP COLUMN ' + attrName + ' RESTRICT';

        // Run Query
        client.query(query, function __REMOVE_ATTRIBUTE__(err, result) {
          if(err) return cb(err);
          cb(null, result.rows);
        });

      }, dbs[table], cb);
    },<commit_msg>fix: bug in sails auto-migrations by escaping column name<commit_after>/*---------------------------------------------------------------
  :: sails-postgresql
  -> adapter
---------------------------------------------------------------*/

// Dependencies
var pg = require('pg'),
    _ = require('underscore'),
    async = require('async'),
    Query = require('./query'),
    utils = require('./utils');

module.exports = (function() {

  // Keep track of all the dbs used by the app
  var dbs = {};

  var adapter = {
    identity: 'sails-postgresql',

    syncable: true,

    commitLog: {},

    defaults: {
      host: 'localhost',
      port: 5432,
      pool: false
    },

    /*************************************************************************/
    /* Public Methods for Sails/Waterline Adapter Compatibility              */
    /*************************************************************************/

    // Register a new DB Collection
    registerCollection: function(collection, cb) {
      dbs[collection.identity] = _.find(dbs, function(db) {
        return collection.host === db.host && collection.database === db.database;
      });

      if(!dbs[collection.identity]) {
        dbs[collection.identity] = utils.marshalConfig(collection);
        return cb();
      } else return cb();
    },

    // Teardown
    teardown: function(cb) {
      cb();
    },

    // Raw Query Interface
    query: function(table, query, data, cb) {

      if (_.isFunction(data)) {
        cb = data;
        data = null;
      }

      spawnConnection(function __QUERY__(client, cb) {

        // Run query
        if (data) client.query(query, data, cb);
        else client.query(query, cb);

      }, dbs[table], cb);
    },

    // Describe a table
    describe: function(table, cb) {
      spawnConnection(function __DESCRIBE__(client, cb) {

        // Build Query
        var query = "SELECT column_name, data_type, character_maximum_length FROM information_schema.columns WHERE table_name = '" + table + "';";

        // Run Query
        client.query(query, function(err, result) {
          if(err) return cb();
          if(result.rows.length === 0) return cb();
          cb(null, utils.normalizeSchema(result.rows));
        });

      }, dbs[table], cb);
    },

    // Create a new table
    define: function(table, definition, cb) {
      spawnConnection(function __DEFINE__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Iterate through each attribute, building a query string
        var schema = utils.buildSchema(definition.attributes);

        // Build Query
        var query = 'CREATE TABLE ' + table + ' (' + schema + ')';

        // Run Query
        client.query(query, function __DEFINE__(err, result) {
          if(err) return cb(err);
          cb(null, result);
        });

      }, dbs[table], cb);
    },

    // Drop a table
    drop: function(table, cb) {
      spawnConnection(function __DROP__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Build Query
        var query = 'DROP TABLE ' + table + ';';

        // Run Query
        client.query(query, function __DROP__(err, result) {
          if(err) result = null;
          cb(null, result);
        });

      }, dbs[table], cb);
    },

    // Add a column to a table
    addAttribute: function(table, attrName, attrDef, cb) {
      spawnConnection(function __ADD_ATTRIBUTE__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Setup a Schema Definition
        var attrs = {};
        attrs[attrName] = attrDef;

        var schema = utils.buildSchema(attrs);

        // Build Query
        var query = 'ALTER TABLE ' + table + ' ADD COLUMN ' + schema;

        // Run Query
        client.query(query, function __ADD_ATTRIBUTE__(err, result) {
          if(err) return cb(err);
          cb(null, result.rows);
        });

      }, dbs[table], cb);
    },

    // Remove a column from a table
    removeAttribute: function (table, attrName, cb) {
      spawnConnection(function __REMOVE_ATTRIBUTE__(client, cb) {

        // Escape Table Name
        table = utils.escapeTable(table);

        // Build Query
        var query = 'ALTER TABLE ' + table + ' DROP COLUMN "' + attrName + '" RESTRICT';

        // Run Query
        client.query(query, function __REMOVE_ATTRIBUTE__(err, result) {
          if(err) return cb(err);
          cb(null, result.rows);
        });

      }, dbs[table], cb);
    },
{'fraction_non_alphanumeric': 0.1835387323943662, 'fraction_numerical': 0.0011003521126760564, 'mean_word_length': 1.974149214659686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6222944', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2473, 'n_words': 650}