Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // As funcoes abaixo estão referenciando funções e constantes de minha biblioteca particular,
- // felizmente elas são de fácil substituição e entendimento.
- // Por isso adapte segundo suas necessidades antes de mandar compilar.
- { Usa o utilitario isql.exe para extrair o metadados de um DB e exporta-o para um arquivo }
- function ISQL_Exec(sScriptFile: TFileName = ''; bStopOnWarning: Boolean = false;
- sLogFile: TFileName = ''; sISQL_EXE: TFileName = '';
- ConnParams: String = ''): Boolean;
- var
- sPrefixo, sParams:String;
- sServer, sDatabase, sLogin, sLoginPassword, sCharacterSet, sCollate:String;
- sParam: String;
- nReturnCode: Integer;
- sFullPath: String;
- sISQL_Error:String;
- bError:Boolean;
- L: TStringLIST;
- begin
- Result := false;
- if sISQL_EXE=''
- then sISQL_EXE:=DEFAULT_ISQL; // caminho+nome do isql.exe
- if (not FileExists(sISQL_EXE)) or (not FileExists(sScriptFile)) then
- begin
- ExibeErro('Erro:', 'Erro ao executar o script:' + CR +
- 'Um desses arquivos não foi encontrado:' + CR + TAB4 + 'isql.exe="' +
- sISQL_EXE + '"' + CR + TAB4 + 'script=' + sScriptFile + CR);
- Exit;
- end;
- // Detectando variaveis importantes;
- L:=nil;
- L:=TStringList.Create;
- L.Text:=ConnParams;
- sServer:=L.Values['Server'];
- sDatabase:=L.Values['Database'];
- sCharacterSet:=L.Values['CharacterSet'];
- sLogin:=L.Values['User_Name'];
- sLoginPassword:=L.Values['Password'];
- sCollate:='';
- sISQL_Error:='';
- if UPPERCASE(sCharacterSet)='ISO8859_1'
- then sCollate:='PT_BR';
- if UPPERCASE(sCharacterSet)='WIN1252'
- then sCollate:='WIN_PTBR';
- if UPPERCASE(sCharacterSet)='UTF8'
- then sCollate:='UNICODE_CI_AI';
- // acrescentando : CONNECT 'f:\sesmt\dados\sesmt.fdb' user 'SYSDBA' password 'masterkey';
- L := nil;
- L := TStringLIST.Create;
- if sLogFile = '' then
- sLogFile := DEFAULT_SAVETOLOG+'\firebird-isql.log';
- if sServer = '' then
- sParam := ' "' + sDatabase + '" -U "' + sLogin + '" -P "' +
- sLoginPassword + '" -now -i "' + sScriptFile
- else
- sParam := ' "' + sServer + ':' + sDatabase + '" -U "' + sLogin +
- '" -P "' + sLoginPassword + '" -now -i "' + sScriptFile;
- if sLogFile <> '' then
- sParam := sParam + '" -o "' + sLogFile + '"';
- nReturnCode := WinExecAndWait32(sISQL_EXE, sParam, SW_SHOW);
- ISQL_Code2String(nReturnCode, sISQL_Error, bError);
- Result:=(not bError);
- if (not Result) then
- begin
- if (Pos('[SQLWARNING]',sISQL_Error)>0) then
- begin
- if not bStopOnWarning then
- begin
- Result:=true;
- end;
- end;
- end;
- if (not Result) then
- begin
- L.LoadFromFile(sLogFile);
- ExibeErro('Erro (Exit Code=#'+IntToStr(nReturnCode)+'):', 'Ao executar o script:' + CRLF + TAB4 + '"' +
- sISQL_EXE + '" ' + sParam + CRLF +
- 'Ocorreu o seguinte erro:'+#13#10+
- 'Exit Code: '+IntToStr(nReturnCode)+#13#10+
- 'Exit Message: '+sISQL_Error+#13#10+
- 'Cmd: <observe os detalhes>', L.Text);
- end;
- L.Free;
- // Aguarda 5s
- // Sleep(5000);
- end;
- // Retorna uma descrição do Erro/Sucesso do código que uma execução ISQL retornará
- procedure ISQL_Code2String(nReturnCode:Integer;
- var sISQL_Error:String;
- var bError:Boolean);
- begin
- { http://www.firebirdsql.org/manual/isql-errors.html
- SQLCODE Message Meaning
- <0 SQLERROR Error occurred: statement did not execute
- 0 SUCCESS Successful execution
- +1 to +99 SQLWARNING System warning or information message
- +100 NOT FOUND No qualifying rows found, or end of current active set of rows reached
- }
- bError:=false;
- if (nReturnCode < 0) then // SQLERROR (Error occurred: statement did not execute)
- begin
- bError := true;
- sISQL_Error:='[SQLERROR] Error occurred: statement did not execute';
- end;
- if (nReturnCode > 0) and (nReturnCode < 100)then // SQLERROR (System warning or information message)
- begin
- bError := true;
- sISQL_Error:='[SQLWARNING] System warning or information message';
- end;
- if (nReturnCode >= 100)then // NOT FOUND (System warning or information message)
- begin
- bError := true;
- sISQL_Error:='[NOT FOUND ] No qualifying rows found, or end of current active set of rows reached';
- end;
- end;
- // WinExecAndWait32 executa processos via cmd e retorna < 0 se houve algum erro
- function WinExecAndWait32(FileName:String; sParam:String ; wWindow : Word=SW_SHOW):Longword;
- var { by Pat Ritchey }
- zAppName: array[0..512] of Char;
- zCurDir: array[0..255] of Char;
- WorkDir: string;
- sCommandLine: String;
- StartupInfo: TStartupInfo;
- ProcessInfo: TProcessInformation;
- AppIsRunning: DWORD;
- begin
- //StrPCopy(zAppName, FileName);
- StrPCopy(zAppName,'"'+FileName+'" '+sParam);
- GetDir(0, WorkDir);
- StrPCopy(zCurDir, WorkDir);
- FillChar(StartupInfo, SizeOf(StartupInfo), #0);
- StartupInfo.cb := SizeOf(StartupInfo);
- StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
- StartupInfo.wShowWindow := wWindow;
- if not CreateProcess(nil,
- zAppName, // pointer to command line string
- nil, // pointer to process security attributes
- nil, // pointer to thread security attributes
- False, // handle inheritance flag
- CREATE_NEW_CONSOLE or // creation flags
- NORMAL_PRIORITY_CLASS,
- nil, //pointer to new environment block
- nil, // pointer to current directory name
- StartupInfo, // pointer to STARTUPINFO
- ProcessInfo) // pointer to PROCESS_INF
- then Result := WAIT_FAILED
- else
- begin
- while WaitForSingleObject(ProcessInfo.hProcess, 0) = WAIT_TIMEOUT do
- begin
- Application.ProcessMessages;
- Sleep(50);
- end;
- {
- // or:
- repeat
- AppIsRunning := WaitForSingleObject(ProcessInfo.hProcess, 100);
- Application.ProcessMessages;
- Sleep(50);
- until (AppIsRunning <> WAIT_TIMEOUT);
- }
- WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
- GetExitCodeProcess(ProcessInfo.hProcess, Cardinal(Result));
- CloseHandle(ProcessInfo.hProcess);
- CloseHandle(ProcessInfo.hThread);
- end;
- end; { WinExecAndWait32 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement