multicolumns sort query result, vacuum opt

Add multicolumns sort
Vacuum opt and reindex opt
This commit is contained in:
levinsv 2020-04-11 20:55:49 +05:00
parent 4e01ba92c4
commit ae8ca667e7
10 changed files with 478 additions and 125 deletions

View file

@ -30,11 +30,12 @@ ctlSQLResult::ctlSQLResult(wxWindow *parent, pgConn *_conn, wxWindowID id, const
thread = NULL;
SetTable(new sqlResultTable(), true);
EnableEditing(false);
SetSizer(new wxBoxSizer(wxVERTICAL));
cg=GetGridLineColour();
Connect(wxID_ANY, wxEVT_GRID_RANGE_SELECT, wxGridRangeSelectEventHandler(ctlSQLResult::OnGridSelect));
Connect(wxID_ANY, wxEVT_GRID_COL_SORT, wxGridEventHandler(ctlSQLResult::OnGridColSort));
}
@ -179,7 +180,6 @@ void ctlSQLResult::DisplayData(bool single)
*/
wxGridTableMessage *msg;
sqlResultTable *table = (sqlResultTable *)GetTable();
msg = new wxGridTableMessage(table, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, GetNumberRows());
ProcessTableMessage(*msg);
@ -193,6 +193,7 @@ void ctlSQLResult::DisplayData(bool single)
msg = new wxGridTableMessage(table, wxGRIDTABLE_NOTIFY_COLS_APPENDED, thread->DataSet()->NumCols());
ProcessTableMessage(*msg);
delete msg;
table->initSort();
if (NumRows()<1000) {
for(int row = 0; row < NumRows(); ++row) {
if (row%2==0) {
@ -318,14 +319,17 @@ wxString ctlSQLResult::OnGetItemText(long item, long col) const
}
if (item >= 0)
{
thread->DataSet()->Locate(item + 1);
return thread->DataSet()->GetVal(col);
//thread->DataSet()->Locate(item + 1);
//return thread->DataSet()->GetVal(col);
sqlResultTable *t=(sqlResultTable *)GetTable();
return t->GetValueFast(item,col);
}
else
return thread->DataSet()->ColName(col);
}
return wxEmptyString;
}
wxString ctlSQLResult::SummaryColumn()
{
//ce=cells.Item(0);
@ -404,6 +408,11 @@ wxString ctlSQLResult::SummaryColumn()
result.Printf(wxT("%f"), sum);
return result;
}
void ctlSQLResult::OnGridColSort(wxGridEvent& event)
{
const int col = event.GetCol();
//m_table->Sort(col, !(m_grid->IsSortingBy(col) && m_grid->IsSortOrderAscending()));
}
void ctlSQLResult::OnGridSelect(wxGridRangeSelectEvent &event)
{
@ -413,7 +422,7 @@ void ctlSQLResult::ClearFilter()
{
size_t numRows = GetNumberRows();
int sizerow=GetDefaultRowSize();
for (int i = 0 ; i < numRows; i++)
for (size_t i = 0 ; i < numRows; i++)
{
if (GetRowSize(i)>0) continue;
//SetRowSize(i,sizerow);
@ -424,18 +433,23 @@ void ctlSQLResult::ClearFilter()
}
wxString ctlSQLResult::SetFilter(int row,int col,bool reverse)
{
wxString fltval=GetCellValue(row,col);
wxString result=wxEmptyString;
sqlResultTable *t=(sqlResultTable *)GetTable();
if (!(thread && thread->DataValid())) {result.Printf(wxT("Error thread not valid"));return result; }
//wxString fltval=GetCellValue(row,col);
wxString fltval=t->GetValueFast(row,col);
wxString text;
bool eq;
size_t numRows = GetNumberRows();
int all=0,show=0,hide=0;
for (int i = 0 ; i < numRows; i++)
for (size_t i = 0 ; i < numRows; i++)
{
//str.Append(GetExportLine(i, cols));
//SetRowSize(i,sizerow);
if (GetRowSize(i)==0) continue;
eq=(fltval==GetCellValue(i, col));
eq=(fltval==t->GetValueFast(i,col));
if (reverse) eq=!eq;
if (!eq) {
HideRow(i);
@ -445,18 +459,173 @@ wxString ctlSQLResult::SetFilter(int row,int col,bool reverse)
}
SetGridLineColour(wxColor(0,0,255));
wxString result;
result.Printf(wxT("Show rows:%d hide:%d all:%d"), show,hide,all);
return result;
}
#include <map>
int sqlResultTable::sortColumns()
{
bool no_sort=colsortnumber[0]==-1;
if (!maplines) {
maplines = new int[GetNumberRows()];
for(int i=0;i<GetNumberRows();i++) maplines[i]=i;
}
if (no_sort) {
for(int i=0;i<GetNumberRows();i++) maplines[i]=i;
use_map=true;
return 0;
}
use_map=false;
int *cols[MAX_COL_SORT];
for (int i=0;i<MAX_COL_SORT;i++) cols[i]=NULL;
int col;
for (int k=0;k<MAX_COL_SORT;k++) {
col=colsortnumber[k];
if (col!=-1) {
if (thread && thread->DataSet()->ColTypClass(col) == PGTYPCLASS_NUMERIC)
{
//sort numeric column
std::multimap<double, int> mp;
double d;
for (int i=0;i<GetNumberRows();i++) {
thread->DataSet()->Locate(i+1);
d=thread->DataSet()->GetDouble(col);
mp.insert(std::pair<double, int>(d, i));
}
std::multimap<double, int>::iterator it = mp.begin();
double prev;
int row;
int minval=0;
cols[k] = new int[GetNumberRows()];
for (int i = 0; it != mp.end(); it++, i++) { // âûâîäèì èõ
row=it->second; // row
if (i>0) if (prev!=it->first) minval++;
cols[k][row]=minval;
prev=it->first;
}
} else {
//no numeric sort as string
std::multimap<wxString, int> mp;
wxString s;
for (int i=0;i<GetNumberRows();i++) {
thread->DataSet()->Locate(i+1);
s=thread->DataSet()->GetVal(col);
mp.insert(std::pair<wxString, int>(s, i));
}
std::multimap<wxString, int>::iterator it = mp.begin();
wxString prev;
int row;
int minval=0;
cols[k] = new int[GetNumberRows()];
for (int i = 0; it != mp.end(); it++, i++) {
row=it->second; // row
if (i>0) if (prev!=it->first) minval++;
cols[k][row]=minval;
prev=it->first;
}
}
} else break;
}
class sorter {
int **cols;
sqlResultTable *m;
public:
sorter(int *_cols[], sqlResultTable *my_obj) { cols=_cols;m=my_obj;}
bool operator() (int i,int j) {
bool rez=true;
int a;
int b;
int col,ord;
for (int k=0;k<MAX_COL_SORT;k++) {
col=m->colsortnumber[k];
if (col!=-1) {
ord=m->colorder[col];
if (ord==0) continue;
a=cols[k][i];
b=cols[k][j];
if (a!=b) {
rez=a<b;
} else
{
rez=false;
continue;
}
if (ord==-1) rez=!rez;
}
break;
}
return rez;
}
};
std::sort(maplines,maplines+GetNumberRows(),sorter(cols,this));
for (int i=0;i<MAX_COL_SORT;i++) if (cols[i]!=NULL) delete cols[i];
use_map=true;
return 0;
}
int sqlResultTable::getSortColumn(int col)
{
return colorder[col];
}
int sqlResultTable::setSortColumn(int col)
{
if (col<GetNumberCols()) {
if (colorder[col]==0) colorder[col]=1;
else if (colorder[col]==1) colorder[col]=-1;
else if (colorder[col]==-1) colorder[col]=0; // no sort
int pos=-1,poszero=-1;
for (int i=0;i<MAX_COL_SORT;i++) if (colsortnumber[i]==col) {pos=i; break;}
else if (colsortnumber[i]==-1) { poszero=i; break;}
if (pos==-1 && poszero==-1) { colorder[col]=0; return 0;} // limit sort columns
if (pos==-1 && poszero!=-1) colsortnumber[poszero]=col; // add sort column
if (colorder[col]==0 && pos!=-1) {
// shift array (remove sort for column)
for (int i=pos;i<MAX_COL_SORT-1;i++) colsortnumber[i]=colsortnumber[i+1];
colsortnumber[MAX_COL_SORT-1]=-1;
}
sortColumns();
return colorder[col];
}
return 0;
}
int sqlResultTable::initSort()
{
if (colorder) delete [] colorder;
if (maplines) delete [] maplines;
maplines=NULL;
colorder = new int[GetNumberCols()];
for (int i=0;i<GetNumberCols();i++) colorder[i]=0;
for (int i=0;i<MAX_COL_SORT;i++) colsortnumber[i]=-1;
//maplines = new int[GetNumberRows()];
use_map=false;
return 0;
}
wxString sqlResultTable::GetValueFast(int row, int col)
{
wxString s;
if (thread && thread->DataValid())
{
if (col >= 0)
{
if (use_map) row=maplines[row];
thread->DataSet()->Locate(row + 1);
wxString s = thread->DataSet()->GetVal(col);
return s;
}
}
return "";
}
wxString sqlResultTable::GetValue(int row, int col)
{
if (thread && thread->DataValid())
{
if (col >= 0)
{
if (use_map) row=maplines[row];
thread->DataSet()->Locate(row + 1);
if (settings->GetIndicateNull() && thread->DataSet()->IsNull(col))
return wxT("<NULL>");
@ -508,6 +677,8 @@ wxString sqlResultTable::GetValue(int row, int col)
sqlResultTable::sqlResultTable()
{
thread = NULL;
colorder=NULL;
maplines=NULL;
}
int sqlResultTable::GetNumberRows()