Skip to content

Commit aac0ec8

Browse files
committed
Fixed issue bug #3 on anonymizedb command
1 parent bfd6db2 commit aac0ec8

5 files changed

Lines changed: 56 additions & 26 deletions

File tree

Packages/d103/sqlcmdcli.dpr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ begin
7474
end;
7575

7676
TCommandHandler.ExecuteCommand(LParseResult.Command);
77-
78-
Readln;
77+
TConsole.Log(RS_THANKS_FOR_USING, Info, True);
78+
Exit;
7979
except
80-
on E: Exception do
80+
on E: Exception do
8181
begin
8282
Writeln(E.ClassName, ': ', E.Message);
8383
ExitCode := 1;
84+
TConsole.Log(RS_THANKS_FOR_USING, Info, True);
85+
Exit
8486
end;
8587
end;
8688
end.

Packages/d103/sqlcmdcli.dproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@
112112
<VerInfo_Locale>1033</VerInfo_Locale>
113113
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
114114
<VerInfo_AutoIncVersion>true</VerInfo_AutoIncVersion>
115-
<VerInfo_Keys>CompanyName=;FileDescription=Command-line utility for ad hoc, interactive execution of commands on SQL Server;FileVersion=1.0.0.80;InternalName=sqlcmdcli;LegalCopyright=;LegalTrademarks=;OriginalFilename=sqlcmdcli.exe;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0;Comments=https://github.com/segovoni/sqlcmdcli</VerInfo_Keys>
116-
<VerInfo_Build>80</VerInfo_Build>
115+
<VerInfo_Keys>CompanyName=;FileDescription=Command-line utility for ad hoc, interactive execution of commands on SQL Server;FileVersion=1.0.0.112;InternalName=sqlcmdcli;LegalCopyright=;LegalTrademarks=;OriginalFilename=sqlcmdcli.exe;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0;Comments=https://github.com/segovoni/sqlcmdcli</VerInfo_Keys>
116+
<VerInfo_Build>112</VerInfo_Build>
117117
</PropertyGroup>
118118
<ItemGroup>
119119
<DelphiCompile Include="$(MainSource)">

Sources/sqlcmdcli.AnonymizeDB.pas

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class procedure TAnonymizeDB.Run(const AServerName, ADatabaseName, AUserName,
348348
'AND (' + LSQLDBTableInfo.ColumnName + '<>'''')';
349349

350350
if (AVerbose) then
351-
TConsole.Log(LQry.SQL.Text, Success, True);
351+
TConsole.Log(LQry.SQL.Text, Info, True);
352352

353353
LQry.ExecSQL;
354354
end;
@@ -379,13 +379,16 @@ class procedure TAnonymizeDB.Run(const AServerName, ADatabaseName, AUserName,
379379
TConsole.Log(RS_CMD_ANONYMIZEDB_ENABLE_TR_END, Success, True);
380380

381381
LConnection.CommitTrans;
382-
TConsole.Log(Format(RS_CMD_ANONYMIZEDB_END, [ADatabaseName]), Success, False);
382+
TConsole.Log(Format(RS_COMMIT_TRANSACTION, [ADatabaseName]), Success, True);
383+
TConsole.Log(Format(RS_CMD_ANONYMIZEDB_END, [ADatabaseName]), Success, True);
383384
except
384-
on E: Exception do begin
385-
LConnection.RollbackTrans;
386-
//Writeln(E.ClassName, ': ', E.Message);
387-
TConsole.Log(E.ClassName + ': ' + E.Message, Error, True);
388-
end;
385+
on E: Exception do
386+
begin
387+
if (LConnection.InTransaction) then
388+
LConnection.RollbackTrans;
389+
TConsole.Log(E.ClassName + ': ' + E.Message, Error, True);
390+
TConsole.Log(Format(RS_ROLLBACK_TRANSACTION, [ADatabaseName]), Warning, True);
391+
end;
389392
end;
390393

391394
finally
@@ -394,7 +397,6 @@ class procedure TAnonymizeDB.Run(const AServerName, ADatabaseName, AUserName,
394397
FreeAndNil(LConnection);
395398
FreeAndNil(LTableList);
396399
end;
397-
398400
end;
399401

400402
end.

Sources/sqlcmdcli.Console.pas

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,94 @@ interface
77

88
type
99
TConsoleForegroundColor = (
10+
BlackForeground = 0,
1011
GreenForeground = FOREGROUND_GREEN,
1112
NavyForeground = FOREGROUND_BLUE,
13+
GrayForeground = FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE,
14+
AquaForeground = FOREGROUND_INTENSITY or FOREGROUND_GREEN or FOREGROUND_BLUE,
1215
RedForeground = FOREGROUND_INTENSITY or FOREGROUND_RED,
13-
LimeForeground = FOREGROUND_INTENSITY or FOREGROUND_GREEN
16+
LimeForeground = FOREGROUND_INTENSITY or FOREGROUND_GREEN,
17+
WhiteForeground = FOREGROUND_INTENSITY or FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE
1418
);
1519

1620
TConsoleBackgroundColor = (
17-
BlackBackground = 0
21+
BlackBackground = 0,
22+
WhiteBackground = BACKGROUND_INTENSITY or BACKGROUND_RED or BACKGROUND_GREEN or BACKGROUND_BLUE
1823
);
1924

2025
TConsoleState = (Default, Info, Success, Warning, Error);
2126

2227
TConsole = class(TObject)
2328
private
2429
class var FHandle: Cardinal;
30+
class var FConOut: THandle;
31+
class var FBufInfo: TConsoleScreenBufferInfo;
2532
public
2633
class constructor Create;
2734
class procedure SetForegroundColor(const AValue: TConsoleForegroundColor);
28-
class procedure SetBackgroundColor(const AValue: TConsoleBackgroundColor);
35+
//class procedure SetBackgroundColor(const AValue: TConsoleBackgroundColor);
2936
class procedure SetTitle(const AValue: string);
3037
class procedure Log(const AMessage: string; AState: TConsoleState;
3138
const ANewLine: Boolean);
3239
end;
3340

3441
implementation
3542

43+
uses
44+
sqlcmdcli.Constants;
45+
3646
{ TConsole }
3747

3848
class constructor TConsole.Create;
3949
begin
4050
inherited;
4151
FHandle := GetStdHandle(STD_OUTPUT_HANDLE);
42-
end;
43-
44-
class procedure TConsole.SetBackgroundColor(
45-
const AValue: TConsoleBackgroundColor);
46-
begin
47-
52+
// Get console screen buffer handle
53+
FConOut := TTextRec(Output).Handle;
54+
// Save current text attributes
55+
GetConsoleScreenBufferInfo(FConOut, FBufInfo);
4856
end;
4957

5058
class procedure TConsole.SetForegroundColor(
5159
const AValue: TConsoleForegroundColor);
5260
begin
53-
//SetConsoleTextAttribute(FHandle, AValue);
61+
SetConsoleTextAttribute(FHandle, Word(AValue));
5462
end;
5563

5664
class procedure TConsole.SetTitle(const AValue: string);
5765
begin
58-
66+
SetConsoleTitle(PChar(AValue));
5967
end;
6068

6169
class procedure TConsole.Log(const AMessage: string; AState: TConsoleState;
6270
const ANewLine: Boolean);
71+
var
72+
LConOut: THandle;
73+
LBufInfo: TConsoleScreenBufferInfo;
6374
begin
75+
// Get console screen buffer handle
76+
LConOut := TTextRec(Output).Handle;
77+
78+
// Save current text attributes
79+
GetConsoleScreenBufferInfo(LConOut, LBufInfo);
80+
6481
case AState of
65-
Default: SetForegroundColor(GreenForeground);
66-
Info: SetForegroundColor(NavyForeground);
82+
Default: SetConsoleTextAttribute(FConOut, FBufInfo.wAttributes);
83+
Info: SetForegroundColor(AquaForeground);
6784
Success: SetForegroundColor(GreenForeground);
6885
Warning: SetForegroundColor(RedForeground);
6986
Error: SetForegroundColor(RedForeground);
87+
else
88+
SetForegroundColor(BlackForeground);
7089
end;
90+
7191
if ANewLine then
7292
Writeln(AMessage)
7393
else
7494
Write(AMessage);
95+
96+
// Reset to defaults
97+
SetConsoleTextAttribute(LConOut, LBufInfo.wAttributes);
7598
end;
7699

77100
end.

Sources/sqlcmdcli.ResourceStrings.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ interface
8181
RS_ERROR_SQL_SERVER_NATIVE_CLIENT = 'SQL Server Native Client %s is not installed! ' +
8282
'Check here to know how to install SQL ' +
8383
'Server Native Client: https://bit.ly/3wFVJXz';
84+
RS_THANKS_FOR_USING = 'Thanks for using sqlcmdcli https://github.com/segovoni/sqlcmdcli';
85+
RS_COMMIT_TRANSACTION = 'The transaction has been committed!';
86+
RS_ROLLBACK_TRANSACTION = 'The transaction has been rejected!';
8487

8588
implementation
8689

0 commit comments

Comments
 (0)