Advertisement
nzisaacnz

PHP SQL

Sep 1st, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2.  
  3. interface SomeOtherSQL{
  4.  
  5.   const HelloWorld = [
  6.     "SELECT 'hello' as 'world';"
  7.   ];
  8. }
  9.  
  10. interface Example extends SomeOtherSQL{
  11.  
  12.   const EchoExample = [
  13.     'in'=>[
  14.       'pin'=>'VARCHAR(50)'
  15.     ],
  16.     "SELECT {0} as 'input';"
  17.   ];
  18.  
  19.   const AddExample = [
  20.     'in'=>[
  21.       'pa'=>'VARCHAR(50)',
  22.       'pb'=>'VARCHAR(50)'
  23.     ],
  24.     "SELECT {0}+{1} as 'added';"
  25.   ];
  26.  
  27. }
  28.  
  29.  
  30. class sql_base{
  31.   static function __callStatic($command,$params){
  32.     $marks = str_pad("",count($params)*2-1,"?,");
  33.     $host     = static::$host;
  34.     $username = static::$username;
  35.     $database = static::$database;
  36.     $port     = static::$port;
  37.     $password = static::$password;
  38.     $dsn      = "mysql:host=$host;port=$port;dbname=$database";
  39.     $db = new PDO($dsn, $username, $password);
  40.     $query = $db->prepare("SHOW PROCEDURE STATUS where `Name`=? AND `Db`='$database'");
  41.    
  42.     $query->execute(array($command));
  43.     $procExists = $query->fetch(PDO::FETCH_ASSOC,PDO::FETCH_ORI_NEXT);
  44.     if(!$procExists)
  45.     {
  46.       $class = get_called_class();
  47.       if(defined("$class::$command")){
  48.         $code = constant("$class::$command");
  49.         $sql = "";
  50.         $i=-1;
  51.         while(isset($code[++$i])){
  52.           $sql.=$code[$i]."\n";
  53.         }
  54.         $type = isset($code['type'])?$code['type']:"procedure";
  55.         $newParams = [];
  56.         foreach(['in','out'] as $d){
  57.           if(isset($code[$d]))foreach($code[$d] as $p=>$n){
  58.             $newParams[] = $type=="procedure"?"$d `$p` $n":"`$p` $n";
  59.           }
  60.         }
  61.         if(isset($code['in'])){
  62.           $paramKeys = array_keys($code['in']);
  63.           $sql = preg_replace_callback("/\\{([0-9]+)\\}/",function($matches) use (&$paramKeys){
  64.             return '`'.$paramKeys[$matches[1]].'`';
  65.           },$sql);
  66.         }
  67.         $newParams = implode(", ",$newParams);
  68.         $procSql = "CREATE $type $command($newParams) \n BEGIN $sql \n END";
  69.        
  70.         $query = $db->prepare($procSql);
  71.         if(!$query)
  72.           throw new Exception(json_encode($db->errorInfo()));
  73.         else if(!$query->execute())
  74.           throw new Exception(json_encode(array("errorinfo"=>$query->errorInfo(),"parameters"=>$newParams,"sqlcode"=>$code)));
  75.       }
  76.       else throw new Exception("Missing sql procedure '$command'");
  77.     }
  78.    
  79.     $query = $db->prepare("CALL $command($marks)");
  80.     if(!$query)
  81.       throw new Exception(json_encode($db->errorInfo()));
  82.     else{
  83.       if(!$query->execute($params)) throw new Exception(json_encode(array($query->errorInfo(),$params)));
  84.       else{
  85.         $return = $query->fetchAll(PDO::FETCH_ASSOC);
  86.         if(count($return)==0)return $db->lastInsertId();
  87.         return $return;
  88.       }
  89.     }
  90.   }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. class sql extends sql_base implements Example{ // fill out fields
  97.   protected static $host = "";
  98.   protected static $username = "";
  99.   protected static $database = "";
  100.   protected static $port = "";
  101.   protected static $password = "";
  102.  
  103. }
  104.  
  105. print_r(sql::HelloWorld());
  106. echo "<br>";
  107. print_r(sql::AddExample(3,4));
  108. echo "<br>";
  109. print_r(sql::EchoExample("uwotm8"));
  110.  
  111.  
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement