Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('db.service', [])
- .factory("DBService", function($q) {
- var db;
- function openDb() {
- try {
- db = window.openDatabase("test", "", "test", 15000000);
- } catch (err) {
- alert("Error opening DB - " + err);
- }
- }
- function getAllEmployees(){
- var defer = $q.defer();
- var data = {"employees":[]};
- db.transaction(function(tx){
- var empQuery="SELECT Id,Name FROM employee ";
- tx.executeSql(empQuery,[],function(tx,results){
- var empLength=results.rows.length;
- for(var eIndex = 0; eIndex < empLength; eIndex++){
- var empId=results.rows.item(eIndex).Id;
- var emp=
- {"id":results.rows.item(eIndex).Id,
- "name":results.rows.item(eIndex).Name,
- "companies":[]
- }
- var companies=[];
- var companyQuery="SELECT comp.Name as CompanyName FROM company AS comp " +
- "LEFT JOIN employee_company_mapping AS ecm ON ecm.CompanyId = comp.Id " +
- "LEFT JOIN employee AS e on e.Id = ecm.EmployeeId " +
- "WHERE e.Id = "+empId;
- tx.executeSql(companyQuery,[],function(tx,compResults){
- var compLength=compResults.rows.length;
- for(var cIndex = 0; cIndex < compLength; cIndex++){
- var c={"name" :compResults.rows.item(cIndex).CompanyName}
- companies.push(c);
- }
- emp.companies.push(companies);
- data.employees.push(emp);
- },null);
- }
- defer.resolve(data);
- },null);
- });
- return defer.promise;
- }
- return{
- init:function(){
- return openDb();
- },
- fetch:function(){
- return getAllEmployees();
- }
- }
- DBService.fetch().then(function(response){
- console.log(JSON.stringify(response);
- //putting the data in $scope
- // doing other things
- }
- );
- {
- "employees":[
- {"id":1,"name":"Rosh","companies":[{"name":"google"},{"name":"ibm"}]},
- {"id":2,"name":"Pink","companies":[{"name":"asus"},{"name":"pizzahut"}]},
- {"id":2,"name":"Brahm","companies":[{"name":"tosiba"},{"name":"xiomi"}]}
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement