论坛首页 编程语言技术论坛

第一个ActionScript MySQL Driver:asSQL原理与实践

浏览 9225 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-26  

 熟悉Flex 的开发者都知道,在Flex中不能直接访问数据库,而是采用HTTPService/WebService/RemoteObject等方式实现。asSQL的出现多少让大家感到意外(抛开这两种方式的优劣不提),也可见它的威力。

1.
简介

大型软件系统都采取了分层设计的原则,将其分为大致表现(UI)、业务逻辑以及EIS三部分。在Java语言,直接与数据库打交道曾是家常便饭。HibernateO/R Mapping工具流行,直接使用JDBC编程变得少见了,但有时这种方式更加简便、速度更快。

ActionScript
用于在Flash实现动画设计,访问数据库自然不是它的强项,甚至大家都觉得没有必要。asSQL的出现使许多Flash的开发 者感到惊喜(也许在Java开发人员看来这实在算不了什么)。asSQL现在是alpha版本,还很稚嫩有一些问题。但基本工作正常。

2. asSQL
驱动

a
Connection

asSQL
也提供了类似Java语言的Connection,确切的说asSQL中的Connection是类,而JavaConnectio是接口。 Connection负责与mySQL Server通讯(通过socket建立连接,握手,接收/发送数据包),并按照mySQL协议解析、加密数据包(此处略去协议处理细节,感兴趣的请访问 http://www.redferni.uklinux.net/mysql/MySQL-Protocol.html)。

b
MysqlService

虽然ConnectionasSQL的核心对象,但与客户打交道仅有一个UI组件MysqlService。该组件需要用户输入hostport database等信息,在内部创建Connection,这避免了用户直接操作Connection可能引发的错误。<o:p></o:p>
 

xml 代码
 
  1. <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)使用,如下所示:<o:p></o:p>  

xml 代码
 
  1. <mx:DataGrid x="55" y="137" dataProvider="{sqlService.lastResult}">  
  2.     <mx:columns>  
  3.         <mx:DataGridColumn headerText="UserId" dataField="userid"/>  
  4.         <mx:DataGridColumn headerText="Email" dataField="email"/>  
  5.         <mx:DataGridColumn headerText="First Name" dataField="firstname"/>  
  6.     mx:columns>  
  7. mx:DataGrid>  

在一个按钮click事件中,调用myService.send('SELECT userid,email,firstname FROM account;')即可触发上述DataGrid加载数据。

MysqlService支持3个事件,分别是resultsresponseerrors,供调用者定制操作结果。

errors
:显然是发生错误
results
response的区别是前者针对SELECT操作,后者针对UPDATEDELETE......


遗憾的是,MysqlService设计的不够完美,Connection的创建和释放可能被分离到类内部和外部实现。如果用户提供上述事件的响应函数,就必须在该函数中显示关闭connection。如下所示:
 

<o:p></o:p>

ActionScript 代码
 
  1. private function onResults(e:ResultsEvent):void{   
  2.       var st:Statement = Statement(e.target);   
  3.       var con:Connection = st.getConnection();   
  4.       var rs:ResultSet = e.resultSet;   
  5.   
  6.       while ( rs.next() ) {   
  7.           var userid = rs.getString("userid");   
  8.           //var email = rs.getString(2);   
  9.           trace(userid);   
  10.       }   
  11.   
  12.       con.disconnect();   
  13. }   
  14.   
  15. private function onResponse(e:ResponseEvent):void{     
  16.      var st:Statement = Statement(e.target);   
  17.      var con:Connection = st.getConnection();   
  18.         
  19.      var affectedRows:int = e.affectedRows;   
  20.      var insertID:int = e.insertID;   
  21.   
  22.      con.disconnect();   
  23. }   
  24.   
  25. private function onError(e:SQLErrorEvent):void{   
  26.      var st:Statement = Statement(e.target);   
  27.      var con:Connection = st.getConnection();   
  28.         
  29.      var message:String = e.msg;   
  30.      var errorNo:int = e.id;   
  31.      var text:String = e.text; // Equals SQLError #{id}: {msg}   
  32.   
  33.      con.disconnect();   
  34. }   

3. 示例

    asSQL
目前仅支持mySQL,它以UI组件的形式供用户调用,并以数据源的形式把数据和UI组件绑定。

   
开发环境:Flex Builder 2,以及MySql 5,还有Spring <st1:chsdate month="12" islunardate="False" day="30" year="1899" w:st="on" isrocdate="False">1.2.8</st1:chsdate>中实例JPetStoreSchema

步骤1MySQL中建立一个叫做JPetStoreDatabase。并把JPetStoreSchema导入其中。
步骤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的界面元素,增加1ComboBox1DataGrid,以及2个按钮。然后编辑MXML文件。

附件是运行界面和源代码。
  • 大小: 409.6 KB
   发表时间:2007-05-27  
客户端直接链接数据库,安全如何保证?
0 请登录后投票
   发表时间:2007-05-27  
顶顶jindw.
如此原则性的问题,冒昧地说句,不能乱来!
0 请登录后投票
   发表时间:2007-05-27  
Name: Matt MacLean
Hometown: Calgary, AB, Canada
Birthday: June 11, 1986
Employer: Essentialtalk
Languages: AS3, Java, Javascript, C# and whatever else I need to learn.

作者信息。
牛,86年人,看示例的提示筐是简体中文,和大中华有何渊源,呵呵?
0 请登录后投票
   发表时间:2007-05-27  
玩Flash的很多的都是平面 、code 双轨能力很强的人
如果作者属于这种人,那真是牛。
0 请登录后投票
   发表时间:2007-05-27  
呵呵,仔细看了一下,安全性还是有考虑的。

原文:
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).
0 请登录后投票
   发表时间:2007-05-27  
安全问题的确不能忽视。

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。可设置授权访问域。
0 请登录后投票
   发表时间:2007-05-31  
数据访问写在客户界面层了?
0 请登录后投票
   发表时间:2007-06-12  
你的例子程序不能运行!连接不上数据库呀!
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics