From 3d213454f90f47353341640c2479aece6c26e736 Mon Sep 17 00:00:00 2001 From: Jan Zaloudek Date: Sun, 30 Mar 2014 15:05:01 +0200 Subject: [PATCH] basic features --- .gitignore | 4 +++- main.js | 10 +++++++--- parser.js | 50 +++++++++++++++++++++++++++++++++++++++++++++---- test-input2.xml | 11 ++++++++--- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 30bc162..30212d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/node_modules \ No newline at end of file +test-input2.xml +/node_modules +out.sql \ No newline at end of file diff --git a/main.js b/main.js index 9be5349..318c7d1 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,10 @@ var Parser = require('./parser'); commander .version(package.version) - .usage('[options] ') + .usage('[options] ') + .option('-p, --pedantic', 'Pedantic mode is checking for inconsistency in map', true) + .option('-f, --foreign-keys', 'Disable foreign keys') + .option('-l, --leave-content', 'Does not truncate tables') .parse(process.argv); if(commander.args.length === 0) { @@ -16,8 +19,9 @@ if(commander.args.length === 0) { } commander.args.forEach(function(file) { - var parser = new Parser(file); - parser.parse(); + var parser = new Parser(file, commander); + + process.stdout.write(parser.parse()); }); diff --git a/parser.js b/parser.js index b2e6192..ce58790 100644 --- a/parser.js +++ b/parser.js @@ -10,7 +10,19 @@ var Parser = function(file_name, options) { } Parser.prototype.getEdgesQueries_ = function() { + if(!this.dom_) { + throw new Error('XML DOM is not loaded'); + } + + var queries = []; + var self = this; + var counter = 0; + + this.dom_('nodes node edge').each(function(index, node) { + queries.push('INSERT INTO `edge` (`id`, `from_id`, `to_id`) VALUES ('+(++counter)+', '+this.attr('fromid')+', '+this.attr('toid')+');'); + }); + return queries; } Parser.prototype.getNodesQueries_ = function() { @@ -21,9 +33,11 @@ Parser.prototype.getNodesQueries_ = function() { var queries = []; var self = this; - // this.dom_('nodes').each(function(index, node) { - // console.log(self.dom_('edge', this).toArray()); - // }); + this.dom_('nodes node').each(function(index, node) { + queries.push('INSERT INTO `node` (`id`) VALUES ('+this.attr('id')+');'); + }); + + return queries; } Parser.prototype.parse = function() { @@ -40,7 +54,35 @@ Parser.prototype.parse = function() { var nodes_queries = this.getNodesQueries_() || []; var edges_queries = this.getEdgesQueries_() || []; - var result + var result = ''; + + result += '-- Nodes for ZUMScore --\n\n'; + result += '-- Source: '+this.file_name+'\n'; + result += '-- Generated at: '+(new Date().toUTCString())+'\n'; + result += '-- Repository: https://github.com/fitak/zum-nodes-parser.git'+'\n'; + result += '\n'; + + if(this.options['foreignKeys']) { + result += 'SET FOREIGN_KEY_CHECKS=0;\n'; + } + + if(!this.options['leaveContent']) { + result += 'TRUNCATE edge;\n' + result += 'TRUNCATE node;\n'; + } + + result += '\n'; + + result += '-- Generating nodes\n'; + result += nodes_queries.join('\n'); + result += '\n\n'; + + result += '-- Generating edges\n'; + result += edges_queries.join('\n'); + + result += '\n\n-- End of '+this.file_name+'\n'; + + return result; } module.exports = Parser; \ No newline at end of file diff --git a/test-input2.xml b/test-input2.xml index d773b25..479e47c 100644 --- a/test-input2.xml +++ b/test-input2.xml @@ -5,7 +5,12 @@ - - + - Pokus \ No newline at end of file + + + + + + + \ No newline at end of file