Home > node-poormansmysql

node-poormansmysql

Node-poormansmysql is a project mainly written in JavaScript, based on the MIT license.

MySQL driver module for node.js that executes queries using the mysql command-line client.

node-poormansmysql

node-poormansmysql is a MySQL driver for node.js that executes queries using the mysql command-line client.

Requirements

  • node.js 0.1.92+
  • libxmljs -- tested with 7903666cd29001b19fb0
    • Note: libxmljs.node should be placed in the same directory as the node-poormansmysql.js module.

Documentation

MysqlConnection Methods:

  • (constructor) MysqlConnection(config)

    • Description: Performs initialization and sets the connection configuration. All properties supplied in config overwrite the defaults listed below.
    • Parameters:

      • config: (Object) An object containing the necessary connection information to connect to the MySQL server and perform queries. At least the user and password should be supplied. The default configuration is:

        user: null,
        password: null,
        db: null,
        host: 'localhost',
        port: 3306,
        force: false, // execute ALL queries in the case of a query error
        connect_timeout: 0, // the time to wait for a connection to the MySQL server (in seconds)
        execute_timeout: 0 // the time to wait for ALL queries to execute (in seconds) (TODO)
    • Return Value: (MysqlConnection) An instance of MysqlConnection.
  • setConfig(config)
    • Description: Sets the connection configuration. This is only needed if you are reusing the instance and need to change the connection information. As with the constructor, all properties supplied in config are merged with the defaults.
    • Parameters:
      • config: (Object) An object containing the necessary connection information to connect to the MySQL server and perform queries. At least the user and password should be supplied. See above for the default configuration.
    • Return Value: None
  • query(sql [, cbRow[, cbComplete[, cbError]]])
    • Description: Enqueues an SQL statement.
    • Parameters:
      • sql: (String) A valid SQL statement. Do not include a trailing semicolon at the end of the statement.
      • cbRow (Optional): (Function) A callback for when a row resulted from the query.
        • Parameters:
          • row: (Object) A hash containing a single row from the results of the query.
      • cbComplete (Optional): (Function) A callback for when the query has completed and all rows have been returned.
        • Parameters:
          • totalRows: (Integer) The total number of rows returned by the query.
      • cbError (Optional): (Function) A callback for when the query results in a MySQL error.
        • Parameters:
          • err: (String) The entire MySQL error message.
    • Return Value: None
  • execute([cbQueriesComplete])
    • Description: Begins executing the enqueued SQL statements all at once.
    • Parameters:
      • cbQueriesComplete (Optional): (Function) A callback that is called when each query in the queue has completed.
        • Parameters: None
    • Return Value: (Boolean) true if there were statements to be executed and the 'user' and 'password' connection details have been supplied, false otherwise.
  • (getter) affectedRows
    • Description: Returns the number of rows updated, inserted, or deleted by the last SQL statement.
    • Return Value: (Integer) The number of rows affected by the last SQL statement.
  • (getter) lastInsertId
    • Description: Returns the first automatically generated value that was set for an AUTO_INCREMENT column by the most recent INSERT statement. See here for more details.
    • Return Value: (Integer) The value of the AUTO_INCREMENT column for the last INSERTed row.
  • (getter) lastError
    • Description: Returns the last error set by the MysqlConnection instance (either a child process spawn error or a MySQL error).
    • Return Value: (String) The value of the last error.

MysqlConnection Events:

  • error
    • Description: Fired when an error occurs. This can be either a non-query-specific (i.e. system or server-specific) MySQL error or a child process spawning error.
    • Parameters:
      • err: (String) The error details.

Utility methods exposed:

  • escape(str)
    • Description: Performs basic string escaping, similar to the PHP function mysql_escape_string.
    • Parameters:
      • str: (String) The string to escape.

Example

var pmm = require('./node-poormansmysql'), sys = require('sys');

var fieldvalue = "o'reilly";
var conn = new pmm.MysqlConnection({user: 'foo', password: 'bar', db: 'baz'});

conn.addListener('error', function(err) {
    sys.puts('Uh oh, ' + err);
});

conn.query("SELECT * FROM table",
    function(row) {
        sys.puts("Got result for first query: " + JSON.stringify(row));
    },
    function(totalRows) {
        sys.puts("First query is done! Retrieved " + totalRows + " row(s)");
    },
    function(err) {
        sys.puts("First query resulted in error: " + err);
    }
);
conn.query("SELECT * FROM table2 WHERE field = '" + pmm.escape(fieldvalue) + "'",
    function(row) {
        sys.puts("Got result for second query: " + JSON.stringify(row));
    },
    function(totalRows) {
        sys.puts("Second query is done! Retrieved " + totalRows + " row(s)");
    },
    function(err) {
        sys.puts("Second query resulted in error: " + err);
    }
);
conn.execute();

License

See LICENSE file.

Previous:ci