      1 var Type = require("./index").Type,
      2         comb = require("comb");
      3
      4 var dateDefaults = {
      5     allowNull : true,
      6     primaryKey : false,
      7     foreignKey : false,
      8     "default" : null,
      9     unique : false,
     10     description : ""
     11 };
     12
     13
     14 var setDateType = function(type, cmpFun) {
     15     return function(val) {
     16         if (!comb.isDate(val)) {
     17             var patt = "yyyy";
     18             if (type == "TIME") {
     19                 patt = "h:m:s";
     20             } else if (type == "DATE") {
     21                 patt += "-MM-dd";
     22             } else if ((type == "DATETIME" || type == 'TIMESTAMP')) {
     23                 patt += "-MM-dd h:m:s";
     24             }
     25             val = comb.date.parse(val, patt);
     26             if (!val) {
     27                 throw new Error("DATE must be formatted as " + patt);
     28             }
     29         }
     30         cmpFun && cmpFun(val);
     31         return val;
     32     };
     33 };
     34
     35 var checkDateType = function(type) {
     36     return function(val) {
     37         if (!(val instanceof Date)) {
     38             throw new Error(type + " requires a date type");
     39         }
     40     };
     41 };
     42
     43 /**
     44  * Mysql DATE datatype
     45  *
     46  * @function
     47  * @param {Object} options options for the DATE data type.
     48  * @param {Boolean} [options.allowNull=true] should the field allow null
     49  * @param {Boolean} [options.default = null] default value of the field
     50  * @param {Boolean} [options.unsigned = false] unsigned number
     51  * @param {Boolean} [options.description = ""] description fo the field.
     52  *
     53  * @return {Type} A Type representing a DATE column.
     54  *
     55  * @name DATE
     56  * @memberOf moose.adapters.mysql.types
     57  *
     58  */
     59 exports.DATE = function(options) {
     60     var ops = comb.merge({}, dateDefaults, {type : "DATE"}, options || {});
     61     ops.setSql = setDateType(ops.type, checkDateType(ops.type));
     62     ops.checkType = checkDateType(ops.type);
     63     return new Type(ops);
     64 };
     65
     66 /**
     67  * Mysql TIME datatype
     68  *
     69  * @function
     70  * @param {Object} options options for the TIME data type.
     71  * @param {Boolean} [options.allowNull=true] should the field allow null
     72  * @param {Boolean} [options.default = null] default value of the field
     73  * @param {Boolean} [options.unsigned = false] unsigned number
     74  * @param {Boolean} [options.description = ""] description fo the field.
     75  *
     76  * @return {Type} A Type representing a TIME column.
     77  *
     78  * @name TIME
     79  * @memberOf moose.adapters.mysql.types
     80  *
     81  */
     82 exports.TIME = function(options) {
     83     var ops = comb.merge({}, dateDefaults, {type : "TIME"}, options || {});
     84     ops.setSql = setDateType(ops.type, checkDateType(ops.type));
     85     ops.checkType = checkDateType(ops.type);
     86     return new Type(ops);
     87 };
     88
     89
     90 /**
     91  * Mysql TIMESTAMP datatype
     92  *
     93  * @function
     94  * @param {Object} options options for the TIMESTAMP data type.
     95  * @param {Boolean} [options.allowNull=true] should the field allow null
     96  * @param {Boolean} [options.default = null] default value of the field
     97  * @param {Boolean} [options.unsigned = false] unsigned number
     98  * @param {Boolean} [options.description = ""] description fo the field.
     99  *
    100  * @return {Type} A Type representing a TIMESTAMP column.
    101  *
    102  * @name TIMESTAMP
    103  * @memberOf moose.adapters.mysql.types
    104  *
    105  */
    106 exports.TIMESTAMP = function(options) {
    107     var ops = comb.merge({}, dateDefaults, {type : "TIMESTAMP", allowNull : false}, options || {});
    108     ops.setSql = setDateType(ops.type, checkDateType(ops.type));
    109     ops.checkType = checkDateType(ops.type);
    110     return new Type(ops);
    111 };
    112
    113
    114 /**
    115  * Mysql YEAR datatype
    116  *
    117  * @function
    118  * @param {Object} options options for the YEAR data type.
    119  * @param {Boolean} [options.allowNull=true] should the field allow null
    120  * @param {Boolean} [options.default = null] default value of the field
    121  * @param {Boolean} [options.unsigned = false] unsigned number
    122  * @param {Boolean} [options.description = ""] description fo the field.
    123  *
    124  * @return {Type} A Type representing a YEAR column.
    125  *
    126  * @name YEAR
    127  * @memberOf moose.adapters.mysql.types
    128  *
    129  */
    130 exports.YEAR = function(options) {
    131     var ops = comb.merge({}, dateDefaults, {type : "YEAR"}, options || {});
    132     ops.setSql = setDateType(ops.type, checkDateType(ops.type));
    133     ops.checkType = checkDateType(ops.type);
    134     return new Type(ops);
    135 };
    136
    137
    138 /**
    139  * Mysql DATETIME datatype
    140  *
    141  * @function
    142  * @param {Object} options options for the DATETIME data type.
    143  * @param {Boolean} [options.allowNull=true] should the field allow null
    144  * @param {Boolean} [options.default = null] default value of the field
    145  * @param {Boolean} [options.unsigned = false] unsigned number
    146  * @param {Boolean} [options.description = ""] description fo the field.
    147  *
    148  * @return {Type} A Type representing a DATETIME column.
    149  *
    150  * @name DATETIME
    151  * @memberOf moose.adapters.mysql.types
    152  *
    153  */
    154 exports.DATETIME = function(options) {
    155     var ops = comb.merge({}, dateDefaults, {type : "DATETIME"}, options || {});
    156     ops.setSql = setDateType(ops.type, checkDateType(ops.type));
    157     ops.checkType = checkDateType(ops.type);
    158     return new Type(ops);
    159 };