jeudi 25 février 2016

nodejs mysql design pattern for function - conditional statement with iteration, change the values

I have two node mysql statements to retrieve data.

//one query
pool.getConnection(function(err, connection) {
connection.query('select * from tablejohn' , function(err, rows, fields) {  
infoCleanup(rows,"rows[i]");
} });


//2 queries (multiqueries in one statement)
pool.getConnection(function(err, connection) {
connection.query('select count(*)  as Companycount from tablejohn WHERE elif in (3,4);select * FROM tablejohn WHERE  elif in (3,4)', function(err, rows, fields) {
infoCleanup(rows,"rows[1][i]");
} });

to iterate and change the values I've created a function for this

var infoCleanup = function(rows,type){

if (type=="rows[i]"){
//iterate and change the value
for(var i=0;i<rows.length; i++)
rows[i].a = toTittleCase(rows[i].a);
rows[i].b = toTittleCase(rows[i].b);
rows[i].c = toTittleCase(rows[i].c);
}

else if(type=="rows[1][i]"){
//iterate and change the value
for(var i=0;i<rows[1].length; i++)
rows[1][i].a = toTittleCase(rows[1][i].a);
rows[1][i].b = toTittleCase(rows[1][i].b);
rows[1][i].c = toTittleCase(rows[1][i].c);
}

}

For now I have been using the above infoCleanup(rows,type) function and from there set a conditional statement.

The question how can I improve my infoCleanUp function for a better maintainable code (i.e design pattern ,oop etc)?

As you can see the execute statement( "iterate and change the value") for both conditional do the same things, only different is either variable rows[i] or variable rows[1][i] . So I guess we dont have conditional statement in this case and reduce line of code.

Aucun commentaire:

Enregistrer un commentaire