OOP / dynamic SQL class
| From: | jochen schultz | Date: | Thu, 22 Aug 2002 10:10:31 +0000 |
| Subject: | OOP / dynamic SQL class | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-113601@lists.php.net to get a copy of this message | ||
Hi again,
i got a question concerning OOP, would be nice if anyone could help.
I am trying to build a class that builds SQL statements dynamically like i send tablenames,
fieldnames, etc and it should build select, insert, update... statements.
e.g. i have a table PERS_DATA with following structur :
ID | FIRSTNAME varchar(40) | LASTNAME varchar(40) | REGISTRATION date | AGE int (5)
(sure it makes no sence to save the age but it´s an example)
Now i want to save postdata of a registerform in this table and it should work like this :
$query_obj = new sql_class('insert','PERS_DATA',$postvars);
$db_obj = new db_class();
$db_obj -> query ($query_obj -> sql);
sql_class should instance a check_persdat_postvars_class that sends me a predefined error in case
the entries of the form where wrong (age not numeric for example)
Now the sql class would have to build a insertstatement like this :
$fields = "";
$vals = "";
$i=0;
while ( list ( $key,$val ) = each ( $param_arr ) )
{
if (!empty($i))
{
$fields .= ',';
$vals .= ',';
}
$fields .= $key;
$vals .= $val;
$i++;
}
$this -> SQL = "insert into $table ($fields) values ($vals)";
but elements of $vals 'can' be strings and some can be numeric...
Do you think it´s a good idea to make queries to find out about fieldtypes as i need that info to
set quotes for the string entries?
Or do you think i should predefine it instead in the class that checks the postvars?
Or do you think i have a absolute stupid idea?
Tia
regards
Jochen