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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
| unit uParadoxToSQLForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, ExtCtrls, TypInfo, Types, StrUtils,
ComCtrls, Contnrs, clipbrd, epcStringUtils, DBCtrls, ADODB, IniFiles,
Buttons;
type
TParadoxToSQLForm = class(TForm)
PanelButton: TPanel;
EdDataBaseName: TLabeledEdit;
EdTableName: TLabeledEdit;
BtnOpen: TButton;
MemoSQLTemplate: TMemo;
MemoValues: TMemo;
BtnExtractValues: TButton;
DataSourceValues: TDataSource;
TableValues: TTable;
OpenDialogTemplate: TOpenDialog;
BtnOpenTemplate: TButton;
BtnSaveTemplate: TButton;
SaveDialogTemplate: TSaveDialog;
BtnExecuteByBDE: TButton;
QueryValues: TQuery;
EdDelphiDateTimeFormat: TLabeledEdit;
EdSQLDateTimeFormat: TLabeledEdit;
EdDelphiDateFormat: TLabeledEdit;
EdSQLDateFormat: TLabeledEdit;
SplitterGridBottom: TSplitter;
SplitterGridTop: TSplitter;
PanelGrid: TPanel;
DBGridValues: TDBGrid;
DBNavigatorValues: TDBNavigator;
ProgressBarValues: TProgressBar;
BtnSaveSQL: TButton;
ADOQueryValues: TADOQuery;
BtnExecuteByADO: TButton;
SaveDialogSQL: TSaveDialog;
BtnRunWorkList: TSpeedButton;
procedure BtnOpenClick(Sender: TObject);
procedure BtnExtractValuesClick(Sender: TObject);
procedure BtnOpenTemplateClick(Sender: TObject);
procedure BtnSaveTemplateClick(Sender: TObject);
procedure BtnExecuteByBDEClick(Sender: TObject);
procedure MemoValuesKeyPress(Sender: TObject; var Key: Char);
procedure BtnExecuteByADOClick(Sender: TObject);
procedure BtnSaveSQLClick(Sender: TObject);
procedure BtnRunWorkListClick(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
ParadoxToSQLForm: TParadoxToSQLForm;
implementation
{$R *.dfm}
function ReplaceChar(const S: string; const OldChar, NewChar: Char): string;
var
i: Integer;
p: PChar;
begin
if OldChar = NewChar then
Exit;
Result := S;
p := @Result[1];
for i := 0 to Length(S)-1 do
if PChar(p+i)^ = OldChar then
PChar(p+i)^ := NewChar;
end;
procedure TParadoxToSQLForm.MemoValuesKeyPress(Sender: TObject; var Key: Char);
begin
if (Key = #3) or (Key = #24) then // Copier / Couper
begin
Key := #0;
Clipboard.AsText := ReplaceChar(MemoValues.SelText, #$A0, #10);
end;
end;
procedure TParadoxToSQLForm.BtnOpenClick(Sender: TObject);
begin
TableValues.Close();
TableValues.DataBaseName := EdDataBaseName.Text;
TableValues.TableName := EdTableName.Text;
DataSourceValues.DataSet := TableValues;
TableValues.Open();
end;
procedure TParadoxToSQLForm.BtnExtractValuesClick(Sender: TObject);
var
LineValues: array of string;
Value, Line, Template: string;
I: Integer;
begin
Template := Trim(MemoSQLTemplate.Text);
if (Template = '') or (Pos(';', Template) > 0) then
begin
MessageDlg('Template SQL Incorrect', mtError, [mbAbort], 0);
Exit;
end;
MemoValues.Lines.Clear();
TableValues.First();
SetLength(LineValues, TableValues.Fields.Count);
while not TableValues.Eof do
begin
ZeroMemory(@LineValues[0], SizeOf(string) * Length(LineValues));
for I := 0 to TableValues.Fields.Count - 1 do
begin
if TableValues.Fields.Fields[I].IsNull then
begin
Value := 'NULL';
end
else
begin
try
case TableValues.Fields.Fields[I].DataType of
ftString : Value := QuotedStr(Format('%s', [TableValues.Fields.Fields[I].AsString]));
ftAutoInc : Value := TableValues.Fields.Fields[I].AsString;
ftInteger : Value := TableValues.Fields.Fields[I].AsString;
ftFloat : Value := ReplaceChar(TableValues.Fields.Fields[I].AsString, ',', '.');
ftDate : Value := Format(EdSQLDateFormat.Text, [FormatDateTime(EdDelphiDateFormat.Text, TableValues.Fields.Fields[I].AsDateTime)]);
ftDateTime : Value := Format(EdSQLDateTimeFormat.Text, [FormatDateTime(EdDelphiDateTimeFormat.Text, TableValues.Fields.Fields[I].AsDateTime)]);
ftBlob : Value := QuotedStr(Format('%s', [TableValues.Fields.Fields[I].AsString]));
ftMemo : Value := QuotedStr(Format('%s', [TableValues.Fields.Fields[I].AsString]));
ftFmtMemo : Value := QuotedStr(Format('%s', [TableValues.Fields.Fields[I].AsString]));
ftBoolean : Value := QuotedStr(Format('%s', [IfThen(TableValues.Fields.Fields[I].AsBoolean, 'T', 'F')]));
else
MessageDlg(Format('Type "%s" non géré', [GetEnumName(TypeInfo(TFieldType), Ord(TableValues.Fields.Fields[I].DataType))]), mtWarning, [mbAbort], 0);
Exit;
end;
except
on E: Exception do
begin
MessageDlg(Format('Valeur du Champ "%s" incorrect remplacé par NULL', [TableValues.Fields.Fields[I].FieldName]), mtInformation, [mbIgnore], 0);
Value := 'NULL';
end;
end;
end;
LineValues[I] := ReplaceChar(ReplaceChar(Value, #0, ' '), #$A0, ' '); // les Blobs contiennent un Zéro terminal ... le Blank est un séparateur interne, on le supprime des contenus
end;
Line := LineValues[Low(LineValues)];
for I := Succ(Low(LineValues)) to High(LineValues) do
Line := Line + ',' + LineValues[I];
MemoValues.Lines.Add(Format(Template, [Line]) + ';'#$A0);
TableValues.Next();
end;
end;
procedure TParadoxToSQLForm.BtnOpenTemplateClick(Sender: TObject);
begin
OpenDialogTemplate.InitialDir := ExtractFileDir(Application.ExeName);
if OpenDialogTemplate.Execute then
begin
MemoSQLTemplate.Lines.LoadFromFile(OpenDialogTemplate.FileName);
SaveDialogTemplate.FileName := OpenDialogTemplate.FileName;
end;
if Pos(';', MemoSQLTemplate.Lines.Text) > 0 then
begin
MessageDlg('Caractère ";" Interdit', mtError, [mbAbort], 0);
MemoSQLTemplate.Lines.Clear();
end;
end;
procedure TParadoxToSQLForm.BtnSaveTemplateClick(Sender: TObject);
begin
if Pos(';', MemoSQLTemplate.Lines.Text) > 0 then
begin
MessageDlg('Caractère ";" Interdit', mtError, [mbAbort], 0);
Exit;
end;
SaveDialogTemplate.InitialDir := ExtractFileDir(Application.ExeName);
if SaveDialogTemplate.Execute then
begin
MemoSQLTemplate.Lines.SaveToFile(SaveDialogTemplate.FileName);
OpenDialogTemplate.FileName := SaveDialogTemplate.FileName;
end;
end;
procedure TParadoxToSQLForm.BtnExecuteByBDEClick(Sender: TObject);
var
DataBaseName: string;
SQL: string;
I: Integer;
A: Types.TStringDynArray;
begin
DatabaseName := QueryValues.DatabaseName;
if not Self.Enabled or InputQuery('Paradox To SQL DataBase', 'SQL To DataBase Name', DataBaseName) then
begin
QueryValues.DatabaseName := DataBaseName;
QueryValues.ParamCheck := False;
ExplodeLazy(MemoValues.Text, A, #$A0);
ProgressBarValues.Max := Length(A);
ProgressBarValues.Position := 0;
TableValues.First();
for I := Low(A) to High(A) do
begin
TableValues.Next();
SQL := Trim(Copy(A[I], 1, Length(A[I]) - 1));
if SQL <> '' then
begin
QueryValues.SQL.Text := SQL;
try
QueryValues.ExecSQL();
except
on E: Exception do
raise ExceptClass(E.ClassType).CreateFmt('SQL = '#13'%s'#13#13'Error : "%s"', [SQL, E.Message]);
end;
end;
ProgressBarValues.StepIt();
end;
end;
end;
procedure TParadoxToSQLForm.BtnExecuteByADOClick(Sender: TObject);
var
ConnectionString: string;
SQL: string;
I: Integer;
A: Types.TStringDynArray;
begin
ConnectionString := ADOQueryValues.ConnectionString;
if not Self.Enabled or InputQuery('Paradox To SQL DataBase', 'ODBC ConnectionString', ConnectionString) then
begin
ADOQueryValues.ConnectionString := ConnectionString;
ADOQueryValues.ParamCheck := False;
ExplodeLazy(MemoValues.Text, A, #$A0);
ProgressBarValues.Max := Length(A);
ProgressBarValues.Position := 0;
TableValues.First();
for I := Low(A) to High(A) do
begin
TableValues.Next();
SQL := Trim(Copy(A[I], 1, Length(A[I]) - 1));
if SQL <> '' then
begin
ADOQueryValues.SQL.Text := SQL;
try
ADOQueryValues.ExecSQL();
except
on E: Exception do
raise ExceptClass(E.ClassType).CreateFmt('SQL = '#13'%s'#13#13'Error : "%s"', [SQL, E.Message]);
end;
end;
ProgressBarValues.StepIt();
end;
end;
end;
procedure TParadoxToSQLForm.BtnSaveSQLClick(Sender: TObject);
var
SaveList: TStringList;
begin
SaveDialogSQL.InitialDir := ExtractFileDir(Application.ExeName);
if SaveDialogSQL.Execute then
begin
SaveList := TStringList.Create();
try
SaveList.Add(ReplaceChar(MemoValues.SelText, #$A0, #10));
SaveList.SaveToFile(SaveDialogSQL.FileName);
finally
SaveList.Free();
end;
end;
end;
procedure TParadoxToSQLForm.BtnRunWorkListClick(Sender: TObject);
var
WorkList: TIniFile;
SQLMode: string;
IsBDE, IsADO, DoReadSQL: Boolean;
TablesNames: TStringList;
I: Integer;
Template: string;
begin
Self.Enabled := False;
try
WorkList := TIniFile.Create(ChangeFileExt(Application.ExeName, '.SQL.WL'));
try
if WorkList.SectionExists('PARAMS') and WorkList.SectionExists('SQL') then
begin
SQLMode := WorkList.ReadString('PARAMS', 'SQL_MODE', '');
DoReadSQL := False;
IsBDE := SQLMode = 'BDE';
if IsBDE then
begin
QueryValues.DatabaseName := WorkList.ReadString('PARAMS', 'SQL_DATABASE_NAME', '');
QueryValues.ParamCheck := False;
DoReadSQL := True;
end;
IsADO := SQLMode = 'ADO';
if IsADO then
begin
ADOQueryValues.ConnectionString := WorkList.ReadString('PARAMS', 'SQL_CONNEXION_STRING', '');
ADOQueryValues.ParamCheck := False;
DoReadSQL := True;
end;
if DoReadSQL then
begin
EdDataBaseName.Text := WorkList.ReadString('PARAMS', 'PARADOX_DATABASE_NAME', '');
if EdDataBaseName.Text <> '' then
begin
MessageDlg('Pière de surveiller le Traitemet car des Messages peuvent survenir durant celui-ci !', mtInformation, [mbOK], 0);
TablesNames := TStringList.Create();
try
WorkList.ReadSection('SQL', TablesNames);
for I := 0 to TablesNames.Count - 1 do
begin
EdTableName.Text := TablesNames[I];
Template := WorkList.ReadString('SQL', TablesNames[I], '');
if Template <> '' then
begin
MemoSQLTemplate.Lines.LoadFromFile(Template);
if Pos(';', MemoSQLTemplate.Lines.Text) = 0 then
begin
BtnOpen.Click();
BtnExtractValues.Click();
if IsBDE then
BtnExecuteByBDE.Click()
else
BtnExecuteByADO.Click();
end;
end;
end;
finally
TablesNames.Free();
end;
end;
end;
end;
finally
WorkList.Free();
end;
finally
Self.Enabled := True;
end;
end;
end. |
Partager