var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'classicmodels',
  password : 'Nr4VCCub6NuG8Vzd',
  database : 'classicmodels'
});

function escapeHtml(unsafe) {
  return unsafe
      .replace(/&(?!amp;)/g, "&")
      .replace(/<(?!lt;)/g, "&lt;")
      .replace(/>(?!gt;)/g, "&gt;")
      .replace(/"(?!quot;)/g, "&quot;")
      .replace(/'(?!#039;)/g, "&#039;");
}

connection.connect();

var pipe = process.stdout;

function oneStep(next) {
        pipe.write("<h1>Kunden</h1>\n");
        pipe.write("<table>\n");
        pipe.write("<tr><th>Kunde</th><th>Stadt</th><th>Land</th></tr>\n");
        connection.query("SELECT customerName, city, country FROM customers ORDER BY customerName", function(err, rows, fields) {
                if(err) throw err;
                var fertig = false;
                connection.query("SELECT productCode, productName, productVendor FROM products ORDER BY productCode", function(err, rows, fields) {
                        if(err) throw err;

                        function printResult() {
                                for(var i = 0; i < rows.length; i++) {
                                        var product = rows[i];

                                        var code = escapeHtml(product.productCode);
                                        var name = escapeHtml(product.productName);
                                        var vendor = escapeHtml(product.productVendor);

                                        pipe.write("<tr><td>"+code+"</td><td>"+name+"</td><td>"+vendor+"</td></tr>\n");
                                }

                                pipe.write("</table>\n");
                                // fertig
                                next();
                        }

                        // Entweder direkt ausführen oder, wenn er noch schreibt, schreiben
                        if(fertig) printResult(); else fertig = printResult;
                });

                for(var i = 0; i < rows.length; i++) {
                        var customer = rows[i];

                        var name = escapeHtml(customer.customerName);
                        var city = escapeHtml(customer.city);
                        var country = escapeHtml(customer.country);

                        pipe.write("<tr><td>"+name+"</td><td>"+city+"</td><td>"+country+"</td></tr>\n");
                }

                pipe.write("</table>");


                pipe.write("<h1>Produkte</h1>\n");
                pipe.write("<table>\n");
                pipe.write("<tr><th>Code</th><th>Name</th><th>Anbieter</th></tr>\n");

                // sind die nächsten Daten schon da, losrechnen. Sonst Signal speichern
                if(fertig) fertig(); else fertig = true;
        });
}

var count = 1000;

function nextStep() {
        if(count) {
                count--;
                oneStep(nextStep);
        } else {
                connection.end();
                // nach 1000 Iterationen beenden
        }
}
// Kette von 1000 Aufrufen starten
nextStep();