Icontem

File: sql.js

Recommend this page to a friend!
  Classes of Tom Schaefer  >  SQL  >  sql.js  >  Download  
File: sql.js
Role: Class source
Content type: text/plain
Description: classes
Class: SQL
Compose and execute SQL queries with SQLite
Author: By
Last change: bug fix _esc =>SQL.Var
Date: 7 years ago
Size: 30,691 bytes
 

Contents

Class file image Download
var SQL, sprintf, _and, _create, _delete, _drop, _endsswith, _eq, _esc, _from, _gt, _in, _insert, _isnull, _like, _lt, _notin, _notnull, _or, _startswith, _unlike, _update;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
  for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
  function ctor() { this.constructor = child; }
  ctor.prototype = parent.prototype;
  child.prototype = new ctor;
  child.__super__ = parent.prototype;
  return child;
};
sprintf = function() {
  var a, doFormat, format, formatBaseX, formatString, i, justify, pad, regex;
  regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g;
  a = arguments;
  i = 0;
  format = a[i++];
  pad = function(str, len, chr, leftJustify) {
    var padding;
    if (!chr) {
      chr = " ";
    }
    padding = (str.length >= len ? "" : Array(1 + len - str.length >>> 0).join(chr));
    if (leftJustify) {
      return str + padding;
    } else {
      return padding + str;
    }
  };
  justify = function(value, prefix, leftJustify, minWidth, zeroPad, customPadChar) {
    var diff;
    diff = minWidth - value.length;
    if (diff > 0) {
      if (leftJustify || !zeroPad) {
        value = pad(value, minWidth, customPadChar, leftJustify);
      } else {
        value = value.slice(0, prefix.length) + pad("", diff, "0", true) + value.slice(prefix.length);
      }
    }
    return value;
  };
  formatBaseX = function(value, base, prefix, leftJustify, minWidth, precision, zeroPad) {
    var number;
    number = value >>> 0;
    prefix = prefix && number && {
      2: "0b",
      8: "0",
      16: "0x"
    };
    [
      {
        base: base
      }
    ] || "";
    value = prefix + pad(number.toString(base), precision || 0, "0", false);
    return justify(value, prefix, leftJustify, minWidth, zeroPad);
  };
  formatString = function(value, leftJustify, minWidth, precision, zeroPad, customPadChar) {
    if (precision != null) {
      value = value.slice(0, precision);
    }
    return justify(value, "", leftJustify, minWidth, zeroPad, customPadChar);
  };
  doFormat = function(substring, valueIndex, flags, minWidth, _, precision, type) {
    var customPadChar, flagsl, j, leftJustify, method, number, positivePrefix, prefix, prefixBaseX, textTransform, value, zeroPad;
    number = void 0;
    prefix = void 0;
    method = void 0;
    textTransform = void 0;
    value = void 0;
    if (substring === "%%") {
      return "%";
    }
    leftJustify = false;
    positivePrefix = "";
    zeroPad = false;
    prefixBaseX = false;
    customPadChar = " ";
    flagsl = flags.length;
    j = 0;
    while (flags && j < flagsl) {
      switch (flags.charAt(j)) {
        case " ":
          positivePrefix = " ";
          break;
        case "+":
          positivePrefix = "+";
          break;
        case "-":
          leftJustify = true;
          break;
        case "'":
          customPadChar = flags.charAt(j + 1);
          break;
        case "0":
          zeroPad = true;
          break;
        case "#":
          prefixBaseX = true;
      }
      j++;
    }
    if (!minWidth) {
      minWidth = 0;
    } else if (minWidth === "*") {
      minWidth = +a[i++];
    } else if (minWidth.charAt(0) === "*") {
      minWidth = +a[minWidth.slice(1, -1)];
    } else {
      minWidth = +minWidth;
    }
    if (minWidth < 0) {
      minWidth = -minWidth;
      leftJustify = true;
    }
    if (!isFinite(minWidth)) {
      throw new Error("sprintf: (minimum-)width must be finite");
    }
    if (!precision) {
      precision = ("fFeE".indexOf(type) > -1 ? 6 : (type === "d" ? 0 : undefined));
    } else if (precision === "*") {
      precision = +a[i++];
    } else if (precision.charAt(0) === "*") {
      precision = +a[precision.slice(1, -1)];
    } else {
      precision = +precision;
    }
    value = (valueIndex ? a[valueIndex.slice(0, -1)] : a[i++]);
    switch (type) {
      case "s":
        return formatString(String(value), leftJustify, minWidth, precision, zeroPad, customPadChar);
      case "c":
        return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad);
      case "b":
        return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
      case "o":
        return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
      case "x":
        return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
      case "X":
        return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase();
      case "u":
        return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad);
      case "i":
      case "d":
        number = (+value) | 0;
        prefix = (number < 0 ? "-" : positivePrefix);
        value = prefix + pad(String(Math.abs(number)), precision, "0", false);
        return justify(value, prefix, leftJustify, minWidth, zeroPad);
      case "e":
      case "E":
      case "f":
      case "F":
      case "g":
      case "G":
        number = +value;
        prefix = (number < 0 ? "-" : positivePrefix);
        method = ["toExponential", "toFixed", "toPrecision"]["efg".indexOf(type.toLowerCase())];
        textTransform = ["toString", "toUpperCase"]["eEfFgG".indexOf(type) % 2];
        value = prefix + Math.abs(number)[method](precision);
        return justify(value, prefix, leftJustify, minWidth, zeroPad)[textTransform]();
      default:
        return substring;
    }
  };
  return format.replace(regex, doFormat);
};
if (!SQL) {
  SQL = {};
}
SQL.Node = (function() {
  Node.prototype.Command = null;
  function Node() {}
  Node.prototype.cast = function(value, valueType) {
    if (!value) {
      return 'NULL';
    }
    if (value === 'NULL') {
      return 'NULL';
    } else if (typeof value === 'number') {
      return value;
    } else if (typeof value === 'boolean' && value === 1) {
      return 1;
    } else if (typeof value === 'boolean' && value === 0) {
      return 0;
    } else if (value instanceof SQL.Junction) {
      return value.toString();
    } else {
      return "'" + value + "'";
    }
  };
  return Node;
})();
SQL.Var = (function() {
  function Var(name, value) {
    this.name = name;
    this.value = value;
    
    if (this.value===null) {
      this.value = 1;
    } else if (this.value===false){
      this.value = false;
    } else {
      this.value = 0;
    }
  }
  Var.prototype.toString = function() {
    if (this.value === 1) {
      return "'" + this.value + "'";
    } else {
      if(this.value==false)
    	  return this.name;
      return this.value;
    }
  };
  return Var;
})();
SQL.Struct = (function() {
  __extends(Struct, SQL.Node);
  Struct.prototype.name = '';
  Struct.prototype.value = '';
  Struct.prototype.operator = '=';
  function Struct(name, value, operator) {
    this.name = name;
    this.value = value;
    this.operator = operator;
  }
  Struct.prototype.toString = function() {
    var sql;
    sql = [];
    sql.push(this.name);
    switch (this.operator) {
      case 'like':
        sql.push(sprintf("LIKE '%%%s%%'", this.value));
        break;
      case 'unlike':
        sql.push(sprintf("NOT LIKE '%%%s%%'", this.value));
        break;
      case 'startswith':
        sql.push(sprintf("LIKE '%s%%'", this.value));
        break;
      case 'endswith':
        sql.push(sprintf("LIKE '%%%s'", this.value));
        break;
      case 'in':
        sql.push(sprintf("IN %s", this.value.join(",")));
        break;
      case 'notin':
        sql.push(sprintf("NOT IN %s", this.value.join(",")));
        break;
      case 'isnull':
        sql.push("IS NULL");
        break;
      case 'notnull':
        sql.push("IS NOT NULL");
        break;
      default:
        sql.push(this.operator);
        sql.push(this.value);
    }
    return sql.join(" ");
  };
  return Struct;
})();
SQL.Join = (function() {
  __extends(Join, SQL.Node);
  Join.prototype.type = '';
  Join.prototype.to = '';
  Join.prototype.struct = '';
  Join.prototype.statement = null;
  Join.prototype.alias = null;
  function Join(to, struct, operator, type) {
    this.to = to;
    this.struct = struct;
    this.operator = operator;
    this.type = type;
    if (!operator) {
      this.operator = '=';
    }
    switch (type) {
      case 1:
        this.type = 'LEFT JOIN';
        break;
      case 2:
        this.type = 'LEFT OUTER JOIN';
        break;
      case 3:
        this.type = 'INNER JOIN';
        break;
      case 4:
        this.type = 'CROSS JOIN';
        break;
      default:
        this.type = 'JOIN';
    }
  }
  Join.prototype.statement = function(statement, alias) {
    this.statement = statement;
    this.alias = alias;
  };
  Join.prototype.toString = function() {
    var sql;
    sql = [];
    if (this.statement instanceof SQL.Select) {
      sql.push('(');
      sql.push(this.statement);
      sql.push("AS");
      sql.push(this.alias);
      sql.push(')');
    } else {
      sql.push(this.type);
      sql.push(this.to);
      sql.push("ON");
      sql.push(this.struct);
    }
    return sql.join(" ");
  };
  return Join;
})();
SQL.Junction = (function() {
  __extends(Junction, SQL.Node);
  function Junction() {
    Junction.__super__.constructor.apply(this, arguments);
  }
  Junction.prototype.Struct = null;
  return Junction;
})();
SQL.And = (function() {
  __extends(And, SQL.Junction);
  And.prototype.Command = 'AND';
  function And() {
    this.Struct = [];
    return this;
  }
  And.prototype.add = function(struct) {
    if (struct instanceof SQL.Struct) {
      this.Struct.push(struct);
    } else if (struct instanceof SQL.Junction) {
      this.Struct.push(struct);
    }
    return this;
  };
  And.prototype.toString = function() {
    var sql, str, _i, _len, _ref;
    sql = [];
    if (this.Struct.length > 0) {
      _ref = this.Struct;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        str = _ref[_i];
        sql.push('(' + str + ')');
      }
    }
    return sql.join(' ' + this.Command + ' ');
  };
  return And;
})();
SQL.Or = (function() {
  __extends(Or, SQL.Junction);
  Or.prototype.Command = 'OR';
  function Or() {
    this.Struct = [];
    return this;
  }
  Or.prototype.add = function(struct) {
    if (struct instanceof SQL.Struct) {
      this.Struct.push(struct);
    } else if (struct instanceof SQL.Junction) {
      this.Struct.push(struct);
    }
    return this;
  };
  Or.prototype.toString = function() {
    var sql, str, _i, _len, _ref;
    sql = [];
    if (this.Struct.length > 0) {
      _ref = this.Struct;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        str = _ref[_i];
        sql.push('(' + str + ')');
      }
    }
    return sql.join(' ' + this.Command + ' ');
  };
  return Or;
})();
SQL.Select = (function() {
  __extends(Select, SQL.Node);
  Select.prototype.Command = 'SELECT';
  Select.prototype.Distinct = null;
  Select.prototype.Columns = [];
  Select.prototype.Table = [];
  Select.prototype.Alias = null;
  Select.prototype.Joins = [];
  Select.prototype.Where = [];
  Select.prototype.Group = [];
  Select.prototype.Having = [];
  Select.prototype.Order = [];
  Select.prototype.Limit = [];
  function Select() {
    this.Distinct = null;
    this.Alias = null;
    this.Columns = [];
    this.Table = [];
    this.Joins = [];
    this.Group = [];
    this.Order = [];
    this.Limit = [0, 10];
    return this;
  }
  Select.prototype.columns = function(Columns) {
    this.Columns = Columns;
    return this;
  };
  Select.prototype.distinct = function() {
    this.Distinct = 'DISTINCT';
    return this;
  };
  Select.prototype.all = function() {
    this.Distinct = 'ALL';
    return this;
  };
  Select.prototype.from = function(from) {
    this.Table.push(from);
    return this;
  };
  Select.prototype.alias = function(alias) {
    this.Alias = alias;
    return this;
  };
  Select.prototype.left = function(name, struct, operator) {
    this.Joins.push(new SQL.Join(name, struct, operator, 1));
    return this;
  };
  Select.prototype.leftouter = function(name, struct, operator) {
    this.Joins.push(new SQL.Join(name, struct, operator, 2));
    return this;
  };
  Select.prototype.selectJoin = function(statement, alias) {
    var sm;
    sm = new SQL.Join();
    sm.statement(statement, alias);
    this.Table.push(sm);
    return this;
  };
  Select.prototype.inner = function(name, struct) {
    this.Joins.push(new SQL.Join(name, struct, operator, 3));
    return this;
  };
  Select.prototype.cross = function(name, struct) {
    this.Joins.push(new SQL.Join(name, struct, operator, 4));
    return this;
  };
  Select.prototype.where = function(where) {
    this.Where = where;
    return this;
  };
  Select.prototype.group = function(group) {
    this.Group.push(group);
    return this;
  };
  Select.prototype.having = function(having) {
    this.Having = having;
    return this;
  };
  Select.prototype.order = function(name, dir) {
    if (!dir) {
      dir = 'ASC';
    } else {
      dir = 'DESC';
    }
    this.Order.push(name + " " + dir);
    return this;
  };
  Select.prototype.limit = function(offset, limit) {
    if (typeof limit !== "undefined") {
      this.Limit.push(offset);
      this.Limit.push(limit);
    } else {
      this.Limit.push(offset);
      this.Limit.push(10);
    }
    return this;
  };
  Select.prototype.toString = function() {
    var col, join, mc, mf, sql, ssql, table, _i, _len, _ref;
    sql = [];
    sql.push(this.Command);
    if (this.Distinct) {
      sql.push(this.Distinct);
    }
    if (this.Columns.length > 0) {
      ssql = [];
      mc = (function() {
        var _i, _len, _ref, _results;
        _ref = this.Columns;
        _results = [];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          col = _ref[_i];
          _results.push(col instanceof SQL.Select ? ssql.push('(' + col + ') AS ' + col.Alias) : ssql.push(col));
        }
        return _results;
      }).call(this);
      sql.push(ssql.join(","));
    } else {
      sql.push('*');
    }
    sql.push('FROM');
    if (this.Table.length > 0) {
      ssql = [];
      mf = (function() {
        var _i, _len, _ref, _results;
        _ref = this.Table;
        _results = [];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          table = _ref[_i];
          _results.push(table instanceof SQL.Select ? ssql.push('(' + table + ') AS ' + table.Alias) : void 0);
        }
        return _results;
      }).call(this);
      if (ssql.length > 0) {
        sql.push(ssql.join(","));
      } else {
        sql.push(this.Table.join(","));
      }
    }
    if (this.Joins.length > 0) {
      _ref = this.Joins;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        join = _ref[_i];
        sql.push('(' + join + ')');
      }
    }
    if (this.Where instanceof SQL.Junction) {
      sql.push('WHERE');
      sql.push(this.Where.toString());
    } else if (this.Where instanceof SQL.Struct) {
      sql.push('WHERE');
      sql.push(this.Where.toString());
    }
    if (this.Group.length > 0) {
      sql.push("GROUP BY");
      sql.push(this.Group.join(","));
    }
    if (this.Having instanceof SQL.Junction) {
      sql.push('HAVING');
      sql.push(this.Having.toString());
    } else if (this.Having instanceof SQL.Struct) {
      sql.push('HAVING');
      sql.push(this.Having.toString());
    }
    if (this.Order.length > 0) {
      sql.push("ORDER BY");
      sql.push(this.Order.join(","));
    }
    if (this.Limit.length === 2) {
      sql.push("LIMIT");
      sql.push(this.Limit.pop());
      sql.push("OFFSET");
      sql.push(this.Limit.pop());
    }
    return sql.join(' ');
  };
  return Select;
})();
SQL.Insert = (function() {
  __extends(Insert, SQL.Node);
  Insert.prototype.Command = 'INSERT';
  Insert.prototype.Table = [];
  Insert.prototype.Columns = [];
  Insert.prototype.Values = [];
  Insert.prototype.Or = [];
  function Insert() {
    this.Or = null;
    this.Columns = [];
    this.Values = [];
    this.Table = [];
    return this;
  }
  Insert.prototype.rollback = function() {
    this.type("rollback");
    return this;
  };
  Insert.prototype.abort = function() {
    this.type("abort");
    return this;
  };
  Insert.prototype.replace = function() {
    this.type("replace");
    return this;
  };
  Insert.prototype.fail = function() {
    this.type("fail");
    return this;
  };
  Insert.prototype.ignore = function() {
    this.type("ignore");
    return this;
  };
  Insert.prototype.type = function(type) {
    switch (type) {
      case "rollback":
      case "abort":
      case "replace":
      case "fail":
      case "ignore":
        this.Or = type.toUpperCase();
        break;
      default:
        this.Or = null;
    }
    return this;
  };
  Insert.prototype.into = function(table) {
    this.table = table;
    return this;
  };
  Insert.prototype.columns = function(Columns) {
    this.Columns = Columns;
    return this;
  };
  Insert.prototype.values = function(Values) {
    this.Values.push(Values);
    return this;
  };
  Insert.prototype.toString = function() {
    var sql, ssql, sssql, val, vals, _i, _j, _len, _len2, _ref;
    sql = [];
    sql.push(this.Command);
    if (this.Or) {
      sql.push("OR");
      sql.push(this.Or);
    }
    sql.push("INTO");
    sql.push(this.table);
    if (this.Columns.length > 0) {
      sql.push("(");
      sql.push(this.Columns.join(","));
      sql.push(")");
    }
    if (this.Values.length > 0) {
      ssql = [];
      sql.push('VALUES');
      _ref = this.Values;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        vals = _ref[_i];
        sssql = [];
        for (_j = 0, _len2 = vals.length; _j < _len2; _j++) {
          val = vals[_j];
          sssql.push(this.cast(val));
        }
        ssql.push("(" + sssql.join(",") + ")");
      }
      sql.push(ssql.join(","));
    } else {
      sql.push("DEFAULT VALUES");
    }
    return sql.join(' ');
  };
  return Insert;
})();
SQL.Update = (function() {
  __extends(Update, SQL.Node);
  Update.prototype.Command = 'UPDATE';
  Update.prototype.Table = [];
  Update.prototype.Columns = [];
  Update.prototype.Values = [];
  Update.prototype.Or = [];
  Update.prototype.Where = [];
  Update.prototype.Order = [];
  Update.prototype.Limit = [];
  function Update() {
    this.Or = null;
    this.Columns = [];
    this.Values = [];
    this.Table = [];
    this.Order = [];
    this.Limit = [];
    return this;
  }
  Update.prototype.rollback = function() {
    this.type("rollback");
    return this;
  };
  Update.prototype.abort = function() {
    this.type("abort");
    return this;
  };
  Update.prototype.replace = function() {
    this.type("replace");
    return this;
  };
  Update.prototype.fail = function() {
    this.type("fail");
    return this;
  };
  Update.prototype.ignore = function() {
    this.type("ignore");
    return this;
  };
  Update.prototype.type = function(type) {
    switch (type) {
      case "rollback":
      case "abort":
      case "replace":
      case "fail":
      case "ignore":
        this.Or = type.toUpperCase();
        break;
      default:
        this.Or = null;
    }
    return this;
  };
  Update.prototype.table = function(table) {
    this.table = table;
    return this;
  };
  Update.prototype.where = function(where) {
    this.Where = where;
    return this;
  };
  Update.prototype.column = function(name) {
    this.Columns.push(name);
    return this;
  };
  Update.prototype.columns = function(Columns) {
    this.Columns = Columns;
    return this;
  };
  Update.prototype.values = function(Values) {
    this.Values.push(Values);
    return this;
  };
  Update.prototype.value = function(Value) {
    this.Values.push(Value);
    return this;
  };
  Update.prototype.order = function(name, dir) {
    if (!dir) {
      dir = 'ASC';
    } else {
      dir = 'DESC';
    }
    this.Order.push(name + " " + dir);
    return this;
  };
  Update.prototype.limit = function(offset, limit) {
    if (typeof limit !== "undefined") {
      this.Limit.push(offset);
      this.Limit.push(limit);
    } else {
      this.Limit.push(offset);
      this.Limit.push(1);
    }
    return this;
  };
  Update.prototype.toString = function() {
    var col, i, sql, ssql, _i, _len, _ref;
    sql = [];
    sql.push(this.Command);
    if (this.Or) {
      sql.push("OR");
      sql.push(this.Or);
    }
    sql.push(this.table);
    if (this.Columns.length > 0) {
      sql.push("SET");
      ssql = [];
      i = 0;
      _ref = this.Columns;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        ssql.push(col + "=" + this.cast(this.Values[i]));
        i++;
      }
      sql.push(ssql.join(","));
    }
    if (this.Where instanceof SQL.Junction) {
      sql.push('WHERE');
      sql.push(this.Where.toString());
    } else if (this.Where instanceof SQL.Struct) {
      sql.push('WHERE');
      sql.push(this.Where.toString());
    }
    if (this.Order.length > 0) {
      sql.push("ORDER BY");
      sql.push(this.Order.join(","));
    }
    if (this.Limit.length === 2) {
      sql.push("LIMIT");
      sql.push(this.Limit.pop());
      sql.push("OFFSET");
      sql.push(this.Limit.pop());
    }
    return sql.join(' ');
  };
  return Update;
})();
SQL.Delete = (function() {
  __extends(Delete, SQL.Node);
  Delete.prototype.Command = 'DELETE';
  Delete.prototype.Table = [];
  Delete.prototype.Where = [];
  Delete.prototype.Order = [];
  Delete.prototype.Limit = [];
  function Delete() {
    this.Table = [];
    this.Order = [];
    this.Limit = [];
    return this;
  }
  Delete.prototype.from = function(Table) {
    this.Table = Table;
    return this;
  };
  Delete.prototype.where = function(where) {
    this.Where = where;
    return this;
  };
  Delete.prototype.order = function(name, dir) {
    if (!dir) {
      dir = 'ASC';
    } else {
      dir = 'DESC';
    }
    this.Order.push(name + " " + dir);
    return this;
  };
  Delete.prototype.limit = function(offset, limit) {
    if (typeof limit !== "undefined") {
      this.Limit.push(offset);
      this.Limit.push(limit);
    } else {
      this.Limit.push(offset);
      this.Limit.push(1);
    }
    return this;
  };
  Delete.prototype.toString = function() {
    var sql;
    sql = [];
    sql.push(this.Command);
    sql.push(this.Table);
    if (this.Where instanceof SQL.Junction) {
      sql.push('WHERE');
      sql.push(this.Where.toString());
    } else if (this.Where instanceof SQL.Struct) {
      sql.push('WHERE');
      sql.push(this.Where.toString());
    }
    if (this.Order.length > 0) {
      sql.push("ORDER BY");
      sql.push(this.Order.join(","));
    }
    if (this.Limit.length === 2) {
      sql.push("LIMIT");
      sql.push(this.Limit.pop());
      sql.push("OFFSET");
      sql.push(this.Limit.pop());
    }
    return sql.join(' ');
  };
  return Delete;
})();
SQL.Type = (function() {
  __extends(Type, SQL.Node);
  Type.prototype.Name = null;
  Type.prototype._type = null;
  Type.prototype._primary = null;
  Type.prototype._notnull = null;
  Type.prototype._unique = null;
  function Type(Name) {
    this.Name = Name;
    return this;
  }
  Type.prototype.type = function(t, n, d) {
	switch (t) {
      case "numeric":
      case "integer":
      case "smallint":
      case "tinyint":
      case "mediumint":
      case "bigint":
      case "int2":
      case "int8":
        t = "INTEGER";
        break;
      case "string":
      case "text":
      case "nchar":
      case "varchar":
      case "char":
      case "clob":
      case "character":
      case "native character":
      case "nvarchar":
        t = "TEXT";
        break;
      case "double":
      case "float":
      case "double precision":
      case "real":
        t = "REAL";
        break;
      case "blob":
        t = "NONE";
        break;
      case "decimal":
      case "boolean":
      case "date":
      case "datetime":
      case "numeric":
        t = "NUMERIC";
        break;
      default:
        t = null;
    }
    if (n && d) {
      t + sprintf("(%d,%d)", n, d);
    } else if (n && !d) {
      t + sprintf("(%d)", n);
    }
    this._type = t;
    return this;
  };
  Type.prototype._onConflict = function(type) {
    var s;
    s = "ON CONFLICT ";
    switch (type) {
      case "rollback":
      case "abort":
      case "fail":
      case "ignore":
      case "replace":
        return s + type.toUpperCase();
    }
  };
  Type.prototype.primary = function(auto, dir, conflict) {
    var a, p, pk;
    p = "PRIMARY KEY";
    a = "AUTOINCREMENT";
    pk = p + " " + (dir === "asc" || dir === "desc" ? this.cast(dir) : '');
    if (auto && conflict) {
      pk = pk + this._onConflict(conflict) + " " + a;
    } else if (!auto && conflict) {
      pk = pk + this._onConflict(conflict);
    } else if (auto && !conflict) {
      pk = pk + " " + a;
    }
    this._primary = pk;
    return this;
  };
  Type.prototype.notnull = function(conflict) {
    var s;
    s = "NOT NULL";
    if (conflict) {
      s = s + this._onConflict(conflict);
    }
    this._notnull = s;
    return this;
  };
  Type.prototype.unique = function(conflict) {
    var s;
    s = "UNIQUE";
    if (conflict) {
      s = s + this._onConflict(conflict);
    }
    this._unique = s;
    return this;
  };
  Type.prototype.toString = function() {
    var sql;
    sql = this.Name;
    sql += this._type ? " " + this._type : '';
    sql += this._primary ? " " + this._primary : '';
    sql += this._notnull ? " " + this._notnull : '';
    sql += this._unique ? " " + this._unique : '';
    return sql;
  };
  return Type;
})();
SQL.Drop = (function() {
  __extends(Drop, SQL.Node);
  function Drop() {
    Drop.__super__.constructor.apply(this, arguments);
  }
  Drop.prototype.Command = 'DROP';
  return Drop;
})();
SQL.Drop.Table = (function() {
  var Exist;
  __extends(Table, SQL.Drop);
  Table.prototype.CommandType = 'TABLE';
  Exist = false;
  function Table(Table) {
    this.Table = Table;
    this.Columns = [];
  }
  Table.prototype.exists = function() {
    this.Exist = true;
    return this;
  };
  Table.prototype.toString = function() {
    var sql;
    sql = [];
    sql.push(this.Command);
    sql.push(this.CommandType);
    if (this.Exist) {
      sql.push("IF EXISTS");
    }
    sql.push(this.Table);
    return sql.join(" ");
  };
  return Table;
})();
SQL.Create = (function() {
  __extends(Create, SQL.Node);
  function Create() {
    Create.__super__.constructor.apply(this, arguments);
  }
  Create.prototype.Command = 'CREATE';
  return Create;
})();
SQL.Create.Table = (function() {
  var Exist, Temp;
  __extends(Table, SQL.Create);
  Table.prototype.CommandType = 'TABLE';
  Temp = null;
  Exist = true;
  Table;
  Table.prototype.Statement = null;
  Table.prototype.Columns = [];
  Table.prototype.notexists = function() {
    this.Exist = false;
    return this;
  };
  function Table(Table) {
    this.Table = Table;
    this.Columns = [];
    return this;
  }
  Table.prototype.column = function(type) {
    if (type instanceof SQL.Type) {
      this.Columns.push(type);
      console.log(this)
    }
    return this;
  };
  Table.prototype.statement = function(Statement) {
    this.Statement = Statement;
    return this;
  };
  Table.prototype.toString = function() {
    var column, sql, ssql, _i, _len, _ref;
    sql = [];
    sql.push(this.Command);
    if (Temp) {
      sql.push("TEMP");
    }
    sql.push(this.CommandType);
    if (!this.Exist) {
      sql.push("IF NOT EXISTS");
    }
    sql.push(this.Table);
    if (this.statement instanceof SQL.Select) {
      sql.push("AS");
      sql.push(this.statement);
    } else if (this.Columns.length > 0) {
      ssql = [];
      _ref = this.Columns;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        column = _ref[_i];
        ssql.push(column);
      }
      sql.push("(" + ssql.join(",") + ")");
    }
    return sql.join(" ");
  };
  return Table;
})();
_drop = function(table) {
  return new SQL.Drop.Table(table).exists();
};
_create = function(table) {
  return new SQL.Create.Table(table).notexists();
};
_delete = function(table) {
  return new SQL.Delete().from(table);
};
_insert = function(table) {
  return new SQL.Insert().into(table);
};
_update = function(table) {
  return new SQL.Update().table(table);
};
_from = function(from) {
  return new SQL.Select().from(from);
};
_type = function(name) {
  var r = new SQL.Type(name);
  return r;
};
_and = function() {
  return new SQL.And();
};
_or = function() {
  return new SQL.Or;
};
_eq = function(n, v) {
  return new SQL.Struct(n, v, '=');
};
_gt = function(n, v) {
  return new SQL.Struct(n, v, '>');
};
_lt = function(n, v) {
  return new SQL.Struct(n, v, '<');
};
_in = function(n, v) {
  return new SQL.Struct(n, v, 'in');
};
_notin = function(n, v) {
  return new SQL.Struct(n, v, 'notin');
};
_like = function(n, v) {
  return new SQL.Struct(n, v, 'like');
};
_unlike = function(n, v) {
  return new SQL.Struct(n, v, 'unlike');
};
_startswith = function(n, v) {
  return new SQL.Struct(n, v, 'startswith');
};
_endswith = function(n, v) {
  return new SQL.Struct(n, v, 'endswith');
};
_isnull = function(n, v) {
  return new SQL.Struct(n, v, 'isnull');
};
_notnull = function(n, v) {
  return new SQL.Struct(n, v, 'notnull');
};
_esc = function(v) {
	return new SQL.Var(v, false);
};