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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// frmImport.cpp - Import database dialogue
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
#include <wx/settings.h>
#include <wx/filepicker.h>
// App headers
#include "pgAdmin3.h"
#include "db/pgConn.h"
#include "frm/frmMain.h"
#include "frm/frmImport.h"
#include "utils/sysLogger.h"
#include "schema/pgSchema.h"
#include "schema/pgTable.h"
#include "schema/pgColumn.h"
// Icons
#include "images/reload.pngc"
#define nbNotebook CTRL_NOTEBOOK("nbNotebook")
#define pickerImportfile CTRL_FILEPICKER("pickerImportfile")
#define cbFormat CTRL_COMBOBOX("cbFormat")
#define cbEncoding CTRL_COMBOBOX("cbEncoding")
#define chkOids CTRL_CHECKBOX("chkOids")
#define chkHeader CTRL_CHECKBOX("chkHeader")
#define cbDelimiter CTRL_COMBOBOX("cbDelimiter")
#define cbQuote CTRL_COMBOBOX("cbQuote")
#define cbEscape CTRL_COMBOBOX("cbEscape")
#define txtNull CTRL_TEXT("txtNull")
#define gauge CTRL_GAUGE("gauge")
#define lstColumnsToImport CTRL_CHECKLISTBOX("lstColumnsToImport")
#define lstIgnoreForColumns CTRL_CHECKLISTBOX("lstIgnoreForColumns")
BEGIN_EVENT_TABLE(frmImport, pgDialog)
EVT_COMBOBOX(XRCID("cbFormat"), frmImport::OnChangeFormat)
EVT_BUTTON(wxID_OK, frmImport::OnOK)
EVT_BUTTON (wxID_HELP, frmImport::OnHelp)
END_EVENT_TABLE()
frmImport::frmImport(frmMain *form, pgObject *_object, pgConn *_conn)
{
// Initialize variables
connection = _conn;
object = _object;
done = false;
// Set-up window
SetFont(settings->GetSystemFont());
LoadResource(form, wxT("frmImport"));
RestorePosition();
SetTitle(wxT("Import data from file into ") + object->GetName());
SetIcon(*reload_png_ico);
// Fill the encoding combobox
long encNo = 0;
wxString encStr;
cbEncoding->Append(wxT(""));
do
{
encStr = object->GetConnection()->ExecuteScalar(
wxT("SELECT pg_encoding_to_char(") + NumToStr(encNo) + wxT(")"));
if (pgConn::IsValidServerEncoding(encNo) && !encStr.IsEmpty())
cbEncoding->Append(encStr);
encNo++;
}
while (!encStr.IsEmpty());
cbEncoding->SetSelection(0);
// Fill the columns checklistboxes
wxCookieType cookie;
pgObject *data = 0;
wxTreeItemId columnsItem;
wxTreeItemId item = form->GetBrowser()->GetFirstChild(object->GetId(), cookie);
while (item)
{
data = form->GetBrowser()->GetObject(item);
pgaFactory *factory = data->GetFactory();
if (factory == columnFactory.GetCollectionFactory())
columnsItem = item;
if (data->GetMetaType() == PGM_COLUMN && data->IsCollection())
columnsItem = item;
if (columnsItem)
break;
item = form->GetBrowser()->GetNextChild(object->GetId(), cookie);
}
if (columnsItem)
{
pgCollection *coll = (pgCollection *)data;
coll->ShowTreeDetail(form->GetBrowser());
item = form->GetBrowser()->GetFirstChild(columnsItem, cookie);
while (item)
{
data = form->GetBrowser()->GetObject(item);
if (data->IsCreatedBy(columnFactory))
{
pgColumn *column = (pgColumn *)data;
column->ShowTreeDetail(form->GetBrowser());
if (column->GetColNumber() > 0)
{
lstColumnsToImport->Append(column->GetName());
lstIgnoreForColumns->Append(column->GetName());
}
}
item = form->GetBrowser()->GetNextChild(columnsItem, cookie);
}
for (unsigned int x = 0; x < lstColumnsToImport->GetCount(); x++)
lstColumnsToImport->Check(x, true);
}
// Fix some widgets
wxCommandEvent ev;
OnChangeFormat(ev);
}
frmImport::~frmImport()
{
SavePosition();
}
void frmImport::OnHelp(wxCommandEvent &ev)
{
DisplayHelp(wxT("sql-copy"), HELP_POSTGRESQL);
}
void frmImport::OnChangeFormat(wxCommandEvent &ev)
{
bool enabled = cbFormat->GetValue() == wxT("csv");
chkHeader->Enable(enabled);
cbQuote->Enable(enabled);
cbEscape->Enable(enabled);
lstIgnoreForColumns->Enable(enabled);
}
void frmImport::OnOK(wxCommandEvent &ev)
{
wxString query = wxEmptyString;
wxString columnsToImport = wxEmptyString;
wxString columnsToIgnoreForNulls = wxEmptyString;
bool allColumnsToImport = true;
bool someColumnsToIgnoreForNulls = false;
wxFileName fn;
wxFile *file;
bool copydone = false;
bool goterror = false;
int nCount = 8192;
if (!done)
{
// Check checklistboxes
for (unsigned int x = 0; x < lstColumnsToImport->GetCount(); x++)
{
if (lstColumnsToImport->IsChecked(x))
{
if (!columnsToImport.IsEmpty())
{
columnsToImport.Append(wxT(","));
}
columnsToImport.Append(qtIdent(lstColumnsToImport->GetString(x)));
}
else
{
allColumnsToImport = false;
}
}
for (unsigned int x = 0; x < lstIgnoreForColumns->GetCount(); x++)
{
if (lstIgnoreForColumns->IsChecked(x))
{
if (!columnsToIgnoreForNulls.IsEmpty())
{
columnsToIgnoreForNulls.Append(wxT(","));
}
columnsToIgnoreForNulls.Append(qtIdent(lstIgnoreForColumns->GetString(x)));
someColumnsToIgnoreForNulls = true;
}
}
// Check at least one column is selected else raise a warning
if (!allColumnsToImport && columnsToImport.Length() <= 0)
{
wxMessageBox(_("Please select at least one column to import."), _("Import"), wxICON_WARNING | wxOK, this);
return;
}
// Build COPY query
query = wxT("COPY ") + object->GetSchema()->GetQuotedIdentifier() + wxT(".") + object->GetQuotedIdentifier();
if (!allColumnsToImport)
{
query += wxT("(") + columnsToImport + wxT(")");
}
query += wxT(" FROM STDIN ");
if (connection->BackendMinimumVersion(9, 0))
{
query += wxT("(");
query += wxT("FORMAT '") + cbFormat->GetValue() + wxT("'");
if (chkOids->GetValue())
query += wxT(", OIDS");
if (cbDelimiter->GetValue() == wxT("[tab]"))
query += wxT(", DELIMITER E'\\t'");
else if (!cbDelimiter->GetValue().IsEmpty())
query += wxT(", DELIMITER ") + connection->qtDbString(cbDelimiter->GetValue());
if (!txtNull->GetValue().IsEmpty())
query += wxT(", NULL ") + connection->qtDbString(txtNull->GetValue());
if (cbFormat->GetValue() == wxT("csv"))
{
if (chkHeader->GetValue())
query += wxT(", HEADER");
if (!cbQuote->GetValue().IsEmpty())
query += wxT(", QUOTE ") + connection->qtDbString(cbQuote->GetValue());
if (!cbEscape->GetValue().IsEmpty())
query += wxT(", ESCAPE ") + connection->qtDbString(cbEscape->GetValue());
}
if (connection->BackendMinimumVersion(9, 1) && !cbEncoding->GetValue().IsEmpty())
query += wxT(", ENCODING ") + connection->qtDbString(cbEncoding->GetValue());
if (cbFormat->GetValue() == wxT("csv"))
{
if (someColumnsToIgnoreForNulls)
query += wxT(", FORCE_NOT_NULL (") + columnsToIgnoreForNulls + wxT(")");
}
query += wxT(")");
}
else
{
query += wxT("WITH ");
if (cbFormat->GetValue() == wxT("binary"))
query += wxT("BINARY ");
if (chkOids->GetValue())
query += wxT("OIDS ");
if (!cbDelimiter->GetValue().IsEmpty())
query += wxT("DELIMITER ") + connection->qtDbString(cbDelimiter->GetValue());
if (!txtNull->GetValue().IsEmpty())
query += wxT("NULL ") + connection->qtDbString(txtNull->GetValue());
if (cbFormat->GetValue() == wxT("csv"))
{
query += wxT("CSV ");
if (connection->BackendMinimumVersion(8, 0) && chkHeader->GetValue())
query += wxT("HEADER ");
if (connection->BackendMinimumVersion(8, 0) && !cbQuote->GetValue().IsEmpty())
query += wxT("QUOTE ") + connection->qtDbString(cbQuote->GetValue());
if (connection->BackendMinimumVersion(8, 0) && !cbEscape->GetValue().IsEmpty())
query += wxT("ESCAPE ") + connection->qtDbString(cbEscape->GetValue());
if (someColumnsToIgnoreForNulls)
query += wxT("FORCE NOT NULL ") + columnsToIgnoreForNulls + wxT(" ");
}
}
// Check CSV file
if (!wxFileName::FileExists(pickerImportfile->GetPath()))
{
wxString msg;
msg.Printf(_("The file %s doesn't exist.\nPlease select a valid file."), pickerImportfile->GetPath().c_str());
wxLogError(msg);
return;
}
// Open CSV file
fn = wxFileName(pickerImportfile->GetPath());
file = new wxFile(fn.GetFullPath(), wxFile::read);
// Set range for gauge
gauge->SetRange(file->Length());
// Start COPY
if (!connection->StartCopy(query))
{
goterror = true;
}
// Send COPY data
while (!copydone && !goterror)
{
// Read a buffer
char *buffer = new char[nCount + 4];
memset(buffer, 0, nCount + 4);
off_t len = file->Read(buffer, nCount);
// If we read something, send it to the server
if (len > 0)
{
if (!connection->PutCopyData(buffer, len))
{
goterror = true;
}
else
{
gauge->SetValue(gauge->GetValue() + len);
Update();
}
}
// If we read nothing, we're done
copydone = len == 0;
// Clean the buffer
delete[] buffer;
}
// Close CSV file
file->Close();
// End COPY
if (goterror)
{
connection->EndPutCopy(_("Copy failed!"));
}
else
{
goterror = !connection->EndPutCopy(wxT(""));
}
// Get final status of the COPY command
if (!connection->GetCopyFinalStatus())
{
goterror = true;
}
if (goterror)
{
wxLogError(_("Copy failed!\n") + connection->GetLastError());
}
else
{
btnOK->SetLabel(wxT("Done"));
done = true;
}
}
else
{
Close();
}
}
importFactory::importFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
mnu->Append(id, _("&Import..."), _("Import CSV file into a relation"));
}
wxWindow *importFactory::StartDialog(frmMain *form, pgObject *obj)
{
pgDatabase *db = obj->GetDatabase();
wxString applicationname = appearanceFactory->GetLongAppName() + _(" - Import Tool");
pgConn *conn = db->CreateConn(applicationname);
if (conn)
{
frmImport *frm = new frmImport(form, obj, conn);
frm->Show();
}
return 0;
}
bool importFactory::CheckEnable(pgObject *obj)
{
return obj && obj->IsCreatedBy(tableFactory);;
}
|