You can skip the step of sending this to a ByteArray and
Base64Encoding it, by using the additional affinities added for AIR
applications.  For example, using the following create statement:

CREATE TABLE vo (objID String UNIQUE NOT NULL, objValue Object);

You can then store the objects directly using a parameterized query
like the following:

myStatement.text = "INSERT INTO vo VALUES (:objID, :objValue);";
myStatement.parameters[":objID"] = vo.uid;
myStatement.parameters[":objValue"] = vo;
myStatement.execute();
 
Reading them out of the database is done with a simple select:

myStatement.text = "SELECT objValue FROM vo WHERE objID = :objID";
myStatement.parameters[":objID"] = textInput.text;
myStatement.execute();

The SQLResult.data property will then have a single object in the
array (should it be found) that is the stored object (no conversion
from bytearray, or encoding required).  You still have to use the
[RemoteObject] metadata.  Don't forget that this approach occludes the
 object from creating selective queries, but, you can hoist properties
that are deemed necessary to columns in the table and allow searching
against those.
  

--- In [email protected], "jer_ela" <[EMAIL PROTECTED]> wrote:
>
> Here is one I have been working on.  It serializes the object with AMF
> so it only needs one table.  Note that your classes need to have a
> [RemoteClass] metadata tag ( no alias needed) in order for them to be
> deserialized correctly.  Note that the static initialize method needs
> to be called before the class can be instantiated.
> 
> package CloudSplitter.CodeGenerator.business
> {     
>       import flash.data.SQLConnection;
>       import flash.data.SQLResult;
>       import flash.data.SQLStatement;
>       import flash.errors.SQLError;
>       import flash.filesystem.File;
>       import flash.utils.ByteArray;
>       
>       import mx.controls.Alert;
>       import mx.utils.Base64Decoder;
>       import mx.utils.Base64Encoder;
> 
>       public class PersistanceManager
>       {
>               static private var connection:SQLConnection;
>               private var tableName:String = "persistant_object";
>               
>               static public function initialize(persistanceDbName:String):void
>               {
>                       if (connection == null)
>                       {       connection = 
> createConnection(persistanceDbName);
>                       }
>               }
>               
>               static public function getConnection():SQLConnection
>               {
>                       if (connection == null)
>                       {       
>                               throw new Error("PersistanceManager has not 
> been initialzed");
>                       }
>                       else
>                       {
>                               return connection;
>                       }
>                       
>               }
>               
>               public function PersistanceManager()
>               {
>                       if (connection == null)
>                       {       
>                               throw new Error("PersistanceManager can not be 
> instantiated until
> it has been initialzed");
>                       }
>               }
>               
>               static private function
> createConnection(persistanceDbName:String):SQLConnection
>               {
>                       var connection:SQLConnection = new SQLConnection();
>                       var dbFile:File =
> File.applicationStorageDirectory.resolvePath(persistanceDbName);
>                       try
>                       {
>                               connection.open(dbFile);
>                       }
>                       catch (error:SQLError)
>                       {
>                               showError(error);
>                       }       
>                       return connection;              
>               }
>               
>               public function createPersistanceTable():Boolean
>               {
>                       var createStmt:SQLStatement = new SQLStatement();
>                       createStmt.sqlConnection = connection;
>                       var sql:String =
>                               "CREATE TABLE IF NOT EXISTS " + tableName + " ( 
> " +
>                                       tableName + "_id INTEGER PRIMARY KEY 
> AUTOINCREMENT" +
>                               "       ,identifier TEXT UNIQUE NOT NULL" +
>                               "       ,object_data BLOB" +
>                               ")";
>                       createStmt.text = sql;
>                       try
>                       {
>                               createStmt.execute();
>                               var success:Boolean = true;
>                       }
>                       catch (error:SQLError)
>                       {
>                               showError(error);
>                               success = false;
>                       }
>                       return success;
>               }
>               
>               
>               public function persistObject(identifier:String, 
> object:Object):void
>               {
>                       var byteArr:ByteArray = new ByteArray();
>                       byteArr.writeObject(object);
>                       var encoder:Base64Encoder = new Base64Encoder();
>                       encoder.encodeBytes(byteArr);
>                       var encoded:String = encoder.toString()
>                       var sqlResult:SQLResult = retrieveRecord(identifier);
>                       if (sqlResult.data == null)
>                       {
>                               var sql:String = 
>                                       "INSERT INTO " + tableName +
>                                       "       ( identifier, object_data ) " +
>                                       "       values ( '" + identifier + "', 
> '" + encoded + "' )";
>                       }
>                       else
>                       {
>                               sql = 
>                                       "UPDATE " + tableName +
>                                       "       SET object_data = '" + encoded 
> + "' " +
>                                       "       where  identifier = '" + 
> identifier + "'";
>                       }
>                       execSql(sql);
>               }
>               
>               private function retrieveRecord(identifier:String): SQLResult
>               {
>                       var sql:String = 
>                               "SELECT * FROM " + tableName +
>                               "       WHERE identifier = '" + identifier + 
> "'";
>                       
>                       var sqlResult:SQLResult = execSql(sql);
>                       return sqlResult
>               }
>               
>               public function retrieveObject(identifier:String):Object
>               {
>                       var sqlResult:SQLResult = retrieveRecord(identifier);
>                       var retrievedObject:Object;
>                       if (sqlResult.data != null)
>                       {       
>                               var encodedObject:String = 
> sqlResult.data[0].object_data;
>                               var decoder:Base64Decoder = new Base64Decoder();
>                               decoder.decode(encodedObject);
>                               var byteArr:ByteArray = decoder.toByteArray();
>                               retrievedObject = byteArr.readObject();
>                       }
>                       return retrievedObject;
>               }
>               
>               public function deleteObject(identifier:String):void
>               {
>                       var sql:String = 
>                               "DELETE  FROM " + tableName +
>                               "       WHERE identifier = '" + identifier + 
> "'";
>                       var result:SQLResult = execSql(sql);
>               }
>               
>               private function execSql(sql:String):SQLResult
>               {
>                       var statement:SQLStatement = new SQLStatement();
>                       statement.text = sql;
>                       statement.sqlConnection = connection;
>                       statement.execute();
>                       var sqlResult:SQLResult = statement.getResult();
>                       return sqlResult
>               }
>               
>               static private function showError(error:SQLError):void
>               {
>                       Alert.show("Details: " + error.details, "Error message: 
> " +
> error.message); 
>               }
> 
>       }
> }
>   
> 
> --- In [email protected], "Cameron Childress" <cameronc@>
> wrote:
> >
> > Is anyone aware of any Lightweight AIR Persistence mechanisms (in
> the spirit
> > of Hibernate) for VOs?  I am working alot of AIR and SQLite and
> mostly when
> > I am using SQLite, I'm just mimicking the VO structure for storing
> objects
> > and then bringing them back out later.  It's alot more code than I
> like to
> > write and it just doesn't feel right.
> > 
> > I'd love to be able to say "myValueObject.save(dbAlias)" or
> > "myDBEngine.save(myValueObject)" and have my object stored in a
> SQLite DB
> > for later retrieval, creating tables on the fly as needed based on
> the VO's
> > property datatypes.  Anyone aware of any efforts along these lines?
> > 
> > Any other ways of looking at this problem that I am simply missing?
> > 
> > -Cameron
> > 
> > -- 
> > Cameron Childress
> > Sumo Consulting Inc
> > http://www.sumoc.com
> > ---
> > cell: 678.637.5072
> > aim: cameroncf
> > email: cameronc@
> >
>


Reply via email to