一、 DBQuery对象
现在,我们的DBQuery对象简单地模仿一个存储过程—一旦被执行,即返回一个必须进行保存的结果资源;并且如果你想使用该结果集上的函数(例如num_rows()或fetch_row())的话,你必须传递MySqlDB对象。那么,如果由DBQuery对象来实现MySqlDB对象(其设计目的是对一个执行查询的结果进行操作)实现的函数,效果如何呢?让我们继续使用上一篇示例中的代码;并且让我们假定,现在由DBQuery对象管理我们的结果资源。DBQuery类的源码如列表1所示。
列表1.使用DBQuery类。
<ol class="dp-c"> <li class="alt"><span><span class="keyword">require</span><span> </span><span class="string">'mysql_db.php'</span><span>; </span></span></li> <li> <span class="keyword">require_once</span><span> </span><span class="string">'query.php'</span><span>; </span> </li> <li class="alt"> <span class="vars">$db</span><span> = </span><span class="keyword">new</span><span> MySqlDb; </span> </li> <li> <span class="vars">$db</span><span>->connect(</span><span class="string">'host'</span><span>, </span><span class="string">'username'</span><span>, </span><span class="string">'pass'</span><span>); </span> </li> <li class="alt"> <span class="vars">$db</span><span>->query(</span><span class="string">'use content_management_system'</span><span>); </span> </li> <li> <span class="vars">$query</span><span> = </span><span class="keyword">new</span><span> DBQuery(</span><span class="vars">$db</span><span>); </span> </li> <li class="alt"> <span class="vars">$query</span><span>->prepare(</span><span class="string">'SELECT fname,sname FROM users WHERE username=:1S AND pword=:2S AND expire_time<:3i><span>); </span><li><span>try { </span></li> <li class="alt"> <span> </span><span class="keyword">if</span><span>(</span><span class="vars">$query</span><span>->execute(</span><span class="string">"visualad"</span><span>, </span><span class="string">"apron"</span><span>, time()))->num_rows() == 1) { </span> </li> <li> <span></span><span class="func">echo</span><span>(</span><span class="string">'Correct Credentials'</span><span>); </span> </li> <li class="alt"> <span> } </span><span class="keyword">else</span><span> { </span> </li> <li> <span></span><span class="func">echo</span><span>(</span><span class="string">'Incorrect Credentials / Session Expired'</span><span>); </span> </li> <li class="alt"><span> } </span></li> <li> <span>} catch (QueryException </span><span class="vars">$e</span><span>) { </span> </li> <li class="alt"> <span> </span><span class="func">echo</span><span>(</span><span class="string">'Error executing query: '</span><span> . </span><span class="vars">$e</span><span>); </span> </li> <li><span>} </span></li></:3i></span> </li> </ol>
上面修改后的代码中我们最感兴趣的是,catch语句和execute语句。
◆execute语句不再返回一个结果资源,现在它返回DBQuery对象本身。
◆DBQuery对象现在实现num_rows()函数—我们从DB接口中已经熟悉。
◆如果查询执行失败,它抛出一个QueryException类型的异常。当被转换成一个字符串时,它将返回发生的错误的细节信息。
为此,你需要使用PHP代理。事实上,你在我们的DBQuery对象中已经使用代理了,但是现在将更为深入地使用它来把它与MySqlDB对象紧密绑定。该DBQuery对象已经被使用一个实现DB接口的对象初始化,并且它已经包含一个成员函数execute—由它调用DB对象的query()方法来执行该查询。这个DBQuery对象本身并不实际地查询数据库,它把这项任务交由DB对象来完成。这就是代理,其实是一个进程—借助于这个进程,通过把消息发送给另一个实现相同的或类似行为的对象,一个对象可以实现一个特别的行为。
为此,你需要修改DBQuery对象以便包括所有的函数—它们操作一个来自DB对象的结果资源。当执行查询以调用DB对象的相应函数并且返回它的结果时,你需要使用存储的结果。下列函数将被添加:
列表2:使用代理扩展DBQuery类。
<ol class="dp-c"> <li class="alt"><span><span class="keyword">class</span><span> DBQuery </span></span></li> <li><span>{ </span></li> <li class="alt"><span> ..... </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_array() </span> </li> <li><span> { </span></li> <li class="alt"> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_array(</span><span class="vars">$this</span><span>->result); </span> </li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_row() </span> </li> <li class="alt"><span> { </span></li> <li> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li class="alt"> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_row(</span><span class="vars">$this</span><span>->result); </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_assoc() </span> </li> <li><span> { </span></li> <li class="alt"> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_assoc(</span><span class="vars">$this</span><span>->result); </span> </li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> fetch_object() </span> </li> <li class="alt"><span> { </span></li> <li> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li class="alt"> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->fetch_object(</span><span class="vars">$this</span><span>->result); </span> </li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"> <span> </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> num_rows() </span> </li> <li><span> { </span></li> <li class="alt"> <span></span><span class="keyword">if</span><span> (! </span><span class="func">is_resource</span><span>(</span><span class="vars">$this</span><span>->result)) { </span> </li> <li> <span> </span><span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(</span><span class="string">'Query not executed.'</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span></span><span class="keyword">return</span><span> </span><span class="vars">$this</span><span>->db->num_rows(</span><span class="vars">$this</span><span>->result); </span> </li> <li><span> } </span></li> <li class="alt"><span>} </span></li> </ol>
每个函数的实现相当简单。它首先进行检查,以确保已经执行查询,然后把任务代理到DB对象,返回它的结果就好象它是查询对象本身(称作是基本数据库函数)一样。 1