Create statement using reflection to generate SQL _php tutorial

Source: Internet
Author: User
The following program uses reflection to construct the SQL statement for CREATE TABLE. If you are not familiar with the reflection mechanism, you can see the charm and effect of reflection from this program.

  * @copyright Chris Tankersley * @package phporm_cli */class phporm_cli_generatesql{/** * Use a MySQL databas    E */Const MySQL = ' mysql ';    /** * Use a SQLite database */Const SQLite = ' SQLite '; /** * Types that is allowed to has a length * @var array */protected $_haslength = Array (' integer ', ' var    Char ');        /** * Regexes needed to pulling out the different comments * @var array */protected $_regexes = Array ( ' Type ' = ' + '/type= ([a-z_]*)/', ' length ' = = '/length= ([0-9]*)/', ' default ' = '/' default= ' (. *) "    /', ' null ' = = '/null/', ');        /** * Types that we support * @var array */protected $_validtypes = Array (' Boolean ' = ' BOOL '),  ' Date ' = ' date ', ' integer ' = ' int ', ' primary_autoincrement ' = ' + ' int auto_increment primary KEY ', ' text ' = ' text ', ' timestamp ' = ' timestamp ', ' varchar ' = ' varchar '',    );    /** * Name of the class we'll interperet * @var String */protected $_classname;    /** * Name of the table we are generating * @var String */protected $_tablename;    /** * The type of database we are generating * @var String */protected $_type; /** * Sets the name of the class we is working with * @param string $class * @param string $table _name * @param string $type */Public function __construct ($class, $table _name, $type = self::mysql) {$this        _classname = $class;        $this->_tablename = $table _name;    $this->_type = $type;    }/** * Builds an SQL line for a property * @param reflectionproperty $property * @return String */        protected function _getdefinition ($property) {$type = ';        $length = ";                $null = ";        Preg_match ($this->_regexes[' type '), $property->getdoccomment (), $matches); if (Count ($matches) = = 2) {if (Array_key_exists ($matches [1], $this->_validtypes)) {$type = $this->_val                idtypes[$matches [1]];                if (In_array ($matches [1], $this->_haslength)) {$length = $this->_getlength ($property); } if ($matches [1]! = ' primary_autoincrement ') {$null = $this->_getnull ($propert                y); } $sql = ". $property->getname (). ' ". $type. ' '. $length. '                '. $null;            return $sql;            } else {throw new Exception (' Type '. $matches [1]. ' is not a supported SQL Type '); }} else {throw new Exception (' Found '. Count ($matches). ' When checking Type is property '. $property-&G        T;getname ());     }}/** * Extracts the Length from a property * @param reflectionproperty $property * @return string */protected function _getlength ($property) {Preg_match ($this-≫_regexes[' length '], $property->getdoccomment (), $matches);        if (count ($matches) = = 2) {return ' ('. $matches [1]. ') ';        } else {return ';  }}/** * Determines if a property was allowed to be null * @param reflectionproperty $property * @return String */protected function _getnull ($property) {preg_match ($this->_regexes[' null '], $property        Getdoccomment (), $matches);        if (count ($matches) = = 1) {return ' NULL ';        } else {return ' not NULL '; }}/** * generates a block of SQL to create a table from an Entity * @return String */Public Functi        On GetSQL () {$class = new Reflectionclass ($this->_classname);        $definitions = Array ();                foreach ($class->getproperties () as $property) {if (Strpos ($property->getname (), ' _ ') = = = = False) {            $definitions [] = $this->_getdefinition ($property); }       } $columns = Implode (", n", $definitions); $sql = "CREATE TABLE". $this->_tablename. "        (". $columns.") ";        if ($this->_type = = self::mysql) {$sql. = "Engine=myisam";    } return $sql. ";"; }}

http://www.bkjia.com/PHPjc/752416.html www.bkjia.com true http://www.bkjia.com/PHPjc/752416.html techarticle The following program uses reflection to construct the SQL statement for CREATE TABLE. If you are not familiar with the reflection mechanism, you can see the charm and effect of reflection from this program. php/** * Crea ...

  • 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.