Login   Register  
PHP Classes
elePHPant
Icontem

File: class.sql.builder.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of JImmy Bo  >  Bobo's Simple SQL Builder Class  >  class.sql.builder.php  >  Download  
File: class.sql.builder.php
Role: Class source
Content type: text/plain
Description: PHP SQL Query Building Class
Class: Bobo's Simple SQL Builder Class
Compose SQL queries from parameters
Author: By
Last change:
Date: 2 years ago
Size: 4,627 bytes
 

Contents

Class file image Download
<?php
/* Version 1
 * Please leave this comment in at the top of the script if you use this script
 * in it's entirety. That is my only request. Other then that have fun.
 *
 * Bobo's Simple SQL Builder Class
 * http://www.phpclasses.org/package/7306-PHP-Simple-SQL-Query-Building-Class.html
 *
 *
 * also check out my db access class (Bobo DB) for easy database access in PHP:
 * http://www.phpclasses.org/package/5283-PHP-Simple-MySQL-database-access-wrapper.html
 *
 * I created this class to simplify queries to and from the database. Instead of a whole bunch of 
 * redundant lines of code this makes your code cleaner and easier to understand.
 * 
 * 
 * also check out my websites:
 * http://insidefame.com
 * http://wellerit.com
 *
 * Also if you're looking for help from me directly you can usually find me
 * slumming it on #phphelp on the undernet irc network.
 *
 *
 */ 


    
class sql_builder
    
{
        var 
$update;
        var 
$insert;
        var 
$delete;
        var 
$select;
        
        function 
sql_builder()
        {
            
$this->update = new sql_update();
            
$this->insert = new sql_insert();
            
$this->delete = new sql_delete();
            
$this->select = new sql_select();
        }
    }
    
    class 
sql_update 
    
{
        var 
$table;
        var 
$sets;
        var 
$s;
        
        function 
sql_update()
        {
        }
        
        function 
reset()
        {
            
$this->sets '';
            
$this->'';
            
$this->table '';
        }
        
        function 
table($tablename)
        {
            
$this->reset();
            
$this->table $tablename;
            
$this->s[0] = "UPDATE $tablename SET ";
        }
        
        function 
set($key$value)
        {
            
$this->sets[$key] = $value;
            
            
// update sql //
            
$count 0;
            
$this->s[1] = '';
            
            
$num_sets count($this->sets);
            
            foreach(
$this->sets as $key => $value)
            {                
                
$this->s[1] .= $key."=".$value;
                
                if(
$count $num_sets 1)
                    
$this->s[1] .= ', ';
                else
                    
$this->s[1] .= ' ';
                
$count++;
            }
        }
        
        function 
where($str)
        {
            if(empty(
$str)) return false;
                
            
$this->s[2] = "WHERE ".$str." ";
        }
        
        function 
limit($val)
        {
            if(empty(
$val))    return false;
            
            
$this->s[3] = " LIMIT ".$val." \r\n";
        }
        
        function 
sql()
        {
            
$sql '';
            
            foreach(
$this->as $str)
                
$sql .= $str;

            return 
$sql;
        }
    }
    
    class 
sql_insert
    
{
        var 
$table;
        var 
$sets;
        var 
$s;
        
        function 
sql_insert()
        {
        }
        
        function 
reset()
        {
            
$this->sets '';
            
$this->'';
            
$this->table '';
        }
        
        function 
table($tablename)
        {
            
$this->reset();
            
$this->table $tablename;
            
$this->s[0] = "INSERT INTO $tablename SET ";
        }
        
        function 
set($key$value)
        {
            
$this->sets[$key] = $value;
            
            
// update sql //
            
$count 0;
            
$this->s[1] = '';
            
            
$num_sets count($this->sets);
            
            foreach(
$this->sets as $key => $value)
            {                
                
$this->s[1] .= $key."=".$value;
                
                if(
$count $num_sets 1)
                    
$this->s[1] .= ', ';
                else
                    
$this->s[1] .= ' ';
                
$count++;
            }
        }
        
        function 
sql()
        {
            
$sql '';
            
            foreach(
$this->as $str)
                
$sql .= $str;

            return 
$sql;
        }
    }
    
    class 
sql_delete
    
{
        var 
$table;
        var 
$s;
        
        function 
sql_delete()
        {
        }
        
        function 
reset()
        {
            
$this->sets '';
            
$this->'';
            
$this->table '';
        }

        function 
table($tablename)
        {
            
$this->reset();
            
$this->table $tablename;
            
$this->s[0] = "DELETE FROM $tablename ";
        }
        
        function 
where($str)
        {
            if(empty(
$str)) return false;
                
            
$this->s[2] = "WHERE ".$str." ";
        }
        
        
        function 
sql()
        {
            
$sql '';
            
            foreach(
$this->as $str)
                
$sql .= $str;

            return 
$sql;
        }        
    }
    
    class 
sql_select
    
{
        var 
$table;
        var 
$s;
        
        function 
sql_select()
        {
        }
        
        function 
reset()
        {
            
$this->sets '';
            
$this->'';
            
$this->table '';
        }

        function 
table($tablename)
        {
            
$this->reset();
            
            
$this->table $tablename;
            
$this->s[0] = "SELECT ";
            
$this->s[1] = "*";
            
$this->s[2] = " FROM $tablename ";
        }
        
        function 
cols($str)
        {
            if(empty(
$str))
                
$this->s[1] = "*";
            else
                
$this->s[1] = $str;
        }
        
        function 
where($str)
        {
            if(empty(
$str)) return false;
                
            
$this->s[4] = " WHERE ".$str." ";
        }
        
        function 
limit($val)
        {
            if(empty(
$val))    return false;
            
            
$this->s[5] = " LIMIT ".$val." \r\n";
        }
        
        function 
sql()
        {
            
$sql '';
            
            foreach(
$this->as $str)
                
$sql .= $str;

            return 
$sql;
        }        
    }
    
    
    
// $s = new sql_builder();
?>