Overriding AsString in TFields and TStringField - database

When we swapped database components from ODBC Express to ADO one of the biggest problems we ran into was that ODBC Express "right-trimmed" the results from CHAR fields and ADO didn't.
CHAR fields fill up their entire assigned field length so when we query them with ADO you get a lot of extra spaces.
We have a lot of databases and code that basically depends on the results from the database being trimmed.
Our solution was to edit the delphi source DB.pas and change the getasstring methods from TField and TStringField.
This works but is not a solid solution is there another way to get those results trimmed without changing delphi source code?
When switchting to ADO we decided to not use the TADOQuery class directly but instead we derived our own:
TOurAdoQuery = class(TADOQuery)
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ExecSQL: Integer;
procedure Open;
end;

Descend from TStringField, something like:
TYourStringField = class(TStringField)
protected
function GetAsString: string; override;
end;
implementation
function TYourStringField.GetAsString: string;
begin
Result := TrimRight(inherited GetAsString);
end;
Register your class (you will probably do this in initialization section):
RegisterClass(TYourStringField);
Put this line where you usually initialize stuff for your application :
DefaultFieldClasses[ftString] := TYourStringField;
That's it, next time you create dataset all ftString field types will use TYourStringField definition.

Related

Is it possible to make a create which returns a boolean

hi i dont know if i asked the question clearly.
i have a deferred class "animal" which contains two features : "bite" (this returns a boolean -> bite:BOOLEAN) and "speak"(speak(word:BOOLEAN)).
now i made a class named "dog" what inherit from "animal". i redefined the two features without a compiler error. now i want to make a create, which contains the function bite (create bite:BOOLEAN).
this gaves me a compiler error, if i try it with the other feature it works fine.
my error code : VGCP(2) creators part lists improper identifier.
thank u for ur help
My Application.e:
note
description : "root class of the application"
date : "$Date$"
revision : "$Revision$"
class
APPLICATION
inherit
ARGUMENTS_32
create
make
feature
d1:DOG
feature {NONE} -- Initialization
make
-- Run application.
do
print(d1.bite)
print ("Hello Eiffel World!%N")
end
end
my animal class:
deferred class
ANIMAL
feature
bite_:BOOLEAN
deferred
end
feature
speak(word:BOOLEAN)
deferred
end
end
my dog class:
class
DOG
inherit
ANIMAL
redefine
bite_,speak_
end
create
bite_
feature
bite_:BOOLEAN
do
Result:=5<3
end
feature
speak(word:BOOLEAN)
do
print("yes")
end
I'm not sure what is the intended semantics of feature speak, so I'll focus solely on feature bite, or, to express the supposed intention better can_bite. Class ANIMAL is similar to what you have:
deferred class
ANIMAL
feature -- Status report
can_bite: BOOLEAN
-- Can this animal bite?
deferred
end
end
With class DOG, two variations are possible: when every dog bites (possibly, depending on some internal state), when the ability to bite is set at object creation time.
In the first scenario, can_bite is a function and the class looks like
class
DOG
inherit
ANIMAL
feature -- Status report
can_bite: BOOLEAN
-- Can this animal bite?
do
Result := True -- It could also be some expression.
end
end
In the second scenario, can_bite is an attribute initialized by a creation procedure, and the class looks like
class
DOG
inherit
ANIMAL
create
make
feature {NONE} -- Creation
make (is_biting: BOOLEAN)
-- Initialize with ability `is_biting`.
do
can_bite := is_biting
end
feature -- Status report
can_bite: BOOLEAN
-- Can this animal bite?
end
Note that in both cases the client has to create the dog object before making any calls on it. In the first case, the default creation procedure is used, and the client code looks like
create d1
print (d1.can_bite)
In the second case, the specific creation procedure is used (it initializes can_bite), and the client code looks like
create d1.make (True)
print (d1.can_bite)

adding new methods to LINQ to Entities

Is there any way to define the SQL conversion component for additional functions to Linq2Entities.
For example:
myQuery.Where(entity => entity.Contains('foo', SearchFlags.All))
Ideally I am looking for something that doesn't require editing and building a new version the EntityFramework.dll directly. Is there any way to allow extension methods to entity framework that can support SQL generation.
So far I have a template which would represent the method I need to replace for LINQ to Entities:
public static bool Contains(this object source, string searchTerms, SearchFlags flags)
{
return true;
}
Of course this causes the error:
LINQ to Entities does not recognize the method 'Boolean
CONTAINS(System.Object, System.String, SearchFlags)' method, and this method
cannot be translated into a store expression.
To be clear, I don't want to do:
myQuery.AsEnumerable().Where(entity => entity.Contains('foo', SearchFlags.All))
Because I want to be able to execute code in SQL space and not return all the entities manually.
I also cannot use the .ToString() of the IQueryable and execute it manually because I need Entity Framework to populate the objects from several .Include joins.
I don't understand your Q clearly. However if your problem is that you can't use your own methods or other linq to objects method, just use .AsEnumerable() and do your other jobs through linq to objects, not L2E:
myQuery.AsEnumerable().Where(entity => entity.Contains('foo', SearchFlags.All))
And if you need to use your myQuery several times somewhere else, first load it to memory, then use it as many as you want:
var myQuery = from e in context.myEntities
select d;
myQuery.Load();
// ...
var myOtherQuery = from d in context.myEntities.Local
select d;
// Now any L2O method is supported...
I ended up doing the following (which works but is very far from perfect):
All my entities inherit from an IEntity which defines long Id { get; set; }
I then added a redundant restriction
context.myEntities.Where(entity => entity.Id != 0) this is
redundant since the identity starts at 1, but Linq2Entities doesn't
know that.
I then call .ToString() on the IQueryable after I have done all
my other queries, since it is of type DBQuery<Entity> it returns
the SQL Command Text, I do a simple replace with my query restriction.
In order to get all the .Include(...) to work I actually execute
two different sql commands. There is no other more pretty way to tap into this because of query execution plan caching causes issues otherwise (even when disabled).
As a result my code looks like this:
public IQueryable<IEntity> MyNewFunction(IQueryable<IEntity> myQueryable, string queryRestriction)
{
string rawSQL = myQueryable.Select(entity => entity.Id).ToString().Replace("[Extent1].Id <> 0", queryRestriction);
List<long> ids = // now execute rawSQL, get the list of ids;
return myQuerable.Where(entity => ids.Contains(entity.Id));
}
In short, other than manually executing the SQL or running a similar SQL command and appending the restriction using the existing commands the only way to write your own methods to Linq-to-Entities is to manually alter and build your own EntityFramework.dll from the EF6 source.

What's the "caHide, caNone" Database Equivalent?

When I want to confirm a form exit, I use a DialogMsg call with an if to set Action parameter to caNone (No) or caHide (yes).
When I use that approach with database table row posting, I get a problem.
If I make a Save button with a confirmation dialog, I could call Post or Cancel, but when I have a Database Grid in the form, the problem comes when the user alter a row and later, selects another row, witch saves the previous row automatically.
The solution I found was to put the save confirmation dialog inside the OnBeforePost, but if the uses says No, how should I cancel the Post command to Continue? I've tryed Cancel and Abort commands, but it continues saving the row.
You need to create your own descendant TMyQuery of the TXxxQuery (or what else dataset kind you are using). And in your class override Post method and add a new event, allowing to decide is the Post needed. Then register new component and replace TXxxQuery in your application with TMyQuery. And create the event handler.
For example, the TMyQuery:
interface
type
TMyAllowPostEvent = procedure (ASender: TDataSet; var AAllow: Boolean) of object;
TMyQuery = class (TXxxQuery)
private
FOnAllowPost: TMyAllowPostEvent;
public
procedure Post; override;
published
property OnAllowPost: TMyAllowPostEvent read FOnAllowPost write FOnAllowPost;
end;
implementation
procedure TMyQuery.Post;
var
lAllow: Boolean;
begin
lAllow := True;
if Assigned(OnAllowPost) then
OnAllowPost(Self, lAllow);
if lAllow then
inherited Post;
end;
And the example of the event handler:
procedure TForm1.MyQuery1AllowPost(ASender: TDataSet; var AAllow: Boolean);
begin
AAllow := MessageDlg('Would you like to save changes ?', mtConfirmation, mbYesNo, -1) = mrYes;
end;

Delphi: array of TImage

This is my whole code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Images: array[0..29,0..39] of TImage; //array
implementation
{$R *.dfm}
//form create
procedure TForm1.FormCreate(Sender: TObject);
var xx,yy: Integer; //local variables
begin
for xx:=0 to 29 do
for yy:=0 to 39 do
begin
Images[xx,yy]:=Timage.Create(Form1);
Images[xx,yy].Canvas.Rectangle(0,0,17,17);
Images[xx,yy].Left:=xx*16;
Images[xx,yy].Top:=yy*16;
end;
end;
end.
And I always get the error : "Project Project1.exe has raised the exception class EClassNotFound with message "TImage not found". Process stopped. Use step or run to continue "
I have tried other codes on the internet like:
Delphi: TImage.Create causes Access violation http://www.delphi-central.com/tutorials/memory_game_2.aspx
Nothing helps! Why is this happening?
Thank you.
Are you sure you get the exception at the line with TImage.Create? Could it be you have an invalid DFM file still containig a TImage instance which is missing from the TForm1 declaration?
Normally all classes used as children in a form or datamodule are automatically registered for streaming. As there is no TImage declared in the form and no other form of the application contains a TImage, there is no registration.
You can simply test by dropping a TImage onto the form.
and if you want to show in form add this code to loop:
Images[xx,yy].Parent:= Self;

Designing a generic DB utility class

Time and again I find myself creating a database utility class which has multiple functions which all do almost the same thing but treat the result set slightly differently.
For example, consider a Java class which has many functions which all look like this:
public void doSomeDatabaseOperation() {
Connection con = DriverManager.getConnection("jdbc:mydriver", "user", "pass");
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT whatever FROM table"); // query will be different each time
while (rs.next()) {
// handle result set - differently each time
}
} catch (Exception e) {
// handle
} finally {
con.close();
}
}
Now imagine a class with 20 of these functions.
As you can see, tons of boilerplate (opening a connection, try-finally block), and the only thing that changes would be the query and the way you handle the result set. This type of code occurs in many languages (considering you're not using an ORM).
How do you manage your DB utility classes so as to reduce code duplication? What does a typical DB utility class look like in your language/framework?
The way I have done in one of my project is that I followed what Spring does with JDBC template and came up with a Query framework. Basically create a common class which can take select statement or pl/sql calls and bind parameters. If the query returns resultset, also pass the Rowmapper. This rowmapper object will be called by the framework to convert each row into an object of any kind.
Example -
Query execute = new Query("{any select or pl/sql}",
// Inputs and Outputs are for bind variables.
new SQL.Inputs(Integer.class, ...),
// Outputs is only meaningful for PL/SQL since the
// ResultSetMetaData should be used to obtain queried columns.
new SQL.Outputs(String.class));
If you want the rowmapper -
Query execute = new Query("{any select or pl/sql}",
// Inputs and Outputs are for bind variables.
new SQL.Inputs(Integer.class, ...),
// Outputs is only meaningful for PL/SQL since the
// ResultSetMetaData should be used to obtain queried columns.
new SQL.Outputs(String.class), new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setSurname(rs.getString("surname"));
return actor;
});
Finally a Row class is the output which will have list of objects if you have passed the RowMapper -
for (Row r : execute.query(conn, id)) {
// Handle the rows
}
You can go fancy and use Templates so that type safety is guaranteed.
Sounds like you could make use of a Template Method pattern here. That would allow you to define the common steps (and default implementations of them, where applicable) that all subclasses will take to perform the action. Then subclasses need only override the steps which differ: SQL query, DB-field-to-object-field mapping, etc.
When using .net, the Data Access Application Block is in fairly widespread use to provide support for the following:
The [data access] application block
was designed to achieve the following
goals:
Encapsulate the logic used to perform
the most common data access tasks.
Eliminate common coding errors, such
as failing to close connections.
Relieve developers of the need to
write duplicated code for common data
access tasks.
Reduce the need for
custom code.
Incorporate best
practices for data access, as
described in the .NET Data Access
Architecture Guide.
Ensure that, as
far as possible, the application block
functions work with different types of
databases.
Ensure that applications
written for one type of database are,
in terms of data access, the same as
applications written for another type
of database.
There are plenty of examples and tutorials of usage too: a google search will find msdn.microsoft, 4guysfromrolla.com, codersource.com and others.

Resources