File: db_helpers.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (334 lines) | stat: -rw-r--r-- 11,124 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
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
/* 
 * Copyright (c) 2010, 2016, 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 "db_helpers.h"
#include "base/string_utilities.h"
#include "grtpp_util.h"
#include "base/log.h"

std::string bec::get_host_identifier_for_connection(const db_mgmt_ConnectionRef &connection)
{
  grt::DictRef params(connection->parameterValues());
  std::string host_id;
  
  if (connection->driver().is_valid())
  {
    std::string host_identifier = *connection->driver()->hostIdentifierTemplate();
    for (grt::DictRef::const_iterator par = params.begin(); par != params.end(); ++par)
    {
      base::replace(host_identifier, "%" + par->first + "%", par->second.toString());
    }
    return host_identifier;
  }
  else
    return connection->name();
}


std::string bec::get_description_for_connection(const db_mgmt_ConnectionRef &connection)
{
  std::string conn_type;
  std::string driver, server;
  grt::DictRef params(connection->parameterValues());
  
  if (connection->driver().is_valid())
  {
    driver = connection->driver()->name();
    server = db_mgmt_RdbmsRef::cast_from(connection->driver()->owner())->caption();
  }
  else
    return "Invalid Connection Description";

  std::string user = params.get_string("userName");
  
  if (g_str_has_suffix(driver.c_str(), "Socket"))
  {
    std::string path = base::trim(params.get_string("socket"));
    conn_type = base::strfmt("%s using local socket/pipe at \"%s\" with user %s", 
                             server.c_str(),
                             path.empty()?"default path":path.c_str(),
                             user.c_str());
  }
  else if (g_str_has_suffix(driver.c_str(), "SSH"))
  {    
    conn_type = base::strfmt("%s at %s:%i through SSH tunnel at %s@%s with user %s",
                             server.c_str(),
                             params.get_string("hostName").c_str(),
                             (int) params.get_int("port"),
                             params.get_string("sshUserName").c_str(),
                             params.get_string("sshHost").c_str(),
                             user.c_str());
  }
  else if (g_str_has_suffix(driver.c_str(), "Fabric"))
  {
    conn_type = base::strfmt("%s at %s:%i fabric node with user %s",
                             server.c_str(),
                             params.get_string("hostName").c_str(),
                             (int) params.get_int("port"),
                             user.c_str());
  }
  else // TCP
  {
    conn_type = base::strfmt("%s at %s:%i with user %s",
                             server.c_str(),
                             params.get_string("hostName").c_str(),
                             (int) params.get_int("port"),
                             user.c_str());
  }

  return conn_type;  
}

//--------------------------------------------------------------------------------------------------

std::string bec::sanitize_server_version_number(const std::string &version)
{
  int major, minor, release, patch;
  if (sscanf(version.c_str(), "%i.%i.%i-%i", &major, &minor, &release, &patch) == 4)
  {
    return base::strfmt("%i.%i.%i-%i", major, minor, release, patch);
  }
  else if (sscanf(version.c_str(), "%i.%i.%i", &major, &minor, &release) == 3)
  {
    return base::strfmt("%i.%i.%i", major, minor, release);
  }
  return version;
}

//--------------------------------------------------------------------------------------------------

/**
 * Parses the given version string into its components and returns a GRT version class.
 * Unspecified components are set to -1 to allow for fuzzy comparisons.
 */
GrtVersionRef bec::parse_version(grt::GRT *grt, const std::string &target_version)
{
  int major = 0, minor = -1, release = -1, build = -1;
  
  sscanf(target_version.c_str(), "%i.%i.%i.%i", &major, &minor, &release, &build);
  
  GrtVersionRef version(grt);
  version->name("Version");
  version->majorNumber(major);
  version->minorNumber(minor);
  version->releaseNumber(release);
  version->buildNumber(build);
  
  return version;
}

//--------------------------------------------------------------------------------------------------

/**
 * Converts a grt version struct into a plain long usable by parsers.
 * Returns a default version number if the given version is invalid or has no major version.
 */
int bec::version_to_int(const GrtVersionRef &version)
{
  if (!version.is_valid() || version->majorNumber() == -1)
    return 50100;

  size_t result = version->majorNumber() * 10000;
  if (version->minorNumber() > -1)
    result += version->minorNumber() * 100;
  if (version->releaseNumber() > -1)
    result += version->releaseNumber();

  return (int)result;
}

//--------------------------------------------------------------------------------------------------

/**
 * Converts the int form of a server version to a grt version ref.
 * The build member in the returned version is always -1.
 */
GrtVersionRef bec::int_to_version(grt::GRT *grt, int version)
{

  int major = version / 10000, minor = (version / 100) % 100, release = version % 100, build = -1;

  GrtVersionRef version_(grt);
  version_->name("Version");
  version_->majorNumber(major);
  version_->minorNumber(minor);
  version_->releaseNumber(release);
  version_->buildNumber(build);

  return version_;
}

//--------------------------------------------------------------------------------------------------

/**
 * Compares the given version numbers to see if a is equal to b.
 * In order to support wildcards, missing values are interpreted as matching, e.g. 5 is equal to 5.1
 * and 5.1 is equal to 5.1.1.
 *
 * Do not use for comparing supported MySQL server versions.
 */
bool bec::version_equal(GrtVersionRef a, GrtVersionRef b)
{
  // Major version number is always there.
  if (a->majorNumber() != b->majorNumber())
    return false;
  
  if (a->minorNumber() == -1 || b->minorNumber() == -1)
    return true;
  
  if (a->minorNumber() != b->minorNumber())
    return false;

  if (a->releaseNumber() == -1 || b->releaseNumber() == -1)
    return true;
  
  if (a->releaseNumber() != b->releaseNumber())
    return false;

  if (a->buildNumber() == -1 || b->buildNumber() == -1)
    return true;

  if (a->buildNumber() != b->buildNumber())
    return false;

  return true;
}

/**
 * This test is similar to the one above (except that it tests for greater-than relations).
 * For this however we have to treat unset values differently to handle cases like
 * 5 > 5.1 (false) or 5.1 > 5 (true) correctly.
 *
 * Do not use for comparing supported MySQL server versions.
 */
bool bec::version_greater(GrtVersionRef a, GrtVersionRef b)
{
  if (a->majorNumber() > b->majorNumber()) // Major number should always be set.
    return true;

  if (a->majorNumber() == b->majorNumber())
  {
    if (a->minorNumber() == -1) // An unset value can never be larger than anything else.
      return false;
    
    if (b->minorNumber() == -1) // An unset value is always smaller than any other set value.
        return true;
    
    if (a->minorNumber() > b->minorNumber())
      return true;
    
    // Same approach for release + build numbers.
    if (a->minorNumber() == b->minorNumber())
    {
      if (a->releaseNumber() == -1)
        return false;

      if (b->releaseNumber() == -1)
        return true;

      if (a->releaseNumber() > b->releaseNumber())
        return true;

      if (a->releaseNumber() == b->releaseNumber())
      {
        if (a->buildNumber() == -1)
          return false;

        if (b->buildNumber() == -1)
          return true;

        if (a->buildNumber() > b->buildNumber())
          return true;
      }
      return false;
    }
    return false;
  }

  return false;
}


/** Checks if the given server version numbers is in the set of supported MySQL servers
 */
bool bec::is_supported_mysql_version(int mysql_major, int mysql_minor, int mysql_release)
{
  return ((mysql_major == 5 &&
          (mysql_minor == 1 || mysql_minor == 5 || mysql_minor == 6 || mysql_minor == 7)) ||
          (mysql_major == 8 && mysql_minor == 0));
}


bool bec::is_supported_mysql_version(const std::string &mysql_version)
{
  int my_major = 0, my_minor = -1, my_release = -1, my_build = -1;
  
  sscanf(mysql_version.c_str(), "%i.%i.%i.%i", &my_major, &my_minor, &my_release, &my_build);

  return is_supported_mysql_version(my_major, my_minor, my_release);
}


/** Checks whether the version number supplied is in the known set of versions larger than it.
 Use for server version checks for features.
 */
bool bec::is_supported_mysql_version_at_least(int mysql_major, int mysql_minor, int mysql_release,
                                              int major, int minor, int release)
{
  // if the version required is older (<) than 5.6, then any server that matches is fine
  // if the version required is newer (>=) than 5.6, then we can only guarantee that known servers versions have the feature

  assert(mysql_major < 100 && mysql_minor < 100 && mysql_release < 1000);
  assert(major < 100 && minor < 100 && release < 1000);

  // assemble MMNNRRR
  unsigned int required = major * 100000 + minor * 1000 + (release < 0 ? 0 : release);
  // if the available release number is negative, that's meant to signify "any release number", so we make it the max value possible
  unsigned int available = mysql_major * 100000 + mysql_minor * 1000 + (mysql_release < 0 ? 999 : mysql_release);

  if (major < 5 || (major == 5 && minor < 6) || (major == 8 && minor == 0))
  {
    return (required <= available);
  }
  else if (is_supported_mysql_version(mysql_major, mysql_minor, mysql_release))
  {
    return (required <= available);
  }
  return false;
}


bool bec::is_supported_mysql_version_at_least(const std::string &mysql_version,
                                              int major, int minor, int release)
{
  int my_major = 0, my_minor = -1, my_release = -1, my_build = -1;
  
  sscanf(mysql_version.c_str(), "%i.%i.%i.%i", &my_major, &my_minor, &my_release, &my_build);

  return is_supported_mysql_version_at_least(my_major, my_minor, my_release, major, minor, release);
}


bool bec::is_supported_mysql_version_at_least(const GrtVersionRef &mysql_version, int major, int minor, int release)
{
  if (mysql_version.is_valid())
    return is_supported_mysql_version_at_least((int)mysql_version->majorNumber(),
      (int)mysql_version->minorNumber(), (int)mysql_version->releaseNumber(), major, minor, release);
  return false;
}