第一个ActionScript MySQL Driver:asSQL原理与实践
关键字: Flex 熟悉Flex 的开发者都知道,在Flex中不能直接访问数据库,而是采用HTTPService/WebService/RemoteObject等方式实现。asSQL的出现多少让大家感到意外(抛开这两种方式的优劣不提),也可见它的威力。
1. 简介
大型软件系统都采取了分层设计的原则,将其分为大致表现(UI)、业务逻辑以及EIS三部分。在Java语言,直接与数据库打交道曾是家常便饭。Hibernate等O/R Mapping工具流行,直接使用JDBC编程变得少见了,但有时这种方式更加简便、速度更快。
ActionScript用于在Flash实现动画设计,访问数据库自然不是它的强项,甚至大家都觉得没有必要。asSQL的出现使许多Flash的开发 者感到惊喜(也许在Java开发人员看来这实在算不了什么)。asSQL现在是alpha版本,还很稚嫩有一些问题。但基本工作正常。
2. asSQL驱动
a)Connection
asSQL也提供了类似Java语言的Connection,确切的说asSQL中的Connection是类,而Java的Connectio是接口。 Connection负责与mySQL Server通讯(通过socket建立连接,握手,接收/发送数据包),并按照mySQL协议解析、加密数据包(此处略去协议处理细节,感兴趣的请访问 http://www.redferni.uklinux.net/mysql/MySQL-Protocol.html)。
b)MysqlService
虽然Connection是asSQL的核心对象,但与客户打交道仅有一个UI组件MysqlService。该组件需要用户输入host,port, database等信息,在内部创建Connection,这避免了用户直接操作Connection可能引发的错误。
- <controls:MysqlService id="sqlService" host="localhost" port="3306" user="root" scrambler="{new PlainTextScrambler('sa')}" database="jpetstore" response="onResponse(event)" error="onError(event)" />
将操作结果保存到lastResult中,因此它可作为数据源(data provider)供其它组件(如DataGrid)使用,如下所示:
- <mx:DataGrid x="55" y="137" dataProvider="{sqlService.lastResult}">
- <mx:columns>
- <mx:DataGridColumn headerText="UserId" dataField="userid"/>
- <mx:DataGridColumn headerText="Email" dataField="email"/>
- <mx:DataGridColumn headerText="First Name" dataField="firstname"/>
- mx:columns>
- mx:DataGrid>
在一个按钮click事件中,调用myService.send('SELECT userid,email,firstname FROM account;')即可触发上述DataGrid加载数据。
MysqlService支持3个事件,分别是results、response和errors,供调用者定制操作结果。
errors :显然是发生错误
results、response的区别是前者针对SELECT操作,后者针对UPDATE,DELETE......
遗憾的是,MysqlService设计的不够完美,Connection的创建和释放可能被分离到类内部和外部实现。如果用户提供上述事件的响应函数,就必须在该函数中显示关闭connection。如下所示:
- private function onResults(e:ResultsEvent):void{
- var st:Statement = Statement(e.target);
- var con:Connection = st.getConnection();
- var rs:ResultSet = e.resultSet;
- while ( rs.next() ) {
- var userid = rs.getString("userid");
- //var email = rs.getString(2);
- trace(userid);
- }
- con.disconnect();
- }
- private function onResponse(e:ResponseEvent):void{
- var st:Statement = Statement(e.target);
- var con:Connection = st.getConnection();
- var affectedRows:int = e.affectedRows;
- var insertID:int = e.insertID;
- con.disconnect();
- }
- private function onError(e:SQLErrorEvent):void{
- var st:Statement = Statement(e.target);
- var con:Connection = st.getConnection();
- var message:String = e.msg;
- var errorNo:int = e.id;
- var text:String = e.text; // Equals SQLError #{id}: {msg}
- con.disconnect();
- }
3. 示例
asSQL目前仅支持mySQL,它以UI组件的形式供用户调用,并以数据源的形式把数据和UI组件绑定。
开发环境:Flex Builder 2,以及MySql 5,还有Spring
步骤1:MySQL中建立一个叫做JPetStore的Database。并把JPetStore的Schema导入其中。
步骤2:从http://maclema.com/assql/downloads/asSQL-0.1alpha.swc下载asSQL SWC
步骤3:新建Flex Project项目,然后选中项目根目录,右键Properties->Flex Build Path->Library Path;点击右侧按钮Add SWC,在浏览窗口找到刚下载的SWC文件。
步骤4:编辑Flex的界面元素,增加1个ComboBox和1个DataGrid,以及2个按钮。然后编辑MXML文件。
附件是运行界面和源代码。
评论
MySQL Client/Server通讯协议大体流程(仅介绍客户认证过程):
1.客户与Server连接建立,服务器向客户端发生握手包( Handshake Initialization Packet)也叫做问候包,其中还包含了Server信息如版本号、线程号,此外还有服务器的capabilities以及最重要的scramble_buff字段包含用于实现客户认证的crypt_seed。
2.客户认证包仍然包含scramble_buff字段,存放SHA1加密后的数据。
The server sends a random string to the client, in scramble_buff.
The client encrypts the scramble_buff value using the password that the user
enters. This happens in sql/password.c:scramble() function.
The client sends the encrypted scramble_buff value to the server.
The server encrypts the original random string using a value in the mysql
database, mysql.user.Password.
The server compares its encrypted random string to what the client sent
in scramble_buff.
If they are the same, the password is okay.
由于和AJAX关系不大,此处简单的描述一下,更多信息请参考http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol。
此外,服务器端有一道防止外部非法链接访问的安全屏障:security sandbox。可设置授权访问域。
原文:
Scrambling operations
The client first calculates hash1 = SHA1(password), then hash2 = SHA1(hash1). The server already knows this, as that is what is held in its password table (preceded with a *).
It then appends hash2 to the salt sent by the server and hashes that, and finally exclusive ors the result with hash1.
So it sends to the server SHA1(password) XOR SHA1(salt.SHA1(SHA1(password)).
The server does effectively the same calculation and checks the result (though for some reason it prefers to undo the Exclusive-OR and compare with hash1).
如果作者属于这种人,那真是牛。
Hometown: Calgary, AB, Canada
Birthday: June 11, 1986
Employer: Essentialtalk
Languages: AS3, Java, Javascript, C# and whatever else I need to learn.
作者信息。
牛,86年人,看示例的提示筐是简体中文,和大中华有何渊源,呵呵?
如此原则性的问题,冒昧地说句,不能乱来!
- 浏览: 69526 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
基于MVC的Flex framework ...
PureMVC 不错。 简单易学,也好用
-- by zhouzhao21 -
PSP开发环境
请问一下这段代码有什么问题么?? bool flag = mResourceMg ...
-- by wangshu3000 -
Tomcat下设置session超时 ...
"Tomcat默认的会话超时是1800秒,测试一次需要等太久" -- 把系统时 ...
-- by sam.ds.chen -
[翻译]Ext vs. Dojo
netfishx 写道ext始终没有机会用于正式的项目中,但它确实是我所见过最协 ...
-- by blackanger -
TDD in AJAX
现在yui,jquery,dojo等都用自己的testcase框架,很是郁闷,为 ...
-- by hyysguyang






评论排行榜