// Generated by CoffeeScript 1.7.1
(function() {
  var MAX, TYPES, Table, declare, _ref,
    __slice = [].slice;

  _ref = require('./datatypes'), TYPES = _ref.TYPES, declare = _ref.declare;

  MAX = 65535;

  Table = (function() {
    function Table(name) {
      var path, _ref1;
      if (name) {
        path = name.match(/^(\[?([^\]]*)\]?\.)?(\[?([^\]]*)\]?\.)?\[?([^\]]*)\]?$/);
        this.name = path[5];
        this.schema = path[4] != null ? path[4] : path[2] ? path[2] : null;
        this.database = (_ref1 = path[2]) != null ? _ref1 : null;
        this.path = "" + (this.database ? "[" + this.database + "]." : "") + (this.schema ? "[" + this.schema + "]." : "") + "[" + this.name + "]";
        this.temporary = this.name.charAt(0) === '#';
      }
      this.columns = [];
      this.rows = [];
      Object.defineProperty(this.columns, "add", {
        value: function(name, column, options) {
          if (options == null) {
            options = {};
          }
          if (column == null) {
            throw new Error("Column data type is not defined.");
          }
          if (column instanceof Function) {
            column = column();
          }
          column.name = name;
          column.nullable = options.nullable;
          return this.push(column);
        }
      });
      Object.defineProperty(this.rows, "add", {
        value: function() {
          var values;
          values = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
          return this.push(values);
        }
      });
    }


    /*
    	@private
     */

    Table.prototype._makeBulk = function() {
      var col, _i, _len, _ref1;
      _ref1 = this.columns;
      for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
        col = _ref1[_i];
        switch (col.type) {
          case TYPES.Xml:
            col.type = TYPES.NVarChar(MAX).type;
            break;
          case TYPES.UDT:
          case TYPES.Geography:
          case TYPES.Geometry:
            col.type = TYPES.VarBinary(MAX).type;
        }
      }
      return this;
    };

    Table.prototype.declare = function() {
      var col;
      return "create table " + this.path + " (" + (((function() {
        var _i, _len, _ref1, _results;
        _ref1 = this.columns;
        _results = [];
        for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
          col = _ref1[_i];
          _results.push("[" + col.name + "] " + (declare(col.type, col)) + (col.nullable === true ? " null" : col.nullable === false ? " not null" : ""));
        }
        return _results;
      }).call(this)).join(', ')) + ")";
    };

    Table.fromRecordset = function(recordset) {
      var col, name, row, t, _i, _len, _ref1, _ref2;
      t = new this;
      _ref1 = recordset.columns;
      for (name in _ref1) {
        col = _ref1[name];
        t.columns.add(name, {
          type: col.type,
          length: col.length,
          scale: col.scale,
          precision: col.precision
        }, {
          nullable: col.nullable
        });
      }
      for (_i = 0, _len = recordset.length; _i < _len; _i++) {
        row = recordset[_i];
        (_ref2 = t.rows).add.apply(_ref2, (function() {
          var _j, _len1, _ref2, _results;
          _ref2 = t.columns;
          _results = [];
          for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
            col = _ref2[_j];
            _results.push(row[col.name]);
          }
          return _results;
        })());
      }
      return t;
    };

    return Table;

  })();

  module.exports = Table;

}).call(this);
