Code: Select all
{**
Sets a variant value into specified parameter.
@param ParameterIndex a index of the parameter.
@param SqlType a parameter SQL type.
@paran Value a new parameter value.
}
procedure TZAdoPreparedStatement.SetInParam(ParameterIndex: Integer;
SQLType: TZSQLType; Value: TZVariant);
var
S: Integer;
HR: HResult;
T: Integer;
PC: Integer;
P: ZPlainAdo.Parameter;
B: IZBlob;
V: Variant;
OleDBCommand: IUnknown;
OleDBCmdParams: ICommandWithParameters;
OleDBCmdPrepare: ICommandPrepare;
OleDBPC: Cardinal;
ParamInfo: PDBParamInfo;
NamesBuffer: PPOleStr;
begin
PC := 0;
if FAdoCommand.CommandType = adCmdStoredProc then
begin
try
//some providers generates exceptions here mainly for update statements
PC := FAdoCommand.Parameters.Count;
except
end;
end
else
begin
OleDBCommand := (FAdoCommand as ADOCommandConstruction).OLEDBCommand;
OleDBCommand.QueryInterface(ICommandWithParameters, OleDBCmdParams);
ParamInfo := nil;
NamesBuffer := nil;
if Assigned(OleDBCmdParams) then
begin
HR := OleDBCmdParams.GetParameterInfo(OleDBPC, ParamInfo, NamesBuffer);
//Access needs to be prepared for parameters
if HR = DB_E_NOTPREPARED then
begin
OleDBCommand.QueryInterface(ICommandPrepare, OleDBCmdPrepare);
if Assigned(OleDBCmdPrepare) then
begin
OleDBCmdPrepare.Prepare(0);
OleDBCmdParams.GetParameterInfo(OleDBPC, ParamInfo, NamesBuffer);
end
end;
if Assigned(ParamInfo) then ZAdoMalloc.Free(ParamInfo);
if Assigned(NamesBuffer) then ZAdoMalloc.Free(NamesBuffer);
PC := OleDBPC;
end;
end;
if (SQLType in [stAsciiStream, stUnicodeStream, stBinaryStream]) then
begin
B := DefVarManager.GetAsInterface(Value) as IZBlob;
case SQLType of
stAsciiStream:
begin
if Assigned(B) then
DefVarManager.SetAsString(Value, B.GetString);
SQLType := stString;
end;
stUnicodeStream:
begin
if Assigned(B) then
DefVarManager.SetAsUnicodeString(Value, B.GetUnicodeString);
SQLType := stUnicodeString;
end;
stBinaryStream:
begin
if Assigned(B) then
DefVarManager.SetAsString(Value, BytesToStr(B.GetBytes));
SQLType := stBytes;
end;
end;
end;
case Value.VType of
vtNull: V := Null;
vtBoolean: V := SoftVarManager.GetAsBoolean(Value);
vtInteger: V := Integer(SoftVarManager.GetAsInteger(Value));
vtFloat: V := SoftVarManager.GetAsFloat(Value);
vtString: V := SoftVarManager.GetAsString(Value);
vtUnicodeString: V := SoftVarManager.GetAsUnicodeString(Value);
vtDateTime: V := SoftVarManager.GetAsDateTime(Value);
end;
S := 0;
if SQLType = stString then
begin
S := Length(VarToStr(V));
if S = 0 then
begin
S := 1;
V := Null;
end;
end;
if SQLType in [stUnicodeString] then
begin
S := Length(VarToWideStr(V));
if S = 0 then
begin
S := 1;
V := Null;
end;
end;
if SQLType = stBytes then
begin
V := StrToBytes(VarToStr(V));
if (VarType(V) and varArray) <> 0 then
S := VarArrayHighBound(V, 1) + 1;
if S = 0 then V := Null;
end;
if VarIsNull(V) then
T := ConvertSqlTypeToAdo(SQLType)
else
T := ConvertVariantToAdo(VarType(V));
if ParameterIndex <= PC then
begin
P := FAdoCommand.Parameters.Item[ParameterIndex - 1];
FAdoCommand.Parameters.Item[ParameterIndex - 1].Type_ := T;
FAdoCommand.Parameters.Item[ParameterIndex - 1].Size := S;
FAdoCommand.Parameters.Item[ParameterIndex - 1].Value := V;
end
else
begin
FAdoCommand.Parameters.Append(FAdoCommand.CreateParameter(
'P' + IntToStr(ParameterIndex), T, adParamInput, S, V));
end;
end;
Code: Select all
{**
Sets a variant value into specified parameter.
@param ParameterIndex a index of the parameter.
@param SqlType a parameter SQL type.
@paran Value a new parameter value.
}
procedure TZAdoPreparedStatement.SetInParam(ParameterIndex: Integer;
SQLType: TZSQLType; const Value: TZVariant);
var
S: Integer;
HR: HResult;
T: Integer;
PC: Integer;
P: ZPlainAdo.Parameter;
B: IZBlob;
V: Variant;
OleDBCommand: IUnknown;
OleDBCmdParams: ICommandWithParameters;
OleDBCmdPrepare: ICommandPrepare;
OleDBPC: Cardinal;
ParamInfo: PDBParamInfo;
NamesBuffer: PPOleStr;
RetValue: TZVariant;
begin
PC := 0;
if FAdoCommand.CommandType = adCmdStoredProc then
begin
try
//some providers generates exceptions here mainly for update statements
PC := FAdoCommand.Parameters.Count;
except
end;
end
else
begin
OleDBCommand := (FAdoCommand as ADOCommandConstruction).OLEDBCommand;
OleDBCommand.QueryInterface(ICommandWithParameters, OleDBCmdParams);
ParamInfo := nil;
NamesBuffer := nil;
if Assigned(OleDBCmdParams) then
begin
HR := OleDBCmdParams.GetParameterInfo(OleDBPC, ParamInfo, NamesBuffer);
//Access needs to be prepared for parameters
if HR = DB_E_NOTPREPARED then
begin
OleDBCommand.QueryInterface(ICommandPrepare, OleDBCmdPrepare);
if Assigned(OleDBCmdPrepare) then
begin
OleDBCmdPrepare.Prepare(0);
OleDBCmdParams.GetParameterInfo(OleDBPC, ParamInfo, NamesBuffer);
end
end;
if Assigned(ParamInfo) then ZAdoMalloc.Free(ParamInfo);
if Assigned(NamesBuffer) then ZAdoMalloc.Free(NamesBuffer);
PC := OleDBPC;
end;
end;
if (SQLType in [stAsciiStream, stUnicodeStream, stBinaryStream]) then
begin
B := DefVarManager.GetAsInterface(Value) as IZBlob;
case SQLType of
stAsciiStream:
begin
if Assigned(B) then
DefVarManager.SetAsString(RetValue, B.GetString);
SQLType := stString;
end;
stUnicodeStream:
begin
if Assigned(B) then
DefVarManager.SetAsUnicodeString(RetValue, B.GetUnicodeString);
SQLType := stUnicodeString;
end;
stBinaryStream:
begin
if Assigned(B) then
DefVarManager.SetAsString(RetValue, BytesToStr(B.GetBytes));
SQLType := stBytes;
end;
end;
end;
case RetValue.VType of
vtNull: V := Null;
vtBoolean: V := SoftVarManager.GetAsBoolean(RetValue);
vtInteger: V := Integer(SoftVarManager.GetAsInteger(RetValue));
vtFloat: V := SoftVarManager.GetAsFloat(RetValue);
vtString: V := SoftVarManager.GetAsString(RetValue);
vtUnicodeString: V := SoftVarManager.GetAsUnicodeString(RetValue);
vtDateTime: V := SoftVarManager.GetAsDateTime(RetValue);
end;
S := 0;
if SQLType = stString then
begin
S := Length(VarToStr(V));
if S = 0 then
begin
S := 1;
V := Null;
end;
end;
if SQLType in [stUnicodeString] then
begin
S := Length(VarToWideStr(V));
if S = 0 then
begin
S := 1;
V := Null;
end;
end;
if SQLType = stBytes then
begin
V := StrToBytes(VarToStr(V));
if (VarType(V) and varArray) <> 0 then
S := VarArrayHighBound(V, 1) + 1;
if S = 0 then V := Null;
end;
if VarIsNull(V) then
T := ConvertSqlTypeToAdo(SQLType)
else
T := ConvertVariantToAdo(VarType(V));
if ParameterIndex <= PC then
begin
P := FAdoCommand.Parameters.Item[ParameterIndex - 1];
FAdoCommand.Parameters.Item[ParameterIndex - 1].Type_ := T;
FAdoCommand.Parameters.Item[ParameterIndex - 1].Size := S;
FAdoCommand.Parameters.Item[ParameterIndex - 1].Value := V;
end
else
begin
FAdoCommand.Parameters.Append(FAdoCommand.CreateParameter(
'P' + IntToStr(ParameterIndex), T, adParamInput, S, V));
end;
end;