📄 umysqlstreams.pas
字号:
unit uMysqlStreams;
interface
uses
Classes, SysUtils, uMysqlHelpers;
{ converts a stream to a string }
function StreamToMysqlString(aStream: TStream; aLength: integer = 0): string;
{ converts a file to a string }
function FileToMysqlString(aFileName: string): string;
{ converts a mysql value to a stream }
procedure MysqlStringToStream(aValue: pchar; aLength: integer; aStream: TStream);
{ converts a mysql value to a file }
procedure MysqlStringToFile(aValue: pchar; aLength: integer; const aFileName: string; anOverWrite: boolean);
implementation
function StreamToMysqlString(aStream: TStream; aLength: integer = 0): string;
var
_ln: integer;
begin
{ if no size has been specified, assume the rest of the stream size }
result:='';
if (aLength <= 0) then
_ln:= aStream.Size - aStream.Position
else
_ln:= aLength;
{ get memory for the string }
SetLength(result, _ln);
{ if any data }
if (_ln > 0) then
begin
{ load it from stream }
aStream.Read(Result[1], _ln);
{ escape it for sql query string }
result:= EscapeStr(result);
end;
end;
function FileToMysqlString(aFileName: string): string;
var
_f: TFileStream;
begin
{ create a temporary stream - open read only}
_f:= TFileStream.Create(aFileName, fmOpenRead);
try
{ convert it to mysql string }
result:= StreamToMysqlString(_f, _f.Size);
finally
{ free the temp stream }
_f.Free;
end;
end;
procedure MysqlStringToStream(aValue: pchar; aLength: integer; aStream: TStream);
var
_sz: integer;
_pos: integer;
begin
{ if the field is not empty }
if (aLength>0) then
begin
{ make sure we have enough space }
_sz:= aStream.Position + aLength;
if (_sz > aStream.Size) then
begin
_pos:= aStream.Position;
aStream.Size:= _sz;
aStream.Position:= _pos;
end;
aStream.Write(aValue[0], aLength);
end;
end;
{ converts a mysql value to a file }
procedure MysqlStringToFile(aValue: pchar; aLength: integer; const aFileName: string; anOverWrite: boolean);
var
_f: TFileStream;
begin
{ check if the file exists, and if yes if we should overwrite it }
if (FileExists(aFileName) and not anOverWrite) then
raise EInOutError.CreateFmt('File "%s" exists.', [aFileName]);
{ create a file stream }
_f:= TFileStream.Create(aFileName, fmCreate);
try
MysqlStringToStream(aValue, aLength, _f);
finally
{ free the temp stream }
_f.Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -