Skip to content

Commit c2362f4

Browse files
committed
fix: CAST(x AS CHAR) not supported by MySQL below v4.0.2
Refs #2531
1 parent 179a484 commit c2362f4

7 files changed

Lines changed: 20 additions & 8 deletions

source/dbconnection.pas

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10986,26 +10986,28 @@ procedure TTableColumn.ParseDatatype(Source: String);
1098610986

1098710987

1098810988
function TTableColumn.CastAsText: String;
10989+
var
10990+
ColTypeAllowsCast: Boolean;
1098910991
begin
1099010992
// Cast data types which are incompatible to string functions to text columns
1099110993
Result := FConnection.QuoteIdent(Name);
10994+
ColTypeAllowsCast := True;
1099210995
case FConnection.Parameters.NetTypeGroup of
1099310996
ngMySQL, ngSQLite: begin
10994-
if DataType.Index in [dbdtUnknown, dbdtDate, dbdtDatetime, dbdtTime, dbdtTimestamp, dbdtJson, dbdtJsonB] then
10995-
Result := 'CAST('+Result+' AS CHAR)';
10997+
ColTypeAllowsCast := DataType.Index in [dbdtUnknown, dbdtDate, dbdtDatetime, dbdtTime, dbdtTimestamp, dbdtJson, dbdtJsonB];
1099610998
end;
1099710999
ngMSSQL: begin
1099811000
// Be sure LEFT() and "col LIKE xyz" work with MSSQL
1099911001
// Also, prevent exceeding size limit of 8000 for NVARCHAR
11000-
if DataType.Index in [dbdtUnknown, dbdtNtext, dbdtText] then
11001-
Result := 'CAST('+Result+' AS NVARCHAR('+IntToStr(GRIDMAXDATA)+'))';
11002+
ColTypeAllowsCast := DataType.Index in [dbdtUnknown, dbdtNtext, dbdtText];
1100211003
end;
1100311004
ngPgSQL: begin
1100411005
// Cast most datatypes, including VARCHAR and TEXT, which may have an [] array attribute
11005-
if not (DataType.Category in [dtcInteger, dtcReal]) then
11006-
Result := Result + '::text';
11006+
ColTypeAllowsCast := not (DataType.Category in [dtcInteger, dtcReal]);
1100711007
end;
1100811008
end;
11009+
if ColTypeAllowsCast and FConnection.SqlProvider.Has(qCastAsText) then
11010+
Result := FConnection.SqlProvider.GetSql(qCastAsText, [Result]);
1100911011
end;
1101011012

1101111013

source/dbstructures.interbase.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ function TInterbaseProvider.GetSql(AId: TQueryId): string;
234234
' RDB$CHARACTER_SET_ID'+
235235
' FROM RDB$COLLATIONS';
236236
qGetCharsets: Result := 'SELECT RDB$CHARACTER_SET_NAME AS "Charset", RDB$CHARACTER_SET_NAME AS "Description" FROM RDB$CHARACTER_SETS';
237+
else Result := inherited;
237238
end;
238239
end;
239240

source/dbstructures.mssql.pas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
interface
44

55
uses
6-
dbstructures, StrUtils;
6+
dbstructures, StrUtils, SysUtils;
77

88
type
99
TMsSqlProvider = class(TSqlProvider)
@@ -413,6 +413,7 @@ TMsSqlProvider = class(TSqlProvider)
413413

414414
implementation
415415

416+
{$I const.inc}
416417

417418
function TMsSqlProvider.GetSql(AId: TQueryId): string;
418419
begin
@@ -480,6 +481,7 @@ function TMsSqlProvider.GetSql(AId: TQueryId): string;
480481
'SELECT SUM("rows") FROM "sys"."partitions" WHERE "index_id" IN (0, 1) AND "object_id" = object_id(:EscapedDbSchemaName)',
481482
''
482483
);
484+
qCastAsText: Result := 'CAST(%s AS NVARCHAR('+IntToStr(GRIDMAXDATA)+'))';
483485
else Result := inherited;
484486
end;
485487
end;

source/dbstructures.mysql.pas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,11 @@ function TMySqlProvider.GetSql(AId: TQueryId): string;
34233423
'SELECT PLUGIN_NAME FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_TYPE=''AUTHENTICATION'' AND PLUGIN_STATUS=''ACTIVE''',
34243424
''
34253425
);
3426+
qCastAsText: Result := IfThen(
3427+
FServerVersion >= 40002,
3428+
'CAST(%s AS CHAR)',
3429+
''
3430+
);
34263431
else Result := inherited;
34273432
end;
34283433
end;

source/dbstructures.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ interface
5252
qShowFunctionStatus, qShowProcedureStatus, qShowTriggers, qShowEvents, qShowCreateTrigger,
5353
qHelpKeyword, qShowWarnings, qGetEnumTypes,
5454
qDropUser, qCreateRole, qDropRole, qReloadPrivileges, qGrantRole, qRevokeRole, qSetDefaultRole,
55-
qAutoInc, qIndexVisible, qIndexInvisible, qGetAuthPlugins);
55+
qAutoInc, qIndexVisible, qIndexInvisible, qGetAuthPlugins, qCastAsText);
5656
TSqlProvider = class
5757
strict protected
5858
FNetType: TNetType;

source/dbstructures.postgresql.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ function TPostgreSQLProvider.GetSql(AId: TQueryId): string;
743743
'' // ServerVersion < 9
744744
);
745745
qAutoInc: Result := 'SERIAL';
746+
qCastAsText: Result := '%s::text';
746747
else Result := inherited;
747748
end;
748749
end;

source/dbstructures.sqlite.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ function TSQLiteProvider.GetSql(AId: TQueryId): string;
427427
qGetCharsets: Result := 'SELECT ''UTF-8'' AS "Charset", ''UTF-8'' AS "Description" '+
428428
'UNION SELECT ''UTF-16le'', ''UTF-16 Little Endian'' '+
429429
'UNION SELECT ''UTF-16be'', ''UTF-16 Big Endian''';
430+
qCastAsText: Result := 'CAST(%s AS CHAR)';
430431
else Result := inherited;
431432
end;
432433
end;

0 commit comments

Comments
 (0)