I have just finished spending hours and hours working on getting code to find the variables so I can create them. I'm not very experienced in coding with Delphi, so forgive any bad coding, and feel free to make recommendations. This code will search for variables in your SQL statement, and then prompt the user for values where necessary. The arrays are global, so the variable values are remembered between the queries. I wanted to have the values of variables remembered between queries, so if I run a query with one set of variables, then a query with another set, then the first set again, the values I entered in are still remembered. The code for finding the variables in the SQL was given to me to Jim McDaniel, aka the Toadman. I still had to make some adjustments to work with my code, but want him to get the credit for his work.
I hope this helps you.
Type
TVarRec = Record
var_name, var_value: String;
var_idx: integer;
End;
Var
ds_variables, ds_var_values: Array[0..10] Of TVarRec;
i_var_count : integer;
Function SetDataSetVariables(ds: TOracleDataSet) : Boolean;
Var
i_set_var_values_idx, i_var_values_idx, i_variable_idx, i_var_name_idx:
Integer;
Begin
Result := False;
FindVariables(ds) ;
ds.DeleteVariables;
For i_set_var_values_idx := 0 To i_var_count - 1 Do
// for each variable that is set, loop through the values to find matches
With ds_variables[i_set_var_values_idx] Do
if var_name '' then
Begin
ds.declareVariable(var_name, otString);
For i_var_values_idx := 0 To 10 Do
If var_name = ds_var_values[i_var_values_idx] .var_name Then
var_value := ds_var_values[i_var_values_idx] .var_value;
End;
For i_variable_idx := 0 To ds.VariableCount - 1 Do
Begin
If Not InputQuery('Enter Parameter: ',
replace(ds_variables[i_variable_idx] .var_name, '_', ' ') ,
ds_variables[i_variable_idx] .var_value)
Then Exit;
ds.SetVariable(i_variable_idx, ds_variables[i_variable_idx] .var_value) ;
ds_var_values[i_variable_idx] := ds_variables[i_variable_idx];
End;
Result := True;
End;
Function FindVariables(ds: TOracleDataSet) : boolean;
Var
s_query_text : String;
i_sql_string_idx :
Integer;
Mode : Char; // S - String, V - Variable, C - Comment
quoted_string : boolean;
VarName, Char_Type,
EndCharacters : String;
Begin
s_query_text := ds.SQL.Text;
i_var_count := 0;
Mode := 'S';
char_type := 'String';
EndCharacters := '';
quoted_string := false;
result := false;
s_query_text := Replace(s_query_text, #9, ' ') ; // #9 is tab
For i_sql_string_idx := 1 To Length(s_query_text) Do
Begin
If ((s_query_text[i_sql_string_idx] = #39) And (char_type 'Comment') )
Then // #39 is single quote
quoted_string := Not quoted_string
Else
If Not quoted_string Then
Case Mode Of
'S':
Begin
If (s_query_text[i_sql_string_idx] = ':') Then
Begin
i_var_count := i_var_count + 1;
Mode := 'V';
char_type := 'Variable';
VarName := '';
End;
If (s_query_text[i_sql_string_idx] = '/') And
(i_sql_string_idx < length(s_query_text) )
And (s_query_text[i_sql_string_idx + 1] = '*') Then
Begin
Mode := 'C';
char_type := 'Comment';
EndCharacters := '*/';
End;
If (s_query_text[i_sql_string_idx] = '-') And
(i_sql_string_idx < length(s_query_text) ) And
(s_query_text[i_sql_string_idx + 1] = '-') Then
Begin
Mode := 'C';
char_type := 'Comment';
EndCharacters := #13#10; // line feed and return
End;
End;
'V':
Begin
If (i_sql_string_idx = length(s_query_text) ) Or (Not
(s_query_text[i_sql_string_idx] In ['a'..'z', 'A'..'Z',
'0'..'9', '_', '#', '$'] ) ) Then
Begin
ds_variables[i_var_count - 1] .var_Name := varName;
Mode := 'S';
char_type := 'String';
End
Else
VarName := VarName + s_query_text[i_sql_string_idx] ;
End;
'C':
If (s_query_text[i_sql_string_idx] = EndCharacters[1] ) And
(i_sql_string_idx < length(s_query_text) ) And
(s_query_text[i_sql_string_idx + 1] = EndCharacters[2] )
Then
Begin
mode := 'S';
char_type := 'String';
End;
End;
End;
End;
------------------
Phyllis Helton
Information Services Manager
The JESUS Film Project
www.jesusfilm.org