Created
May 2, 2024 18:19
-
-
Save wellington1993/4bdc3a3d40bfb4b09ae855f4ea87a3bf to your computer and use it in GitHub Desktop.
Exporta de um banco A(Firebird) para um novo banco B, mudando colunas BIGINT para INTEGER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uses | |
SysUtils, Classes, ShellApi, Windows, IBDatabase, IBSQL; | |
const | |
CAMINHO_BANCO_ORIGEM = 'caminho_do_banco_origem.fdb'; | |
USUARIO_ORIGEM = 'usuario_origem'; | |
SENHA_ORIGEM = 'senha_origem'; | |
CAMINHO_BANCO_DESTINO = 'caminho_do_novo_banco_de_dados.fdb'; | |
USUARIO_DESTINO = 'usuario_destino'; | |
SENHA_DESTINO = 'senha_destino'; | |
CAMINHO_EXPORTACAO = 'caminho_temporario_exportacao\'; | |
NOME_ARQUIVO_ESTRUTURA = 'estrutura.sql'; | |
NOME_ARQUIVO_ESTRUTURA_MODIFICADO = 'estrutura_modificada.sql'; | |
function ExecutarComandoEsperar(const Comando, Parametros: String): Boolean; | |
var | |
ExecInfo: TShellExecuteInfo; | |
ExitCode: DWORD; | |
begin | |
FillChar(ExecInfo, SizeOf(ExecInfo), 0); | |
ExecInfo.cbSize := SizeOf(ExecInfo); | |
ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS; | |
ExecInfo.lpFile := PChar(Comando); | |
ExecInfo.lpParameters := PChar(Parametros); | |
ExecInfo.nShow := SW_HIDE; | |
if ShellExecuteEx(@ExecInfo) then | |
begin | |
WaitForSingleObject(ExecInfo.hProcess, INFINITE); | |
GetExitCodeProcess(ExecInfo.hProcess, ExitCode); | |
Result := (ExitCode = 0); | |
CloseHandle(ExecInfo.hProcess); | |
end | |
else | |
Result := False; | |
end; | |
procedure ExportarEstruturaEMigrarDados; | |
var | |
Conexao: TIBDatabase; | |
Transacao: TIBTransaction; | |
SQL: TIBSQL; | |
ListaTabelas: TStringList; | |
NomeTabela: String; | |
begin | |
// Conectar ao banco de dados de origem e obter a lista de tabelas | |
Conexao := TIBDatabase.Create(nil); | |
Transacao := TIBTransaction.Create(nil); | |
SQL := TIBSQL.Create(nil); | |
ListaTabelas := TStringList.Create; | |
try | |
Conexao.DatabaseName := CAMINHO_BANCO_ORIGEM; | |
Conexao.LoginPrompt := False; | |
Conexao.Params.Values['user_name'] := USUARIO_ORIGEM; | |
Conexao.Params.Values['password'] := SENHA_ORIGEM; | |
Conexao.DefaultTransaction := Transacao; | |
Transacao.DefaultDatabase := Conexao; | |
Conexao.Connected := True; | |
Transacao.StartTransaction; | |
SQL.Database := Conexao; | |
SQL.Transaction := Transacao; | |
SQL.SQL.Text := 'SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0'; | |
SQL.ExecQuery; | |
while not SQL.Eof do | |
begin | |
NomeTabela := Trim(SQL.FieldByName('RDB$RELATION_NAME').AsString); | |
ListaTabelas.Add(NomeTabela); | |
SQL.Next; | |
end; | |
SQL.Close; | |
Transacao.Commit; | |
// Iterar pelas tabelas e chamar o FBExport para cada uma | |
for NomeTabela in ListaTabelas do | |
begin | |
if ExecutarComandoEsperar('FBExport', '-Si -D ' + CAMINHO_BANCO_ORIGEM + ' -U ' + USUARIO_ORIGEM + ' -P ' + SENHA_ORIGEM + | |
' -F ' + CAMINHO_EXPORTACAO + NomeTabela + '.csv -V ' + NomeTabela) then | |
begin | |
ExecutarComandoEsperar('FBExport', '-Si -D ' + CAMINHO_BANCO_DESTINO + ' -U ' + USUARIO_DESTINO + ' -P ' + SENHA_DESTINO + | |
' -F ' + CAMINHO_EXPORTACAO + NomeTabela + '.csv -V ' + NomeTabela); | |
end; | |
end; | |
finally | |
ListaTabelas.Free; | |
SQL.Free; | |
Transacao.Free; | |
Conexao.Free; | |
end; | |
end; | |
begin | |
ExportarEstruturaEMigrarDados; | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://sl.bing.net/g8pqkSf5Bu0 | |
Este código conecta-se ao banco de dados de origem, obtém a lista de tabelas e, para cada tabela, chama o FBExport para exportar os dados para um arquivo CSV e, em seguida, importa esses dados para o banco de dados de destino. A função ExecutarComandoEsperar é usada para garantir que cada comando seja concluído antes de prosseguir para o próximo. | |
Lembre-se de substituir os valores das constantes pelos caminhos e credenciais corretos do seu ambiente de banco de dados Firebird. | |
Espero que isso atenda às suas necessidades! Se precisar de mais assistência, estou à disposição. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment