Interesting... More or less just stuffs everything into a database "as
is". Doesn't leave any room for aggregate SQL statements against
stored properties, but then I didn't ask for that I guess. :)
-Cameron
On Wed, May 14, 2008 at 10:30 AM, 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" <[EMAIL PROTECTED]>
> 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: [EMAIL PROTECTED]
> >
>
>
>
> ------------------------------------
>
> --
> Flexcoders Mailing List
> FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> Search Archives:
> http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links
>
>
>
>
--
Cameron Childress
Sumo Consulting Inc
http://www.sumoc.com
---
cell: 678.637.5072
aim: cameroncf
email: [EMAIL PROTECTED]