server Class Ape.MySQL

Summary

MySql Object (Database connection)
File
<./src/Ape_Server/modules/libape-spidermonkey.c>

MySQL Class Detail

Ape.MySQL (host, username, password, database)
Ape.MySQL is a class constructor. You can connect and use a MySQL database.
File:
<./src/Ape_Server/modules/libape-spidermonkey.c>
Author:
Special thanks to Louis Charette for repairing this!
Parameters:
{string} host
IP-address:port or unix socket
{string} username
MySql username
{string} password
MySql username
{string} database
MySql database
Returns:
Examples
//Database connection
var sql = new Ape.MySQL("ip:port", "user", "password", "database");
	sql.onConnect = function() {
	Ape.log('Connected to mysql server');
}
// Select
sql.query("SELECT * FROM table", function(res, errorNo) {
	if (errorNo) Ape.log('Request error : ' + errorNo + ' : ' + this.errorString());
	else {
	Ape.log('Fetching ' + res.length);
		res.each(function(data) {
			Ape.log(data.content);//data.
		});
	}
});
//Insert
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
	if (errorNo) Ape.log('Request error : ' + errorNo + ' : ' + this.errorString());
	else Ape.log('Inserted: ' + MySQL.getInsertId ());
});

Summary

Own
Methods Attributes Methods Name Methods Description
<static>   Ape.MySQL. errorString () Get the last mysql error message.
<static>   Ape.MySQL. escape (text) Escape a string for mysql.
<static>   Ape.MySQL. getInsertId () Get the last id insert with auto-increment.
<static>   Ape.MySQL. query (sql, fn) Execute a mysql query.

<static> {string} Ape.MySQL. errorString ()

Get the last mysql error message.

This returns the errorstring from the Database.

Returns:
{string} the last mysql error message for the connection
Example
sql.onError = function(errorNo) {
	Ape.log('Connection Error : ' + errorNo + ' : ' + this.errorString());
}
See:
Ape.MySQL.onError

<static> {string} Ape.MySQL. escape (text)

Escape a string for mysql.
Parameters:
{string} text The string to be escaped with mysql_escape_string to prepare it for a query
Returns:
{string} The escaped string
Example
sql.query("SELECT nick FROM user WHERE login = '" + Ape.MySQL.escape(mylogin) + "'");

<static> {integer} Ape.MySQL. getInsertId ()

Get the last id insert with auto-increment.

Beware: This is the last id insert for the current connection (object instance).

Returns:
{integer} Used to get the last insert id with auto-increment.for the connection
Example
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
	if (errorNo) Ape.log('Request error : ' + errorNo + ' : ' + this.errorString());
	else Ape.log('Inserted: ' + MySQL.getInsertId ());
});

<static> {void} Ape.MySQL. query (sql, fn)

Execute a mysql query.
Parameters:
{string} sql The SQL statement
{function} fn callback function
{Array} fn.res An array of objects if the query was a 'SELECT'
{integer} fn.errorNo if an error occured the errorNo != 0
Returns:
{void}
Examples
sql.query("SELECT * FROM table", function(res, errorNo) {
	if (errorNo) Ape.log('Request error : ' + errorNo + ' : ' + this.errorString());
	else {
		Ape.log('Fetching ' + res.length);
		for(var i = 0; i < res.length; i++) {
			Ape.log(res[i].title);//res[i].
		});
	}
});
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
	if (errorNo) Ape.log('Request error : ' + errorNo + ' : ' + this.errorString());
	else Ape.log('Inserted');
});

Summary

Own
Attributes Name Description
<static>   Ape.MySQL. onConnect () Mysql connection established callback.
<static>   Ape.MySQL. onError (errorNo) Mysql error occurance callback.

<static> Ape.MySQL. onConnect ()

Mysql connection established callback.

This callback is called when mysql connection (and database selection) has been established.

Examples
sql.onConnect = function() {
	Ape.log('Connected to mysql server');
}
var sql = new Ape.MySQL('ip:port', 'user', 'password', 'database');
sql.onConnect = function() {
	Ape.log('Connected to mysql server');
}
sql.onError = function(errorNo) {
	Ape.log('Connection Error : ' + errorNo + ' : ' + this.errorString());
}
See:
Ape.MySQL
Ape.MySQL.onError

<static> Ape.MySQL. onError (errorNo)

Mysql error occurance callback.

This callback is called when a mysql error occurs.

The error codes from MySac.

  1. MYSAC_START
  2. MYSAC_CONN_CHECK
  3. MYSAC_READ_GREATINGS
  4. MYSAC_SEND_AUTH_1
  5. MYSAC_RECV_AUTH_1
  6. MYSAC_SEND_AUTH_2
  7. MYSAC_SEND_QUERY
  8. MYSAC_RECV_QUERY_COLNUM
  9. MYSAC_RECV_QUERY_COLDESC1
  10. MYSAC_RECV_QUERY_COLDESC2
  11. YSAC_RECV_QUERY_EOF1
  12. MYSAC_RECV_QUERY_EOF2
  13. MYSAC_RECV_QUERY_DATA
  14. MYSAC_SEND_INIT_DB
  15. MYSAC_RECV_INIT_DB
  16. MYSAC_SEND_STMT_QUERY
  17. MYSAC_RECV_STMT_QUERY
  18. MYSAC_SEND_STMT_EXECUTE
  19. MYSAC_RECV_STMT_EXECUTE
  20. MYSAC_READ_NUM
  21. MYSAC_READ_HEADER
  22. MYSAC_READ_LINE
Parameters:
{integer} errorNo MySql MySac errorcode
Examples
sql.onError = function(errorNo) {
	Ape.log('Connection Error : ' + errorNo + ' : ' + this.errorString());
}
var sql = new Ape.MySQL("ip:port", "user", "password", "database");
sql.onConnect = function() {
	Ape.log('Connected to mysql server');
}
sql.onError = function(errorNo) {
	Ape.log('Connection Error : ' + errorNo + ' : ' + this.errorString());
}
See:
Ape.MySQL
Ape.MySQL.onConnect
Ape.MySQL.errorString