Hi everyone,
I have a database with about 18 related tables, and i realize that I will be retrieving data with a LOT of different where statements.
For example,
initially i was thinking of programming separate functions that retrieve data specifically according to hard coded SQL "WHERE" i put in.
So i would write;
getAllBooks(); //SQL no WHERE statement
getBooksOwnedByUser() //SQL has a WHERE user.username = XX
getBooksinCategory() // SQL has a WHERE cat.catname = XX
getBooksUserCat() // SQL has a WHERE cat.catname = XX AND user.username = XX
Needless to say, I'd be programming to infinity if i hardcoded the sql statement in each function, and end up with like 100 functions if i had a lot of related tables and alot of different "WHERE" requirements.
I was thinking of a better way to program a universal getBooks() function, where i pass into it an array of where requirements,
and then dynamically generate the WHERE clause.
I would put in a "WHERE 1" into the Base SQL case, and then just keep appeninding AND xxx.xxx = XXX AND xxx.xxx= XXX depending on what the WHERE requirements the array i pass in contains.
Ok thats the simple part.
THe problem is, the SQL will not be a simple one that just queries one table. I would also have to append in the "JOINS" because a lot of the fields are in separate related tables.
This is not even taking into account the order of the joins, which should be arranged such that the query is fastest.
Is this possible? Is there any existing example code that would allow me to do this? Or am i fated to programming 10000000 getBooksXXXXX() statements?
Many thanks....
I have a database with about 18 related tables, and i realize that I will be retrieving data with a LOT of different where statements.
For example,
initially i was thinking of programming separate functions that retrieve data specifically according to hard coded SQL "WHERE" i put in.
So i would write;
getAllBooks(); //SQL no WHERE statement
getBooksOwnedByUser() //SQL has a WHERE user.username = XX
getBooksinCategory() // SQL has a WHERE cat.catname = XX
getBooksUserCat() // SQL has a WHERE cat.catname = XX AND user.username = XX
Needless to say, I'd be programming to infinity if i hardcoded the sql statement in each function, and end up with like 100 functions if i had a lot of related tables and alot of different "WHERE" requirements.
I was thinking of a better way to program a universal getBooks() function, where i pass into it an array of where requirements,
and then dynamically generate the WHERE clause.
I would put in a "WHERE 1" into the Base SQL case, and then just keep appeninding AND xxx.xxx = XXX AND xxx.xxx= XXX depending on what the WHERE requirements the array i pass in contains.
Ok thats the simple part.
THe problem is, the SQL will not be a simple one that just queries one table. I would also have to append in the "JOINS" because a lot of the fields are in separate related tables.
This is not even taking into account the order of the joins, which should be arranged such that the query is fastest.
Is this possible? Is there any existing example code that would allow me to do this? Or am i fated to programming 10000000 getBooksXXXXX() statements?
Many thanks....