HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

sed-like attempt from database

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sedlikeattemptdatabasefrom

Problem

The following code and test data works, but I have had to add delays between writes to MySQL otherwise it acts funny (especially on the target data set which is over 700 rows). The table is simply a number and value (the number is another table's 'foreign key(?)'.

I am looking for suggestions on how I might reorganize this for better behavior. Perhaps a Generator? It's written in JavaScript, and uses node.js 0.10, because I believe I need to become much better in JavaScript. The real goal is to be able to change the last two funcs out to whatever data base hack I need to preform today. This code preforms a Name hack, but in the future I plan on different code that would do what ever data field hacking I need. The mental model is sed for MySQL.

I ended up stripping almost all comments out as they were formatting funny.

JavaScript for node.js 0.10

```
#!/usr/bin/env node
var path = require('path');
var size = require('window-size');
var mysql = require('mysql');

/*****
Yargs setup array. This beast describes the arguments we take, help, and defaults.
See https://github.com/bcoe/yargs for detailed docs

*****/
var argv = require('yargs')
.usage('Usage: $0 --host=192.168.5.100 --user=dude --pass=SurfsUp! -D LexPaper ')
.example('$0 --host=192.168.5.100 --user=jep --pass=rats --db=LexPaper')
.demand(['host', 'user', 'pass', 'db'])
.describe('host', 'name/IP address of computer with MySQL running')
.describe('user', 'User name to login to MySQL with')
.describe('pass', 'Password to login to MySQL with')
.describe('db', 'MySQL Database that we will be using')
// .describe('', '')

Solution

if (typeof wait.counter == 'undefined' ) {


Checking if something is undefined by checking it's type is unnecessary. Since undefined is a falsey value, simply putting the thing you'd like to check in a conditional will do the trick:

if( !wait.counter ) {


console.log ("count= " + ++(wait.counter) + "  queryComplete= " + queryComplete);


All those + signs between "count= " and " queryComplete= " are a little confusing. It seems like you tried to make it more readable by putting ()s around wait.counter, but it would be more readable if you just moved ++wait.counter to a line of it's own.

++wait.counter;
console.log ("count= " + wait.counter + "  queryComplete= " + queryComplete);


Thanks to IsmaelMiguel for recommending to move the ++wait.counter out of console.log all together.

Add some documentation to your functions to describe what they are doing, what they need, why they need it, and what it is returning.

This is all I can review, as I am not familiar with SQL.

P.S.

fixed = true;
break;

Code Snippets

if (typeof wait.counter == 'undefined' ) {
if( !wait.counter ) {
console.log ("count= " + ++(wait.counter) + "  queryComplete= " + queryComplete);
++wait.counter;
console.log ("count= " + wait.counter + "  queryComplete= " + queryComplete);
fixed = true;
break;

Context

StackExchange Code Review Q#94829, answer score: 4

Revisions (0)

No revisions yet.