Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.79 KB | None | 0 0
  1. /* db.php */
  2.  
  3. <?php
  4.  
  5.     Config::write ('Database',array (
  6.         'default'=>array (
  7.             'type'=>'mysql',
  8.             'host'=>'localhost',
  9.             'user'=>'root',
  10.             'password'=>'',
  11.             'database'=>'test',
  12.             'prefix'=>'ts_'
  13.         )
  14.     )) ;
  15.  
  16. ?>
  17.  
  18. /* mysql */
  19.  
  20.  
  21. <?php
  22.  
  23.     class MySQLException extends Exception{
  24.         function __construct ($what,$query='',$collect=true) {
  25.             if ($collect) {
  26.                 $this->error = mysql_error () ;
  27.                 $this->errno = mysql_errno () ;
  28.             }
  29.             $this->query = $query ;
  30.             $this->what = $what ;          
  31.         }
  32.        
  33.         function brief () {
  34.             return $this->what ;
  35.         }
  36.        
  37.         function what () {
  38.             return $this->what. ' : '. $this->query.' : '.$this->error.' (errno : '.$this->errno.')' ;
  39.         }
  40.     }
  41.    
  42.    
  43.     class MySQLDatabase {
  44.        
  45.         private $link ;
  46.         private $prefix ;
  47.         private $name ;
  48.         private $qlog ;
  49.         private $params ;
  50.         private $args ;
  51.        
  52.         public function __construct ($vars,$name='default') {
  53.             if (is_array ($vars)) {    
  54.                 $this->params = $vars ;
  55.                 extract ($vars) ;              
  56.                 $this->name = $name ;              
  57.                 $this->link = mysql_connect (
  58.                     isset ($host) ? $host : 'localhost',
  59.                     isset ($user) ? $user : 'root',
  60.                     isset ($password) ? $password : '',
  61.                     isset ($newlink) ? $newlink : false,
  62.                     isset ($clientflags) ? $clientflags : 0) ;
  63.                    
  64.                 if (!$this->link)
  65.                     throw new MySQLException ('Couldn\'t connect to database : '.$name) ;
  66.                
  67.                 $this->prefix = $prefix ;
  68.                
  69.                 if (!mysql_select_db ($database,$this->link)) {
  70.                     throw new MySQLException ('Could not select database : '.$database) ;              
  71.                 }              
  72.             }
  73.         }
  74.        
  75.        
  76.         private function auxEscape ($v) {
  77.             return mysql_real_escape_string ($v,$this->link) ;
  78.         }
  79.        
  80.         private function auxInsertId () {
  81.             return mysql_insert_id ($this->link) ;     
  82.         }
  83.                
  84.         private function auxQuery ($q) {
  85.             return mysql_query ($q,$this->link) ;      
  86.         }
  87.        
  88.         private function auxFetchObject ($r) {
  89.             return mysql_fetch_object ($r) ;
  90.         }  
  91.        
  92.         private function auxFetchArray ($r,$what=MYSQL_NUM) {
  93.             return mysql_fetch_array ($r,$what) ;
  94.         }
  95.        
  96.         private function auxAffectedRows () {
  97.             return mysql_affected_rows ($this->link) ;
  98.         }
  99.        
  100.         private function auxLog ($q,$meta='') {
  101.             $this->qlog [] = $q . (empty ($meta) ? '' : '->' . $meta) ;
  102.         }
  103.  
  104.        
  105.         private function auxQueryPrepare ($q,$args = array ()) {
  106.             if (empty ($args)) {
  107.                 return $q ;
  108.             }  
  109.             $this->args = $args ;
  110.             return preg_replace_callback ('/\[\[([^\]]+)\]\]/',array ($this,'auxQueryParse'),$q) ;
  111.         }
  112.        
  113.         private function auxQueryParse ($match) {
  114.             $key = $match [1] ;
  115.            
  116.             if (preg_match ('/^([@&|])?([?!=])?([`\'])?([a-zA-Z0-9_]+)?$/',$key,$data)) {
  117.                 $prefix = $data[1] ;
  118.                 $op = $data[2] ;
  119.                 $quote = $data[3] ;
  120.                 $key = $data[4] ;
  121.                
  122.                 if ($prefix == '@') {      
  123.                     /*if (isset ($this->args [$key])) {    
  124.                         return $quote.$this->prefix.$this->escape ($this->args [$key]).$quote ;
  125.                     }*/            
  126.                     return $quote.$this->prefix.$this->escape ($key).$quote ;
  127.                 }
  128.                
  129.                 if (strlen (trim ($key)) < 1) {
  130.                     $data = $this->args ;
  131.                 }  
  132.                 else {
  133.                     $data = $this->args [$key] ;
  134.                 }              
  135.                                            
  136.                 if (strlen (trim ($key)) > 0 && !isset ($this->args[$key])) {                  
  137.                     return $match [0] ;
  138.                 }
  139.                                
  140.                 if (empty ($op) && !is_array ($data)) {            
  141.                     return $quote.$this->escape ($data).$quote ;
  142.                 }
  143.                
  144.                 if (is_array ($data)) {
  145.                     $kdata = $vdata = array () ;
  146.                     foreach ($data as $k=>$v) {
  147.                         if (is_array ($v))
  148.                             continue ;
  149.                         $kdata [] = $quote.$this->escape ($k).$quote ;
  150.                         $vdata [] = $quote.$this->escape ($v).$quote ;         
  151.                         if (!empty ($quote)) {
  152.                             $adata [] = "`".$this->escape ($k)."`='".$this->escape ($v)."'" ;
  153.                         }
  154.                         else {
  155.                             $adata [] = $this->escape ($k)."=".$this->escape ($v) ;                    
  156.                         }
  157.                     }
  158.                     switch ($op) {
  159.                         case '=' :
  160.                             $sep = ',' ;
  161.                             if ($prefix == '&') $sep = ' AND ' ;
  162.                             if ($prefix == '|') $sep = ' OR ' ;
  163.                            
  164.                             return implode ($sep,$adata) ;
  165.                         case '!' :
  166.                             return implode (',',$kdata) ;                                      
  167.                         case '?' :                         
  168.                         default :
  169.                             return implode (',',$vdata) ;
  170.                     }
  171.                 }                      
  172.             }
  173.            
  174.             return $match [0] ;
  175.         }
  176.                
  177.        
  178.        
  179.         private function auxQueryResults ($result,$shorten=true,$query='') {
  180.             if ($result === FALSE) {
  181.                 throw new MySQLException ('Error executing query',$query) ;
  182.             }
  183.             if (is_resource ($result)) {
  184.                 $rows = array () ;
  185.                 if (!$shorten) {
  186.                     while ($r = $this->auxFetchObject ($result)) {
  187.                         $rows [] = $r ;
  188.                     }                  
  189.                     return $rows ;
  190.                 }
  191.                 if (mysql_num_fields ($result) < 2) {
  192.                     while (list ($r) = $this->auxFetchArray ($result)) {
  193.                         $rows [] = $r ;
  194.                     }                                                      
  195.                 }
  196.                 else {
  197.                     while ($r = $this->auxFetchObject ($result)) {
  198.                         $rows [] = $r ;
  199.                     }                  
  200.                 }
  201.                 if (count ($rows) == 1 && !empty ($query) && preg_match ('/LIMIT (1|[0-9]+\,1)\s*$/i',$query)  ) {                 
  202.                     return array_shift ($rows) ;
  203.                 }
  204.                 return $rows ;
  205.             }
  206.            
  207.             if ($result === TRUE) {
  208.            
  209.                 if (empty ($query)) {
  210.                     return TRUE ;
  211.                 }
  212.                
  213.                 if (preg_match ('/^\s*INSERT/i',$query)) {
  214.                     return $this->auxInsertId () ;
  215.                 }              
  216.                
  217.                 if (preg_match ('/^\s*(UPDATE|DELETE|REPLACE)/i',$query)) {
  218.                     return $this->auxAffectedRows () ;
  219.                 }
  220.                
  221.                 return TRUE ;
  222.             }
  223.            
  224.             return FALSE ;
  225.            
  226.         }
  227.        
  228.         public function escape ($v) {
  229.             $vv = $this->auxEscape ($v) ;
  230.             if ($vv !== FALSE)
  231.                 return $vv ;
  232.             throw new MySQLException ('Can\'t escape string \"'.$v.'\"') ;
  233.             return $v ;
  234.         }      
  235.        
  236.         public function query ($q,$args=array (),$shorten=true) {
  237.             $q = $this->auxQueryPrepare ($q,$args) ;
  238.             $r = $this->auxQuery ($q) ;
  239.             $this->auxLog ($q) ;
  240.             return $this->auxQueryResults ($r,$shorten,$q) ;
  241.         }  
  242.        
  243.         public function getLog () {
  244.             return $this->qlog ;
  245.         }
  246.        
  247.         public function insert ($table,$args) {
  248.             return $this->query ("INSERT INTO [[@`{$table}]] ([[!`]]) VALUES ([[?']])",$args) ;
  249.         }
  250.        
  251.         public function update ($table,$set,$where,$limit=NULL) {
  252.             if (!isset ($limit)) {
  253.                 return $this->query ("UPDATE [[@`{$table}]] SET [[=`set]] WHERE [[&=where]]",compact ('set','where')) ;
  254.             }
  255.             return $this->query ("UPDATE [[@`{$table}]] SET [[=`set]] WHERE [[&=`where]] LIMIT [[limit]]",compact ('set','where','limit')) ;
  256.         }
  257.        
  258.         public function delete ($table,$where,$limit=NULL) {
  259.             if (!isset ($limit)) {
  260.                 return $this->query ("DELETE FROM [[@`{$table}]] WHERE [[&=`where]]",compact ('where','limit')) ;
  261.             }
  262.             return $this->query ("DELETE FROM [[@`{$table}]] WHERE [[&=`where]] LIMIT [[limit]]",compact ('where','limit')) ;
  263.         }
  264.        
  265.         public function select ($table,$where,$limit=NULL,$shorten=true) {
  266.             if (!isset ($limit)) {
  267.                 return $this->query ("SELECT * FROM [[@`{$table}]] WHERE [[&=`where]]",compact ('where','limit'),$shorten) ;
  268.             }
  269.             return $this->query ("SELECT * FROM [[@`{$table}]] WHERE [[&=`where]] LIMIT [[limit]]",compact ('where','limit'),$shorten) ;       
  270.         }
  271.     }
  272.    
  273.     class MySQLTable {
  274.        
  275.         public function __construct ($connection,$tableName) {
  276.             $this->table = $tableName ;
  277.             $this->connection = $connection ;
  278.         }      
  279.        
  280.         public function query ($q,$args=array (),$shorten=true) {
  281.             return $this->connection->query ($q,$args,$shorten) ;
  282.         }
  283.  
  284.         public function update ($set,$where,$limit=NULL) {
  285.             return $this->connection->update ($this->table,$set,$where,$limit) ;
  286.         }
  287.        
  288.         public function insert ($what) {
  289.             return $this->connection->insert ($this->table,$what) ;
  290.         }
  291.        
  292.         public function delete ($where,$limit=NULL) {
  293.             return $this->connection->delete ($this->table,$where,$limit) ;
  294.         }
  295.        
  296.         public function select ($where,$limit=NULL,$shorten=true) {
  297.             return $this->connection->select ($this->table,$where,$limit,$shorten) ;
  298.         }
  299.     }
  300. ?>
  301.  
  302. /* config */
  303.  
  304. <?php
  305.  
  306.     class Config {
  307.         static $data ;
  308.        
  309.         function write ($key,$data) {
  310.             return self::$data [$key] = $data ;
  311.         }
  312.                
  313.         function read ($key) {
  314.             return self::$data [$key] ;
  315.         }
  316.     } ;
  317.    
  318.    
  319. ?>
  320.  
  321. /* index */
  322.  
  323. <?php
  324.     header ('Content-Type: text/plain;charset=utf-8') ;
  325.     require_once ('lib/config.php') ;
  326.     require_once ('config/default.php') ;
  327.     require_once ('lib/mysql.php') ;
  328.    
  329.     try {
  330.         $db = Config::read ('Database') ;      
  331.         $con = new MySQLDatabase ($db ['default']) ;
  332.         $tbl = new MySQLTable ($con,'test') ;
  333.        
  334.         /*
  335.         for ($i = 0;$i < 10;++$i)
  336.             for ($j = 0;$j < 10;++$j)          
  337.                 $tbl->insert (array ('a'=>$i,'b'=>$j,'c'=>($i*$j % 2 == 1) ? 'odd' : 'even')) ;
  338.         */
  339.        
  340.         $tbl->update (array ('a'=>10,'b'=>20),array ('id'=>1,'a'=>20),2) ;
  341.         $tbl->delete (array ('id'=>1,'a'=>20),2);
  342.         $tbl->insert (array ('b'=>1,'a'=>20));
  343.         $tbl->select (array ('id'=>1,'b'=>20),array(1,2));
  344.         echo "\n", implode ("\n",$con->getLog ()) ;
  345.     }
  346.     catch (Exception $e) {  
  347.         echo $e->what () ;
  348.     }
  349. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement