dracoblue.net

Escaping without NO_BACKSLASH_ESCAPE in Mysql

While working with Node.JS and dbslayer today, I was facing a weird issue with escaping strings.

Best thing would have been, if I could have forced my server to use NOBACKSLASHESCAPES as sql-mode and then just escape the ' to ''.

But since this server is also used by other programs and NOBACKSLASHESCAPES option cannot be set by option for dbslayer I had to turn it off again.

I ended up with this tiny escape string method:

var db_escape_string = function(string) {
    return string.replace(/([\\\n\r])/g, "\\$&").replace("'", "''", 'g');
};

It replaces \ (backslash), newline and carriage return with \, \n and \r. Since also single quote needs to be escaped, I finally replace all ' with ''.

You may wonder why I do not escape ". The issue is, that a " appearing within a string in between ' does not need to be escaped.

'this is a test"2' => 'this is a test\"2'

That's why I sticked to the rule to put strings within single quote and do not escape those question marks at all.

In javascript, mysql, node.js by DracoBlue @ 21 Mar 2010 | 188 Words

Operation failed. File system input or output error

A while back I explained how to mount sftp/ssh folders in eclipse.

When working with eclipse or zend studio on remote systems, there are times when I get the message "Operation failed. File system input or output error" very often.

Even though several posts in zend forums suggest, that you should restart your IDE in this case, I tried to track down what causes the issue.

I ended up with the following: The ssh-host has already closed the connection, but eclipse thinks it's still available. So it tries to use the connection, but fails.

Whenever I get this issue now, I go into "Remote Systems"-View, right-click the Connection and choose "Disconnect" from the context menu. Right after that, I open that context menu again and press "Connect". Problem resolved.

In eclipse by DracoBlue @ 14 Mar 2010 | 131 Words

What means T_PAAMAYIM_NEKUDOTAYIM?

This is an PHP-Tokenizer error, which just wants to tell you that there are two colons. ;)

In php by DracoBlue @ 10 Mar 2010 | 17 Words

Chain + Group Callbacks for NodeJS

If one develops with Node.JS and wants to use the full event-loop power, you got to use callbacks for everything.

That said, it looks ugly to do that:

posix.rename("/tmp/hello", "/tmp/world").addCallback(function () {
  posix.stat("/etc/passwd").addCallback(function (stats) {
    sys.puts("stats: " + JSON.stringify(stats));
  });
});

The

api advices to use:

posix.rename("/tmp/hello", "/tmp/world").wait();
var stats = posix.stat("/etc/passwd").wait();
sys.puts("stats: " + JSON.stringify(stats));

but this is a problem, too. Why? Because you can't do that too often (just ~10 times) at the same time.

That's why I created two little helper methods:

chain and group.

Continue reading ...

In javascript, node.js, open source by DracoBlue @ 17 Feb 2010 | 503 Words

Linear least squares in Javascript

Today I ran into the problem, that I had a graph full of data points, which obviously did not form a straight line.

There is a method called

Linear least squares to calculate the straight line with least difference to the original data points.

Since I needed the method in javascript, here is what I came up with in the end.

function findLineByLeastSquares(values_x, values_y) {
    var sum_x = 0;
    var sum_y = 0;
    var sum_xy = 0;
    var sum_xx = 0;
    var count = 0;

    /*
     * We'll use those variables for faster read/write access.
     */
    var x = 0;
    var y = 0;
    var values_length = values_x.length;

    if (values_length != values_y.length) {
        throw new Error('The parameters values_x and values_y need to have same size!');
    }

    /*
     * Nothing to do.
     */
    if (values_length === 0) {
        return [ [], [] ];
    }

    /*
     * Calculate the sum for each of the parts necessary.
     */
    for (var v = 0; v < values_length; v++) {
        x = values_x[v];
        y = values_y[v];
        sum_x += x;
        sum_y += y;
        sum_xx += x*x;
        sum_xy += x*y;
        count++;
    }

    /*
     * Calculate m and b for the formular:
     * y = x * m + b
     */
    var m = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
    var b = (sum_y/count) - (m*sum_x)/count;

    /*
     * We will make the x and y result line now
     */
    var result_values_x = [];
    var result_values_y = [];

    for (var v = 0; v < values_length; v++) {
        x = values_x[v];
        y = x * m + b;
        result_values_x.push(x);
        result_values_y.push(y);
    }

    return [result_values_x, result_values_y];
}

In algorithm, javascript by DracoBlue @ 13 Feb 2010 | 274 Words

Page 19 - Page 20 - Page 21

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About