File: frmExport.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (335 lines) | stat: -rw-r--r-- 7,727 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// frmExport.cpp - The export file dialogue
//
//////////////////////////////////////////////////////////////////////////



// App headers
#include "pgAdmin3.h"
#include <wx/file.h>
#include "frm/frmExport.h"
#include "utils/sysSettings.h"
#include "utils/misc.h"
#include "ctl/ctlSQLResult.h"

#define txtFilename     CTRL_TEXT("txtFilename")
#define btnOK           CTRL_BUTTON("wxID_OK")
#define rbUnicode       CTRL_RADIOBUTTON("rbUnicode")
#define rbLocal         CTRL_RADIOBUTTON("rbLocal")
#define rbCRLF          CTRL_RADIOBUTTON("rbCRLF")
#define rbLF            CTRL_RADIOBUTTON("rbLF")
#define rbQuoteStrings  CTRL_RADIOBUTTON("rbQuoteStrings")
#define rbQuoteAll      CTRL_RADIOBUTTON("rbQuoteAll")
#define rbQuoteNone     CTRL_RADIOBUTTON("rbQuoteNone")
#define chkColnames     CTRL_CHECKBOX("chkColnames")
#define cbColSeparator  CTRL_COMBOBOX("cbColSeparator")
#define cbQuoteChar     CTRL_COMBOBOX("cbQuoteChar")


BEGIN_EVENT_TABLE(frmExport, pgDialog)
	EVT_TEXT(XRCID("txtFilename"),          frmExport::OnChange)
	EVT_RADIOBUTTON(XRCID("rbQuoteNone"),   frmExport::OnChange)
	EVT_RADIOBUTTON(XRCID("rbQuoteStrings"), frmExport::OnChange)
	EVT_RADIOBUTTON(XRCID("rbQuoteAll"),    frmExport::OnChange)
	EVT_BUTTON(XRCID("btnFilename"),        frmExport::OnBrowseFile)
	EVT_BUTTON(wxID_HELP,                   frmExport::OnHelp)
	EVT_BUTTON(wxID_OK,                     frmExport::OnOK)
	EVT_BUTTON(wxID_CANCEL,                 frmExport::OnCancel)
END_EVENT_TABLE()



frmExport::frmExport(wxWindow *p)
{
	parent = p;

	SetFont(settings->GetSystemFont());
	LoadResource(p, wxT("frmExport"));
	RestorePosition();

	// Icon
	appearanceFactory->SetIcons(this);
	cbQuoteChar->Disable();
	btnOK->Disable();


	bool uc = settings->GetExportUnicode();
	rbUnicode->SetValue(uc);
	rbLocal->SetValue(!uc);

	bool isCrLf = settings->GetExportRowSeparator() == wxT("\r\n");
	rbCRLF->SetValue(isCrLf);
	rbLF->SetValue(!isCrLf);

	int qt = settings->GetExportQuoting();

	rbQuoteNone->SetValue(qt == 0);
	rbQuoteStrings->SetValue(qt == 1);
	rbQuoteAll->SetValue(qt == 2);

	cbColSeparator->SetValue(settings->GetExportColSeparator());


	cbQuoteChar->SetValue(settings->GetExportQuoteChar());

	wxString val;
	settings->Read(wxT("Export/LastFile"), &val, wxEmptyString);
	txtFilename->SetValue(val);

	wxCommandEvent ev;
	OnChange(ev);
}


frmExport::~frmExport()
{
	SavePosition();
}


void frmExport::OnHelp(wxCommandEvent &ev)
{
	DisplayHelp(wxT("export"), HELP_PGADMIN);
}


void frmExport::OnChange(wxCommandEvent &ev)
{
	cbQuoteChar->Enable(rbQuoteStrings->GetValue() || rbQuoteAll->GetValue());
	btnOK->Enable(!txtFilename->GetValue().IsEmpty() && !cbColSeparator->GetValue().IsEmpty());
}


void frmExport::OnOK(wxCommandEvent &ev)
{
	settings->SetExportUnicode(rbUnicode->GetValue());
	settings->SetExportRowSeparator(rbCRLF->GetValue() ? wxT("\r\n") : wxT("\n"));
	settings->SetExportColSeparator(cbColSeparator->GetValue());

	if (rbQuoteAll->GetValue())
		settings->SetExportQuoting(2);
	else if (rbQuoteStrings->GetValue())
		settings->SetExportQuoting(1);
	else
		settings->SetExportQuoting(0);

	settings->SetExportQuoteChar(cbQuoteChar->GetValue());

	settings->Write(wxT("Export/LastFile"), txtFilename->GetValue());


	if (IsModal())
		EndModal(wxID_OK);
	else
		Destroy();
}



bool frmExport::Export(pgSet *set)
{
	ctlSQLResult *grid = 0;
	if (!set)
	{
		wxLogInfo(wxT("Exporting data from the grid"));
		grid = (ctlSQLResult *)parent;
	}
	else
		wxLogInfo(wxT("Exporting data from a resultset"));

	wxFile file(txtFilename->GetValue(), wxFile::write);
	if (!file.IsOpened())
	{
		wxLogError(__("Failed to open file %s."), txtFilename->GetValue().c_str());
		return false;
	}

	wxString line;
	long skipped = 0;
	wxWX2MBbuf buf;

	int colCount, rowCount;

	if (set)
	{
		colCount = set->NumCols();
		rowCount = set->NumRows();
	}
	else
	{
		colCount = grid->GetNumberCols();
		rowCount = grid->NumRows();
	}

	int col;
	if (chkColnames->GetValue())
	{
		for (col = 0 ; col < colCount ; col++)
		{
			if (!col)
				line = wxEmptyString;
			else
				line += cbColSeparator->GetValue();

			if (rbQuoteStrings->GetValue() || rbQuoteAll->GetValue())
			{
				wxString qc = cbQuoteChar->GetValue();

				wxString hdr;
				if (set)
					hdr = set->ColName(col);
				else
					hdr = grid->OnGetItemText(-1, col + 1).BeforeFirst('\n');

				hdr.Replace(qc, qc + qc);
				line += qc + hdr + qc;
			}
			else
			{
				if (set)
					line += set->ColName(col);
				else
					line += grid->OnGetItemText(-1, col + 1).BeforeFirst('\n');
			}
		}
		if (rbCRLF->GetValue())
			line += wxT("\r\n");
		else
			line += wxT("\n");

		if (rbUnicode->GetValue())
			file.Write(line, wxConvUTF8);
		else
		{
			buf = line.mb_str(wxConvLibc);
			if (!buf)
				skipped++;
			else
				file.Write(line, wxConvLibc);
		}
	}


	wxString text;
	OID typOid;

	int row;
	for (row = 0 ; row < rowCount ; row++)
	{
		for (col = 0 ; col < colCount ; col++)
		{
			if (!col)
				line = wxEmptyString;
			else
				line += cbColSeparator->GetValue();

			bool needQuote = rbQuoteAll->GetValue();

			if (set)
			{
				text = set->GetVal(col);
				typOid = set->ColTypClass(col);
			}
			else
			{
				text = grid->OnGetItemText(row, col + 1);
				typOid = grid->colTypClasses[col];
			}

			if (!needQuote && rbQuoteStrings->GetValue())
			{
				// find out if string
				switch (typOid)
				{
					case PGTYPCLASS_NUMERIC:
					case PGTYPCLASS_BOOL:
						break;
					default:
						needQuote = true;
						break;
				}
			}
			if (needQuote)
			{
				wxString qc = cbQuoteChar->GetValue();
				text.Replace(qc, qc + qc);
				line += qc + text + qc;
			}
			else
				line += text;
		}
		if (rbCRLF->GetValue())
			line += wxT("\r\n");
		else
			line += wxT("\n");

		if (rbUnicode->GetValue())
			file.Write(line, wxConvUTF8);
		else
		{
			buf = line.mb_str(wxConvLibc);
			if (!buf)
				skipped++;
			else
				file.Write(line, wxConvLibc);
		}

		if (set)
			set->MoveNext();
	}
	file.Close();

	if (skipped)
		wxLogError(wxPLURAL(
		               "Data export incomplete.\n\n%d row contained characters that could not be converted to the local charset.\n\nPlease correct the data or try using UTF8 instead.",
		               "Data export incomplete.\n\n%d rows contained characters that could not be converted to the local charset.\n\nPlease correct the data or try using UTF8 instead.",
		               skipped), skipped);
	else
		wxMessageBox(_("Data export completed successfully."), _("Export data"), wxICON_INFORMATION | wxOK);

	return true;
}


void frmExport::OnCancel(wxCommandEvent &ev)
{
	if (IsModal())
		EndModal(wxID_CANCEL);
	else
		Destroy();
}

void frmExport::OnBrowseFile(wxCommandEvent &ev)
{
	wxString directory;
	wxString filename;

	if (txtFilename->GetValue().IsEmpty())
		directory = wxGetHomeDir();
	else
	{
		directory = wxFileName(txtFilename->GetValue()).GetPath();
		filename = wxFileName(txtFilename->GetValue()).GetFullName();
	}

#ifdef __WXMSW__
	wxFileDialog file(this, _("Select export filename"), directory, filename,
	                  _("CSV files (*.csv)|*.csv|Data files (*.dat)|*.dat|All files (*.*)|*.*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
#else
	wxFileDialog file(this, _("Select export filename"), directory, filename,
	                  _("CSV files (*.csv)|*.csv|Data files (*.dat)|*.dat|All files (*)|*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
#endif

	if (file.ShowModal() == wxID_OK)
	{
		txtFilename->SetValue(file.GetPath());
		OnChange(ev);
	}
}