Re: OOP / dynamic SQL class
| From: | Peter J. Schoenster | Date: | Thu, 22 Aug 2002 16:43:00 +0000 |
| Subject: | Re: OOP / dynamic SQL class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-113646@lists.php.net to get a copy of this message | ||
On 22 Aug 2002 at 12:10, jochen schultz wrote:
> 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,
> 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?
Jochen,
First thing is to always search for an example of what you want. Rarely is anything new under the
sun. Well, I have been searching and I
have not found any good example ... I do what you want to do in Perl (I have not bothered to do it
in PHP). Here is what I do in Perl in
plain language:
On every instance of the script I get the current database schema in an array of arrays:
$database = new array(
table1 => (
filed=>(
type=int, or char or enum or date or
lenght=>10 or 20 or 255 tec
)
table2=>
and so on
)
So I have that. Then I want to insert data into the database. I ALWAYS have data in an array that
has been parsed and validated and it's
in a array of arrays (if necessary) as well.
So then I pass to the function as such
This is Perl but you get the idea
my $results = $self->{dbi}->insert_into_tables(
data_model=>$self->{data_model},
data_hash=>$self->{input},
required=>\%required,
tables=>[qw(user_config)],
dbh=>$self->{dbh},
);
$self->{data_model} = database schema
$self->{input} = the data that my script received from wherever it received it, it is put in an
array of arrays as necessary
%required = this is weak, but I pass which fields are required and it will fail if no data is
associated with a required field
tables=>[qw(user_config)], = that is an array of the tables that is in the database, data will
get inserted for each table
dbh=>$self->{dbh}, = that's the database handler
insert_into_tables = the function that accepts the data and iterates over everything to verify that
everythign is what it should be, it returns
information that I store in the $results array.
Someone must be doing somethin gsimilar to this in PHP. When inserting or updating you repeat the
same code over and over, why not
centralize it.
I did not find it. You should post to the DB list.
Peter