[bug_fixed] MySQL Embedded Server Arguments
Posted: 27.09.2006, 06:50
I’m using MySQL Embedded for some of my applications. I’ve tried ZEOSLIB ( rev 98 ) and it work fine. But, I need MySQL Embedded to be configurable via my app. In fact, ZEOSLIB doesn’t provide it. It only support simple ‘--datadir’ which defined in ZPlainMySQLConstants.pas. And it’s absolutely unconfigurable which always point to ./data/ relative to app path.
Figure it, I have connection properties like this:
The most important things is, I need MySQL to be faster enough, like skipping innodb.
So I have modify MySQL library of ZEOSLIB.
1. ZPlainMySQLDriver.pas
a. Add uses clause of Classes
Before
After
b. Add procedure BuildOptions(Options: TStrings); of IZMySQLPlainDriver interface, insert after function GetFieldData(Row: PZMySQLRow; Offset: Cardinal): PChar; so it will like:
c. Add procedure BuildOptions(Options: TStrings); declaration for each of derivated interface of IZMySQLPlainDriver.
d. Insert following code bellow uses clause of implementation
e. Implement procedure BuildOptions(Options: TStrings); in each of driver interface.
f. Replace Init function for existing embedded driver interface with following code (original line is commented).
2. ZdbcMySql.pas
Insert code commented below to the existing code:
I have tested it and it work fine. And you can specify as much as server argument to MySQL Embedded server.
Figure it, I have connection properties like this:
Code: Select all
compress=yes
dbless=no
useresult=no
timeout=30
arg1=--basedir=./mysql
arg2=--datadir=./mysql/data
arg3=--character-sets-dir=./mysql/share/charsets
arg4=--language=./mysql/share/english
arg5=--skip-innodb
arg6=--key_buffer_size=32M
So I have modify MySQL library of ZEOSLIB.
1. ZPlainMySQLDriver.pas
a. Add uses clause of Classes
Before
Code: Select all
uses ZClasses, ...
Code: Select all
uses Classes, ZClasses, ...
Code: Select all
function GetFieldData(Row: PZMySQLRow; Offset: Cardinal): PChar;
procedure BuildOptions(Options: TStrings);
Code: Select all
TZMySQL5PlainDriver = class (TZAbstractObject, IZPlainDriver,
IZMySQLPlainDriver)
....
public
...
procedure BuildOptions(Options: TStrings); virtual;
end;
TZMySQLD5PlainDriver = class (TZMySQL5PlainDriver)
public
...
procedure BuildOptions(Options: TStrings); override;
end;
Code: Select all
uses SysUtils, ZMessages;
var
ServerArgs: array of PChar;
ServerArgsLen: Integer;
const
ServerArgsKeyPrefix = 'arg';
procedure BuildServerArguments(Options: TStrings);
var
Tmp: TStringList;
i: Integer;
begin
Tmp := TStringList.Create;
try
Tmp.Add(ParamStr(0));
for i := 0 to Options.Count - 1 do
if SameText(ServerArgsKeyPrefix, Copy(Options.Names[i], 1, Length(ServerArgsKeyPrefix))) then
Tmp.Add(Options.ValueFromIndex[i]);
ServerArgsLen := Tmp.Count;
SetLength(ServerArgs, ServerArgsLen);
for i := 0 to ServerArgsLen - 1 do
ServerArgs[i] := StrNew(PChar(Tmp[i]));
finally
Tmp.Free;
end;
end;
Code: Select all
procedure TZMySQL5PlainDriver.BuildArguments(Options: TStrings);
begin
// leave blank for non embedded
end;
procedure TZMySQLD5PlainDriver.BuildArguments(Options: TStrings);
begin
BuildServerArguments(Options);
end;
Code: Select all
function TZMySQLD5PlainDriver.Init(var Handle: PZMySQLConnect): PZMySQLConnect;
begin
if @MYSQL_API.mysql_server_init <> nil then
MYSQL_API.mysql_server_init(ServerArgsLen, ServerArgs, @SERVER_GROUPS);
// MYSQL_API.mysql_server_init(3, @DEFAULT_PARAMS, @SERVER_GROUPS);
Handle := MYSQL_API.mysql_init(nil);
Result := Handle;
end;
Insert code commented below to the existing code:
Code: Select all
function TZMySQLDriver.Connect(const Url: string; Info: TStrings): IZConnection;
var
TempInfo: TStrings;
HostName, Database, UserName, Password: string;
Port: Integer;
PlainDriver: IZMySQLPlainDriver;
begin
TempInfo := TStringList.Create;
try
PlainDriver := GetPlainDriver(Url);
ResolveDatabaseUrl(Url, Info, HostName, Port, Database,
UserName, Password, TempInfo);
// insert this line
if PlainDriver <> nil then
PlainDriver.BuildArguments(TempInfo);
// end of insert line
Result := TZMySQLConnection.Create(Self, Url, PlainDriver, HostName, Port,
Database, UserName, Password, TempInfo);
finally
TempInfo.Free;
end;
end;