Thursday 29 May 2014

Alloy Collection CRUD

Alloy Collection CRUD Operations :


In Titanium Alloy, We are using many ways to Update, Delete, Insert or Replace Operation to Database Table.

I have using below way to CRUD Operation on Database Table.

Let's Start From Example : 

Models : Employee.js

exports.definition = {
    config : {
        columns : {
            "Name" : "TEXT",
            "Description" : "TEXT",
            "Gender" : "TEXT"
        },
        adapter : {
            type : "sql",
            collection_name : "employee"
        }
    },
    extendModel : function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
        });

        return Model;
    },
    extendCollection : function(Collection) {
        _.extend(Collection.prototype, {
            // extended functions and properties go here

            insertRecord : function(opts) {
                var collection = this;
       var dbName = collection.config.adapter.db_name;
       var table = collection.config.adapter.collection_name;
       var columns = collection.config.columns;
       var names = [], q = [];
       for (var k in opts.query.columns) {
            names.push(opts.query.columns[k]);
            q.push("?");
       }
       var sql = "INSERT INTO " + table +
                 " (" + names.join(",") + ") VALUES
                 (" +   q.join(",") + ");";

       db = Ti.Database.open(collection.config.adapter.db_name);
       db.execute(sql, opts.query.value);
       db.close();
       collection.trigger('sync');

            },

           insertORReplaceRecord : function(opts) {
       var collection = this;
       var dbName = collection.config.adapter.db_name;
       var table = collection.config.adapter.collection_name;
       var columns = collection.config.columns;
       var names = [], q = [];
       for (var k in opts.query.columns) {
            names.push(opts.query.columns[k]);
            q.push("?");
       }
       var sql = "INSERT OR REPLACE INTO " + table +
                 " (" + names.join(",") + ") VALUES
                 (" +   q.join(",") + ");";

       db = Ti.Database.open(collection.config.adapter.db_name);
       db.execute(sql, opts.query.value);
       db.close();
       collection.trigger('sync');

            },

            updateRecord : function(opts) {           

       var collection = this;
       var dbName = collection.config.adapter.db_name;
       var table = collection.config.adapter.collection_name;
       var columns = collection.config.columns;

       var names = [], whereQ = [], values=[];       
       
       for (var i in opts.query.columns) {
           names.push(opts.query.columns[i]+"=?");
       }
       for (var i in opts.query.whereKey) {
           whereQ.push(opts.query.whereKey[i]+"=?");
       }
               
        //Values of Set Columns and Where Condition
        for (var j in opts.query.values) {
            values.push(opts.query.values[j]);
        }
        for (var k in opts.query.whereValue) {
            values.push(opts.query.whereValue[k]);
        }
                
                var sql = "UPDATE " + table + " SET " + 
                 names.join(",") + " WHERE "+ whereQ.join(" AND ");

       db = Ti.Database.open(collection.config.adapter.db_name);
       db.execute(sql, values);
       db.close();
       collection.trigger('sync');

            },

            deleteRecord : function(opts) {
       var collection = this;
       var dbName = collection.config.adapter.db_name;
       var table = collection.config.adapter.collection_name;
       var columns = collection.config.columns;
       var names = [], q = [];
       for (var k in opts.query.columns) {
            names.push(opts.query.columns[k]);
            q.push("?");
       }
       var sql = "
DELETE FROM " + table + " " + opts.query.sql;

       db = Ti.Database.open(collection.config.adapter.db_name);
       db.execute(sql, opts.query.
params);
       db.close();
       collection.trigger('sync');

            },
          
          deleteAllRecords : function() {
       var collection = this;
       var dbName = collection.config.adapter.db_name;
       var table = collection.config.adapter.collection_name;
       var sql = "
DELETE FROM " + collection.config.adapter.collection_name;

       db = Ti.Database.open(collection.config.adapter.db_name);
       db.execute(sql
);
       db.close();
       collection.trigger('sync');

            }
        });
        return Collection;
    }
};
 

You have checked above js file of  Employee.js  which is a model file of the sample project.
we are going to apply Operations on this Employee mode:

Lets start the operation mean how can i cal the all operation from js files.

1. Insert Record to Employee Collection : 

Alloy.Collections.Employee.insertRecord({
  query : {
     columns : ["
Name", "Description", "Gender"],
     value : ["JOHN", "NO WORDS", "MALE"]
   }
 });

Query : INSERT INTO Employee
("NAME", "Description", "Gender") VALUES
("JOHN", "NO WORDS", "MALE");

 

2. Insert OR Replace Record to Employee Collection : 

Alloy.Collections.Employee.insertORReplaceRecord({
  query : {
     columns : ["
Name", "Description", "Gender"],
     value : ["CARTER", "NO WORDS", "FEMALE"]
   }
 });

Query : INSERT OR REPLACE INTO Employee
("NAME", "Description", "Gender") VALUES
("CARTER", "NO WORDS", "FEMALE");

 

3. Upadate Record to Employee Collection : 

Alloy.Collections.Employee.updateRecord({
  query : {
     columns : ["
Name", "Description"],
     value : ["JOHN Deny", "I AM DEVELOPER"],
     whereKey : ["Name", "Gender"],
     whereValue : ["JOHN", "MALE"]
   }
 });

Query : UPDATE Employee
SET Name="JOHN Deny",Description="I AM DEVELOPER"
WHERE NAME="JOHN" AND Gender="MALE";

 

4. Delete Record to Employee Collection : 

Alloy.Collections.Employee.deleteRecord({
  query : {
     sql :
"WHERE Name=?",
     params : "JOHN"
   }
 });

Query : DELETE FROM Employee
WHERE Name="JOHN"

 

5. Delete All Records to Employee Collection : 

Alloy.Collections.Employee.deleteAllRecords();

Query : DELETE FROM Employee;

 

 

 

 

 

Monday 5 May 2014


Titanium Module Guides and Tricks : 



TiModule.h' file not found:
 

you need to edit your titanium.xcconfig TITANIUM_SDK to reflect your path...
most commonly the needed change is from the defaul:

TITANIUM_SDK = /Library/Application Support/Titanium/mobilesdk/osx
               /$(TITANIUM_SDK_VERSION)

to:

TITANIUM_SDK = ~/Library/Application Support/Titanium/mobilesdk/osx
                /$(TITANIUM_SDK_VERSION)
 
 
 

Adding Special Compiler Directives in module.xcconfig :  

If your module needs a special Framework or other system library or special compiler directives, you can use the module's xcconfig file to define them. Titanium automatically creates a file named module.xcconfig and one named titanium.xcconfig. The titanium.xcconfig is used when compiling your module for packaging. However, module.xcconfig is used by the application compiler when the application is built and your module is referenced. This allows you to control the compiler directives used during this process, too.

First, we'll need to add the Framework in Xcode. In this example, we'll simply add GameKit. Select Frameworks in your project folder, right click "Add -> Existing Frameworks" and select the menu item And choose "GameKit.framework" and click the "Add" button. You should now see the new Framework reference under the Frameworks folder:

If you compile your project, you should now have no errors.

Now that we've added the Framework to the project, we still need to set it up so that the application compiler can also import this Framework during compile. To do that, edit the module.xcconfig and add the following line at the bottom:

OTHER_LDFLAGS=$(inherited) -framework GameKit

If we have number of Frmework in the module then you can edit the module.xcconfig and add the following way :

OTHER_LDFLAGS=$(inherited) -framework AddressBook -framework AddressBookUI -framework UIKit -framework Foundation -framework CoreGraphics

 

Adding Library in module.xcconfig :  

Framework Can be added by above way in module.xcconfig file , but what I really wanted to know was how to set path subproject library file such as "xxxx.a" and header "xxx.h". First i built it as one project, but it was hard to set path header when i package it by "builder.py". Now I solved this problem. I changed like the following. :

I built subprojects each and then get the header and lib(.a)file each.
Then just added those titanium module project in XCODE. Then, set path Header search path in XCODE's Building Setting.

I built subprojects each and then get the header and lib(.a)file each. Then just added those titanium module project in XCODE. Then, set path Header search path in XCODE's Building Setting.

Also added path such as .dylib in module.xconfig like this.  :

other_ldflags = $(inherited) -framework CFNetwork /usr/lib/libiconv.dylib

eg) CFNetwork is framework, libiconv.dylib is usr/lib libarary file.

 

Adding fireEvent in Module and Catch in Titanium: 

 

 testModule.m

#pragma Public APIs
-(id)example:(id)args
{
    if([self _hasListeners:@"addProgress"]){
        NSLog(@"[INFO] firing from iOS method ==> -(id)example:(id)args");
        [self fireEvent:@"addProgress"];
    } else {
        NSLog(@"[INFO] Not firing..1");
    }
    // example method
    return @"hello world.....";
}

index.js from Titanium

// TODO: write your module tests here
var test = require('com.test');
Ti.API.info("module is => " + test);


test.addEventListener("addProgress", function(e) {
    Ti.API.info("addProgress from Titanium");
    alert("Event Fired!");
}); 


You can refer below links for module :
  1. http://www.slideshare.net/omorandi/ticonf
  2. http://developer.appcelerator.com/question/51751/how-to-fire-an-event-in-a-module-and-catch-it-in-js