<commit_before>/**
 * Common JS module to handle data storage including offline handling
 */

// Include the StackMob stuff
Ti.include('/stackmob.cfg.js');
StackMob = require('ti.stackmob')({ publicKey : stackmob_api_key, secure : true });
var _RR = StackMob.Model.extend({ schemaName: 'rr' });

var _networkOnline = false;

exports.init = function() {
	var db = Titanium.Database.open('rr_data');
	db.execute('CREATE TABLE IF NOT EXISTS tblEvents (' +
		'event_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT' +
		', mode INTEGER NOT NULL' +
		', data_json STRING NOT NULL' +
		');');

	db.close();
	db = null;

	_networkOnline = Ti.Network.online;

	// Send any pending events
	_sendPendingData();

	Ti.Network.addEventListener("change",function(_event){
		if(_networkOnline === false && _event.online === true) {
			// Network as become available
			_sendPendingData();
		}
		_networkOnline = _event.online;
	});
}

exports.storeData = function(_mode, _data) {

	// If we not connected, no point even trying to send to StackMob
	if(!Ti.Network.online) {
		_storeInDb(_mode, _data);
		return;
	}

	var rr = new _RR({
		device: Ti.Platform.id,
		fixed_or_timed: (mode>0) ? 'timed' : 'fixed',
		fixed_or_timed_value: (mode>0) ? mode : 7,
		data: _data
	});

	// Persist the object to StackMob
	rr.create({
  		success: function(model, result, options) {
  			Ti.API.debug(model.toJSON());
  			// If we have just succeded then this is a good time to send any pending data
  			_sendPendingData();
  		},
  		error: function(model, error, options) {
  			Ti.API.debug(error);
  			// Store in database
  			_storeInDb(_mode, _data);
  		}
	});

}

_storeInDb = function(_mode, _data) {
	var db = Titanium.Database.open('rr_data');
	db.execute("INSERT INTO tblEvents (mode, data_json) VALUES(?,?);", _mode, JSON.stringify(_data,null,0) );
	db.close();
	db = null;
	Ti.API.debug("Event data stored in DB for sending later.");
}

_sendPendingData = function() {
	if(!Ti.Network.online) {
		return;
	}

	var db = Titanium.Database.open('rr_data');
	var rows = db.execute("SELECT * FROM tblEvents LIMIT 1");
	db.close();
	db = null;

	if(rows.isValidRow()) {
		var event_id = rows.fieldByName('event_id');
		var mode = rows.fieldByName('mode');
		var data = JSON.parse(rows.fieldByName('data_json'));

		var rr = new _RR({
			device: Ti.Platform.id,
			fixed_or_timed: (mode>0) ? 'timed' : 'fixed',
			fixed_or_timed_value: (mode>0) ? mode : 7,
			data: data
		});

		// Send it
		rr.create({
	  		success: function(model, result, options) {
	  			Ti.API.debug(model.toJSON());

				var db = Titanium.Database.open('rr_data');
				var rows = db.execute("DELETE FROM tblEvents WHERE event_id=?", event_id);
				db.close();
				db = null;

	  			// If we have just succeded then delete the one sent and try the next row
	  			_sendPendingData();
	  		},
	  		error: function(model, error, options) {
	  			Ti.API.debug(error);
	  		}
		});
	}

	rows.close();
}<commit_msg>Seems the appcelerator docs are wrong again :( the db can't be closed leaving a valid resultset. Also the db file as not for backup by iOS<commit_after>/**
 * Common JS module to handle data storage including offline handling
 */

// Include the StackMob stuff
Ti.include('/stackmob.cfg.js');
StackMob = require('ti.stackmob')({ publicKey : stackmob_api_key, secure : true });
var _RR = StackMob.Model.extend({ schemaName: 'rr' });

var _networkOnline = false;

exports.init = function() {
	var db = Titanium.Database.open('rr_data');
	db.execute('CREATE TABLE IF NOT EXISTS tblEvents (' +
		'event_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT' +
		', mode INTEGER NOT NULL' +
		', data_json STRING NOT NULL' +
		');');
	db.file.setRemoteBackup(false);
	db.close();
	db = null;

	_networkOnline = Ti.Network.online;

	// Send any pending events
	_sendPendingData();

	Ti.Network.addEventListener("change",function(_event){
		if(_networkOnline === false && _event.online === true) {
			// Network as become available
			 	_sendPendingData();
		}
		_networkOnline = _event.online;
	});
}

exports.storeData = function(_mode, _data) {

	// If we not connected, no point even trying to send to StackMob
	if(!Ti.Network.online) {
		_storeInDb(_mode, _data);
		return;
	}

	var rr = new _RR({
		device: Ti.Platform.id,
		fixed_or_timed: (mode>0) ? 'timed' : 'fixed',
		fixed_or_timed_value: (mode>0) ? mode : 7,
		data: _data
	});

	// Persist the object to StackMob
	rr.create({
  		success: function(model, result, options) {
  			Ti.API.debug(model.toJSON());
  			// If we have just succeded then this is a good time to send any pending data
  			_sendPendingData();
  		},
  		error: function(model, error, options) {
  			Ti.API.debug(error);
  			// Store in database
  			_storeInDb(_mode, _data);
  		}
	});

}

_storeInDb = function(_mode, _data) {
	var db = Titanium.Database.open('rr_data');
	db.execute("INSERT INTO tblEvents (mode, data_json) VALUES(?,?);", _mode, JSON.stringify(_data,null,0) );
	db.close();
	db = null;
	Ti.API.debug("Event data stored in DB for sending later.");
}

_sendPendingData = function() {
	if(!Ti.Network.online) {
		return;
	}

	var db = Titanium.Database.open('rr_data');
	var rows = db.execute("SELECT * FROM tblEvents LIMIT 1");

	if(rows.isValidRow()) {
		Ti.API.debug("Got a valid row: Count=" + rows.rowCount);
		var event_id = rows.fieldByName('event_id');
		var mode = rows.fieldByName('mode');
		var data = JSON.parse(rows.fieldByName('data_json'));

		var rr = new _RR({
			device: Ti.Platform.id,
			fixed_or_timed: (mode>0) ? 'timed' : 'fixed',
			fixed_or_timed_value: (mode>0) ? mode : 7,
			data: data
		});

		// Send it
		rr.create({
	  		success: function(model, result, options) {
	  			Ti.API.debug("SB success: Model = " + model.toJSON());

				var db = Titanium.Database.open('rr_data');
				var rows = db.execute("DELETE FROM tblEvents WHERE event_id=?", event_id);
				db.close();
				db = null;

	  			// If we have just succeded then delete the one sent and try the next row
	  			_sendPendingData();
	  		},
	  		error: function(model, error, options) {
	  			Ti.API.debug(error);
	  		}
		});
	}

	rows.close();
	db.close();
	db = null;
}
{'fraction_non_alphanumeric': 0.1480460515647803, 'fraction_numerical': 0.0025944543538187126, 'mean_word_length': 3.5386313465783665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7108487', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2174, 'n_words': 561}