File: pkgschema.cpp

package info (click to toggle)
postbooks-updater 2.2.5-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 944 kB
  • ctags: 479
  • sloc: cpp: 5,424; sh: 351; makefile: 23; sql: 7
file content (146 lines) | stat: -rw-r--r-- 3,991 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2010 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 "pkgschema.h"

#include <QMessageBox>
#include <QSqlError>
#include <QVariant>     // used by XSqlQuery::bindValue()

#include <xsqlquery.h>

#define TR(a) QObject::tr(a)

#define DEBUG false

QString PkgSchema::_sqlerrtxt = TR("<font color=red>The following error was "
                                  "encountered while trying to import %1 into "
                                  "the database:<br>%2<br>%3</font>");

PkgSchema::PkgSchema(const QString &name, const QString &comment)
{
  _name    = name;
  _comment = comment;
}

PkgSchema::~PkgSchema()
{
}

int PkgSchema::create(QString &errMsg)
{
  if (DEBUG)
    qDebug("PkgSchema::create(&errMsg)");

  if (_name.isEmpty())
  {
    errMsg = QString("Cannot create a schema for this package without a name.");
    return -1;
  }

  int namespaceoid;
  XSqlQuery create;
  // Issue 8835: This SQL looks for an existing schema first.  Would be best if the createPkgSchema 
  // function did this correctly so it could be smart enough to add new tables if necessary,
  // but we can't work that out until it becomes required in a subsequent xTuple ERP release.
  create.prepare("SELECT COALESCE( "
                 " (SELECT oid "
                 "  FROM pg_namespace "
                 "  WHERE (LOWER(nspname)=LOWER(:name))), "
                 " createPkgSchema(:name, :descrip)) AS result;"); 
  create.bindValue(":name",    _name);
  create.bindValue(":descrip", _comment);

  create.exec();
  if (create.first())
    namespaceoid = create.value(0).toInt();
  else if (create.lastError().type() != QSqlError::NoError)
  {
    errMsg = _sqlerrtxt.arg(_name)
                      .arg(create.lastError().databaseText())
                      .arg(create.lastError().driverText());
    return -2;
  }

  int patherr = setPath(errMsg);
  if (patherr < 0)
    return patherr;

  return 0;
}

int PkgSchema::getPath(QString &path, QString &errMsg)
{
  XSqlQuery pathq;
  pathq.exec("SELECT CURRENT_SCHEMAS(false);");
  if (pathq.first())
    path = pathq.value(0).toString();
  else
  {
    errMsg = _sqlerrtxt.arg(_name)
                      .arg(pathq.lastError().databaseText())
                      .arg(pathq.lastError().driverText());
    return -8;
  }

  if (DEBUG)
    qDebug("PkgSchema::getPath() selected %s", qPrintable(path));

  path.remove(0, path.indexOf("{") + 1);
  path.remove(path.indexOf("}"), path.size());

  if (DEBUG)
    qDebug("PkgSchema::getPath() extracted %s", qPrintable(path));

  return 0;
}

int PkgSchema::setPath(QString &errMsg)
{
  QString path;
  int result = getPath(path, errMsg);
  if (result < 0)
    return result;

  XSqlQuery schemaq;
  schemaq.exec(QString("SET SEARCH_PATH TO %1,%2;")
                 .arg(_name.toLower()).arg(path));
  if (schemaq.lastError().type() != QSqlError::NoError)
  {
    errMsg = _sqlerrtxt.arg(_name)
                      .arg(schemaq.lastError().databaseText())
                      .arg(schemaq.lastError().driverText());
    return -8;
  }

  return 0;
}

int PkgSchema::clearPath(QString &errMsg)
{
  QString path;
  int result = getPath(path, errMsg);
  if (result < 0)
    return result;

  path.remove(QRegExp("\\s*" + _name + ",", Qt::CaseInsensitive));

  XSqlQuery schemaq;
  schemaq.exec(QString("SET SEARCH_PATH TO %1;").arg(path));
  if (schemaq.lastError().type() != QSqlError::NoError)
  {
    errMsg = _sqlerrtxt.arg(_name)
                      .arg(schemaq.lastError().databaseText())
                      .arg(schemaq.lastError().driverText());
    return -9;
  }

  return 0;
}