Ver Mensaje Individual
  #6  
Antiguo 08-09-2006
teracat teracat is offline
Registrado
 
Registrado: jun 2006
Posts: 9
Reputación: 0
teracat Va por buen camino
Thumbs up Posible solución

Hola, ya he encontrado una solución al problema que parece que funciona.

Os pongo aquí el código por si a alguien le puede ser útil. Si encontrais algún error, por favor, comentadlo en este foro para que nos entermos. Por supuesto, también se aceptan comentarios, críticas, mejoras, etc.

Ahí va el código:

(Tened en cuenta que se utiliza un FSQL que es del tipo TIBSQL y que la constante cFileExecutorSeparator debe estar definida y usada en el archivo para separar las sentencias [ver ejemplo al final]).

FSQL: TIBSQL; //Faltará inicializarla como es debido

const
cFileExecutorSeparator = '--##@##';

function ExecuteFromFile(const AFilename: string): boolean;
var
FileContents: TStrings;
fContentsLeft, fContentsToExe: string;
iPos: integer;
begin
{ Si el fichero no existe no continuamos }
if not FileExists(AFilename) then
begin
Result := False;
exit;
end;
{ Cargar el contenido del fichero }
FileContents := TStringList.Create;
try
FileContents.LoadFromFile(AFilename);
fContentsLeft := FileContents.Text;
finally
FileContents.Free;
end;
if fContentsLeft='' then
Result := True
else
begin
try
if not FSQL.Transaction.Active then FSQL.Transaction.StartTransaction;
Close;
{ Vamos ejecutando hasta que no quede nada }
while fContentsLeft<>'' do
begin
iPos := Pos(cFileExecutorSeparator,fContentsLeft);
if iPos>0 then
begin
fContentsToExe := Copy(fContentsLeft,1,iPos-1);
fContentsLeft := Copy(fContentsLeft,iPos+Length(cFileExecutorSeparator),Length(fContentsLeft));
end
else
begin
fContentsToExe := fContentsLeft;
fContentsLeft := '';
end;
fContentsToExe := Trim(fContentsToExe);
if fContentsToExe<>'' then
begin
{ Aquí está la ejecución la sentencia }
FSQL.SQL.Text := fContentsToExe;
FSQL.Prepare;
FSQL.ExecQuery;
end;
end;
Result := True;
except
on E: Exception do
begin
Result := False;
{ Tratar la excepción como guste }
end;
end;
end;
end;


Ejemplo de script:

--Creamos una vista
DROP VIEW nombre_vista;

--##@##

--Insertamos alguna fila
INSERT INTO tabla(campos) VALUES(valores);

--##@##

--Si queremos hacer más de un insert, deben ir separados
INSERT INTO tabla(campos) VALUES(valores);

--##@##

--Creamos un dominio
CREATE DOMAIN dominio AS tipo;

--##@##

ALTER PROCEDURE "nombre_procedimiento"
(
parámetros
)
RETURNS
(
retorno
)
AS
BEGIN
--Contenido del procedimiento
END

--##@##


Este script os debería funcionar (poniendo nombres reales, claro).
También comentar que al hacerse todo en la misma transacción, si la función devuelve false deberíamos hacer un Rollback para no validar los cambios.

Espero que sea de ayuda a alguien.

Hasta pronto!
Responder Con Cita