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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// gpExtTable.cpp - Greenplum External Table
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
// App headers
#include "pgAdmin3.h"
#include "utils/misc.h"
#include "schema/pgColumn.h"
#include "schema/gpExtTable.h"
#include "frm/frmHint.h"
gpExtTable::gpExtTable(pgSchema *newSchema, const wxString &newName)
: pgSchemaObject(newSchema, extTableFactory, newName)
{
}
gpExtTable::~gpExtTable()
{
}
wxString gpExtTable::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on external table");
message += wxT(" ") + GetName();
break;
case REFRESHINGDETAILS:
message = _("Refreshing external table");
message += wxT(" ") + GetName();
break;
case DROPINCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop external table \"%s\" including all objects that depend on it?"),
GetFullIdentifier().c_str());
break;
case DROPEXCLUDINGDEPS:
message = wxString::Format(_("Are you sure you wish to drop external table \"%s\"?"),
GetFullIdentifier().c_str());
break;
case DROPCASCADETITLE:
message = _("Drop external table cascaded?");
break;
case DROPTITLE:
message = _("Drop external table?");
break;
case PROPERTIESREPORT:
message = _("External table properties report");
message += wxT(" - ") + GetName();
break;
case PROPERTIES:
message = _("External table properties");
break;
case DDLREPORT:
message = _("External table DDL report");
message += wxT(" - ") + GetName();
break;
case DDL:
message = _("External table DDL");
break;
case DEPENDENCIESREPORT:
message = _("External table dependencies report");
message += wxT(" - ") + GetName();
break;
case DEPENDENCIES:
message = _("External table dependencies");
break;
case DEPENDENTSREPORT:
message = _("External table dependents report");
message += wxT(" - ") + GetName();
break;
case DEPENDENTS:
message = _("External table dependents");
break;
}
return message;
}
bool gpExtTable::IsUpToDate()
{
wxString sql = wxT("SELECT xmin FROM pg_class WHERE oid = ") + this->GetOidStr();
if (!this->GetDatabase()->GetConnection() || this->GetDatabase()->ExecuteScalar(sql) != NumToStr(GetXid()))
return false;
else
return true;
}
wxMenu *gpExtTable::GetNewMenu()
{
wxMenu *menu = pgObject::GetNewMenu();
if (schema->GetCreatePrivilege())
schemaFactory.AppendMenu(menu);
return menu;
}
bool gpExtTable::DropObject(wxFrame *frame, ctlTree *browser, bool cascaded)
{
wxString sql = wxT("DROP EXTERNAL TABLE ") + this->GetSchema()->GetQuotedIdentifier() + wxT(".") + this->GetQuotedIdentifier();
if (cascaded)
sql += wxT(" CASCADE");
return GetDatabase()->ExecuteVoid(sql);
}
wxString gpExtTable::GetSql(ctlTree *browser)
{
wxString colDetails;
wxString prevComment;
wxString q;
if (sql.IsNull())
{
sql = wxT("-- External Table: ") + GetQuotedFullIdentifier() + wxT("\n\n")
+ wxT("-- DROP EXTERNAL TABLE ") + GetQuotedFullIdentifier() + wxT(";\n\n");
/* Now get required information from pg_exttable */
if (GetDatabase()->BackendMinimumVersion(8, 2, 5))
{
q += wxT(
"SELECT x.location, x.fmttype, x.fmtopts, x.command, ")
wxT("x.rejectlimit, x.rejectlimittype,")
wxT("(SELECT relname ")
wxT("FROM pg_class ")
wxT("WHERE Oid=x.fmterrtbl) AS errtblname, ")
wxT("pg_catalog.pg_encoding_to_char(x.encoding) ")
wxT("FROM pg_catalog.pg_exttable x, pg_catalog.pg_class c ")
wxT("WHERE x.reloid = c.oid AND c.oid = ") + GetOidStr();
}
else
{
/* not SREH and encoding colums yet */
q += wxT(
"SELECT x.location, x.fmttype, x.fmtopts, x.command, ")
wxT("-1 as rejectlimit, null as rejectlimittype,")
wxT("null as errtblname, ")
wxT("null as encoding ")
wxT("FROM pg_catalog.pg_exttable x, pg_catalog.pg_class c ")
wxT("WHERE x.reloid = c.oid AND c.oid = ") + GetOidStr();
}
pgSet *extTable = GetDatabase()->ExecuteSet(q);
wxString locations = extTable->GetVal(0);
wxString fmttype = extTable->GetVal(1);
wxString fmtopts = extTable->GetVal(2);
wxString command = extTable->GetVal(3);
wxString rejlim = extTable->GetVal(4);
wxString rejlimtype = extTable->GetVal(5);
wxString errtblname = extTable->GetVal(6);
wxString extencoding = extTable->GetVal(7);
if ((command.Length() > 0) ||
(locations.Mid(1, 4) == wxT("http")))
{
sql += wxT("CREATE EXTERNAL WEB TABLE ") +
GetQuotedFullIdentifier() + wxT("\n(\n");
}
else
{
sql += wxT("CREATE EXTERNAL TABLE ") +
GetQuotedFullIdentifier() + wxT("\n(\n");
}
// Get the columns
pgCollection *columns = browser->FindCollection(columnFactory, GetId());
if (columns)
{
columns->ShowTreeDetail(browser);
treeObjectIterator colIt1(browser, columns);
treeObjectIterator colIt2(browser, columns);
int lastRealCol = 0;
int currentCol = 0;
pgColumn *column;
// Iterate the columns to find the last 'real' one
while ((column = (pgColumn *)colIt1.GetNextObject()) != 0)
{
currentCol++;
if (column->GetInheritedCount() == 0)
lastRealCol = currentCol;
}
// Now build the actual column list
int colCount = 0;
while ((column = (pgColumn *)colIt2.GetNextObject()) != 0)
{
column->ShowTreeDetail(browser);
if (column->GetColNumber() > 0)
{
if (colCount)
{
// Only add a comma if this isn't the last 'real' column
if (colCount != lastRealCol)
sql += wxT(",");
if (!prevComment.IsEmpty())
sql += wxT(" -- ") + firstLineOnly(prevComment);
sql += wxT("\n");
}
/* if (column->GetInheritedCount() > 0)
{
if (!column->GetIsLocal())
sql += wxString::Format(wxT("-- %s "), _("Inherited"))
+ wxT("from table ") + column->GetInheritedTableName() + wxT(":");
}*/
sql += wxT(" ") + column->GetQuotedIdentifier() + wxT(" ")
+ column->GetDefinition();
prevComment = column->GetComment();
// Whilst we are looping round the columns, grab their comments as well.
// Perhaps we should also get storage types here?
colDetails += column->GetCommentSql();
if (colDetails.Length() > 0)
if (colDetails.Last() != '\n') colDetails += wxT("\n");
colCount++;
}
}
}
if (!prevComment.IsEmpty())
sql += wxT(" -- ") + firstLineOnly(prevComment);
sql += wxT("\n)\n");
if(command.Length() > 0)
{
wxString on_clause = locations;
/* remove curly braces */
on_clause = locations.Mid(1, locations.Length() - 2);
/* add EXECUTE clause */
sql += wxT(" EXECUTE E'");
for (size_t i = 0; i < command.Length(); i++)
{
if (command[i] == wxT('\\')) sql += wxT('\\');
if (command[i] == wxT('\'')) sql += wxT('\'');
sql += command[i];
}
sql += wxT("' ");
/* add ON clause */
wxString temp;
if(on_clause.StartsWith(wxT("HOST:"), &temp))
{
sql += wxT("ON HOST '") + temp + wxT("'");
}
else if(on_clause.StartsWith(wxT("PER_HOST"), &temp))
sql += wxT("ON HOST ");
else if(on_clause.StartsWith(wxT("MASTER_ONLY"), &temp))
sql += wxT("ON MASTER ");
else if(on_clause.StartsWith(wxT("SEGMENT_ID:"), &temp))
sql += wxT("ON SEGMENT ") + temp + wxT(" ");
else if(on_clause.StartsWith(wxT("TOTAL_SEGS:"), &temp))
sql += wxT("ON ") + temp + wxT(" ");
else if(on_clause.StartsWith(wxT("ALL_SEGMENTS"), &temp))
sql += wxT("ON ALL ");
else
sql += on_clause;
sql += wxT("\n ");
}
else
{
/* add LOCATION clause */
locations = locations.Mid(1, locations.Length() - 2);
wxStringTokenizer locs(locations, wxT(","));
wxString token;
token = locs.GetNextToken();
sql += wxT(" LOCATION (\n '");
sql += token;
sql += wxT("'");
while (locs.HasMoreTokens())
{
sql += wxT(",\n '");
sql += locs.GetNextToken();
sql += wxT("'");
}
sql += wxT("\n)\n ");
}
/* add FORMAT clause */
sql += wxT("FORMAT '");
sql += fmttype[0] == 't' ? wxT("text") : wxT("csv");
sql += wxT("'");
sql += wxT(" (");
for (size_t i = 0; i < fmtopts.Length(); i++)
{
if (fmtopts[i] == wxT('\\')) sql += wxT('\\');
sql += fmtopts[i];
}
sql += wxT(")\n");
if (GetDatabase()->BackendMinimumVersion(8, 2))
{
/* add ENCODING clause */
sql += wxT("ENCODING '");
sql += extencoding ;
sql += wxT("'");
/* add Single Row Error Handling clause (if any) */
if(rejlim.Length() > 0)
{
sql += wxT("\n");
/*
* NOTE: error tables get automatically generated if don't exist.
* therefore we must be sure that this statment will be dumped after
* the error relation CREATE is dumped, so that we won't try to
* create it twice. For now we rely on the fact that we pick dumpable
* objects sorted by OID, and error table oid *should* always be less
* than its external table oid (could that not be true sometimes?)
*/
if(errtblname.Length() > 0)
{
sql += wxT("LOG ERRORS INTO ");
sql += errtblname;
sql += wxT(" ");
}
/* reject limit */
sql += wxT("SEGMENT REJECT LIMIT ");
sql += rejlim;
/* reject limit type */
if(rejlimtype[0] == 'r')
sql += wxT(" ROWS");
else
sql += wxT(" PERCENT");
}
}
sql += wxT(";\n")
+ GetOwnerSql(7, 3, wxEmptyString, wxT("TABLE"));
sql += GetGrant(wxT("r"), wxT("TABLE ") + GetQuotedFullIdentifier());
sql += GetCommentSql()
+ wxT("\n");
// Column/constraint comments
if (!colDetails.IsEmpty())
sql += colDetails + wxT("\n");
}
return sql;
}
wxString gpExtTable::GetCols(ctlTree *browser, size_t indent, wxString &QMs, bool withQM)
{
wxString sql;
wxString line;
int colcount = 0;
pgSetIterator set(GetConnection(),
wxT("SELECT attname\n")
wxT(" FROM pg_attribute\n")
wxT(" WHERE attrelid=") + GetOidStr() + wxT(" AND attnum>0\n")
wxT(" ORDER BY attnum"));
while (set.RowsLeft())
{
if (colcount++)
{
line += wxT(", ");
QMs += wxT(", ");
}
if (line.Length() > 60)
{
if (!sql.IsEmpty())
{
sql += wxT("\n") + wxString(' ', indent);
}
sql += line;
line = wxEmptyString;
QMs += wxT("\n") + wxString(' ', indent);
}
line += qtIdent(set.GetVal(0));
if (withQM)
line += wxT("=?");
QMs += wxT("?");
}
if (!line.IsEmpty())
{
if (!sql.IsEmpty())
sql += wxT("\n") + wxString(' ', indent);
sql += line;
}
return sql;
}
wxString gpExtTable::GetSelectSql(ctlTree *browser)
{
wxString qms;
wxString sql =
wxT("SELECT ") + GetCols(browser, 7, qms, false) + wxT("\n")
wxT(" FROM ") + GetQuotedFullIdentifier() + wxT(";\n");
return sql;
}
void gpExtTable::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *properties, ctlSQLBox *sqlPane)
{
if (!expandedKids)
{
expandedKids = true;
browser->RemoveDummyChild(this);
browser->AppendCollection(this, columnFactory);
}
if (properties)
{
CreateListColumns(properties);
properties->AppendItem(_("Name"), GetName());
properties->AppendItem(_("OID"), GetOid());
properties->AppendItem(_("Owner"), GetOwner());
properties->AppendItem(_("ACL"), GetAcl());
properties->AppendYesNoItem(_("System Table?"), GetSystemObject());
properties->AppendItem(_("Comment"), firstLineOnly(GetComment()));
}
}
pgObject *gpExtTable::Refresh(ctlTree *browser, const wxTreeItemId item)
{
pgObject *extTable = 0;
pgCollection *coll = browser->GetParentCollection(item);
if (coll)
{
extTable = extTableFactory.CreateObjects(coll, 0, wxT("\n AND c.oid=") + GetOidStr());
}
return extTable;
}
void gpExtTable::ShowHint(frmMain *form, bool force)
{
wxArrayString hints;
hints.Add(HINT_OBJECT_EDITING);
frmHint::ShowHint((wxWindow *)form, hints, GetFullIdentifier(), force);
}
///////////////////////////////////////////////////////
pgObject *gpExtTableFactory::CreateObjects(pgCollection *collection, ctlTree *browser, const wxString &restriction)
{
gpExtTable *extTable = 0;
pgSet *extTables = collection->GetDatabase()->ExecuteSet(
wxT("SELECT c.oid, c.xmin, c.relname, pg_get_userbyid(c.relowner) AS exttableowner, c.relacl AS relacl, description \n")
wxT(" FROM pg_class c\n")
wxT(" LEFT OUTER JOIN pg_description des ON (des.objoid=c.oid AND des.objsubid=0 AND des.classoid='pg_class'::regclass)\n")
wxT(" WHERE (c.relkind = 'x' OR (c.relkind = 'r' AND c.relstorage = 'x'))\n")
wxT(" AND relnamespace = ") + collection->GetSchema()->GetOidStr() + wxT("\n")
+ restriction
+ wxT(" ORDER BY relname"));
if (extTables)
{
while (!extTables->Eof())
{
extTable = new gpExtTable(collection->GetSchema(), extTables->GetVal(wxT("relname")));
extTable->iSetOid(extTables->GetOid(wxT("oid")));
extTable->iSetXid(extTables->GetOid(wxT("xmin")));
extTable->iSetOwner(extTables->GetVal(wxT("exttableowner")));
extTable->iSetComment(extTables->GetVal(wxT("description")));
extTable->iSetAcl(extTables->GetVal(wxT("relacl")));
//extTable->iSetDefinition(extTables->GetVal(wxT("definition")));
if (browser)
{
collection->AppendBrowserItem(browser, extTable);
extTables->MoveNext();
}
else
break;
}
delete extTables;
}
return extTable;
}
/////////////////////////////
gpExtTableCollection::gpExtTableCollection(pgaFactory *factory, pgSchema *sch)
: pgSchemaObjCollection(factory, sch)
{
}
wxString gpExtTableCollection::GetTranslatedMessage(int kindOfMessage) const
{
wxString message = wxEmptyString;
switch (kindOfMessage)
{
case RETRIEVINGDETAILS:
message = _("Retrieving details on external tables");
break;
case REFRESHINGDETAILS:
message = _("Refreshing external tables");
break;
case GRANTWIZARDTITLE:
message = _("Privileges for external tables");
break;
case OBJECTSLISTREPORT:
message = _("External tables list report");
break;
}
return message;
}
/////////////////////////////
#include "images/exttable.pngc"
#include "images/exttable-sm.pngc"
#include "images/exttables.pngc"
gpExtTableFactory::gpExtTableFactory()
: pgSchemaObjFactory(__("External Table"), __("New External Table..."), __("Create a new External Table."), exttable_png_img, exttable_sm_png_img)
{
metaType = GP_EXTTABLE;
}
pgCollection *gpExtTableFactory::CreateCollection(pgObject *obj)
{
return new gpExtTableCollection(GetCollectionFactory(), (pgSchema *)obj);
}
gpExtTableFactory extTableFactory;
static pgaCollectionFactory cf(&extTableFactory, __("External Tables"), exttables_png_img);
|