📄 qimport2.pas
字号:
EraseSection(QI_KEY_COLUMNS);
for i := 0 to KeyColumns.Count - 1 do
WriteString(QI_KEY_COLUMNS, KeyColumns[i], EmptyStr);
{$IFNDEF NOGUI}
WriteInteger(QI_BASE, QI_GRID_CAPTION_ROW, GridCaptionRow);
WriteInteger(QI_BASE, QI_GRID_START_ROW, GridStartRow);
{$ENDIF}
end;
end;
procedure TQImport2.DestinationInsert;
{$IFNDEF NOGUI}
var
i: integer;
{$ENDIF}
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet:
case AddType of
qatAppend: DataSet.Append;
qatInsert: DataSet.Insert;
end;
{$IFNDEF NOGUI}
qidDbGrid:
case AddType of
qatAppend: DBGrid.DataSource.DataSet.Append;
qatInsert: DBGrid.DataSource.DataSet.Insert;
end;
qidListView: begin
FCurrListItem := ListView.Items.Add;
for i := 1 to FImportRow.Count - 1 do
FCurrListItem.SubItems.Add(EmptyStr);
end;
qidStringGrid: begin
if FCurrStrGrRow = StringGrid.RowCount then
StringGrid.RowCount := StringGrid.RowCount + 1;
if FCurrStrGrRow = -1
then FCurrStrGrRow := StringGrid.RowCount - 1;
end;
{$ENDIF}
end;
FLastAction := qiaInsert;
end;
procedure TQImport2.DestinationEdit;
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet: DataSet.Edit;
{$IFNDEF NOGUI}
qidDbGrid: DBGrid.DataSource.DataSet.Edit;
{$ENDIF}
end;
FLastAction := qiaUpdate;
end;
procedure TQImport2.DestinationDelete;
{$IFNDEF NOGUI}
var
i: integer;
{$ENDIF}
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet: DataSet.Delete;
{$IFNDEF NOGUI}
qidDbGrid: DBGrid.DataSource.DataSet.Delete;
qidListView:
if Assigned(FCurrListItem) then FCurrListItem.Delete;
qidStringGrid:
if (FCurrStrGrRow > -1) and (FCurrStrGrRow < StringGrid.RowCount) then begin
for i := FCurrStrGrRow to StringGrid.RowCount - 2 do
StringGrid.Rows[i].Assign(StringGrid.Rows[i+1]);
StringGrid.RowCount:= StringGrid.RowCount - 1;
end;
{$ENDIF}
end;
FLastAction := qiaDelete;
end;
procedure TQImport2.DestinationSetValues;
var
i: integer;
str: string;
errorMsg: string;
begin
for i := 0 to FImportRow.Count - 1 do
begin
if FIsCSV then
begin
str := FImportRow[i].Value;
if FQuote <> #0 then
str := AnsiQuotedStr(str, FQuote);
if i = FImportRow.Count - 1
then str := str + {$IFDEF WIN32}#13#10{$ENDIF}{$IFDEF LINUX}#10{$ENDIF}
else str := str + FComma;
FStream.Write(str[1], Length(str));
Continue;
end;
case ImportDestination of
qidDataSet:
begin
errorMsg := StringToField(DataSet.Fields[FImportRow[i].FColumnIndex],
FImportRow[i].Value, FImportRow[i].IsBinary);
if errorMsg <> EmptyStr then
WriteErrorLog(errorMsg);
end;
{$IFNDEF NOGUI}
qidDBGrid:
begin
errorMsg := StringToField(DBGrid.Columns[FImportRow[i].FColumnIndex].Field,
FImportRow[i].Value, FImportRow[i].IsBinary);
if errorMsg <> EmptyStr then
WriteErrorLog(errorMsg);
end;
qidListView:
if Assigned(FCurrListItem) then
begin
if FImportRow[i].FColumnIndex = 0
then FCurrListItem.Caption := FImportRow[i].Value
else FCurrListItem.SubItems[FImportRow[i].FColumnIndex - 1] :=
FImportRow[i].Value;
end;
qidStringGrid:
if FCurrStrGrRow > -1 then
StringGrid.Cells[FImportRow[i].FColumnIndex, FCurrStrGrRow] :=
FImportRow[i].Value;
{$ENDIF}
end;
end;
end;
procedure TQImport2.DestinationPost;
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet:
if DataSet.State in [dsInsert, dsEdit] then
DataSet.Post;
{$IFNDEF NOGUI}
qidDbGrid:
if DBGrid.DataSource.DataSet.State in [dsInsert, dsEdit] then
DBGrid.DataSource.DataSet.Post;
qidStringGrid:
Inc(FCurrStrGrRow);
{$ENDIF}
end;
end;
procedure TQImport2.DestinationCancel;
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet: if DataSet.State in [dsInsert, dsEdit] then DataSet.Cancel;
{$IFNDEF NOGUI}
qidDbGrid: if DBGrid.DataSource.DataSet.State in [dsInsert, dsEdit] then
DBGrid.DataSource.DataSet.Cancel;
{$ENDIF}
end;
end;
function TQImport2.DestinationFindColumn(const ColName: string): integer;
var
Field: TField;
{$IFNDEF NOGUI}
i: integer;
{$ENDIF}
begin
Result := -1;
if FIsCSV then Exit;
case ImportDestination of
qidDataSet: begin
Field := DataSet.FindField(ColName);
if Assigned(Field) then
Result := Field.Index;
end;
{$IFNDEF NOGUI}
qidDBGrid:
for i := 0 to DBGrid.Columns.Count - 1 do
if AnsiCompareText(DBGrid.Columns[i].Title.Caption, ColName) = 0 then
begin
Result := i;
Exit;
end;
qidListView:
for i := 0 to ListView.Columns.Count - 1 do
if AnsiCompareText(ListView.Columns[i].Caption, ColName) = 0 then
begin
Result := i;
Exit;
end;
qidStringGrid: begin
i := StrToIntDef(ColName, -1);
if i > -1 then
begin
if i < StringGrid.ColCount then Result := i;
end
else begin
if GridCaptionRow > -1 then
for i := 0 to StringGrid.ColCount - 1 do
if AnsiCompareStr(StringGrid.Cells[i, GridCaptionRow], ColName) = 0 then
begin
Result := i;
Exit;
end;
end;
end;
{$ENDIF}
end;
end;
procedure TQImport2.DestinationDisableControls;
begin
if FIsCSV then Exit;
case ImportDestination of
qidDataSet: DataSet.DisableControls;
{$IFNDEF NOGUI}
qidDBGrid: DBGrid.DataSource.DataSet.DisableControls;
qidListView: ListView.Items.BeginUpdate;
{$ENDIF}
end;
end;
procedure TQImport2.DestinationEnableControls;
begin
if FIsCSV then Exit;
case ImportDestination of
qidDataSet: DataSet.EnableControls;
{$IFNDEF NOGUI}
qidDBGrid: DBGrid.DataSource.DataSet.EnableControls;
qidListView: ListView.Items.EndUpdate;
{$ENDIF}
end;
end;
procedure TQImport2.CheckDestination;
begin
QImportCheckDestination(IsCSV, ImportDestination, DataSet
{$IFNDEF NOGUI}, DBGrid, ListView,
StringGrid{$ENDIF});
end;
function TQImport2.DestinationFindByKey: boolean;
var
Keys: string;
Values: Variant;
i{$IFNDEF NOGUI}, j{$ENDIF}: integer;
Col: TQImportCol;
{$IFNDEF NOGUI}Flag: boolean;
{$ENDIF}
begin
Result := false;
if FIsCSV then Exit;
case ImportDestination of
qidDataSet:
if KeyColumns.Count > 0 then
begin
Keys := EmptyStr;
if KeyColumns.Count > 1 then
Values := VarArrayCreate([0, KeyColumns.Count - 1], varVariant);
for i := 0 to KeyColumns.Count - 1 do
begin
Col := FImportRow.ColByName(KeyColumns[i]);
if Assigned(Col) then
begin
Keys := Keys + Col.Name;
if KeyColumns.Count > 1
then Values[i] := Col.Value
else Values := Col.Value;
end;
if i < KeyColumns.Count - 1 then
Keys := Keys + ';';
end;
if Assigned(FOnDestinationLocate) then
FOnDestinationLocate(Self, KeyColumns, FImportRow, Keys, Values);
try
Result := DataSet.Locate(Keys, Values, [loCaseInsensitive])
except
Result := false;
end;
end;
{$IFNDEF NOGUI}
qidDBGrid:
if KeyColumns.Count > 0 then
begin
Keys := EmptyStr;
if KeyColumns.Count > 1 then
Values := VarArrayCreate([0, KeyColumns.Count - 1], varVariant);
for i := 0 to KeyColumns.Count - 1 do
begin
Col := FImportRow.ColByName(KeyColumns[i]);
if Assigned(Col) then
begin
Keys := Keys + DBGrid.Columns[Col.FColumnIndex].Field.FieldName;
if KeyColumns.Count > 1
then Values[i] := Col.Value
else Values := Col.Value;
end;
if i < KeyColumns.Count - 1 then Keys := Keys + ';';
end;
if Assigned(FOnDestinationLocate) then
FOnDestinationLocate(Self, KeyColumns, FImportRow, Keys, Values);
Result := DBGrid.DataSource.DataSet.Locate(Keys, Values, [loCaseInsensitive])
end;
qidListView: begin
for i := 0 to ListView.Items.Count - 1 do
begin
Flag := true;
for j := 0 to KeyColumns.Count - 1 do
begin
Col := FImportRow.ColByName(KeyColumns[j]);
if Assigned(Col) then
begin
if Col.FColumnIndex = 0
then Flag := AnsiCompareText(Col.Value, ListView.Items[i].Caption) = 0
else Flag := AnsiCompareText(Col.Value, ListView.Items[i].SubItems[Col.FColumnIndex - 1]) = 0;
if not Flag then Break;
end
else Exit;
end;
if Flag then
begin
Result := true;
FCurrListItem := ListView.Items[i];
Exit;
end;
end;
end;
qidStringGrid: begin
for i := 0 to StringGrid.RowCount - 1 do
begin
if (GridCaptionRow > -1) and (i = GridCaptionRow) then
Continue;
Flag := true;
for j := 0 to KeyColumns.Count - 1 do
begin
Col := FImportRow.ColByName(KeyColumns[j]);
if Assigned(Col)
then Flag := AnsiCompareText(Col.Value, StringGrid.Cells[Col.FColumnIndex, i]) = 0
else Exit;
if not Flag then
Break;
end;
if Flag then
begin
Result := true;
FCurrStrGrRow := i;
Exit;
end;
end;
end;
{$ENDIF}
end;
end;
function TQImport2.DestinationColCount: integer;
begin
Result := QImportDestinationColCount(IsCSV, ImportDestination, DataSet
{$IFNDEF NOGUI}, DBGrid, ListView, StringGrid{$ENDIF});
end;
function TQImport2.DestinationColName(Index: integer): string;
begin
Result := QImportDestinationColName(IsCSV, ImportDestination, DataSet,
{$IFNDEF NOGUI}DBGrid, ListView, StringGrid, GridCaptionRow,{$ENDIF} Index);
end;
procedure TQImport2.DoBeginImport;
begin
if Assigned(FOnBeforeImport) then FOnBeforeImport(Self);
end;
function TQImport2.DoBeforePost: boolean;
begin
Result := true;
if Assigned(FOnBeforePost) then FOnBeforePost(Self, FImportRow, Result);
end;
procedure TQImport2.DoAfterPost;
(*const
LF = {$IFDEF WIN32}#13#10{$ENDIF}{$IFDEF LINUX}#10{$ENDIF};
sINSERT = 'INSERT INTO %s (%s)' + LF + ' VALUES (%s);';
sUPDATE = 'UPDATE %s' + LF + 'SET %s ' + LF + 'WHERE %s;';
sDELETE = 'DELETE' + LF + 'FROM %s' + LF + 'WHERE %s;';*)
{var
i, j, k: integer;
str, str1, str2: string;
FField: TField;
IsNull: boolean;}
begin
{ if FSQLLog and Assigned(FSQL) then begin
k := 0;
str := EmptyStr;
str1 := EmptyStr;
str2 := EmptyStr;
for i := 0 to FImportRow.Count - 1 do begin
IsNull := false;
for j := 0 to Formats.NullValues.Count - 1 do begin
IsNull := AnsiCompareStr(FImportRow[i].Value, Formats.NullValues[j]) = 0;
if IsNull then Break;
end;
if IsNull then str2 := 'NULL'
else begin
str2 := FImportRow[i].Value;
FField := FDataSet.FindField(Row[i].Name);
if Assigned(FField) then
case FField.DataType of
ftString,
ftWideString,
ftFixedChar: str2 := AnsiQuotedStr(str2, '''');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -