text: // Generated by CozyScript 0.1.1
  (function() {
    var MoeDB, sqlite3,
      __slice = [].slice,
      __partialize = function partialize$(f, args, where){
    var context = this;
    return function(){
      var params = __slice.call(arguments), i,
          len = params.length, wlen = where.length,
          ta = args ? args.concat() : [], tw = where ? where.concat() : [];
      for(i = 0; i < len; ++i) { ta[tw[0]] = params[i]; tw.shift(); }
      return len < wlen && len ?
        partialize$.apply(context, [f, ta, tw]) : f.apply(context, ta);
    };
  };
  
    sqlite3 = require('sqlite3').verbose();
  
    MoeDB = (function() {
  
      function MoeDB(db_path) {
        this.db_path = db_path;
        this.db = new sqlite3.Database(this.db_path);
        this.sql_all_definitions = function() {
          return "SELECT id, def \nFROM definitions;";
        };
        this.sql_all_titles = function() {
          return "SELECT title\nFROM entries;";
        };
        this.sql_only_title = function(query) {
          return "SELECT title\nFROM entries\nWHERE title LIKE '" + query + "%';";
        };
        this.sql = function(col, query) {
          return "SELECT '' || title || '\t' || heteronyms.bopomofo || '\n» ' || group_concat(\n  def, '\n» '\n) || '\n' as result, title, radical, stroke_count, non_radical_stroke_count,\nbopomofo, bopomofo2, pinyin, group_concat(type, '\n') as type, group_concat(def, '\n') as def,\ngroup_concat(example, '\n') as example, group_concat(quote, '\n') as quote,\ngroup_concat(link, '\n') as link, group_concat(synonyms, '\n') as synonyms,\ngroup_concat(antonyms, '\n') as antonyms, lang, lang_def\nFROM entries\nJOIN heteronyms ON heteronyms.entry_id = entries.id\nJOIN (\n    SELECT heteronym_id,\n        CASE WHEN example is NULL or example is '' THEN ' ' ELSE example END as example,\n        CASE WHEN quote is NULL or quote is '' THEN ' ' ELSE quote END as quote,\n        CASE WHEN def is NULL or def is '' THEN ' ' ELSE def END as def,\n        CASE WHEN type is NULL or type is '' THEN ' ' ELSE type END as type,\n        CASE WHEN link is NULL or link is '' THEN ' ' ELSE link END as link,\n        CASE WHEN synonyms is NULL or synonyms is '' THEN ' ' ELSE synonyms END as synonyms,\n        CASE WHEN antonyms is NULL or antonyms is '' THEN ' ' ELSE antonyms END as antonyms\n    FROM definitions\n) ON heteronym_id = heteronyms.id\nJOIN (\n    SELECT entry_id, group_concat(lang, '\n') as lang, group_concat(def, '\n') as lang_def\n    FROM translations GROUP BY entry_id\n) AS translations ON translations.entry_id = entries.id\nWHERE " + col + " LIKE '" + query + "'\nGROUP BY title, heteronyms.bopomofo;";
        };
        this.sql_by_title = __partialize.apply(this, [this.sql, ['title', void 0], [1]]);
        this.sql_by_def = __partialize.apply(this, [this.sql, ['def', void 0], [1]]);
        this.def_cb = function(err, row) {
          console.log(row);
          return console.log(row.result);
        };
      }
  
      MoeDB.prototype.get_all_definitions = function(cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_each(this.sql_all_definitions(), cb);
      };
  
      MoeDB.prototype.get_all_titles = function(cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_all(this.sql_all_titles(), cb);
      };
  
      MoeDB.prototype.find_by_title = function(title, cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_each(this.sql_by_title(title), cb);
      };
  
      MoeDB.prototype.find_by_def = function(def, cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_each(this.sql_by_def(def), cb);
      };
  
      MoeDB.prototype.find_all_titles_by_title = function(title, cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_all(this.sql_only_title(title), cb);
      };
  
      MoeDB.prototype.find_all_by_title = function(title, cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_all(this.sql_by_title(title), cb);
      };
  
      MoeDB.prototype.find_all_by_def = function(def, cb) {
        if (cb == null) {
          cb = this.def_cb;
        }
        return this.find_all(this.sql_by_def(def), cb);
      };
  
      MoeDB.prototype.find_all = function(sql, cb) {
        var _this = this;
        if (this.db != null) {
          return this.db.serialize(function() {
            return _this.db.all(sql, function(err, rows) {
              return cb(err, rows);
            });
          });
        }
      };
  
      MoeDB.prototype.find_each = function(sql, cb) {
        var _this = this;
        if (this.db != null) {
          return this.db.serialize(function() {
            return _this.db.each(sql, function(err, row) {
              return cb(err, row);
            });
          });
        }
      };
  
      MoeDB.prototype.update_def = function(id, new_def) {
        var _this = this;
        if (this.db != null) {
          return this.db.serialize(function() {
            return _this.db.run("UPDATE definitions SET def = '" + new_def + "' WHERE id = " + id + ";");
          });
        }
      };
  
      MoeDB.prototype.close = function() {
        return this.db.close();
      };
  
      return MoeDB;
  
    })();
  
    exports.MoeDB = MoeDB;
  
  }).call(this);
  