开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (6)
标签 (68)
master
lazarus
follow-foreign-key
clickhouse-135
rentalhost-patch-634
new-usermanager
v12.20
v12.20-Windows
v12.19
v12.19-Windows
v12.18
v12.18-Windows
12.17
12.17-Windows
v12.16
12.16-Windows
v12.15.0.7171
v12.15.1.1
v12.14.0.7164
v12.14.1.1
v12.13.0.7147
v12.13.1.1
12.12-Linux
12.12
12.11.1.167
12.11
master
分支 (6)
标签 (68)
master
lazarus
follow-foreign-key
clickhouse-135
rentalhost-patch-634
new-usermanager
v12.20
v12.20-Windows
v12.19
v12.19-Windows
v12.18
v12.18-Windows
12.17
12.17-Windows
v12.16
12.16-Windows
v12.15.0.7171
v12.15.1.1
v12.14.0.7164
v12.14.1.1
v12.13.0.7147
v12.13.1.1
12.12-Linux
12.12
12.11.1.167
12.11
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (6)
标签 (68)
master
lazarus
follow-foreign-key
clickhouse-135
rentalhost-patch-634
new-usermanager
v12.20
v12.20-Windows
v12.19
v12.19-Windows
v12.18
v12.18-Windows
12.17
12.17-Windows
v12.16
12.16-Windows
v12.15.0.7171
v12.15.1.1
v12.14.0.7164
v12.14.1.1
v12.13.0.7147
v12.13.1.1
12.12-Linux
12.12
12.11.1.167
12.11
HeidiSQL
/
source
/
loaddata.pas
HeidiSQL
/
source
/
loaddata.pas
loaddata.pas 26.12 KB
一键复制 编辑 原始数据 按行查看 历史
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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
unit loaddata;
// -------------------------------------
// Load Textfile into table
// -------------------------------------
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.CheckLst,
SynRegExpr, Vcl.Buttons, Vcl.ExtCtrls, Vcl.ToolWin, Vcl.ExtDlgs, System.Math, System.IOUtils, extra_controls,
dbconnection, dbstructures, gnugettext;
type
Tloaddataform = class(TExtForm)
btnImport: TButton;
btnCancel: TButton;
lblDatabase: TLabel;
comboDatabase: TComboBox;
lblTable: TLabel;
comboTable: TComboBox;
lblColumns: TLabel;
chklistColumns: TCheckListBox;
ToolBarColMove: TToolBar;
btnColUp: TToolButton;
btnColDown: TToolButton;
grpFilename: TGroupBox;
editFilename: TButtonedEdit;
grpChars: TGroupBox;
lblFieldTerminater: TLabel;
lblFieldEncloser: TLabel;
lblFieldEscaper: TLabel;
editFieldEscaper: TEdit;
editFieldEncloser: TEdit;
editFieldTerminator: TEdit;
chkFieldsEnclosedOptionally: TCheckBox;
grpOptions: TGroupBox;
lblIgnoreLinesCount: TLabel;
updownIgnoreLines: TUpDown;
editIgnoreLines: TEdit;
editLineTerminator: TEdit;
lblLineTerminator: TLabel;
lblIgnoreLines: TLabel;
lblFilename: TLabel;
comboEncoding: TComboBox;
lblEncoding: TLabel;
grpDuplicates: TRadioGroup;
grpParseMethod: TRadioGroup;
grpDestination: TGroupBox;
chkLowPriority: TCheckBox;
chkLocalNumbers: TCheckBox;
chkTruncateTable: TCheckBox;
btnCheckAll: TToolButton;
chkKeepDialogOpen: TCheckBox;
const ProgressBarSteps=100;
procedure FormCreate(Sender: TObject);
procedure editFilenameChange(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure comboDatabaseChange(Sender: TObject);
procedure comboTablePopulate(SelectTableName: String; RefreshDbObjects: Boolean);
procedure comboTableChange(Sender: TObject);
procedure btnImportClick(Sender: TObject);
procedure ServerParse(Sender: TObject);
procedure ClientParse(Sender: TObject);
procedure btnOpenFileClick(Sender: TObject);
procedure btnColMoveClick(Sender: TObject);
procedure grpParseMethodClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure chklistColumnsClick(Sender: TObject);
procedure btnCheckAllClick(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
FFileEncoding: TEncoding;
FTerm, FEncl, FEscp, FLineTerm: String;
FRowCount, FColumnCount: Integer;
FColumns: TTableColumnList;
FConnection: TDBConnection;
public
{ Public declarations }
property FileEncoding: TEncoding read FFileEncoding;
end;
implementation
uses Main, apphelpers, csv_detector;
{$R *.DFM}
procedure Tloaddataform.FormCreate(Sender: TObject);
begin
HasSizeGrip := True;
// Restore settings
editFilename.Text := AppSettings.ReadString(asCSVImportFilename);
editFieldTerminator.Text := AppSettings.ReadString(asCSVImportSeparator);
editFieldEncloser.Text := AppSettings.ReadString(asCSVImportEncloser);
editLineTerminator.Text := AppSettings.ReadString(asCSVImportTerminator);
chkFieldsEnclosedOptionally.Checked := AppSettings.ReadBool(asCSVImportFieldsEnclosedOptionally);
editFieldEscaper.Text := AppSettings.ReadString(asCSVImportFieldEscaper);
updownIgnoreLines.Position := AppSettings.ReadInt(asCSVImportIgnoreLines);
chkLowPriority.Checked := AppSettings.ReadBool(asCSVImportLowPriority);
chkLocalNumbers.Checked := AppSettings.ReadBool(asCSVImportLocalNumbers);
chkKeepDialogOpen.Checked := AppSettings.ReadBool(asCSVKeepDialogOpen);
// Uncheck critical "Truncate table" checkbox, to avoid accidental data removal
chkTruncateTable.Checked := False;
grpDuplicates.ItemIndex := AppSettings.ReadInt(asCSVImportDuplicateHandling);
grpParseMethod.ItemIndex := AppSettings.ReadInt(asCSVImportParseMethod);
end;
procedure Tloaddataform.FormResize(Sender: TObject);
var
HalfWidth, RightBoxX: Integer;
begin
// Rethink width of side-by-side group boxes
HalfWidth := (ClientWidth - 3 * grpFilename.Left) div 2;
RightBoxX := HalfWidth + 2 * grpFilename.Left;
grpOptions.Width := HalfWidth;
grpDuplicates.Width := HalfWidth;
grpParseMethod.Width := HalfWidth;
grpChars.Width := HalfWidth;
grpDestination.Width := HalfWidth;
// Move right boxes to the right position
grpChars.Left := RightBoxX;
grpDestination.Left := RightBoxX;
end;
procedure Tloaddataform.FormShow(Sender: TObject);
begin
Width := AppSettings.ReadIntDpiAware(asCSVImportWindowWidth, Self);
Height := AppSettings.ReadIntDpiAware(asCSVImportWindowHeight, Self);
FConnection := MainForm.ActiveConnection;
// Disable features supported in MySQL only, if active connection is not MySQL
if not FConnection.Parameters.IsAnyMySQL then begin
grpParseMethod.ItemIndex := 1;
grpDuplicates.ItemIndex := 0;
end;
grpParseMethod.Controls[0].Enabled := FConnection.Parameters.IsAnyMySQL;
grpDuplicates.Controls[1].Enabled := FConnection.Parameters.IsAnyMySQL;
grpDuplicates.Controls[2].Enabled := FConnection.Parameters.IsAnyMySQL;
chkLowPriority.Enabled := FConnection.Parameters.IsAnyMySQL;
// Read databases and tables from active connection
comboDatabase.Items.Clear;
comboDatabase.Items.Assign(FConnection.AllDatabases);
comboDatabase.ItemIndex := comboDatabase.Items.IndexOf(Mainform.ActiveDatabase);
if comboDatabase.ItemIndex = -1 then
comboDatabase.ItemIndex := 0;
comboDatabaseChange(Sender);
editFilename.SetFocus;
end;
procedure Tloaddataform.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// Save settings
AppSettings.WriteIntDpiAware(asCSVImportWindowWidth, Self, Width);
AppSettings.WriteIntDpiAware(asCSVImportWindowHeight, Self, Height);
AppSettings.WriteString(asCSVImportFilename, editFilename.Text);
AppSettings.WriteString(asCSVImportSeparator, editFieldTerminator.Text);
AppSettings.WriteString(asCSVImportEncloser, editFieldEncloser.Text);
AppSettings.WriteString(asCSVImportTerminator, editLineTerminator.Text);
AppSettings.WriteBool(asCSVImportFieldsEnclosedOptionally, chkFieldsEnclosedOptionally.Checked);
AppSettings.WriteString(asCSVImportFieldEscaper, editFieldEscaper.Text);
AppSettings.WriteInt(asCSVImportIgnoreLines, updownIgnoreLines.Position);
AppSettings.WriteBool(asCSVImportLowPriority, chkLowPriority.Checked);
AppSettings.WriteBool(asCSVImportLocalNumbers, chkLocalNumbers.Checked);
AppSettings.WriteBool(asCSVKeepDialogOpen, chkKeepDialogOpen.Checked);
AppSettings.WriteInt(asCSVImportDuplicateHandling, grpDuplicates.ItemIndex);
AppSettings.WriteInt(asCSVImportParseMethod, grpParseMethod.ItemIndex);
end;
procedure Tloaddataform.grpParseMethodClick(Sender: TObject);
var
ServerWillParse: Boolean;
FileCharset: String;
v, i: Integer;
begin
ServerWillParse := grpParseMethod.ItemIndex = 0;
comboEncoding.Enabled := ServerWillParse;
editFieldEscaper.Enabled := ServerWillParse;
chkFieldsEnclosedOptionally.Enabled := ServerWillParse;
comboEncoding.Clear;
if comboEncoding.Enabled then begin
// Populate charset combo
v := FConnection.ServerVersionInt;
if ((v >= 50038) and (v < 50100)) or (v >= 50117) then begin
FileCharset := MainForm.GetCharsetByEncoding(FFileEncoding);
if FileCharset.IsEmpty then
FileCharset := 'utf8';
comboEncoding.Items := FConnection.CharsetList;
// Preselect file encoding, or utf8 as a fallback
for i:=0 to comboEncoding.Items.Count-1 do begin
if ExecRegExpr('^'+QuoteRegExprMetaChars(FileCharset)+'\b', comboEncoding.Items[i]) then begin
comboEncoding.ItemIndex := i;
Break;
end;
end;
end else begin
comboEncoding.Items.Add(_(SUnsupported));
comboEncoding.ItemIndex := 0;
end;
end else begin
comboEncoding.Items.Add(Mainform.GetEncodingName(FFileEncoding));
comboEncoding.ItemIndex := 0;
end;
end;
procedure Tloaddataform.comboDatabaseChange(Sender: TObject);
begin
comboTablePopulate('', False);
grpParseMethod.OnClick(Sender);
comboTableChange(Sender);
end;
procedure Tloaddataform.comboTablePopulate(SelectTableName: String; RefreshDbObjects: Boolean);
var
count, i: Integer;
DBObjects: TDBObjectList;
seldb, seltable: String;
begin
// read tables from db
comboTable.Items.Clear;
seldb := Mainform.ActiveDatabase;
seltable := Mainform.ActiveDbObj.Name;
DBObjects := FConnection.GetDBObjects(comboDatabase.Text, RefreshDbObjects);
for i:=0 to DBObjects.Count-1 do begin
if DBObjects[i].NodeType in [lntTable, lntView] then
comboTable.Items.Add(DBObjects[i].Name);
count := comboTable.Items.Count-1;
if SelectTableName.IsEmpty and (comboDatabase.Text = seldb) and (comboTable.Items[count] = seltable) then
comboTable.ItemIndex := count
else if (not SelectTableName.IsEmpty) and (SelectTableName = comboTable.Items[count]) then
comboTable.ItemIndex := count;
end;
if (comboTable.ItemIndex = -1) and (comboTable.Items.Count >= 1) then
comboTable.ItemIndex := 0; // First real table
comboTable.Items.Add('<'+_('New table')+'>');
end;
procedure Tloaddataform.comboTableChange(Sender: TObject);
var
Col: TTableColumn;
DBObjects: TDBObjectList;
Obj: TDBObject;
begin
// fill columns, or show csv detector:
chklistColumns.Items.Clear;
if comboTable.ItemIndex = comboTable.Items.Count-1 then begin
frmCsvDetector := TfrmCsvDetector.Create(Self);
case frmCsvDetector.ShowModal of
mrOk: begin
// table got created and combo is refreshed
end;
else begin
comboTable.ItemIndex := 0;
end;
end;
frmCsvDetector.Free;
frmCsvDetector := nil; // check for Assigned() must be false in SetupSynEditors
end;
if (comboDatabase.Text <> '') and (comboTable.Text <> '') then begin
if not Assigned(FColumns) then
FColumns := TTableColumnList.Create;
DBObjects := FConnection.GetDBObjects(comboDatabase.Text);
for Obj in DBObjects do begin
if (Obj.Database=comboDatabase.Text) and (Obj.Name=comboTable.Text) then begin
case Obj.NodeType of
lntTable, lntView: FColumns := Obj.TableColumns;
end;
end;
end;
for Col in FColumns do
chklistColumns.Items.Add(Col.Name);
end;
// select all:
chklistColumns.CheckAll(cbChecked);
chklistColumns.OnClick(Sender);
// Ensure valid state of Import-Button
editFilenameChange(Sender);
end;
procedure Tloaddataform.btnImportClick(Sender: TObject);
var
StartTickCount: Cardinal;
i: Integer;
begin
Screen.Cursor := crHourglass;
StartTickCount := GetTickCount;
MainForm.EnableProgress(ProgressBarSteps);
// Truncate table before importing
if chkTruncateTable.Checked then try
FConnection.Query('TRUNCATE TABLE ' + FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text));
FConnection.ShowWarnings;
except
try
FConnection.Query('DELETE FROM ' + FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text));
FConnection.ShowWarnings;
except
on E:EDbError do
ErrorDialog(_('Cannot truncate table'), E.Message);
end;
end;
FColumnCount := 0;
for i:=0 to chkListColumns.Items.Count-1 do begin
if chkListColumns.Checked[i] then
Inc(FColumnCount);
end;
FTerm := FConnection.UnescapeString(editFieldTerminator.Text);
FEncl := FConnection.UnescapeString(editFieldEncloser.Text);
FLineTerm := FConnection.UnescapeString(editLineTerminator.Text);
FEscp := FConnection.UnescapeString(editFieldEscaper.Text);
if chkKeepDialogOpen.Checked then
ModalResult := mrNone;
try
case grpParseMethod.ItemIndex of
0: ServerParse(Sender);
1: ClientParse(Sender);
end;
MainForm.LogSQL(FormatNumber(FRowCount)+' rows imported in '+FormatNumber((GetTickcount-StartTickCount)/1000, 3)+' seconds.');
// Hint user if zero rows were detected in file
if FRowCount = 0 then begin
ErrorDialog(_('No rows were imported'),
_('This can have several causes:')+CRLF+
_(' - File is empty')+CRLF+
_(' - Wrong file encoding was selected or detected')+CRLF+
_(' - Field and/or line terminator do not fit to the file contents')
);
ModalResult := mrNone;
end;
except
on E:EDbError do begin
ModalResult := mrNone;
MainForm.SetProgressState(pbsError);
ErrorDialog(E.Message);
end;
on E:EStreamError do begin
// all file stream errors, eg. EFOpenError and EReadError
// http://docwiki.embarcadero.com/Libraries/Sydney/en/System.Classes.EStreamError
ModalResult := mrNone;
MainForm.SetProgressState(pbsError);
ErrorDialog(E.Message + sLineBreak + sLineBreak + editFilename.Text);
end;
end;
if ModalResult = mrNone then
btnCancel.Caption := _('Close');
Mainform.ShowStatusMsg;
MainForm.DisableProgress;
Screen.Cursor := crDefault;
end;
procedure Tloaddataform.ServerParse(Sender: TObject);
var
SQL, SetColVars, SelectedCharset: String;
i: Integer;
Filename: String;
begin
SQL := 'LOAD DATA ';
if chkLowPriority.Checked and chkLowPriority.Enabled then
SQL := SQL + 'LOW_PRIORITY ';
// Issue #1387: Use 8.3 filename, to prevent "file not found" error from MySQL library
// Todo: test on Wine
Filename := ExtractShortPathName(editFilename.Text);
if not Filename.IsEmpty then
MainForm.LogSQL('Converting filename to 8.3 format: '+editFilename.Text+' => '+Filename, lcInfo)
else
Filename := editFilename.Text;
SQL := SQL + 'LOCAL INFILE ' + FConnection.EscapeString(Filename) + ' ';
case grpDuplicates.ItemIndex of
1: SQL := SQL + 'IGNORE ';
2: SQL := SQL + 'REPLACE ';
end;
SQL := SQL + 'INTO TABLE ' + FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text) + ' ';
SelectedCharset := RegExprGetMatch('^(\w+)\b', comboEncoding.Text, 1);
if not SelectedCharset.IsEmpty then begin
SQL := SQL + 'CHARACTER SET '+SelectedCharset+' ';
end;
// Fields:
if (FTerm <> '') or (FEncl <> '') or (FEscp <> '') then
SQL := SQL + 'FIELDS ';
if editFieldTerminator.Text <> '' then
SQL := SQL + 'TERMINATED BY ' + FConnection.EscapeString(FTerm) + ' ';
if FEncl <> '' then begin
if chkFieldsEnclosedOptionally.Checked then
SQL := SQL + 'OPTIONALLY ';
SQL := SQL + 'ENCLOSED BY ' + FConnection.EscapeString(FEncl) + ' ';
end;
if FEscp <> '' then
SQL := SQL + 'ESCAPED BY ' + FConnection.EscapeString(FEscp) + ' ';
// Lines:
if FLineTerm <> '' then
SQL := SQL + 'LINES TERMINATED BY ' + FConnection.EscapeString(FLineTerm) + ' ';
if updownIgnoreLines.Position > 0 then
SQL := SQL + 'IGNORE ' + inttostr(updownIgnoreLines.Position) + ' LINES ';
// Column listing
SQL := SQL + '(';
SetColVars := '';
for i:=0 to chklistColumns.Items.Count-1 do begin
if chklistColumns.Checked[i] then begin
if chkLocalNumbers.Checked and (FColumns[i].DataType.Category in [dtcInteger, dtcReal]) then begin
SQL := SQL + '@ColVar' + IntToStr(i) + ', ';
SetColVars := SetColVars + FConnection.QuoteIdent(chklistColumns.Items[i]) +
' = REPLACE(REPLACE(@ColVar' + IntToStr(i) + ', '+FConnection.EscapeString(FormatSettings.ThousandSeparator)+', ''''), '+FConnection.EscapeString(FormatSettings.DecimalSeparator)+', ''.''), ';
end else
SQL := SQL + FConnection.QuoteIdent(chklistColumns.Items[i]) + ', ';
end;
end;
SetLength(SQL, Length(SQL)-2);
SQL := SQL + ')';
if SetColVars <> '' then begin
SetLength(SetColVars, Length(SetColVars)-2);
SQL := SQL + ' SET ' + SetColVars;
end;
FConnection.Query(SQL);
FRowCount := Max(FConnection.RowsAffected, 0);
FConnection.ShowWarnings;
end;
procedure Tloaddataform.ClientParse(Sender: TObject);
var
P, ContentLen, ProgressCharsPerStep, ProgressChars: Integer;
IgnoreLines, ValueCount, PacketSize: Integer;
LineNum: Int64;
RowCountInChunk: Int64;
EnclLen, TermLen, LineTermLen: Integer;
Contents: String;
EnclTest, TermTest, LineTermTest: String;
Value, SQL: String;
IsEncl, IsTerm, IsLineTerm, IsEof: Boolean;
InEncl: Boolean;
OutStream: TMemoryStream;
procedure NextChar;
begin
Inc(P);
Inc(ProgressChars);
if ProgressChars >= ProgressCharsPerStep then begin
Mainform.ProgressStep;
Mainform.ShowStatusMsg(f_('Importing textfile, row %s, %d%%', [FormatNumber(FRowCount-IgnoreLines), Mainform.ProgressBarStatus.Position]));
ProgressChars := 0;
end;
end;
function TestLeftChars(var Portion: String; CompareTo: String; Len: Integer): Boolean;
var i: Integer;
begin
if Len > 0 then begin
for i:=1 to Len-1 do
Portion[i] := Portion[i+1];
Portion[Len] := Contents[P];
Result := Portion = CompareTo;
end else
Result := False;
end;
procedure AddValue;
var
i: Integer;
LowPrio: String;
ColumnIndex: Integer;
ValuesCounted: Integer;
begin
Inc(ValueCount);
if ValueCount <= FColumnCount then begin
if Copy(Value, 1, EnclLen) = FEncl then begin
Delete(Value, 1, EnclLen);
Delete(Value, Length(Value)-EnclLen+1, EnclLen);
end;
if SQL = '' then begin
LowPrio := '';
if chkLowPriority.Checked and chkLowPriority.Enabled then
LowPrio := 'LOW_PRIORITY ';
case grpDuplicates.ItemIndex of
0: SQL := 'INSERT '+LowPrio;
1: SQL := 'INSERT '+LowPrio+'IGNORE ';
2: SQL := 'REPLACE '+LowPrio;
end;
SQL := SQL + 'INTO '+FConnection.QuotedDbAndTableName(comboDatabase.Text, comboTable.Text)+' (';
for i:=0 to chkListColumns.Items.Count-1 do begin
if chkListColumns.Checked[i] then
SQL := SQL + FConnection.QuoteIdent(chkListColumns.Items[i]) + ', ';
end;
SetLength(SQL, Length(SQL)-2);
SQL := SQL + ') VALUES (';
end;
// Bugfix for #327: Determine column to retrieve the type from by counting the number of columns before the current one INCLUDING the omitted ones
ColumnIndex := 0;
ValuesCounted := 0;
for i:=0 to chkListColumns.Items.Count-1 do
begin
if chkListColumns.Checked[i] then // column was already counted
Inc(ValuesCounted); // increase number of counted columns
if ValuesCounted = ValueCount then // did we count all included columns up to the current column?
Break;
Inc(ColumnIndex); // if all columns (until the current column) are checked, ColumnIndex is ValueCount-1, like before this patch
end;
if Value <> 'NULL' then begin
if chkLocalNumbers.Checked and (FColumns[ColumnIndex].DataType.Category in [dtcInteger, dtcReal]) then
Value := UnformatNumber(Value)
else
Value := FConnection.EscapeString(Value, FColumns[ColumnIndex].DataType);
end;
SQL := SQL + Value + ', ';
end;
Value := '';
end;
procedure AddRow;
var
SA: AnsiString;
ChunkSize: Int64;
i: Integer;
begin
if SQL = '' then
Exit;
Inc(LineNum);
for i:=ValueCount to FColumnCount do begin
Value := 'NULL';
AddValue;
end;
ValueCount := 0;
if LineNum > IgnoreLines then begin
Delete(SQL, Length(SQL)-1, 2);
StreamWrite(OutStream, SQL + ')');
SQL := '';
Inc(RowCountInChunk);
if (OutStream.Size < PacketSize) and (P < ContentLen) and (RowCountInChunk < FConnection.MaxRowsPerInsert) then begin
SQL := SQL + ', (';
end else begin
OutStream.Position := 0;
ChunkSize := OutStream.Size;
SetLength(SA, ChunkSize div SizeOf(AnsiChar));
OutStream.Read(PAnsiChar(SA)^, ChunkSize);
OutStream.Size := 0;
FConnection.Query(UTF8ToString(SA), False, lcScript);
Inc(FRowCount, Max(FConnection.RowsAffected, 0));
FConnection.ShowWarnings;
SQL := '';
RowCountInChunk := 0;
end;
end else
SQL := '';
end;
begin
TermLen := Length(FTerm);
EnclLen := Length(FEncl);
LineTermLen := Length(FLineTerm);
SetLength(TermTest, TermLen);
SetLength(EnclTest, EnclLen);
SetLength(LineTermTest, LineTermLen);
InEncl := False;
SQL := '';
Value := '';
OutStream := TMemoryStream.Create;
// Turns SQL errors into warnings, e.g. when providing an empty string for an integer column
if FConnection.Parameters.IsAnyMySQL then begin
FConnection.Query('/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='''' */');
end;
MainForm.ShowStatusMsg(f_('Reading textfile (%s) ...', [FormatByteNumber(_GetFileSize(editFilename.Text))]));
Contents := ReadTextfile(editFilename.Text, FFileEncoding);
ContentLen := Length(Contents);
MainForm.ShowStatusMsg;
P := 0;
ProgressCharsPerStep := ContentLen div ProgressBarSteps;
ProgressChars := 0;
FRowCount := 0;
LineNum := 0;
RowCountInChunk := 0;
IgnoreLines := UpDownIgnoreLines.Position;
ValueCount := 0;
PacketSize := SIZE_MB div 2;
NextChar;
// TODO: read chunks!
while P <= ContentLen do begin
// Check characters left-side from current position
IsEncl := TestLeftChars(EnclTest, FEncl, EnclLen);
IsTerm := TestLeftChars(TermTest, FTerm, TermLen);
IsLineTerm := TestLeftChars(LineTermTest, FLineTerm, LineTermLen) and (ValueCount >= FColumnCount-1);
IsEof := P = ContentLen;
Value := Value + Contents[P];
if IsEncl then
InEncl := not InEncl;
if IsEof or (not InEncl) then begin
if IsLineTerm then begin
SetLength(Value, Length(Value)-LineTermLen);
AddValue;
end else if IsEof then begin
AddValue;
end else if IsTerm then begin
SetLength(Value, Length(Value)-TermLen);
AddValue;
end;
end;
if IsLineTerm and (not InEncl) then
AddRow;
NextChar;
end;
// Will check if SQL is empty and not run any query in that case:
AddRow;
Contents := '';
FreeAndNil(OutStream);
if FConnection.Parameters.IsAnyMySQL then begin
FConnection.Query('/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '''') */');
end;
end;
procedure Tloaddataform.btnOpenFileClick(Sender: TObject);
var
Dialog: TExtFileOpenDialog;
TestStream: TFileStream;
begin
AppSettings.ResetPath;
Dialog := TExtFileOpenDialog.Create(Self);
Dialog.DefaultFolder := ExtractFilePath(editFilename.Text);
Dialog.FileName := ExtractFileName(editFilename.Text);
Dialog.AddFileType('*.csv', _('CSV files'));
Dialog.AddFileType('*.txt', _('Text files'));
Dialog.AddFileType('*.*', _('All files'));
Dialog.DefaultExtension := 'csv';
Dialog.Encodings.Assign(Mainform.FileEncodings);
Dialog.EncodingIndex := AppSettings.ReadInt(asFileDialogEncoding, Self.Name);
if Dialog.Execute then begin
editfilename.Text := Dialog.FileName;
FFileEncoding := Mainform.GetEncodingByName(Dialog.Encodings[Dialog.EncodingIndex]);
if FFileEncoding = nil then begin
MessageDialog(_('Auto detecting the encoding of a file is highly discouraged. You may experience data loss if the detection fails.') +
SLineBreak + SLineBreak +
_('To avoid this message select the correct encoding before pressing Open.'),
mtWarning, [mbOK]);
TestStream := TFileStream.Create(Dialog.Filename, fmOpenRead or fmShareDenyNone);
FFileEncoding := DetectEncoding(TestStream);
TestStream.Free;
end;
grpParseMethod.OnClick(Sender);
AppSettings.WriteInt(asFileDialogEncoding, Dialog.EncodingIndex, Self.Name);
end;
Dialog.Free;
end;
procedure Tloaddataform.chklistColumnsClick(Sender: TObject);
var
i, CheckedNum: Integer;
begin
btnColDown.Enabled := (chklistColumns.ItemIndex > -1)
and (chklistColumns.ItemIndex < chklistColumns.Count-1);
btnColUp.Enabled := (chklistColumns.ItemIndex > -1)
and (chklistColumns.ItemIndex > 0);
// Toggle icon when none is selected
CheckedNum := 0;
for i:=0 to chklistColumns.Items.Count-1 do begin
if chklistColumns.Checked[i] then
Inc(CheckedNum);
end;
if CheckedNum < chklistColumns.Items.Count then
btnCheckAll.ImageIndex := 127
else
btnCheckAll.ImageIndex := 128;
end;
procedure Tloaddataform.btnCheckAllClick(Sender: TObject);
var
i, CheckedNum: Integer;
begin
CheckedNum := 0;
for i:=0 to chklistColumns.Items.Count-1 do begin
if chklistColumns.Checked[i] then
Inc(CheckedNum);
end;
if CheckedNum < chklistColumns.Items.Count then
chklistColumns.CheckAll(cbChecked)
else
chklistColumns.CheckAll(cbUnchecked);
chklistColumns.OnClick(Sender);
end;
procedure Tloaddataform.btnColMoveClick(Sender: TObject);
var
CheckedSelected, CheckedTarget: Boolean;
TargetIndex: Integer;
begin
// Move column name and its checkstate up or down
if Sender = btnColUp then
TargetIndex := chklistColumns.ItemIndex-1
else
TargetIndex := chklistColumns.ItemIndex+1;
if (TargetIndex > -1) and (TargetIndex < chklistColumns.Count) then begin
CheckedSelected := chklistColumns.Checked[chklistColumns.ItemIndex];
CheckedTarget := chklistColumns.Checked[TargetIndex];
chklistColumns.Items.Exchange(chklistColumns.ItemIndex, TargetIndex);
chklistColumns.Checked[chklistColumns.ItemIndex] := CheckedTarget;
chklistColumns.Checked[TargetIndex] := CheckedSelected;
chklistColumns.ItemIndex := TargetIndex;
end;
chklistColumns.OnClick(Sender);
end;
{** Make "OK"-button only clickable if
- filename is not empty
- table is selected
- columnnames could be fetched normally
- filename exists
}
procedure Tloaddataform.editFilenameChange(Sender: TObject);
begin
btnImport.Enabled := (editFilename.Text <> '')
and (chklistColumns.Items.Count > 0)
and (FileExists(editFilename.Text));
end;
end.
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

A lightweight client for managing MariaDB, MySQL, SQL Server, PostgreSQL, SQLite, Interbase and Firebird, written in Delphi
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_HeidiSQL/HeidiSQL.git
git@gitee.com:mirrors_HeidiSQL/HeidiSQL.git
mirrors_HeidiSQL
HeidiSQL
HeidiSQL
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /