'use strict';

const format = require('util').format;
const log = require('util').log;

const Promise = require('bluebird');
const db = require('../shared/db');

module.exports = {
    insert: insertEntries,
    delete: deleteEntries
};

function deleteEntries(distribution) {
    return Promise.using(db(), function(client) {
        log(format('Deleting all the entries for %s...', distribution));
        return client.queryAsync({
            name: 'delete_entries',
            text: 'delete from source where distribution = $1',
            values: [distribution]
        }).then(function() {
            log(format('Entries for %s deleted.', distribution));
            return distribution;
        });
    });
}

function insertEntries(sources) {
    log(format('Inserting %d entries for %s...', sources.length, sources[0].distribution));
    // Pseudo query builder
    let params = [];
    let chunks = [];
    for (let i = 0; i < sources.length; i++) {
        let source = sources[i];
        let valuesClause = [];

        params.push(source.name);
        valuesClause.push('$' + params.length);

        params.push(source.distribution);
        valuesClause.push('$' + params.length);

        params.push(source.version);
        valuesClause.push('$' + params.length);

        chunks.push('(' + valuesClause.join(', ') + ')');
    }
    return Promise.using(db(), function(client) {
        return client.queryAsync({
            name: 'insert_sources',
            text: 'insert into source(name, distribution, version) values ' +
                chunks.join(', '),
            values: params
        }).then(function() {
            log(format('Entries for %s inserted.', sources[0].distribution));
        });
    });
}

@@ -848,295 +848,124 @@
     
-// Pseudo query builder%0A    let params = %5B%5D;%0A    let chunks = %5B%5D;%0A    for (let i = 0; i %3C sources.length; i++) %7B%0A        let source = sources%5Bi%5D;%0A        let valuesClause = %5B%5D;%0A%0A        params.push(source.name);%0A        valuesClause.push('$' + params.length);%0A%0A        params.push(
+return Promise.map(sources, insertEntry).then(function() %7B%0A        log(format('Entries for %25s inserted.', 
 source
+s%5B0%5D
 .dis
@@ -978,208 +978,52 @@
 ion)
+)
 ;%0A    
-    valuesClause.push('$' + params.length);%0A%0A        params.push(source.version);%0A        valuesClause.push('$' + params.length);%0A%0A        chunks.push('(' + valuesClause.join(', ') + ')');%0A    %7D
+%7D);%0A%7D%0A%0Afunction insertEntry(source) %7B
 %0A   
@@ -1136,17 +1136,16 @@
 t_source
-s
 ',%0A     
@@ -1217,160 +1217,63 @@
 ues 
-' +%0A                chunks.join(', '),%0A            values: params%0A        %7D).then(function() %7B%0A            log(format('Entries for %25s inserted.'
+($1, $2, $3)',%0A            values: %5Bsource.name
 , source
 s%5B0%5D
@@ -1260,36 +1260,32 @@
 rce.name, source
-s%5B0%5D
 .distribution));
@@ -1273,35 +1273,49 @@
 rce.distribution
-));
+, source.version%5D
 %0A        %7D);%0A   

Use one query per source instead of a single query