summaryrefslogtreecommitdiff
path: root/bin/importSqlFile.js
blob: 5cdf46e5d8927d75161c7275cf2640844f19225b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var startTime = new Date().getTime();

require("ep_etherpad-lite/node_modules/npm").load({}, function(er,npm) {

  var fs = require("fs");

  var ueberDB = require("ep_etherpad-lite/node_modules/ueberDB");
  var settings = require("ep_etherpad-lite/node/utils/Settings");
  var log4js = require('ep_etherpad-lite/node_modules/log4js');

  var dbWrapperSettings = {
    cache: 0,
    writeInterval: 100,
    json: false // data is already json encoded
  };
  var db = new ueberDB.database(settings.dbType, settings.dbSettings, dbWrapperSettings, log4js.getLogger("ueberDB"));

  var sqlFile = process.argv[2];

  //stop if the settings file is not set
  if(!sqlFile)
  {
    console.error("Use: node importSqlFile.js $SQLFILE");
    process.exit(1);
  }

  log("initializing db");
  db.init(function(err)
  {
    //there was an error while initializing the database, output it and stop
    if(err)
    {
      console.error("ERROR: Problem while initializing the database");
      console.error(err.stack ? err.stack : err);
      process.exit(1);
    }
    else
    {
      log("done");

      log("open output file...");
      var lines = fs.readFileSync(sqlFile, 'utf8').split("\n");

      var count = lines.length;
      var keyNo = 0;

      process.stdout.write("Start importing " + count + " keys...\n");
      lines.forEach(function(l) {
        if (l.substr(0, 27) == "REPLACE INTO store VALUES (") {
          var pos = l.indexOf("', '");
          var key = l.substr(28, pos - 28);
          var value = l.substr(pos + 3);
          value = value.substr(0, value.length - 2);
          console.log("key: " + key + " val: " + value);
          console.log("unval: " + unescape(value));
          db.set(key, unescape(value), null);
          keyNo++;
          if (keyNo % 1000 == 0) {
            process.stdout.write(" " + keyNo + "/" + count + "\n");
          }
        }
      });
      process.stdout.write("\n");
      process.stdout.write("done. waiting for db to finish transaction. depended on dbms this may take some time...\n");

      db.doShutdown(function() {
        log("finished, imported " + keyNo + " keys.");
        process.exit(0);
      });
    }
  });
});

function log(str)
{
  console.log((new Date().getTime() - startTime)/1000 + "\t" + str);
}

unescape = function(val) {
  // value is a string
  if (val.substr(0, 1) == "'") {
    val = val.substr(0, val.length - 1).substr(1);

    return val.replace(/\\[0nrbtZ\\'"]/g, function(s) {
      switch(s) {
        case "\\0": return "\0";
        case "\\n": return "\n";
        case "\\r": return "\r";
        case "\\b": return "\b";
        case "\\t": return "\t";
        case "\\Z": return "\x1a";
        default: return s.substr(1);
      }
    });
  }

  // value is a boolean or NULL
  if (val == 'NULL') {
    return null;
  }
  if (val == 'true') {
    return true;
  }
  if (val == 'false') {
    return false;
  }

  // value is a number
  return val;
};