1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| class function TepcMySQLAssistantPassThrough.InternalExecuteSQL(CallType: TTypeSQLExecute; const SQLText: string; out Error: String; out Latency: Int64): TDataSet;
function TypeMySQLToTypeDB(FieldType: enum_field_types): TFieldType;
begin
case FieldType of
FIELD_TYPE_DECIMAL : Result := ftString; // Unpacked fixed-point number
FIELD_TYPE_TINY : Result := ftSmallint;
FIELD_TYPE_SHORT : Result := ftSmallint;
FIELD_TYPE_LONG : Result := ftInteger;
FIELD_TYPE_FLOAT : Result := ftFloat;
FIELD_TYPE_DOUBLE : Result := ftFloat;
FIELD_TYPE_NULL : Result := ftBlob;
FIELD_TYPE_TIMESTAMP : Result := ftString;
FIELD_TYPE_LONGLONG : Result := ftLargeint;
FIELD_TYPE_INT24 : Result := ftInteger;
FIELD_TYPE_DATE : Result := ftDate;
FIELD_TYPE_TIME : Result := ftTime;
FIELD_TYPE_DATETIME : Result := ftDateTime;
FIELD_TYPE_YEAR : Result := ftString;
FIELD_TYPE_NEWDATE : Result := ftDate;
FIELD_TYPE_ENUM : Result := ftBlob;
FIELD_TYPE_SET : Result := ftBlob;
FIELD_TYPE_TINY_BLOB : Result := ftBlob;
FIELD_TYPE_MEDIUM_BLOB : Result := ftBlob;
FIELD_TYPE_LONG_BLOB : Result := ftBlob;
FIELD_TYPE_BLOB : Result := ftBlob;
FIELD_TYPE_VAR_STRING : Result := ftBlob;
FIELD_TYPE_STRING : Result := ftString;
else
Result := ftString;
end;
end;
function ValueMySQLToDB(FieldType: enum_field_types; const Value: string): string;
begin
Result := Value;
case FieldType of
FIELD_TYPE_FLOAT : TepcDBEntiteToolHelp.SQLToLocaleStringFloat(Result);
FIELD_TYPE_DOUBLE : TepcDBEntiteToolHelp.SQLToLocaleStringFloat(Result);
FIELD_TYPE_DATE : TepcDBEntiteToolHelp.HyphenToSlashStringDate(Result);
end;
end;
function GetLengthTypeMySQL(FieldType: enum_field_types; MaxLength: Integer): Integer;
begin
case FieldType of
FIELD_TYPE_DECIMAL : Result := MaxLength; // Unpacked fixed-point number
FIELD_TYPE_YEAR : Result := MaxLength;
FIELD_TYPE_TINY_BLOB : Result := MaxLength;
FIELD_TYPE_MEDIUM_BLOB : Result := MaxLength;
FIELD_TYPE_LONG_BLOB : Result := MaxLength;
FIELD_TYPE_BLOB : Result := MaxLength;
FIELD_TYPE_VAR_STRING : Result := MaxLength;
FIELD_TYPE_STRING : Result := MaxLength;
else
Result := 0;
end;
end;
var
Connexion: TepcMySQLConnexionPassThrough;
Result_RES: PMYSQL_RES;
Field_RES: PMYSQL_FIELD;
Record_RES: PMYSQL_ROW;
iCol: Integer;
begin
Error := '';
Result := nil;
Result_RES := nil;
Latency := 0;
Connexion:= TepcMySQLConnexionPassThrough.Create();
try
Connexion.ServerName := GetDB().Connection.Server;
Connexion.Login := GetDB().Connection.Username;
Connexion.PW := GetDB().Connection.Password;
Connexion.DataBaseName := GetDB().Connection.Database;
Connexion.Connect();
if Connexion.Connected then
begin
IncExecuteCount();
DebugExecuteSQL(CallType, SQLText);
if not GetSimulated(CallType) then
begin
if mysql_real_query(Connexion.FConnexion , PChar(SQLText), Length(SQLText)) <> 0 then
begin
// raise EMyError.CreateFmt('Echec de la Requête : %d - %s ', [mysql_errno(Connexion.FConnexion), mysql_error(Connexion.FConnexion)]);
Error := Format('Echec de la Requête : %d - %s ', [mysql_errno(Connexion.FConnexion), mysql_error(Connexion.FConnexion)]);
Exit;
end;
Result_RES := mysql_store_result(Connexion.FConnexion);
if not Assigned(Result_RES) then
begin
// Erreur sur un select ou un script ?
if mysql_field_count(Connexion.FConnexion) > 0 then
begin
// raise EMyError.CreateFmt('%d - %s ', [mysql_errno(Connexion.FConnexion), mysql_error(Connexion.FConnexion)]);
Error := Format('%d - %s ', [mysql_errno(Connexion.FConnexion), mysql_error(Connexion.FConnexion)]);
Exit;
end else
begin
Result := TClientDataSet.Create(nil);
with TClientDataSet(Result) do
begin
AddField('Affected_Rows', ftInteger);
Open();
Append();
Fields[0].AsInteger := mysql_affected_rows(Connexion.FConnexion);
Post();
end;
Exit; // C'est un Script
end;
end;
// traitement des données
if CallType = tsQuery then
begin
if mysql_field_count(Connexion.FConnexion) > 0 then
begin
Result := TClientDataSet.Create(nil);
with TClientDataSet(Result) do
begin
// initialisation des colonnes
mysql_field_seek(Result_RES, 0);
for iCol := 0 to mysql_num_fields(Result_RES)-1 do
begin
Field_RES := mysql_fetch_field(Result_RES);
AddField(Field_RES.name,
TypeMySQLToTypeDB(Field_RES._type),
GetLengthTypeMySQL(Field_RES._type, Field_RES.max_length));
end;
Open();
// Remplissage des Champs
Record_RES := mysql_fetch_row(Result_RES);
while Assigned(Record_RES) do
begin
mysql_field_seek(Result_RES,0);
Append();
for iCol := 0 to mysql_num_fields(Result_RES)-1 do
begin
Field_RES := mysql_fetch_field(Result_RES);
if Assigned(Record_RES[iCol]) then
begin
Fields[iCol].AsString := ValueMySQLToDB(Field_RES._type, Record_RES[iCol]);
end;
end;
Post();
Record_RES := mysql_fetch_row(Result_RES);
end;
end;
end;
end;
end;
end else
begin
Result := inherited InternalExecuteSQL(CallType, SQLText, Error, Latency);
end;
finally
if Assigned(Result_RES) then
mysql_free_result(Result_RES);
Connexion.Free();
end;
end; |
Partager