File: db_Column.cpp

package info (click to toggle)
mysql-workbench 6.2.3%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 102,612 kB
  • ctags: 84,593
  • sloc: ansic: 804,682; cpp: 438,759; yacc: 59,129; python: 54,293; xml: 48,851; sql: 5,512; objc: 1,414; makefile: 505; sh: 455; java: 237; ruby: 6; perl: 5; php: 1
file content (239 lines) | stat: -rw-r--r-- 7,204 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
/* 
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include <grts/structs.db.h>

#include "base/string_utilities.h"
#include <grtpp_undo_manager.h>

#include "grt/parse_utils.h"
#include "grt/common.h"
#include "grtdb/db_object_helpers.h"

using namespace base;

//================================================================================
// db_Column

static void notify_visible_member_change(const std::string &member, const grt::ValueRef &ovalue, db_Column* ref)
{
  if (member == "name" || member == "simpleType" || member == "userType")
  {
    if (ovalue != ref->get_member(member) && ref->owner().is_valid())
      (*db_TableRef::cast_from(ref->owner())->signal_refreshDisplay())("column");
  }
}

void db_Column::init()
{
  //No need in disconnet management since signal it part of object
  _changed_signal.connect(boost::bind(notify_visible_member_change, _1, _2, this));
}

db_Column::~db_Column()
{
}


template<class T>
class auto_array_ptr
{
  T *_ptr;

public:
  auto_array_ptr(T* ptr) : _ptr(ptr) {}
  ~auto_array_ptr() { delete[] _ptr; }

  operator T* () { return _ptr; }
};


grt::StringRef db_Column::formattedRawType() const
{
  if (this->userType().is_valid())
  {
    std::string arguments;

    // if no simple or structured datatype is set,
    // simply take the parameters
    if (this->length() != bec::EMPTY_COLUMN_LENGTH)
    {
      arguments= strfmt("(%i)", (int)this->length());
    }
    else if (this->precision() != bec::EMPTY_COLUMN_PRECISION)
    {
      std::string tmp;
      if (this->scale() != bec::EMPTY_COLUMN_SCALE)
        tmp= strfmt("(%i,%i)", (int)this->precision(), (int)this->scale());
  else
        tmp= strfmt("(%i)", (int)this->precision());
      arguments= tmp;
    }
    else if (this->datatypeExplicitParams().is_valid() && *this->datatypeExplicitParams() != "")
    {
      arguments= *this->datatypeExplicitParams();      
    }
    
    return std::string(this->userType()->name()).append(arguments);
  }
  else
    return formattedType();
}


grt::StringRef db_Column::formattedType() const
{
  db_SimpleDatatypeRef simpleType(this->simpleType());
  db_StructuredDatatypeRef structuredType(this->structuredType());
  std::string caption;

  if (simpleType.is_valid())
  {
    ssize_t ptype = simpleType->parameterFormatType();
    caption= simpleType->name();

    if (simpleType->numericPrecision() != bec::EMPTY_TYPE_PRECISION)
    {
      std::string tmp;
      if (this->precision() != bec::EMPTY_COLUMN_PRECISION && this->scale() != bec::EMPTY_COLUMN_SCALE && (ptype == 3 || ptype == 4 || ptype == 5 || ptype == 6))
        tmp= strfmt("(%i,%i)", (int)this->precision(), (int)this->scale());
      else if (this->precision() != bec::EMPTY_COLUMN_PRECISION && (ptype == 1 || ptype == 2 || ptype == 4 || ptype == 6))
        tmp= strfmt("(%i)", (int)this->precision());
      caption.append(tmp);
    }
    else
    {
      if (*simpleType->characterMaximumLength() != bec::EMPTY_TYPE_MAXIMUM_LENGTH 
          || *simpleType->characterOctetLength() != bec::EMPTY_TYPE_OCTET_LENGTH)
      {
        if (this->length() != bec::EMPTY_COLUMN_LENGTH && (ptype == 1 || ptype == 2 || ptype == 4 || ptype == 6))
        {
          caption.append(strfmt("(%i)", (int)this->length()));
        }
      }
      else if (this->datatypeExplicitParams().is_valid() && *this->datatypeExplicitParams() != "")
        caption.append(*this->datatypeExplicitParams());
    }
  }
  else if (structuredType.is_valid())
  {
  }
  else
  {
    std::string arguments;
    
    if (this->userType().is_valid())
      caption= this->userType()->sqlDefinition();

    // if no simple or structured datatype is set,
    // simply take the parameters
    if (this->length() != bec::EMPTY_COLUMN_LENGTH)
    {
      arguments= strfmt("(%i)", (int)this->length());
    }
    else if (this->precision() != bec::EMPTY_COLUMN_PRECISION)
    {
      std::string tmp;
      if (this->scale() != bec::EMPTY_COLUMN_SCALE)
        tmp= strfmt("(%i,%i)", (int)this->precision(), (int)this->scale());
      else
        tmp= strfmt("(%i)", (int)this->precision());
      arguments= tmp;
    }
    else if (this->datatypeExplicitParams().is_valid() && *this->datatypeExplicitParams() != "")
    {
      arguments= *this->datatypeExplicitParams();      
    }
    
    if (!arguments.empty())
    {
      std::string::size_type p;
      if ((p= caption.find('(')) != std::string::npos)
        caption= caption.substr(0, p);
      
      caption.append(arguments);
    }
  }

  return caption;
}


void db_Column::formattedType(const grt::StringRef &value)
{
  if (formattedType() == value.c_str())
    return;
  // add code here
  g_warning("got a request to change %s.formattedType() from '%s' to '%s'",
            owner().is_valid() ? owner()->name().c_str() : "<null>", formattedType().c_str(), value.c_str());
}

/** Sets the datatype defined by a string to the column.
 *
 * Assigns a datatype defined by a string like NUMERIC(7, 2) to the
 * given column.
 *
 * @param rdbms
 * @param type
 * 
 * @return 1 on success or 0 on parse error or invalid type/invalid params
 */
grt::IntegerRef db_Column::setParseType(const std::string &type, const grt::ListRef<db_SimpleDatatype> &typeList)
{
  grt::ListRef<db_UserDatatype> user_types;
  grt::ListRef<db_SimpleDatatype> default_type_list;
  GrtVersionRef target_version;
  if (owner().is_valid() && owner()->owner().is_valid() && owner()->owner()->owner().is_valid())
  {
    db_CatalogRef catalog= db_CatalogRef::cast_from(owner()->owner()->owner());
    user_types= catalog->userDatatypes();
    default_type_list = catalog->simpleDatatypes();
    target_version = catalog->version();
  }

  db_UserDatatypeRef userType;
  db_SimpleDatatypeRef simpleType;
  int precision= bec::EMPTY_COLUMN_PRECISION;
  int scale= bec::EMPTY_COLUMN_SCALE;
  int length= bec::EMPTY_COLUMN_LENGTH;
  std::string datatypeExplicitParams;
  grt::AutoUndo undo(get_grt(), !is_global());

  if (!bec::parseType(type, target_version, typeList, user_types, default_type_list,
      simpleType, userType, precision, scale, length, datatypeExplicitParams))
      return false;
  this->userType(userType);
  this->simpleType(simpleType);
  this->precision(precision);
  this->scale(scale);
  this->length(length);
  this->datatypeExplicitParams(datatypeExplicitParams);

  if (_owner.is_valid())
    (*db_TableRef::cast_from(_owner)->signal_refreshDisplay())("column");

  undo.end(_("Change Column Type"));

  return true;
}