File: csvaddmapinputdialog.cpp

package info (click to toggle)
csvimp 0.4.7-2~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 1,164 kB
  • sloc: cpp: 7,182; sh: 356; makefile: 13
file content (167 lines) | stat: -rw-r--r-- 4,978 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
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2012 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "csvaddmapinputdialog.h"

#include <QMessageBox>
#include <QSqlError>

#include "xsqlquery.h"

#define DEBUG false

CSVAddMapInputDialog::CSVAddMapInputDialog(QWidget *parent, Qt::WindowFlags f)
  : QDialog(parent, f)
{
  setupUi(this);

  connect(_schema, SIGNAL(currentIndexChanged(int)), this, SLOT(populateTable()));

  populateSchema();
  _schema->setCurrentIndex(0);
}

CSVAddMapInputDialog::~CSVAddMapInputDialog()
{
}

void CSVAddMapInputDialog::languageChange()
{
  retranslateUi(this);
}

QString CSVAddMapInputDialog::mapname() const
{
  return _name->text();
}

void CSVAddMapInputDialog::populateSchema()
{
  XSqlQuery nspq("SELECT '[ All schemas ]' AS nspname, 1 AS seq"
                 " UNION "
                 "SELECT 'public', 2"
                 " UNION "
                 "SELECT nspname, 3"
                 "  FROM pg_namespace"
                 " WHERE ((nspname !~ '^pg_')"
                 "   AND  (nspname != 'public')"
                 "   AND  (nspname != 'information_schema'))"
                 " ORDER BY seq, nspname;");
  if (nspq.exec())
    _schema->clear();
  while (nspq.next())
    _schema->addItem(nspq.value("nspname").toString());
  if (nspq.lastError().type() != QSqlError::NoError)
  {
    QMessageBox::critical(this, tr("Database Error"), nspq.lastError().text());
    return;
  }
}

void CSVAddMapInputDialog::populateTable()
{
  XSqlQuery relq;
  if (_schema->currentIndex() == 0)
    relq.prepare("SELECT CASE nspname WHEN 'public' THEN relname"
                 "                    ELSE nspname || '.' || relname"
                 "       END AS relname,"
                 "       CASE nspname WHEN 'public' THEN 0 ELSE 1 END AS seq"
                 "  FROM pg_class"
                 "  JOIN pg_namespace ON (relnamespace=pg_namespace.oid)"
                 " WHERE ((relkind IN ('r', 'v'))"
                 "   AND  (nspname !~ '^pg_')"
                 "   AND  (nspname != 'information_schema'))"
                 " ORDER BY seq, relname;");
  else
  {
    relq.prepare("SELECT relname"
                 "  FROM pg_class"
                 "  JOIN pg_namespace ON (relnamespace=pg_namespace.oid)"
                 " WHERE ((relkind IN ('r', 'v'))"
                 "   AND  (nspname = :nspname))"
                 " ORDER BY relname;");
    relq.bindValue(":nspname", _schema->currentText());
  }
  if (relq.exec())
    _table->clear();
  while (relq.next())
    _table->addItem(relq.value("relname").toString());
  if (relq.lastError().type() != QSqlError::NoError)
  {
    QMessageBox::critical(this, tr("Database Error"), relq.lastError().text());
    return;
  }
}

QString CSVAddMapInputDialog::qualifiedTable() const
{
  if (_schema->currentIndex() == 0)
    return _table->currentText();
  else
    return QString("%1.%2").arg(_schema->currentText(), _table->currentText());
}

QString CSVAddMapInputDialog::schema() const
{
  if (_schema->currentIndex() <= 0)
    return QString::null;
  else
    return _schema->currentText();
}

void CSVAddMapInputDialog::setMapname(const QString mapname)
{
  if (DEBUG) qDebug("setMapname(%s) entered", qPrintable(mapname));
  _name->setText(mapname);
}

void CSVAddMapInputDialog::setSchema(const QString schema)
{
  if (DEBUG) qDebug("setSchema(%s) entered", qPrintable(schema));
  _schema->setCurrentIndex(_schema->findData(schema, Qt::DisplayRole));
  if (_schema->currentIndex() < 0)
    _schema->setCurrentIndex(0);
}

void CSVAddMapInputDialog::setTable(const QString table)
{
  if (DEBUG) qDebug("setTable(%s) entered", qPrintable(table));
  _table->setCurrentIndex(_table->findData(table, Qt::DisplayRole));
  if (_table->currentIndex() < 0 && table.contains("."))
  {
    QString unqualrelname = table.right(table.length() -
                                        table.lastIndexOf(".") - 1);
    if (DEBUG)
      qDebug("stripped . off %s to get %s",
             qPrintable(table), qPrintable(unqualrelname));
    _table->setCurrentIndex(_table->findData(unqualrelname, Qt::DisplayRole));
  }
}

QString CSVAddMapInputDialog::table() const
{
  return _table->currentText();
}

QString CSVAddMapInputDialog::unqualifiedTable() const
{
  QString result = _table->currentText();
  if (result.contains("."))
  {
    QString unqualrelname = result.right(result.length() -
                                         result.lastIndexOf(".") - 1);
    if (DEBUG)
      qDebug("stripped . off %s to get %s",
             qPrintable(result), qPrintable(unqualrelname));
    result = unqualrelname;
  }

  return result;
}