Pedro Pinto wrote:
I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:
There's no way to automatically execute code found in a string. C code
is compiled by a compiler into machine code. Once your program is
running, the compiler is no longer available. If you need to evaluate
arbitrary expressions supplied at run time, you must write your own
interpreter.
UPDATE Table_name SET colum_name = new_value WHERE colum_name =
old_value
or
SELECT column FROM table WHERE column operator value
You want to write a SQL server? You want to write a program that
understands and executes UPDATE and SELECT commands?
You need to parse the command into an array or list of tokens:
{"SELECT", "column", "FROM", "table",
"WHERE", "column", "operator", "value"}
Then identify the keywords, and get the variable data (column name, the
table name, etc). You will need to write code that searches through the
data. The simplest way would be to go through each record in your
database and match it against the given conditions.
Here's some pseudocode. You need to implement the functions parse,
get_value, and add_result.
parse("SELECT name FROM classmates WHERE sex = female",
select_column,
select_table,
where_column,
where_operator,
where_value);
/* After tokenisation and parsing now
select_column is "name",
select_table is "classmates",
where_column is "sex",
where_operator is "=",
and where_value is "female"
*/
for(row = 0; row < num_rows; row++)
{
const char *data_value = get_value(row, where_column);
if(!strcmp(where_operator, "=")
{
if(!strcmp(where_value, data_value)) add_result(row);
}
else if(!strcmp(where_operator, "!=")
{
if(strcmp(where_value, data_value)) add_result(row);
}
else
{
fprintf(stderr, "Unrecognised operator %s\n", where_operator);
}
}
--
Simon.