What does the transactional scripting pattern mean?

Source: Internet
Author: User
Tags dsn
/* Transaction script mode: Similar to the model layer in thinkphp, or the class that operates the database. Personally feel that the use of practice is quite simple and convenient, that is, the SQL statement written dead, flexibility is not enough.                 The sample code is as follows: */namespace Woo\process;abstract class base{static $DB; PDO object Static $stmts = Array (); SQL statement handle function __construct () {$dsn = \woo\base\applicationregistry::getdsn (); if (Is_null ($DSN)) {throw new \woo\        Base\appexception ("No DSN");        } self:: $DB = new PDO ($DSN);    Self:: $DB->setattribute (\pdo::attr_errmode,\pdo::errmode_exception);        } function Preparestatement ($stmt _s) {//cache SQL statement handle if (Isset (self:: $stmts ($stmt _s)) {return self:: $stmts [$stmt _s];        } $stmt _handle = self:: $DB->prepare ($stmt _s);    Self:: $stmts [$stmt _s] = $stmt _handle;return $stmt _handle; } protected function Dostatement ($stmt _s, $values _a) {//Execute SQL and get a statement resource $sth = $this->preparestatement ($stmt _s); $sth    ->closecursor (); $db _result = $sth->execute ($values _a); return $sth; }}//This class is to write some data to the database, relatively simple not much to say the class Venuemanager extends base{static $add _venue = "INSERT into venue (name) VALUES (?) "; static $add _space = "INSERT into space (name,venue) VALUES (?,?)"; static $check _slot = "Select Id,name from event WHERE space =?" and (start+duration); and start <? ";    static $add _event = "INSERT into event (name,space,start,duration) VALUES (?,?,?,?)"; function Addvenue ($name, $space _array) {$ret = array (); $ret [' venue '] = array ($name); $this->dostatement (self:: $add _ Venue, $ret [' venue ']), $v _id = self:: $DB->lastinsertid (); $ret [' spaces '] = Array (); foreach ($space _array as $space _ Name) {$values = array ($space _name, $v _id) $this->dostatement (self:: $add _space, $values); $s _id = self:: $DB        Lastinsertid (); Array_unshift ($values, $s _id); $ret [' spaces '] [] = $values;    }return $ret; } function Bookevent ($space _id, $name, $time, $duration) {$values = array ($space _id, $time, ($time + $duration)); $stmt = $ This->dostatement (self:: $check _slot, $values, false); if ($result = $stmt->fetch ()) {throw new \woo\base\ Appexception ("Double booked!        Try again "); } $this->dOstatement (self:: $add _event,array ($name, $space _id, $time, $duration)); }}//client, it is relatively easy to use $venue = new Venuemanager (), $venue->addvenue (' Test ', Array (' test1 ', ' test2 ', ' test3 ');
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.