<?php
/***************************************************** 
 *         Prosty interfejs do bazy danych           * 
 ***************************************************** 
 * Wersja: 1.0                                       * 
 * Autor: Jacek Kowalski (http://jacekk.info)        * 
 *                                                   * 
 * Utwór rozprowadzany na licencji                   * 
 * http://creativecommons.org/licenses/by-nc-sa/2.5/ * 
 *****************************************************/

// Kodowanie znakow: ISO-8859-2

function error($prior$message$file$line) {
    
// Funkcja przechwytywania błędów
}

class 
sql {
    static 
$db;
    
    static function 
connect() {
        
self::$db = @mysql_connect('''''');
        if(!
self::$db) {
            
error(E_ERRORmysql_error());
        }
        
        if(!@
mysql_select_db('baza')) {
            
error(E_ERRORmysql_error());
        }
    }
    
    static function 
query($q) {
        if(!
self::$db) {
            
self::connect();
        }
        
        if(!@
mysql_ping(self::$db)) {
            
self::connect();
        }
        
        
$r = @mysql_query($qself::$db);
        if(
$r===FALSE) {
            
error(E_ERRORmysql_error(), __FILE____LINE__);
        }
        
        return 
$r;
    }
    
    static function 
fetchone($q) {
        return 
mysql_fetch_array($q);
    }
    
    static function 
fetch($q) {
        while (
$entry mysql_fetch_array($q)) {
            
$r[] = $entry;
        }
        
        return 
$r;
    }
    
    static function 
affected() {
        return 
mysql_affected_rows(self::$db);
    }
    
    static function 
escape($q) {
        if(!
self::$db) {
            
self::connect();
        }
        
        if(!@
mysql_ping(self::$db)) {
            
self::connect();
        }
        
        return 
mysql_real_escape_string($qself::$db);
    }
    
    static function 
close() {
        
mysql_close(self::$db);
    }
}

class 
db {
    static function 
__combine_where($where$value) {
        if(
$where!==NULL) {
            if(!
is_array($where)) {
                
$where = array($where);
            }
            
            if(!
is_array($value)) {
                
$value = array($value);
            }
            
            
$combine array_combine($where$value);
            unset(
$where$value);
            
$where = array();
            
            foreach(
$combine as $a => $b) {
                if(
substr($a01)=='!') {
                    
$where[] = '`'.sql::escape(substr($a1)).'`!=\''.sql::escape($b).'\'';
                }
                elseif(
substr($b01)=='<') {
                    
$where[] = '`'.sql::escape($a).'`<\''.sql::escape(substr($b1)).'\'';
                }
                elseif(
substr($b01)=='>') {
                    
$where[] = '`'.sql::escape($a).'`>\''.sql::escape(substr($b1)).'\'';
                }
                else
                {
                    
$where[] = '`'.sql::escape($a).'`=\''.sql::escape($b).'\'';
                }
            }
            
            return 
' WHERE '.implode(' AND '$where);
        }
        else
        {
            return 
'';
        }
    }
    
    static function 
get($baza$what$where=NULL$value=NULL$limit=NULL$start=NULL$ad=NULL) {
        if(
is_array($what)) {
            foreach(
$what as &$el) {
                
$el '`'.sql::escape($el).'`';
            }
            
$what implode(', '$what);
        }
        elseif(
$what!='*') {
            
$what '`'.sql::escape($what).'`';
        }
        
        if(
ctype_digit($limit) AND $limit!==NULL) {
            if(
ctype_digit($start) AND $start!==NULL) {
                
$addon ' '.$ad.' LIMIT '.$start.','.$limit;
            }
            else
            {
                
$addon ' '.$ad.' LIMIT '.$limit;
            }
        }
        elseif(
$ad) {
            
$addon ' '.$ad;
        }
        
        return 
sql::query('SELECT '.$what.' FROM `'.$baza.'`'.self::__combine_where($where$value).$addon);
    }
    
    static function 
add($baza$key$value) {
        if(!
is_array($key)) {
            
$key = array($key);
        }
        if(!
is_array($value)) {
            
$value = array($value);
        }
        
        foreach(
$key as &$v) {
            
$v '`'.sql::escape($v).'`';
        }
        
$key implode(', '$key);
        
        foreach(
$value as &$v) {
            
$v '\''.sql::escape($v).'\'';
        }
        
$value implode(', '$value);
        
        
$sql sql::query('INSERT INTO `'.$baza.'` ('.$key.') VALUES ('.$value.')');
        if(
sql::affected() == 0) {
            
error(E_ERRORmysql_error(), __FILE____LINE__);
            die();
        }
        
        return 
$sql;
    }
    
    static function 
del($baza$where=NULL$value=NULL) {
        
$sql sql::query('DELETE FROM `'.$baza.'`'.self::__combine_where($where$value));
        return 
$sql;
    }
    
    static function 
edit($baza$key$val$where=NULL$value=NULL) {
        if(!
is_array($key)) {
            
$key = array($key);
        }
        
        if(!
is_array($val)) {
            
$val = array($val);
        }
        
        
$combine array_combine($key$val);
        unset(
$key$val);
        
$key = array();
        
        foreach(
$combine as $a => $b) {
            
$key[] = '`'.$a.'`=\''.sql::escape($b).'\'';
        }
        
        
$key implode(', '$key);
        
        
$sql sql::query('UPDATE `'.$baza.'` SET '.$key.self::__combine_where($where$value));
        return 
$sql;
    }
}
?>