<?php
/********************************************************************
Project: sql_Generator
Version: 0.9
Author: Carsten Gehling (hide@address.com)
Date: 2003-07-29
Copyright (c) 2003 Carsten Gehling <hide@address.com>
-------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
NOTES FROM AUTHOR
-------------------
If you make any change in the code, please let me know by email.
If I find your enhancements useful, I will publish a new version
with the changes, of course with credit to you.
********************************************************************/
/////////////////////////////////////////////////////////////////////
// Class sql_Generator
// ------------------------------------------------------------------
// This class is used to generate insert and update sql statements
//
// Usage:
//
class sql_Generator {
// private
var $tablename = "";
var $fields = array();
// public
function sql_Generator($tablename = "") {
if ($tablename != "")
$this->create($tablename);
}
// public
function create($tablename) {
$this->tablename = $tablename;
$this->fields = array();
}
// public
function addField($key, $value, $type = "string") {
$this->fields[$key] = array("type" => $type, "value" => $value);
}
// public
function makeInsert() {
if ($this->tablename == "")
return "";
if (count($this->fields) == 0)
return "";
$sql_key = "";
$sql_val = "";
reset($this->fields);
while (list($key, $field) = each($this->fields)) {
$val = $field["value"];
if (is_string($val))
$val = addslashes($val);
if ($field["type"] == "string")
$val = "'$val'";
$sql_key .= ($sql_key != "" ? ", " : "") . "$key";
$sql_val .= ($sql_val != "" ? ", " : "") . "$val";
}
$sql = "insert into $this->tablename ($sql_key) values ($sql_val) ";
return $sql;
}
// public
function makeUpdateKey($key, $value, $type = "string") {
if (is_string($value))
$value = addslashes($value);
if ($type == "string")
$value = "'$value'";
return $this->makeUpdate("$key = $value");
}
// public
function makeUpdate($whereexpr) {
if ($this->tablename == "")
return "";
if (count($this->fields) == 0)
return "";
$sql_upd = "";
reset($this->fields);
while (list($key, $field) = each($this->fields)) {
$val = $field["value"];
if (is_string($val))
$val = addslashes($val);
if ($field["type"] == "string")
$val = "'$val'";
$sql_upd .= ($sql_upd != "" ? ", " : "") . "$key = $val ";
}
$sql = "update $this->tablename set $sql_upd ";
$sql .= "where $whereexpr ";
return $sql;
}
}
?>