Example #1
0
function startDatabase(){

    database.connect();

    database.query("CREATE DATABASE IF NOT EXISTS texterDB");
    database.query("USE texterDB");

    database.query("CREATE TABLE IF NOT EXISTS messages ( id bigint unique auto_increment primary key, userName varchar(255), " +
		   "message varchar(4096), room varchar(255) )");
}
Example #2
0
function query( sql, value, cb ){
	console.log( sql );
	var callback = function( err, docs ){
		
		cb( err, docs );
	}	
	if( Object.prototype.toString.call( value ) == '[object Function]' ){
		cb = value;
		client.query( sql, callback );
	} else {
		console.log( value );
		client.query( sql, value, callback );
	}	
}
 client.query("SELECT * FROM test1", function(err, result_test1) {
   client.query("SELECT * FROM test2", function(err, result_test2) {
     result_test1.length.should.equal(0);
     result_test2.length.should.equal(0);
     done();
   });
 });
Example #4
0
 client.query('INSERT INTO test1(title) VALUES(?)', ["foobar"], function() {
   client.query('INSERT INTO test2(title) VALUES(?)', ["foobar"], function() {
     client.query('CREATE TABLE schema_migrations (id INTEGER NOT NULL AUTO_INCREMENT, version VARCHAR(255) NOT NULL, PRIMARY KEY(id));', function() {
       client.query('INSERT INTO schema_migrations(version) VALUES(?)', ["20150716190240"], done);
     });
   });
 });
Example #5
0
 client.query("DROP TABLE test1", function() {
   client.query("DROP TABLE test2", function() {
     client.query("DROP TABLE schema_migrations", function() {
       done();
     })
   });
 });
Example #6
0
// la mise à jour prend les paramètres suivants :
// - table
// - hash where (identique read, delete)
// - hash values (identique insert)
// - callback
function update(table, where, values, callback) {
	var whereClause = hashToClause(where, ' AND ');
	var valuesClause = hashToClause(values, ' AND ');
	var q = 'UPDATE ' + table + ' SET ' + valuesClause.clause + ' WHERE ' +
		whereClause.clause + ';';
	client.query(q, whereClause.values.concat(valuesClause.values), callback);
}
 client.query('CREATE TABLE test1 (id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', function() {
   client.query('CREATE TABLE test2 (id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', function() {
     client.query('INSERT INTO test1(title) VALUES(?)', ["foobar"], function() {
       client.query('INSERT INTO test2(title) VALUES(?)', ["foobar"], done);
     });
   });
 });
  beforeEach(function(done) {
    client.query('CREATE DATABASE database_cleaner', function(err) {
      if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
         throw err;
      }
    });

    client.query('CREATE TABLE test1 (id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', function() {
      client.query('CREATE TABLE test2 (id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', function() {
        client.query('INSERT INTO test1(title) VALUES(?)', ["foobar"], function() {
          client.query('INSERT INTO test2(title) VALUES(?)', ["foobar"], done);
        });
      });
    });

  });
Example #9
0
	module.exports.getCalendar = function ( email, cb ) {
		if (!_ready) return false;
		_db.query('SELECT calendar_cache, calendar_cache_updated FROM users WHERE email = ?', [email], function(err, result) {
			var row = result[0];
			cb( row.calendar_cache, row.calendar_cache_updated );
		});
		return true;
	};
 afterEach(function(done) {
   client.query("DROP TABLE test1", function() {
     client.query("DROP TABLE test2", function() {
       client.end();
       done();
     });
   });
 });
  beforeEach(function(done) {
   client.query('CREATE DATABASE database_cleaner', function(err) {
     if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
       throw err;
     }
     done();
   });
 });
Example #12
0
function db_sendMessages(roomName, socket){
    database.query( "select * from messages where room=? order by id desc limit "+MSG_LENGTH, [roomName], function(err, rows){
	if(err){
	    throw err;
	}
	socket.emit('chatHistory', { room: roomName, chatHistory: rows });
    });
}
Example #13
0
	module.exports.tallyDeliver = function ( uid, delay, time ) {
		if (!_ready) return false;
		time = Math.floor( time.getTime() / 1000 );
		_db.query('INSERT INTO stat_delivers (user, duration, count, from_time, to_time, created_at, updated_at) VALUES (?, ?, 1, FROM_UNIXTIME(FLOOR( ? / 604800 )*604800), FROM_UNIXTIME(FLOOR( 1 + ? / 604800 )*604800), NOW(), NOW() ) ' +
			'ON DUPLICATE KEY ' +
			'UPDATE count = count + 1, updated_at = NOW()', [uid, delay, time, time]);
		return true;
	};
Example #14
0
	module.exports.getUser = function ( email, cb ) {
		if (!_ready) return false;
		_db.query('SELECT * FROM users WHERE state = 2 AND email = ?', [email], function(err, result) {
			var row = result[0];
			cb( row.id, row.email, row.refresh_token, row.timezone, row.override, row.override_mode, row.calendar_id );
		});
		return true;
	}
Example #15
0
// Nous pouvons à présent écrire les fonctions CRUD qui seront exposées par
// le module
 
// L'insertion prend en paramètres
// - le nom de la table
// - Un hash ayant pour clés les noms de colonnes, pour valeurs les valeurs
// associées respectives
// - Un callback (optionnel) au format function(err, data)
function insert(table, values, callback) {
	// On construit la requête dynamiquement
	var q = 'INSERT INTO ' + table + ' SET ';
	var clause = hashToClause(values, ', ');
	q += clause.clause + ';';
	// On envoie la reqûete avec le callback fourni.
	// Les paramètres dans clause.values sont automatiquement échappés.
	client.query(q, clause.values, callback);
}
Example #16
0
	module.exports.setCalendar = function ( email, calendar, cb ) {
		if (!_ready) return false;
		var updated = Date.now();
		_db.query('UPDATE users SET calendar_cache = ?, calendar_cache_updated = ? WHERE email = ?', [calendar, updated, email], function(err, result) {
			if (cb)
				cb( calendar, updated );
		});
		return true;
	};
Example #17
0
// La lecture prend les mêmes paramètres que la suppression à l'exception
// du troisième qui précise les colonnes qui sont ramenées dans un tableau.
// si le paramètre est null, on exécute un SELECT *
function read(table, where, columns, callback) {
	var columnsClause = (columns ? columns.join(', ') : '*');
	var q = 'SELECT ' + columnsClause + ' FROM ' + table;
	if (where) {
		var clause = hashToClause(where, ' AND ');
		q += ' WHERE ' + clause.clause;
	}
	client.query(q, (where ? clause.values : callback), callback);
}
Example #18
0
	module.exports.getUsers = function ( cb ) {
		if (!_ready) return false;
		_db.query('SELECT * FROM users WHERE state = 2', function(err, result) {
			for (var i = 0; i < result.length; i += 1) {
				var row = result[i];
				cb( row.id, row.email, row.refresh_token, row.timezone, row.override, row.override_mode, row.calendar_id );
			}
		});
		return true;
	}
  beforeEach(function(done) {
    client.query('CREATE DATABASE database_cleaner', function(err) {
      if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
        throw err;
      }
    });

    async.series([
      queryClient('CREATE TABLE test1 (id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []),
      queryClient('CREATE TABLE test2 (id INTEGER NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, PRIMARY KEY(id))', []),
      queryClient('INSERT INTO test1(title) VALUES(?)', ["foobar"]),
      queryClient('INSERT INTO test2(title) VALUES(?)', ["foobar"]),
      queryClient('CREATE TABLE schema_migrations (id INTEGER NOT NULL AUTO_INCREMENT, version VARCHAR(255) NOT NULL, PRIMARY KEY(id));', []),
      queryClient('INSERT INTO schema_migrations(version) VALUES(?)', ["20150716190240"])
    ], done);
  });
Example #20
0
exports.InsertMsg = function (msg) {

    var connection = new sql.createConnection(config);
    connection.connect();
    console.log('form time '+moment().format("YYYY-MM-DD HH:mm"));

    var param=[
        msg.to,
        msg.from,
        moment().format("YYYY-MM-DD HH:mm"),
        JSON.stringify(msg.content)
    ]
    connection.query(insertSql,param,function(err0,res0){
        console.dir(err0);
        console.dir(res0);
    });

}
Example #21
0
	conn.connect(function(err) {
		if(err) {
			console.log(err);
			callback(null, err);
			return;
		}

		conn.query(sql, function(err, data) {

			if(err) {
				console.log(err);
				callback(null, err);
			}
			else {
				callback(data, null);
			}

			conn.end();
		});
	});
 client.query('INSERT INTO test1(title) VALUES(?)', ["foobar"], function() {
   client.query('INSERT INTO test2(title) VALUES(?)', ["foobar"], done);
 });
Example #23
0
function db_addMessage(msg, user, roomName){
    var newMessage = [user, msg, roomName];
    database.query( "insert into messages(userName, message, room) values( ? , ? , ? );", newMessage);
}
Example #24
0
exports.query = function(query, values, callback) {
	return client.query(query, values, callback);
}
Example #25
0
// La suppression prend en paramètres :
// - La table sur laquelle elle est effectuée
// - Un hash ayant pour clés les colonnes contraintes, pour valeurs les
// contraintes. Les différentes contraintes sont des égalitées liées par des
// 'AND'.
function remove(table, where, callback) {
	var q = 'DELETE FROM ' + table + ' WHERE ';
	var clause = hashToClause(where, ' AND ');
	q += clause.clause;
	client.query(q, clause.values, callback);
}
var queryClient = _.curry(function(query, values, next) {
  client.query(query, values, next);
});
 databaseCleaner.clean(client, function() {
   client.query("SELECT * FROM schema_migrations", function(err, result) {
     result.length.should.equal(0);
     done();
   });
 });