( SELECT
'C' AS tipo_doc,
MIN( c.contrato_id ) AS num_doc,
MIN( num_versao ) AS versao_doc
FROM
contrato c
INNER JOIN contrato_versao cv ON c.contrato_id = cv.contrato_id
WHERE
data_versao >= '2024-10-01'
AND data_versao < '2024-11-01'
AND cv.status_contrato <> 0
GROUP BY
num_fatura
) UNION ALL
(
SELECT
'IND' AS tipo_doc,
indenizacao_id AS num_doc,
0 AS versao_doc
FROM
indenizacao_proposta ip
INNER JOIN cliente cli ON ip.cliente_id = cli.cliente_id
WHERE
data_emissao_fatura >= '2024-10-01'
AND data_emissao_fatura < '2024-11-01'
) UNION ALL
(
SELECT
'MANINT' AS tipo_doc,
manutencao_id AS num_doc,
0 AS versao_doc
FROM
manutencao_proposta mp
INNER JOIN cliente cli ON mp.cliente_id = cli.cliente_id
WHERE
data_emissao_fatura >= '2024-10-01'
AND data_emissao_fatura < '2024-11-01'
) UNION ALL
(
SELECT
'M' AS tipo_doc,
contrato_id AS num_doc,
CAST(
CONCAT(
RIGHT ( CAST( YEAR ( '2024-10-01' ) AS CHAR ), 2 ),
CAST( LPAD( MONTH ( '2024-10-01' ), 2, 0 ) AS CHAR )) AS INTEGER
) AS versao_doc
FROM
medicao_fechamentos
);
Create a Delphi language function that composes the SQL text and have InitialDate and FinalDate (TDateTime) as function parameters
Assistant
Certainly! Below is a Delphi function named BuildSQLQuery that takes two TDateTime parameters, InitialDate and FinalDate, and returns the composed SQL string. The function ensures that the dates are correctly formatted and inserted into the SQL query. It also handles the necessary escaping of single quotes within the SQL string.
The InitialDate and FinalDateTDateTime parameters are formatted into the YYYY-MM-DD string format using Delphi's FormatDateTime function. This ensures that the dates are correctly formatted for the SQL query.
Quoting Strings:
The QuotedStr function is used to safely quote the date strings within the SQL query, preventing SQL injection and ensuring that the dates are correctly interpreted as string literals in SQL.
Building the SQL String:
The SQL query is constructed by concatenating multiple string literals. sLineBreak is used to insert line breaks for better readability.
Each SELECT statement is enclosed in parentheses and combined using UNION ALL.
Special attention is given to escaping single quotes within the SQL string by doubling them ('').
Dynamic Date Usage:
The InitialDate is used not only for filtering but also within SQL functions like YEAR and MONTH in the fourth SELECT statement. This ensures that the versioning logic dynamically reflects the provided date range.
Usage Example
Here's how you might use the BuildSQLQuery function in your Delphi application:
delphi
procedure TForm1.Button1Click(Sender: TObject);
var
SQLQuery: string;
StartDate, EndDate: TDateTime;
begin
// Define your date range
StartDate := EncodeDate(2024, 10, 1);
EndDate := EncodeDate(2024, 11, 1);
// Build the SQL query
SQLQuery := BuildSQLQuery(StartDate, EndDate);
// Output or use the SQL query as needed
ShowMessage(SQLQuery);
// Example: Assign to a TQuery component
// Query1.SQL.Text := SQLQuery;
// Query1.Open;
end;
Important Considerations
SQL Injection Protection:
While QuotedStr helps in preventing SQL injection by properly quoting strings, always ensure that the dates passed to the function are validated and sanitized as necessary.
Date Validity:
Ensure that InitialDate is earlier than FinalDate to avoid logical errors in the SQL query.
Database Compatibility:
The SQL functions used (YEAR, MONTH, LPAD, CAST, CONCAT, etc.) should be supported by your database system. Adjust the syntax if you're using a specific SQL dialect that requires different function names or formats.
This function should integrate seamlessly into your Delphi application, allowing you to dynamically generate the required SQL query based on the provided date range.