Skip to content

Instantly share code, notes, and snippets.

@SuperstrongBE
Created May 9, 2015 07:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuperstrongBE/85a4cd68ae04552be885 to your computer and use it in GitHub Desktop.
Save SuperstrongBE/85a4cd68ae04552be885 to your computer and use it in GitHub Desktop.
var mybApp = angular.module('ss.Constant',[])
.constant ('publicView',
[
'/app/login'
]
)
.constant('DB_CONFIG', {
name: 'mybDB_08'/*+Math.floor(Math.random()*10)*/,
tables: [
{
name: 'users',
columns: [
{name: '_id', type: 'text'},
{name: 'uid', type: 'integer'},
{name: 'username', type: 'text'},
{name: 'name', type: 'text'},
{name: 'email', type: 'text'},
{name: 'token', type: 'text'},
{name: 'currentLevel', type: 'text'},
{name: 'levelNumber', type: 'text'},
{name: 'earnedPoints', type: 'integer'},
{name: 'avatar', type: 'text'},
{name: 'lastSession', type: 'datetime'},
{name: 'lastUpdate', type: 'datetime'},
{name: 'language', type: 'integer'},
{name: 'isActive', type: 'integer'},
{name: 'prefs', type: 'text'}
]
},
{
name:'userProgress',
columns: [
{name: '_id', type: 'integer'},
{name: 'userId', type: 'integer'},
{name: 'exerciseId', type: 'integer'},
{name: 'exerciseCompleteCount', type: 'integer'},
{name: 'isComplete', type: 'integer'},
{name: 'isRemote', type: 'integer'},
{name: 'lastUpdate', type: 'datetime'},
{name: 'syncKey', type: 'text'}
]
},
{
name:'exercises',
columns: [
{name: '_id', type: 'text'},
{name: 'title', type: 'text'},
{name: 'description', type: 'text'},
{name: 'exerciseType', type: 'text'},
{name: 'points', type: 'integer'},
{name: 'minUnitCount', type: 'integer'},
{name: 'minUnitTime', type: 'integer'},
{name: 'minCycleCount', type: 'integer'},
{name: 'minExerciseCount', type: 'integer'},
{name: 'minRequestedLevel', type: 'text'},
{name: 'lastUpdate', type: 'datetime'},
{name: 'language', type: 'text'},
{name: 'rawData', type: 'text'}
]
},
{
name:'levels',
columns: [
{name: '_id', type: 'text'},
{name: 'title', type: 'text'},
{name: 'description', type: 'text'},
{name: 'levelNumber', type: 'integer'},
{name: 'minRequestedPoints', type: 'integer'},
{name: 'maxRequestedPoints', type: 'integer'},
{name: 'lastUpdate', type: 'datetime'},
{name: 'language', type: 'text'}
]
},
{
name:'config',
columns: [
{name: '_id', type: 'text'},
{name: 'rawConfig', type: 'text'},
{name: 'lastUpdate', type: 'datetime'}
]
},
{
name:'exerciseTypes',
columns: [
{name: '_id', type: 'text'},
{name: 'title', type: 'text'},
{name: 'description', type: 'text'},
{name: 'ref', type: 'integer'},
{name: 'language', type: 'text'},
{name: 'lastUpdate', type: 'datetime'}
]
},
{
name:'tutorials',
columns: [
{name: '_id', type: 'text'},
{name: 'title', type: 'text'},
{name: 'description', type: 'text'},
{name: 'language', type: 'text'},
{name: 'lastUpdate', type: 'datetime'},
{name: 'rawData', type: 'text'}
]
},
{
name:'languages',
columns: [
{name: '_id', type: 'text'},
{name: 'label', type: 'text'},
{name: 'shortCode', type: 'text'},
{name: 'isoCode', type: 'text'},
{name: 'lastUpdate', type: 'datetime'}
]
}
]
})
.constant('DEFAULT_PREFS',{
lang:'fr_fr'
,allowNotification:true
,notificationTime:'19h30'
,preparationTime:5
})
//------- In app.js, don't forget to inject sqlite factory
angular.module('mybApp',['ngCordova'])
.run(function(DB){
$ionicPlatform.ready(function() {
DB.init();
})
}
// ---- In controller or service...
// ---- Personally is use some service to encapsulate query logic
insertUser = function (userId,uid,userName,name,email,token,currentLevel,earnedPoints,avatar,lastSession,lastUpdate,language,isActive,preferences){
return DB.query(
'INSERT INTO users (_id,uid,userName,name,email,token,currentLevel,earnedPoints,avatar,lastSession,lastUpdate,language,isActive,prefs) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
[userId,uid,userName,name,email,token,currentLevel,earnedPoints,avatar,lastSession,lastUpdate,language,isActive,preferences]
).then (function (){
//do something if succed
},function (fail){
//do something if Fail
})
};
/**
*
* Created by Rockerz on 9/01/15.
* @description
* Factory that encapsulate the user of sqlite connection
*/
angular.module('ss.SqlLite')
// DB wrapper
.factory('DB', function($q,$cordovaSQLite,DB_CONFIG) {
var self = this;
self.db = null;
/**
* @description
* Create database and corresponding tables given in DB_CONFIG
* @return void
*
*/
self.init = function() {
if (window.cordova) {
self.db = $cordovaSQLite.openDB({ name: DB_CONFIG.name }); //device
}else{
self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'my', 1024 * 1024 * 100); // browser
}
angular.forEach(DB_CONFIG.tables, function(table) {
var columns = [];
angular.forEach(table.columns, function(column) {
columns.push(column.name + ' ' + column.type);
});
var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
self.query(query);
});
};
/**
* @description
* Perform the sql query
* @param query
* The raw sql query
* @param bindings
* An ordered array that contains values to bind with the ? sign inside de raw query
* @return {promise}
*/
self.query = function(query, bindings) {
if (window.cordova) {
return $cordovaSQLite.execute(self.db, query, bindings);
}else {
var defer = $q.defer();
self.db.transaction(function(transaction) {
transaction.executeSql(query, bindings, function(transaction, result) {
defer.resolve(result);
}, function(transaction, error) {
defer.reject(error);
});
});
return defer.promise
}
};
/**
* Treat the sql result and return an array
* @param result
* @return {Array}
*/
self.fetchAll = function(result) {
var output = [];
for (var i = 0; i < result.rows.length; i++) {
output.push(result.rows.item(i));
}
return output;
};
/**
* Treat the sql result and return an object
* @param result
* @return {object}
*/
self.fetch = function(result) {
if (result.rows.length<=0) return false;
return result.rows.item(0);
};
return self;
})
@lakhassane
Copy link

Hi man. I've pretty replicate that code but I'm having the same error. (For precision I'm testing with android emulator)

@lakhassane
Copy link

Oh my bad it's working really fine. You quite save me ! Thanks a lot 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment