Added copying of selected cells IN list format.

Результаты запроса могут скопированы в формате In списка и в формате условия WHERE.
Копирование выполняется через контекстное меню.
This commit is contained in:
lsv 2020-09-02 13:52:23 +05:00 committed by levinsv
parent ec1535f7a1
commit 428ceaa70f
5 changed files with 148 additions and 46 deletions

View file

@ -217,10 +217,12 @@ wxString ctlSQLGrid::GetExportLine(int row, wxArrayInt cols)
if (GetNumberCols() == 0 || GetRowSize(row)==0)
return str;
wxString colsep=settings->GetCopyColSeparator();
if (generatesql) colsep=wxT(",");
if (generatesql == 2) colsep=wxT(",");
if (generatesql == 3) colsep = wxT(" and ");
wxString qtsimbol=settings->GetCopyQuoteChar();
if (generatesql) qtsimbol=wxT("'");
if (generatesql>0) qtsimbol=wxT("'");
wxString head=wxT("insert into tbl(");
if (cols.Count()>1 && generatesql > 1) str.Append("(");
for (col = 0 ; col < cols.Count() ; col++)
{
if (col > 0)
@ -228,6 +230,7 @@ wxString ctlSQLGrid::GetExportLine(int row, wxArrayInt cols)
if (col > 0) head.Append(colsep);
head=head+GetColumnName(cols[col]);
wxString text = GetCellValue(row, cols[col]);
wxString cname = GetColumnName(cols[col]);
bool needQuote = false;
if (settings->GetCopyQuoting() == 1)
{
@ -236,22 +239,29 @@ wxString ctlSQLGrid::GetExportLine(int row, wxArrayInt cols)
else if (settings->GetCopyQuoting() == 2)
/* Quote everything */
needQuote = true;
if (text.Length()==0&&generatesql) {needQuote = false;} else
if (generatesql) needQuote = IsColText(cols[col]);
if (text.Length()==0&&generatesql>0) {needQuote = false;} else
if (generatesql>0) needQuote = IsColText(cols[col]);
if (generatesql > 0) {
if (text.Length() != 0) {
text.Replace(wxT("'"), wxT("''"));
}
else text = wxT("null");
}
if (generatesql == 3) if (text == "null") str.Append(cname).Append(" is "); else
str.Append(cname).Append("=");
if (needQuote)
str.Append(qtsimbol);
if (generatesql) {
if (text.Length()!=0) {
text.Replace(wxT("'"),wxT("''"));
} else text=wxT("null");
}
str.Append(text);
if (needQuote)
str.Append(qtsimbol);
}
if (generatesql) str=head+wxT(") values (")+str+");";
if (cols.Count() > 1 && generatesql > 1) str.Append(")");
if (generatesql == 1) str=head+wxT(") values (")+str+");";
return str;
}
@ -278,35 +288,54 @@ void ctlSQLGrid::AppendColumnHeader(wxString &str, int start, int end)
void ctlSQLGrid::AppendColumnHeader(wxString &str, wxArrayInt columns)
{
if(settings->GetColumnNames()&&!generatesql)
if(settings->GetColumnNames()|| generatesql == 2)
{
bool CopyQuoting = (settings->GetCopyQuoting() == 1 || settings->GetCopyQuoting() == 2);
size_t i;
wxString fielddelim = ",";
if (generatesql == 3) fielddelim = " And ";
if (generatesql == 1) return;
for(i = 0; i < columns.Count() ; i++)
{
long columnPos = columns.Item(i);
if(i > 0)
str.Append(settings->GetCopyColSeparator());
if (generatesql == 2) {
if (i > 0) str.Append(fielddelim);
if (i == 0 && columns.Count() > 1) str.Append("(");
str.Append(GetColumnName(columnPos));
if (i == columns.Count()-1 && columns.Count() > 1 && generatesql == 2) str.Append(") in (");
if (columns.Count() == 1 && generatesql == 2) str.Append(" in (");
if(CopyQuoting)
str.Append(settings->GetCopyQuoteChar());
str.Append(GetColumnName(columnPos));
if(CopyQuoting)
str.Append(settings->GetCopyQuoteChar());
}
else
{
if (i > 0)
str.Append(settings->GetCopyColSeparator());
if (CopyQuoting)
str.Append(settings->GetCopyQuoteChar());
str.Append(GetColumnName(columnPos));
if (CopyQuoting)
str.Append(settings->GetCopyQuoteChar());
}
}
str.Append(END_OF_LINE);
}
}
int ctlSQLGrid::Copy(bool gensql)
int compare_int(int* a, int* b)
{
wxString str,tmp;
if (*a > * b) return 1;
else if (*a < *b) return -1;
else return 0;
}
int ctlSQLGrid::Copy(int gensql)
{
wxString str,tmp,linedelim="";
int copied = 0;
size_t i;
generatesql=gensql;
if (gensql == 2) linedelim = ",";
else if(gensql == 3) linedelim = " or ";
if (GetSelectedRows().GetCount())
{
@ -319,6 +348,7 @@ int ctlSQLGrid::Copy(bool gensql)
tmp=GetExportLine(rows.Item(i));
if (tmp.IsEmpty()) continue;
str.Append(tmp);
if (i < rows.GetCount() - 1 && (generatesql > 1)) str.Append(linedelim);
if (rows.GetCount() > 1)
str.Append(END_OF_LINE);
}
@ -335,48 +365,93 @@ int ctlSQLGrid::Copy(bool gensql)
for (i = 0 ; i < numRows ; i++)
{
str.Append(GetExportLine(i, cols));
if (i<(numRows-1) && (generatesql > 1)) str.Append(linedelim);
if (numRows > 1)
str.Append(END_OF_LINE);
}
copied = numRows;
}
else if (GetSelectionBlockTopLeft().GetCount() > 0 &&
GetSelectionBlockBottomRight().GetCount() > 0)
{
unsigned int x1, x2, y1, y2;
int count = GetSelectionBlockTopLeft().GetCount();
x1 = GetSelectionBlockTopLeft()[0].GetCol();
x2 = GetSelectionBlockBottomRight()[0].GetCol();
y1 = GetSelectionBlockTopLeft()[0].GetRow();
y2 = GetSelectionBlockBottomRight()[0].GetRow();
copied = 0;
AppendColumnHeader(str, x1, x2);
for (i = y1; i <= y2; i++)
for (size_t n = 0; n < count; n++)
{
str.Append(GetExportLine(i, x1, x2));
x1 = GetSelectionBlockTopLeft()[n].GetCol();
x2 = GetSelectionBlockBottomRight()[n].GetCol();
y1 = GetSelectionBlockTopLeft()[n].GetRow();
y2 = GetSelectionBlockBottomRight()[n].GetRow();
if (y2 > y1)
str.Append(END_OF_LINE);
for (i = y1; i <= y2; i++)
{
str.Append(GetExportLine(i, x1, x2));
if (i < y2 && (generatesql > 1)) str.Append(linedelim);
if (y2 > y1)
str.Append(END_OF_LINE);
}
copied = copied+(y2 - y1 + 1);
}
copied = y2 - y1 + 1;
}
else
{
int row, col;
if (generatesql > 1) {
wxGridCellCoordsArray cord = GetSelectedCells();
int count = cord.GetCount();
int curr_row = 1000000000;
int next_row = 1000000000;
for (size_t n = 0; n < count; n++)
{
wxGridCellCoords& coords = cord[n];
if (coords.GetRow() < curr_row) curr_row = coords.GetRow();
}
while (curr_row != 1000000000)
{
wxArrayInt colsNum;
next_row = 1000000000;
for (size_t n = 0; n < count; n++)
{
wxGridCellCoords& coords = cord[n];
//wxString msg;
//msg.Printf(wxT("Êîîðäèíàòû âûáðàíûõ ÿ÷ååê row=%d col=%d.\n"), coords.GetRow(), coords.GetCol());
//wxMessageBox(msg, wxT("About coord"), wxOK | wxICON_INFORMATION, this);
int r = coords.GetRow();
if (r == curr_row) {
colsNum.Add(coords.GetCol());
}
else if (r > curr_row && r < next_row) next_row = r;
row = GetGridCursorRow();
col = GetGridCursorCol();
}
colsNum.Sort(compare_int);
if (generatesql == 2) AppendColumnHeader(str, colsNum);
str.Append(GetExportLine(curr_row, colsNum));
if ((next_row != 1000000000) && (generatesql == 3)) str.Append(linedelim);
if ((next_row != 1000000000) && (generatesql == 2)) str.Append(") or ");
str.Append(END_OF_LINE);
curr_row = next_row;
copied++;
}
}
else {
row = GetGridCursorRow();
col = GetGridCursorCol();
AppendColumnHeader(str, col, col);
AppendColumnHeader(str, col, col);
str.Append(GetExportLine(row, col, col));
copied = 1;
str.Append(GetExportLine(row, col, col));
copied = 1;
}
}
if (copied && (generatesql == 2)) str.Append(")");
if (copied && wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxTextDataObject(str));

View file

@ -121,6 +121,8 @@ BEGIN_EVENT_TABLE(frmQuery, pgFrame)
EVT_MENU(MNU_CLEAR, frmQuery::OnClear)
EVT_MENU(MNU_SUMMARY_COL, frmQuery::OnSummary_Column)
EVT_MENU(MNU_COPY_INSERT, frmQuery::OnCopy_Insert)
EVT_MENU(MNU_COPY_INLIST, frmQuery::OnCopy_InList)
EVT_MENU(MNU_COPY_WHERELIST, frmQuery::OnCopy_WhereList)
EVT_MENU(MNU_CLEAR_FILTER, frmQuery::OnClear_Filter)
EVT_MENU(MNU_FIND, frmQuery::OnSearchReplace)
EVT_MENU(MNU_UNDO, frmQuery::OnUndo)
@ -1544,7 +1546,7 @@ void frmQuery::OnCopy(wxCommandEvent &ev)
{
if (obj == sqlResult)
{
sqlResult->Copy(false);
sqlResult->Copy(0);
break;
}
obj = obj->GetParent();
@ -2011,6 +2013,9 @@ void frmQuery::OnLabelRightClick(wxGridEvent &event)
xmenu->Append(MNU_DELETE, _("&Delete"), _("Delete selected rows."));
xmenu->Append(MNU_SUMMARY_COL, _("Summary"), _("Summary selected cells."));
xmenu->Append(MNU_COPY_INSERT, _("Copy Insert format"), _("Copy Insert format."));
xmenu->Append(MNU_COPY_INLIST, _("IN LIST format copy"), _("Copy In list format."));
xmenu->Append(MNU_COPY_WHERELIST, _("WHERE LIST format copy"), _("Copy where list format."));
xmenu->Append(MNU_CLEAR_FILTER, _("Clear filter"), _("Clear filter"));
if ((rows.GetCount()))
@ -2033,11 +2038,29 @@ void frmQuery::OnCopy_Insert(wxCommandEvent &ev)
// if (currentControl() == sqlResult)
{
wxString s=wxT("Insert into format copy buffer.");
sqlResult->Copy(true);
sqlResult->Copy(1);
SetStatusText(s, STATUSPOS_POS);
}
}
void frmQuery::OnCellLeftDClick(wxGridEvent &event)
void frmQuery::OnCopy_InList(wxCommandEvent& ev)
{
// if (currentControl() == sqlResult)
{
wxString s = wxT("In list format copy buffer.");
sqlResult->Copy(2);
SetStatusText(s, STATUSPOS_POS);
}
}
void frmQuery::OnCopy_WhereList(wxCommandEvent& ev)
{
// if (currentControl() == sqlResult)
{
wxString s = wxT("Where list format copy buffer.");
sqlResult->Copy(3);
SetStatusText(s, STATUSPOS_POS);
}
}
void frmQuery::OnCellLeftDClick(wxGridEvent &event)
{
int row=event.GetRow();
int col=event.GetCol();

View file

@ -30,7 +30,7 @@ public:
{
return false;
}
int Copy(bool gensql);
int Copy(int gensql);
virtual bool CheckRowPresent(int row)
{
@ -55,7 +55,7 @@ public:
wxString GetRowLabelValue( int row );
void SetRowGroup(int row);
GroupRows *grp;
bool generatesql;
int generatesql; // 0 -íåò, 1 - insert , 2 - in_list
WX_DECLARE_STRING_HASH_MAP( int, ColKeySizeHashMap );
DECLARE_DYNAMIC_CLASS(ctlSQLGrid)

View file

@ -212,6 +212,8 @@ private:
void OnClear(wxCommandEvent &event);
void OnSummary_Column(wxCommandEvent &event);
void OnCopy_Insert(wxCommandEvent &event);
void OnCopy_InList(wxCommandEvent& event);
void OnCopy_WhereList(wxCommandEvent& event);
void OnClear_Filter(wxCommandEvent &event);
void OnSearchReplace(wxCommandEvent &event);
void OnUndo(wxCommandEvent &event);

View file

@ -77,6 +77,8 @@ enum
MNU_AUTOSELECTQUERY,
MNU_SUMMARY_COL,
MNU_COPY_INSERT,
MNU_COPY_INLIST,
MNU_COPY_WHERELIST,
MNU_CLEAR_FILTER,
MNU_AUTOROLLBACK,
MNU_AUTOCOMMIT,