I've got a TSimpleDataSet connected to a Firebird database. The dataset's internal dataset's CommandType
is set to ctT开发者_运维百科able
, with CommandText set to a table name. So I expect, when I set it to Active, that it will generate a query that looks like select * from TableName
.
Instead, for some bizarre reason, somewhere in the internals of the DB Express code it's trying to put quotes around the table name, so I end up with select * from "TableName"
, which of course causes syntax errors. Apparently the quotes are coming from the TSqlConnection's Metadata
property, which is read-only, so I can't fix this in code. (That would make too much sense, I guess.)
Does anyone know how I can fix this?
Adding this solution by request. Not going to mark it as accepted, though, because it feels like an ugly hack and if possible I'd like a more elegant way to prevent the problem:
procedure RTTISurgery(connection: TSqlConnection);
var
cls: TRttiType;
begin
cls := TRttiContext.Create.GetType(connection.Metadata.ClassType);
cls.GetField('FQuotePrefix').SetValue(connection.Metadata, '');
cls.GetField('FQuoteSuffix').SetValue(connection.Metadata, '');
end;
NOTE: I do not recommend the use of RTTI surgery techniques as a general purpose solution to programming problems. It should only be used when no better solution is available, since it almost invariably involves violating encapsulation. (Which is the point of using it: a last-ditch solution to fix over-encapsulated bugs.)
Quotes can ensure tables with names that can conflict with other identifiers can still be used. Most databases - don't know FB - allow reserved identifiers to be used as object names as long as they are quoted, i.e. SELECT TIMESTAMP FROM X may not work, while SELECT "TIMESTAMP" FROM X may. IIRC it's a SQL-92 rule to allow bakward compatibility if new keywords are introduced. Be aware that when using quoted, the object identifiers can (or must, I don't remember) become case sensitive, thereby if you have the INVOICE table, select * from "invoice" won't work, while select * from "INVOICE" will.
精彩评论