Skip to content

Commit 7456c38

Browse files
committed
fix: BIT values in MS SQL grid queries prefixed with MySQL b'' style
Closes #264
1 parent 422935a commit 7456c38

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

source/dbconnection.pas

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,11 +5148,13 @@ function TDBConnection.EscapeString(Text: String; ProcessJokerChars: Boolean=fal
51485148
function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String;
51495149
var
51505150
DoQuote: Boolean;
5151+
ValuePrefix: String;
51515152
const
51525153
CategoriesNeedQuote = [dtcText, dtcBinary, dtcTemporal, dtcSpatial, dtcOther];
51535154
begin
51545155
// Quote text based on the passed datatype
51555156
DoQuote := Datatype.Category in CategoriesNeedQuote;
5157+
ValuePrefix := '';
51565158
case Datatype.Category of
51575159
// Some special cases
51585160
dtcBinary: begin
@@ -5162,11 +5164,13 @@ function TDBConnection.EscapeString(Text: String; Datatype: TDBDatatype): String
51625164
dtcInteger, dtcReal: begin
51635165
if (not IsNumeric(Text)) and (not IsHex(Text)) then
51645166
DoQuote := True;
5165-
if Datatype.Index = dbdtBit then
5167+
if (Datatype.Index = dbdtBit) and FParameters.IsAnyMySQL then begin
51665168
DoQuote := True;
5169+
ValuePrefix := 'b';
5170+
end;
51675171
end;
51685172
end;
5169-
Result := EscapeString(Text, False, DoQuote);
5173+
Result := ValuePrefix + EscapeString(Text, False, DoQuote);
51705174
end;
51715175

51725176

@@ -9154,13 +9158,13 @@ function TSqlSrvQuery.Col(Column: Integer; IgnoreErrors: Boolean=False): String;
91549158
except
91559159
Result := String(FCurrentResults.Fields[Column].AsAnsiString);
91569160
end;
9157-
if Datatype(Column).Index = dbdtBit then begin
9158-
if UpperCase(Result) = 'TRUE' then
9159-
Result := '1'
9160-
else
9161-
Result := '0';
9162-
end
91639161
end;
9162+
if Datatype(Column).Index = dbdtBit then begin
9163+
if (UpperCase(Result) = 'TRUE') or (Result = '1') then
9164+
Result := '1'
9165+
else
9166+
Result := '0';
9167+
end
91649168
end else if not IgnoreErrors then
91659169
Raise EDbError.CreateFmt(_(MsgInvalidColumn), [Column, ColumnCount, RecordCount]);
91669170
end;
@@ -9862,11 +9866,8 @@ function TDBQuery.SaveModifications: Boolean;
98629866
else if Cell.NewIsFunction then
98639867
Val := Cell.NewText
98649868
else case Datatype(i).Category of
9865-
dtcInteger, dtcReal: begin
9869+
dtcInteger, dtcReal:
98669870
Val := Connection.EscapeString(Cell.NewText, Datatype(i));
9867-
if (Datatype(i).Index = dbdtBit) and FConnection.Parameters.IsAnyMySQL then
9868-
Val := 'b' + Val;
9869-
end;
98709871
dtcBinary, dtcSpatial:
98719872
Val := FConnection.EscapeBin(Cell.NewText);
98729873
dtcTemporal:
@@ -10251,7 +10252,7 @@ function TDBQuery.GetWhereClause: String;
1025110252
case DataType(j).Category of
1025210253
dtcInteger, dtcReal: begin
1025310254
if DataType(j).Index = dbdtBit then
10254-
Result := Result + '=b' + Connection.EscapeString(ColVal)
10255+
Result := Result + '=' + Connection.EscapeString(ColVal, DataType(j))
1025510256
else begin
1025610257
// Guess (!) the default value silently inserted by the server. This is likely
1025710258
// to be incomplete in cases where a UNIQUE key allows NULL here

source/exportgrid.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ procedure TfrmExportGrid.btnOKClick(Sender: TObject);
10451045
else if GridData.IsNull(ResultCol) then
10461046
Data := 'NULL'
10471047
else if (GridData.DataType(ResultCol).Index = dbdtBit) and GridData.Connection.Parameters.IsAnyMySQL then
1048-
Data := 'b' + GridData.Connection.EscapeString(Data)
1048+
Data := GridData.Connection.EscapeString(Data, GridData.DataType(ResultCol))
10491049
else if (GridData.DataType(ResultCol).Category in [dtcText, dtcTemporal, dtcOther])
10501050
or ((GridData.DataType(ResultCol).Category in [dtcBinary, dtcSpatial]) and Mainform.actBlobAsText.Checked)
10511051
then

source/tabletools.pas

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,12 +2078,6 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
20782078
if Data.IsNull(i) then
20792079
Row := Row + 'NULL'
20802080
else case Data.DataType(i).Category of
2081-
dtcInteger, dtcReal: begin
2082-
if Data.DataType(i).Index = dbdtBit then
2083-
Row := Row + 'b' + Quoter.EscapeString(Data.Col(i))
2084-
else
2085-
Row := Row + Data.Col(i);
2086-
end;
20872081
dtcBinary, dtcSpatial: begin
20882082
BinContent := Data.HexValue(i);
20892083
if Length(BinContent) > 0 then

0 commit comments

Comments
 (0)