Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* db.php */
- <?php
- Config::write ('Database',array (
- 'default'=>array (
- 'type'=>'mysql',
- 'host'=>'localhost',
- 'user'=>'root',
- 'password'=>'',
- 'database'=>'test',
- 'prefix'=>'ts_'
- )
- )) ;
- ?>
- /* mysql */
- <?php
- class MySQLException extends Exception{
- function __construct ($what,$query='',$collect=true) {
- if ($collect) {
- $this->error = mysql_error () ;
- $this->errno = mysql_errno () ;
- }
- $this->query = $query ;
- $this->what = $what ;
- }
- function brief () {
- return $this->what ;
- }
- function what () {
- return $this->what. ' : '. $this->query.' : '.$this->error.' (errno : '.$this->errno.')' ;
- }
- }
- class MySQLDatabase {
- private $link ;
- private $prefix ;
- private $name ;
- private $qlog ;
- private $params ;
- private $args ;
- public function __construct ($vars,$name='default') {
- if (is_array ($vars)) {
- $this->params = $vars ;
- extract ($vars) ;
- $this->name = $name ;
- $this->link = mysql_connect (
- isset ($host) ? $host : 'localhost',
- isset ($user) ? $user : 'root',
- isset ($password) ? $password : '',
- isset ($newlink) ? $newlink : false,
- isset ($clientflags) ? $clientflags : 0) ;
- if (!$this->link)
- throw new MySQLException ('Couldn\'t connect to database : '.$name) ;
- $this->prefix = $prefix ;
- if (!mysql_select_db ($database,$this->link)) {
- throw new MySQLException ('Could not select database : '.$database) ;
- }
- }
- }
- private function auxEscape ($v) {
- return mysql_real_escape_string ($v,$this->link) ;
- }
- private function auxInsertId () {
- return mysql_insert_id ($this->link) ;
- }
- private function auxQuery ($q) {
- return mysql_query ($q,$this->link) ;
- }
- private function auxFetchObject ($r) {
- return mysql_fetch_object ($r) ;
- }
- private function auxFetchArray ($r,$what=MYSQL_NUM) {
- return mysql_fetch_array ($r,$what) ;
- }
- private function auxAffectedRows () {
- return mysql_affected_rows ($this->link) ;
- }
- private function auxLog ($q,$meta='') {
- $this->qlog [] = $q . (empty ($meta) ? '' : '->' . $meta) ;
- }
- private function auxQueryPrepare ($q,$args = array ()) {
- if (empty ($args)) {
- return $q ;
- }
- $this->args = $args ;
- return preg_replace_callback ('/\[\[([^\]]+)\]\]/',array ($this,'auxQueryParse'),$q) ;
- }
- private function auxQueryParse ($match) {
- $key = $match [1] ;
- if (preg_match ('/^([@&|])?([?!=])?([`\'])?([a-zA-Z0-9_]+)?$/',$key,$data)) {
- $prefix = $data[1] ;
- $op = $data[2] ;
- $quote = $data[3] ;
- $key = $data[4] ;
- if ($prefix == '@') {
- /*if (isset ($this->args [$key])) {
- return $quote.$this->prefix.$this->escape ($this->args [$key]).$quote ;
- }*/
- return $quote.$this->prefix.$this->escape ($key).$quote ;
- }
- if (strlen (trim ($key)) < 1) {
- $data = $this->args ;
- }
- else {
- $data = $this->args [$key] ;
- }
- if (strlen (trim ($key)) > 0 && !isset ($this->args[$key])) {
- return $match [0] ;
- }
- if (empty ($op) && !is_array ($data)) {
- return $quote.$this->escape ($data).$quote ;
- }
- if (is_array ($data)) {
- $kdata = $vdata = array () ;
- foreach ($data as $k=>$v) {
- if (is_array ($v))
- continue ;
- $kdata [] = $quote.$this->escape ($k).$quote ;
- $vdata [] = $quote.$this->escape ($v).$quote ;
- if (!empty ($quote)) {
- $adata [] = "`".$this->escape ($k)."`='".$this->escape ($v)."'" ;
- }
- else {
- $adata [] = $this->escape ($k)."=".$this->escape ($v) ;
- }
- }
- switch ($op) {
- case '=' :
- $sep = ',' ;
- if ($prefix == '&') $sep = ' AND ' ;
- if ($prefix == '|') $sep = ' OR ' ;
- return implode ($sep,$adata) ;
- case '!' :
- return implode (',',$kdata) ;
- case '?' :
- default :
- return implode (',',$vdata) ;
- }
- }
- }
- return $match [0] ;
- }
- private function auxQueryResults ($result,$shorten=true,$query='') {
- if ($result === FALSE) {
- throw new MySQLException ('Error executing query',$query) ;
- }
- if (is_resource ($result)) {
- $rows = array () ;
- if (!$shorten) {
- while ($r = $this->auxFetchObject ($result)) {
- $rows [] = $r ;
- }
- return $rows ;
- }
- if (mysql_num_fields ($result) < 2) {
- while (list ($r) = $this->auxFetchArray ($result)) {
- $rows [] = $r ;
- }
- }
- else {
- while ($r = $this->auxFetchObject ($result)) {
- $rows [] = $r ;
- }
- }
- if (count ($rows) == 1 && !empty ($query) && preg_match ('/LIMIT (1|[0-9]+\,1)\s*$/i',$query) ) {
- return array_shift ($rows) ;
- }
- return $rows ;
- }
- if ($result === TRUE) {
- if (empty ($query)) {
- return TRUE ;
- }
- if (preg_match ('/^\s*INSERT/i',$query)) {
- return $this->auxInsertId () ;
- }
- if (preg_match ('/^\s*(UPDATE|DELETE|REPLACE)/i',$query)) {
- return $this->auxAffectedRows () ;
- }
- return TRUE ;
- }
- return FALSE ;
- }
- public function escape ($v) {
- $vv = $this->auxEscape ($v) ;
- if ($vv !== FALSE)
- return $vv ;
- throw new MySQLException ('Can\'t escape string \"'.$v.'\"') ;
- return $v ;
- }
- public function query ($q,$args=array (),$shorten=true) {
- $q = $this->auxQueryPrepare ($q,$args) ;
- $r = $this->auxQuery ($q) ;
- $this->auxLog ($q) ;
- return $this->auxQueryResults ($r,$shorten,$q) ;
- }
- public function getLog () {
- return $this->qlog ;
- }
- public function insert ($table,$args) {
- return $this->query ("INSERT INTO [[@`{$table}]] ([[!`]]) VALUES ([[?']])",$args) ;
- }
- public function update ($table,$set,$where,$limit=NULL) {
- if (!isset ($limit)) {
- return $this->query ("UPDATE [[@`{$table}]] SET [[=`set]] WHERE [[&=where]]",compact ('set','where')) ;
- }
- return $this->query ("UPDATE [[@`{$table}]] SET [[=`set]] WHERE [[&=`where]] LIMIT [[limit]]",compact ('set','where','limit')) ;
- }
- public function delete ($table,$where,$limit=NULL) {
- if (!isset ($limit)) {
- return $this->query ("DELETE FROM [[@`{$table}]] WHERE [[&=`where]]",compact ('where','limit')) ;
- }
- return $this->query ("DELETE FROM [[@`{$table}]] WHERE [[&=`where]] LIMIT [[limit]]",compact ('where','limit')) ;
- }
- public function select ($table,$where,$limit=NULL,$shorten=true) {
- if (!isset ($limit)) {
- return $this->query ("SELECT * FROM [[@`{$table}]] WHERE [[&=`where]]",compact ('where','limit'),$shorten) ;
- }
- return $this->query ("SELECT * FROM [[@`{$table}]] WHERE [[&=`where]] LIMIT [[limit]]",compact ('where','limit'),$shorten) ;
- }
- }
- class MySQLTable {
- public function __construct ($connection,$tableName) {
- $this->table = $tableName ;
- $this->connection = $connection ;
- }
- public function query ($q,$args=array (),$shorten=true) {
- return $this->connection->query ($q,$args,$shorten) ;
- }
- public function update ($set,$where,$limit=NULL) {
- return $this->connection->update ($this->table,$set,$where,$limit) ;
- }
- public function insert ($what) {
- return $this->connection->insert ($this->table,$what) ;
- }
- public function delete ($where,$limit=NULL) {
- return $this->connection->delete ($this->table,$where,$limit) ;
- }
- public function select ($where,$limit=NULL,$shorten=true) {
- return $this->connection->select ($this->table,$where,$limit,$shorten) ;
- }
- }
- ?>
- /* config */
- <?php
- class Config {
- static $data ;
- function write ($key,$data) {
- return self::$data [$key] = $data ;
- }
- function read ($key) {
- return self::$data [$key] ;
- }
- } ;
- ?>
- /* index */
- <?php
- header ('Content-Type: text/plain;charset=utf-8') ;
- require_once ('lib/config.php') ;
- require_once ('config/default.php') ;
- require_once ('lib/mysql.php') ;
- try {
- $db = Config::read ('Database') ;
- $con = new MySQLDatabase ($db ['default']) ;
- $tbl = new MySQLTable ($con,'test') ;
- /*
- for ($i = 0;$i < 10;++$i)
- for ($j = 0;$j < 10;++$j)
- $tbl->insert (array ('a'=>$i,'b'=>$j,'c'=>($i*$j % 2 == 1) ? 'odd' : 'even')) ;
- */
- $tbl->update (array ('a'=>10,'b'=>20),array ('id'=>1,'a'=>20),2) ;
- $tbl->delete (array ('id'=>1,'a'=>20),2);
- $tbl->insert (array ('b'=>1,'a'=>20));
- $tbl->select (array ('id'=>1,'b'=>20),array(1,2));
- echo "\n", implode ("\n",$con->getLog ()) ;
- }
- catch (Exception $e) {
- echo $e->what () ;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement