scylla-cqlsh 6.0.29__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cqlsh/cqlsh.py ADDED
@@ -0,0 +1,2736 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ import cmd
20
+ import codecs
21
+ import configparser
22
+ import csv
23
+ import errno
24
+ import getpass
25
+ import optparse
26
+ import os
27
+ import platform
28
+ import re
29
+ import stat
30
+ import subprocess
31
+ import sys
32
+ import traceback
33
+ import warnings
34
+ import webbrowser
35
+ import logging
36
+ from contextlib import contextmanager
37
+ from glob import glob
38
+ from io import StringIO
39
+ from uuid import UUID
40
+
41
+ if sys.version_info < (3, 6):
42
+ sys.exit("\ncqlsh requires Python 3.6+\n")
43
+
44
+ # see CASSANDRA-10428
45
+ if platform.python_implementation().startswith('Jython'):
46
+ sys.exit("\nCQL Shell does not run on Jython\n")
47
+
48
+ UTF8 = 'utf-8'
49
+
50
+ description = "CQL Shell for Apache Cassandra"
51
+
52
+ readline = None
53
+ try:
54
+ # check if tty first, cause readline doesn't check, and only cares
55
+ # about $TERM. we don't want the funky escape code stuff to be
56
+ # output if not a tty.
57
+ if sys.stdin.isatty():
58
+ import readline
59
+ except ImportError:
60
+ pass
61
+
62
+ CQL_LIB_PREFIX = 'scylla-driver-'
63
+
64
+ CASSANDRA_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
65
+ CASSANDRA_CQL_HTML_FALLBACK = 'https://cassandra.apache.org/doc/latest/cql/index.html'
66
+
67
+ # default location of local CQL.html
68
+ if os.path.exists(CASSANDRA_PATH + '/doc/cql3/CQL.html'):
69
+ # default location of local CQL.html
70
+ CASSANDRA_CQL_HTML = 'file://' + CASSANDRA_PATH + '/doc/cql3/CQL.html'
71
+ elif os.path.exists('/usr/share/doc/cassandra/CQL.html'):
72
+ # fallback to package file
73
+ CASSANDRA_CQL_HTML = 'file:///usr/share/doc/cassandra/CQL.html'
74
+ else:
75
+ # fallback to online version
76
+ CASSANDRA_CQL_HTML = CASSANDRA_CQL_HTML_FALLBACK
77
+
78
+ # On Linux, the Python webbrowser module uses the 'xdg-open' executable
79
+ # to open a file/URL. But that only works, if the current session has been
80
+ # opened from _within_ a desktop environment. I.e. 'xdg-open' will fail,
81
+ # if the session's been opened via ssh to a remote box.
82
+ #
83
+ try:
84
+ webbrowser.register_standard_browsers() # registration is otherwise lazy in Python3
85
+ except AttributeError:
86
+ pass
87
+ if webbrowser._tryorder and webbrowser._tryorder[0] == 'xdg-open' and os.environ.get('XDG_DATA_DIRS', '') == '':
88
+ # only on Linux (some OS with xdg-open)
89
+ webbrowser._tryorder.remove('xdg-open')
90
+ webbrowser._tryorder.append('xdg-open')
91
+
92
+ # use bundled lib for python-cql if available. if there
93
+ # is a ../lib dir, use bundled libs there preferentially.
94
+ ZIPLIB_DIRS = [os.path.join(CASSANDRA_PATH, 'lib')]
95
+
96
+ if platform.system() == 'Linux':
97
+ ZIPLIB_DIRS.append('/usr/share/cassandra/lib')
98
+
99
+ if os.environ.get('CQLSH_NO_BUNDLED', ''):
100
+ ZIPLIB_DIRS = ()
101
+
102
+
103
+ def find_zip(libprefix):
104
+ for ziplibdir in ZIPLIB_DIRS:
105
+ zips = glob(os.path.join(ziplibdir, libprefix + '*.zip'))
106
+ if zips:
107
+ return max(zips) # probably the highest version, if multiple
108
+
109
+
110
+ cql_zip = find_zip(CQL_LIB_PREFIX)
111
+ if cql_zip:
112
+ sys.path.insert(0, cql_zip)
113
+
114
+ # the driver needs dependencies
115
+ third_parties = ('six-', 'pure_sasl-', 'PyYAML-')
116
+
117
+ for lib in third_parties:
118
+ lib_zip = find_zip(lib)
119
+ if lib_zip:
120
+ sys.path.insert(0, lib_zip)
121
+
122
+ warnings.filterwarnings("ignore", r".*blist.*")
123
+ try:
124
+ import cassandra
125
+ except ImportError as e:
126
+ sys.exit("\nPython Cassandra driver not installed, or not on PYTHONPATH.\n"
127
+ 'You might try "pip install cassandra-driver".\n\n'
128
+ 'Python: %s\n'
129
+ 'Module load path: %r\n\n'
130
+ 'Error: %s\n' % (sys.executable, sys.path, e))
131
+
132
+ from cassandra.auth import PlainTextAuthProvider
133
+ from cassandra.cluster import Cluster, EXEC_PROFILE_DEFAULT, ExecutionProfile
134
+ from cassandra.connection import UnixSocketEndPoint
135
+ from cassandra.cqltypes import cql_typename
136
+ from cassandra.marshal import int64_unpack
137
+ from cassandra.metadata import (ColumnMetadata, KeyspaceMetadata, TableMetadata, protect_name, protect_names, protect_value)
138
+ from cassandra.policies import WhiteListRoundRobinPolicy
139
+ from cassandra.query import SimpleStatement, ordered_dict_factory, TraceUnavailable
140
+ from cassandra.util import datetime_from_timestamp
141
+ from cassandra.connection import DRIVER_NAME, DRIVER_VERSION
142
+
143
+ # cqlsh should run correctly when run out of a Cassandra source tree,
144
+ # out of an unpacked Cassandra tarball, and after a proper package install.
145
+ cqlshlibdir = os.path.join(CASSANDRA_PATH, 'pylib')
146
+ if os.path.isdir(cqlshlibdir):
147
+ sys.path.insert(0, cqlshlibdir)
148
+
149
+ from cqlshlib import cql3handling, pylexotron, sslhandling, cqlshhandling, authproviderhandling
150
+ from cqlshlib.copyutil import ExportTask, ImportTask
151
+ from cqlshlib.displaying import (ANSI_RESET, BLUE, COLUMN_NAME_COLORS, CYAN,
152
+ RED, YELLOW, WHITE, FormattedValue, colorme)
153
+ from cqlshlib.formatting import (DEFAULT_DATE_FORMAT, DEFAULT_NANOTIME_FORMAT,
154
+ DEFAULT_TIMESTAMP_FORMAT, CqlType, DateTimeFormat,
155
+ format_by_type)
156
+ from cqlshlib.tracing import print_trace, print_trace_session
157
+ from cqlshlib.util import get_file_encoding_bomsize
158
+ from cqlshlib.util import is_file_secure, trim_if_present
159
+
160
+ try:
161
+ from cqlshlib._version import __version__ as version
162
+ except ImportError:
163
+ # Get the version relative to the current file, suppressing UserWarnings from setuptools_scm
164
+ from setuptools_scm import get_version
165
+
166
+ with warnings.catch_warnings():
167
+ warnings.simplefilter("ignore", UserWarning)
168
+ version = get_version(root='..', relative_to=__file__, tag_regex=r'^(?P<prefix>v)?(?P<version>[^\+-]+)(?P<suffix>-scylla)?$')
169
+
170
+
171
+ DEFAULT_HOST = '127.0.0.1'
172
+ DEFAULT_PORT = 9042
173
+ DEFAULT_SSL = False
174
+ DEFAULT_CONNECT_TIMEOUT_SECONDS = 5
175
+ DEFAULT_REQUEST_TIMEOUT_SECONDS = 10
176
+
177
+ DEFAULT_FLOAT_PRECISION = 5
178
+ DEFAULT_DOUBLE_PRECISION = 5
179
+ DEFAULT_MAX_TRACE_WAIT = 10
180
+
181
+ if readline is not None and readline.__doc__ is not None and 'libedit' in readline.__doc__:
182
+ DEFAULT_COMPLETEKEY = '\t'
183
+ else:
184
+ DEFAULT_COMPLETEKEY = 'tab'
185
+
186
+ cqldocs = None
187
+ cqlruleset = None
188
+
189
+ epilog = """Connects to %(DEFAULT_HOST)s:%(DEFAULT_PORT)d by default. These
190
+ defaults can be changed by setting $CQLSH_HOST and/or $CQLSH_PORT. When a
191
+ host (and optional port number) are given on the command line, they take
192
+ precedence over any defaults.""" % globals()
193
+
194
+ parser = optparse.OptionParser(description=description, epilog=epilog,
195
+ usage="Usage: %prog [options] [host [port]]",
196
+ version='cqlsh ' + version)
197
+ parser.add_option("-C", "--color", action='store_true', dest='color',
198
+ help='Always use color output')
199
+ parser.add_option("--no-color", action='store_false', dest='color',
200
+ help='Never use color output')
201
+ parser.add_option("--browser", dest='browser', help="""The browser to use to display CQL help, where BROWSER can be:
202
+ - one of the supported browsers in https://docs.python.org/3/library/webbrowser.html.
203
+ - browser path followed by %s, example: /usr/bin/google-chrome-stable %s""")
204
+ parser.add_option('--ssl', action='store_true', help='Use SSL', default=False)
205
+ parser.add_option("-u", "--username", help="Authenticate as user.")
206
+ parser.add_option("-p", "--password", help="Authenticate using password.")
207
+ parser.add_option('-k', '--keyspace', help='Authenticate to the given keyspace.')
208
+ parser.add_option("-f", "--file", help="Execute commands from FILE, then exit")
209
+ parser.add_option('--debug', action='store_true',
210
+ help='Show additional debugging information')
211
+ parser.add_option('--driver-debug', action='store_true',
212
+ help='Show additional driver debugging information')
213
+ parser.add_option('--coverage', action='store_true',
214
+ help='Collect coverage data')
215
+ parser.add_option("--encoding", help="Specify a non-default encoding for output."
216
+ + " (Default: %s)" % (UTF8,))
217
+ parser.add_option("--cqlshrc", help="Specify an alternative cqlshrc file location.")
218
+ parser.add_option("--credentials", help="Specify an alternative credentials file location.")
219
+ parser.add_option('--cqlversion', default=None,
220
+ help='Specify a particular CQL version, '
221
+ 'by default the highest version supported by the server will be used.'
222
+ ' Examples: "3.0.3", "3.1.0"')
223
+ parser.add_option("--protocol-version", type="int", default=None,
224
+ help='Specify a specific protocol version otherwise the client will default and downgrade as necessary')
225
+
226
+ parser.add_option("-e", "--execute", help='Execute the statement and quit.')
227
+ parser.add_option("--connect-timeout", default=DEFAULT_CONNECT_TIMEOUT_SECONDS, dest='connect_timeout',
228
+ help='Specify the connection timeout in seconds (default: %default seconds).')
229
+ parser.add_option("--request-timeout", default=DEFAULT_REQUEST_TIMEOUT_SECONDS, dest='request_timeout',
230
+ help='Specify the default request timeout in seconds (default: %default seconds).')
231
+ parser.add_option("-t", "--tty", action='store_true', dest='tty',
232
+ help='Force tty mode (command prompt).')
233
+ parser.add_option('-v', action="version", help='Print the current version of cqlsh.')
234
+
235
+ # This is a hidden option to suppress the warning when the -p/--password command line option is used.
236
+ # Power users may use this option if they know no other people has access to the system where cqlsh is run or don't care about security.
237
+ # Use of this option in scripting is discouraged. Please use a (temporary) credentials file where possible.
238
+ # The Cassandra distributed tests (dtests) also use this option in some tests when a well-known password is supplied via the command line.
239
+ parser.add_option("--insecure-password-without-warning", action='store_true', dest='insecure_password_without_warning',
240
+ help=optparse.SUPPRESS_HELP)
241
+
242
+ opt_values = optparse.Values()
243
+ (options, arguments) = parser.parse_args(sys.argv[1:], values=opt_values)
244
+
245
+ # BEGIN history/config definition
246
+
247
+
248
+ def mkdirp(path):
249
+ """Creates all parent directories up to path parameter or fails when path exists, but it is not a directory."""
250
+
251
+ try:
252
+ os.makedirs(path)
253
+ except OSError:
254
+ if not os.path.isdir(path):
255
+ raise
256
+
257
+
258
+ def resolve_cql_history_file():
259
+ default_cql_history = os.path.expanduser(os.path.join('~', '.cassandra', 'cqlsh_history'))
260
+ if 'CQL_HISTORY' in os.environ:
261
+ return os.environ['CQL_HISTORY']
262
+ else:
263
+ return default_cql_history
264
+
265
+
266
+ HISTORY = resolve_cql_history_file()
267
+ HISTORY_DIR = os.path.dirname(HISTORY)
268
+
269
+ try:
270
+ mkdirp(HISTORY_DIR)
271
+ except OSError:
272
+ print('\nWarning: Cannot create directory at `%s`. Command history will not be saved. Please check what was the environment property CQL_HISTORY set to.\n' % HISTORY_DIR)
273
+
274
+ DEFAULT_CQLSHRC = os.path.expanduser(os.path.join('~', '.cassandra', 'cqlshrc'))
275
+
276
+ if hasattr(options, 'cqlshrc'):
277
+ CONFIG_FILE = os.path.expanduser(options.cqlshrc)
278
+ if not os.path.exists(CONFIG_FILE):
279
+ print('\nWarning: Specified cqlshrc location `%s` does not exist. Using `%s` instead.\n' % (CONFIG_FILE, DEFAULT_CQLSHRC))
280
+ CONFIG_FILE = DEFAULT_CQLSHRC
281
+ else:
282
+ CONFIG_FILE = DEFAULT_CQLSHRC
283
+
284
+ CQL_DIR = os.path.dirname(CONFIG_FILE)
285
+
286
+ CQL_ERRORS = (
287
+ cassandra.AlreadyExists, cassandra.AuthenticationFailed, cassandra.CoordinationFailure,
288
+ cassandra.InvalidRequest, cassandra.Timeout, cassandra.Unauthorized, cassandra.OperationTimedOut,
289
+ cassandra.cluster.NoHostAvailable,
290
+ cassandra.connection.ConnectionBusy, cassandra.connection.ProtocolError, cassandra.connection.ConnectionException,
291
+ cassandra.protocol.ErrorMessage, cassandra.protocol.InternalError, cassandra.query.TraceUnavailable
292
+ )
293
+
294
+ debug_completion = bool(os.environ.get('CQLSH_DEBUG_COMPLETION', '') == 'YES')
295
+
296
+
297
+ class NoKeyspaceError(Exception):
298
+ pass
299
+
300
+
301
+ class KeyspaceNotFound(Exception):
302
+ pass
303
+
304
+
305
+ class ColumnFamilyNotFound(Exception):
306
+ pass
307
+
308
+
309
+ class IndexNotFound(Exception):
310
+ pass
311
+
312
+
313
+ class MaterializedViewNotFound(Exception):
314
+ pass
315
+
316
+
317
+ class ObjectNotFound(Exception):
318
+ pass
319
+
320
+
321
+ class VersionNotSupported(Exception):
322
+ pass
323
+
324
+
325
+ class UserTypeNotFound(Exception):
326
+ pass
327
+
328
+
329
+ class FunctionNotFound(Exception):
330
+ pass
331
+
332
+
333
+ class AggregateNotFound(Exception):
334
+ pass
335
+
336
+
337
+ class DecodeError(Exception):
338
+ verb = 'decode'
339
+
340
+ def __init__(self, thebytes, err, colname=None):
341
+ self.thebytes = thebytes
342
+ self.err = err
343
+ self.colname = colname
344
+
345
+ def __str__(self):
346
+ return str(self.thebytes)
347
+
348
+ def message(self):
349
+ what = 'value %r' % (self.thebytes,)
350
+ if self.colname is not None:
351
+ what = 'value %r (for column %r)' % (self.thebytes, self.colname)
352
+ return 'Failed to %s %s : %s' \
353
+ % (self.verb, what, self.err)
354
+
355
+ def __repr__(self):
356
+ return '<%s %s>' % (self.__class__.__name__, self.message())
357
+
358
+
359
+ def maybe_ensure_text(val):
360
+ return str(val) if val else val
361
+
362
+
363
+ class FormatError(DecodeError):
364
+ verb = 'format'
365
+
366
+
367
+ def full_cql_version(ver):
368
+ while ver.count('.') < 2:
369
+ ver += '.0'
370
+ ver_parts = ver.split('-', 1) + ['']
371
+ vertuple = tuple(list(map(int, ver_parts[0].split('.'))) + [ver_parts[1]])
372
+ return ver, vertuple
373
+
374
+
375
+ def format_value(val, cqltype, encoding, addcolor=False, date_time_format=None,
376
+ float_precision=None, colormap=None, nullval=None):
377
+ if isinstance(val, DecodeError):
378
+ if addcolor:
379
+ return colorme(repr(val.thebytes), colormap, 'error')
380
+ else:
381
+ return FormattedValue(repr(val.thebytes))
382
+ return format_by_type(val, cqltype=cqltype, encoding=encoding, colormap=colormap,
383
+ addcolor=addcolor, nullval=nullval, date_time_format=date_time_format,
384
+ float_precision=float_precision)
385
+
386
+
387
+ def show_warning_without_quoting_line(message, category, filename, lineno, file=None, line=None):
388
+ if file is None:
389
+ file = sys.stderr
390
+ try:
391
+ file.write(warnings.formatwarning(message, category, filename, lineno, line=''))
392
+ except IOError:
393
+ pass
394
+
395
+
396
+ warnings.showwarning = show_warning_without_quoting_line
397
+ warnings.filterwarnings('always', category=cql3handling.UnexpectedTableStructure)
398
+
399
+
400
+ def insert_driver_hooks():
401
+
402
+ class DateOverFlowWarning(RuntimeWarning):
403
+ pass
404
+
405
+ # Native datetime types blow up outside of datetime.[MIN|MAX]_YEAR. We will fall back to an int timestamp
406
+ def deserialize_date_fallback_int(byts, protocol_version):
407
+ timestamp_ms = int64_unpack(byts)
408
+ try:
409
+ return datetime_from_timestamp(timestamp_ms / 1000.0)
410
+ except OverflowError:
411
+ warnings.warn(DateOverFlowWarning("Some timestamps are larger than Python datetime can represent. "
412
+ "Timestamps are displayed in milliseconds from epoch."))
413
+ return timestamp_ms
414
+
415
+ cassandra.cqltypes.DateType.deserialize = staticmethod(deserialize_date_fallback_int)
416
+
417
+ if hasattr(cassandra, 'deserializers'):
418
+ del cassandra.deserializers.DesDateType
419
+
420
+ # Return cassandra.cqltypes.EMPTY instead of None for empty values
421
+ cassandra.cqltypes.CassandraType.support_empty_values = True
422
+
423
+
424
+ class Shell(cmd.Cmd):
425
+ custom_prompt = os.getenv('CQLSH_PROMPT', '')
426
+ if custom_prompt != '':
427
+ custom_prompt += "\n"
428
+ default_prompt = custom_prompt + "cqlsh> "
429
+ continue_prompt = " ... "
430
+ keyspace_prompt = custom_prompt + "cqlsh:{}> "
431
+ keyspace_continue_prompt = "{} ... "
432
+ show_line_nums = False
433
+ debug = False
434
+ coverage = False
435
+ coveragerc_path = None
436
+ stop = False
437
+ last_hist = None
438
+ shunted_query_out = None
439
+ use_paging = True
440
+
441
+ default_page_size = 100
442
+
443
+ def __init__(self, hostname, port, color=False,
444
+ username=None, encoding=None, stdin=None, tty=True,
445
+ completekey=DEFAULT_COMPLETEKEY, browser=None, use_conn=None,
446
+ cqlver=None, keyspace=None,
447
+ tracing_enabled=False, expand_enabled=False,
448
+ display_nanotime_format=DEFAULT_NANOTIME_FORMAT,
449
+ display_timestamp_format=DEFAULT_TIMESTAMP_FORMAT,
450
+ display_date_format=DEFAULT_DATE_FORMAT,
451
+ display_float_precision=DEFAULT_FLOAT_PRECISION,
452
+ display_double_precision=DEFAULT_DOUBLE_PRECISION,
453
+ display_timezone=None,
454
+ max_trace_wait=DEFAULT_MAX_TRACE_WAIT,
455
+ ssl=False,
456
+ single_statement=None,
457
+ request_timeout=DEFAULT_REQUEST_TIMEOUT_SECONDS,
458
+ protocol_version=None,
459
+ connect_timeout=DEFAULT_CONNECT_TIMEOUT_SECONDS,
460
+ is_subshell=False,
461
+ auth_provider=None,
462
+ ):
463
+ cmd.Cmd.__init__(self, completekey=completekey)
464
+ self.hostname = hostname
465
+ self.port = port
466
+ self.auth_provider = auth_provider
467
+ self.username = username
468
+
469
+ if isinstance(auth_provider, PlainTextAuthProvider):
470
+ self.username = auth_provider.username
471
+ if not auth_provider.password:
472
+ # if no password is provided, we need to query the user to get one.
473
+ password = getpass.getpass()
474
+ self.auth_provider = PlainTextAuthProvider(username=auth_provider.username, password=password)
475
+
476
+ self.keyspace = keyspace
477
+ self.ssl = ssl
478
+ self.tracing_enabled = tracing_enabled
479
+ self.page_size = self.default_page_size
480
+ self.expand_enabled = expand_enabled
481
+ if use_conn:
482
+ self.conn = use_conn
483
+ else:
484
+ kwargs = {}
485
+ if protocol_version is not None:
486
+ kwargs['protocol_version'] = protocol_version
487
+
488
+ self.profiles = {
489
+ EXEC_PROFILE_DEFAULT: ExecutionProfile(consistency_level=cassandra.ConsistencyLevel.ONE,
490
+ request_timeout=request_timeout,
491
+ row_factory=ordered_dict_factory)
492
+ }
493
+
494
+ if os.path.exists(self.hostname) and stat.S_ISSOCK(os.stat(self.hostname).st_mode):
495
+ kwargs['contact_points'] = (UnixSocketEndPoint(self.hostname),)
496
+ self.profiles[EXEC_PROFILE_DEFAULT].load_balancing_policy = WhiteListRoundRobinPolicy([UnixSocketEndPoint(self.hostname)])
497
+ else:
498
+ kwargs['contact_points'] = (self.hostname,)
499
+ self.profiles[EXEC_PROFILE_DEFAULT].load_balancing_policy = WhiteListRoundRobinPolicy([self.hostname])
500
+ kwargs['port'] = self.port
501
+ kwargs['ssl_context'] = sslhandling.ssl_settings(hostname, CONFIG_FILE) if ssl else None
502
+ # workaround until driver would know not to lose the DNS names for `server_hostname`
503
+ kwargs['ssl_options'] = {'server_hostname': self.hostname} if ssl else None
504
+
505
+ self.conn = Cluster(cql_version=cqlver,
506
+ auth_provider=self.auth_provider,
507
+ control_connection_timeout=connect_timeout,
508
+ connect_timeout=connect_timeout,
509
+ execution_profiles=self.profiles,
510
+ **kwargs)
511
+ self.owns_connection = not use_conn
512
+
513
+ if keyspace:
514
+ self.session = self.conn.connect(keyspace)
515
+ else:
516
+ self.session = self.conn.connect()
517
+
518
+ if browser == "":
519
+ browser = None
520
+ self.browser = browser
521
+ self.color = color
522
+
523
+ self.display_nanotime_format = display_nanotime_format
524
+ self.display_timestamp_format = display_timestamp_format
525
+ self.display_date_format = display_date_format
526
+
527
+ self.display_float_precision = display_float_precision
528
+ self.display_double_precision = display_double_precision
529
+
530
+ self.display_timezone = display_timezone
531
+
532
+ self.scylla_version = None
533
+
534
+ self.get_connection_versions()
535
+ self.get_scylla_version()
536
+ self.set_expanded_cql_version(self.connection_versions['cql'])
537
+
538
+ self.current_keyspace = keyspace
539
+
540
+ self.max_trace_wait = max_trace_wait
541
+ self.session.max_trace_wait = max_trace_wait
542
+
543
+ self.tty = tty
544
+ self.encoding = encoding
545
+
546
+ self.output_codec = codecs.lookup(encoding)
547
+
548
+ self.statement = StringIO()
549
+ self.lineno = 1
550
+ self.in_comment = False
551
+
552
+ self.prompt = ''
553
+ if stdin is None:
554
+ stdin = sys.stdin
555
+
556
+ if tty:
557
+ self.reset_prompt()
558
+ self.report_connection()
559
+ print('Use HELP for help.')
560
+ else:
561
+ self.show_line_nums = True
562
+ self.stdin = stdin
563
+ self.query_out = sys.stdout
564
+ self.consistency_level = cassandra.ConsistencyLevel.ONE
565
+ self.serial_consistency_level = cassandra.ConsistencyLevel.SERIAL
566
+
567
+ self.empty_lines = 0
568
+ self.statement_error = False
569
+ self.single_statement = single_statement
570
+ self.is_subshell = is_subshell
571
+
572
+ @property
573
+ def batch_mode(self):
574
+ return not self.tty
575
+
576
+ def set_expanded_cql_version(self, ver):
577
+ ver, vertuple = full_cql_version(ver)
578
+ self.cql_version = ver
579
+ self.cql_ver_tuple = vertuple
580
+
581
+ def cqlver_atleast(self, major, minor=0, patch=0):
582
+ return self.cql_ver_tuple[:3] >= (major, minor, patch)
583
+
584
+ def myformat_value(self, val, cqltype=None, **kwargs):
585
+ if isinstance(val, DecodeError):
586
+ self.decoding_errors.append(val)
587
+ try:
588
+ dtformats = DateTimeFormat(timestamp_format=self.display_timestamp_format,
589
+ date_format=self.display_date_format, nanotime_format=self.display_nanotime_format,
590
+ timezone=self.display_timezone)
591
+ precision = self.display_double_precision if cqltype is not None and cqltype.type_name == 'double' \
592
+ else self.display_float_precision
593
+ return format_value(val, cqltype=cqltype, encoding=self.output_codec.name,
594
+ addcolor=self.color, date_time_format=dtformats,
595
+ float_precision=precision, **kwargs)
596
+ except Exception as e:
597
+ err = FormatError(val, e)
598
+ self.decoding_errors.append(err)
599
+ return format_value(err, cqltype=cqltype, encoding=self.output_codec.name, addcolor=self.color)
600
+
601
+ def myformat_colname(self, name, table_meta=None):
602
+ column_colors = COLUMN_NAME_COLORS.copy()
603
+ # check column role and color appropriately
604
+ if table_meta:
605
+ if name in [col.name for col in table_meta.partition_key]:
606
+ column_colors.default_factory = lambda: RED
607
+ elif name in [col.name for col in table_meta.clustering_key]:
608
+ column_colors.default_factory = lambda: CYAN
609
+ elif name in table_meta.columns and table_meta.columns[name].is_static:
610
+ column_colors.default_factory = lambda: WHITE
611
+ return self.myformat_value(name, colormap=column_colors)
612
+
613
+ def report_connection(self):
614
+ self.show_host()
615
+ self.show_version()
616
+
617
+ def show_host(self):
618
+ print("Connected to {0} at {1}:{2}"
619
+ .format(self.applycolor(self.get_cluster_name(), BLUE),
620
+ self.hostname,
621
+ self.port))
622
+
623
+ def show_version(self):
624
+ vers = self.connection_versions.copy()
625
+ vers['shver'] = version
626
+ # system.Versions['cql'] apparently does not reflect changes with
627
+ # set_cql_version.
628
+ vers['cql'] = self.cql_version
629
+ if self.scylla_version:
630
+ vers['build'] = "Scylla {0}".format(self.scylla_version)
631
+ else:
632
+ vers['build'] = "Cassandra %(build)s" % vers
633
+ print("[cqlsh %(shver)s | %(build)s | CQL spec %(cql)s | Native protocol v%(protocol)s]" % vers)
634
+
635
+ def show_session(self, sessionid, partial_session=False):
636
+ print_trace_session(self, self.session, sessionid, partial_session)
637
+
638
+ def show_replicas(self, token_value, keyspace=None):
639
+ ks = self.current_keyspace if keyspace is None else keyspace
640
+ token_map = self.conn.metadata.token_map
641
+ nodes = token_map.get_replicas(ks, token_map.token_class(token_value))
642
+ addresses = [x.address for x in nodes]
643
+ print(f"{addresses}")
644
+
645
+ def get_connection_versions(self):
646
+ result, = self.session.execute("select * from system.local where key = 'local'")
647
+ vers = {
648
+ 'build': result['release_version'],
649
+ 'protocol': self.conn.protocol_version,
650
+ 'cql': result['cql_version'],
651
+ }
652
+ self.connection_versions = vers
653
+
654
+ def get_scylla_version(self):
655
+ try:
656
+ result, = self.session.execute("SELECT * FROM system.versions WHERE key = 'local'")
657
+ self.scylla_version = result['version']
658
+ except CQL_ERRORS:
659
+ pass
660
+
661
+ def get_keyspace_names(self):
662
+ return list(self.conn.metadata.keyspaces)
663
+
664
+ def get_columnfamily_names(self, ksname=None):
665
+ if ksname is None:
666
+ ksname = self.current_keyspace
667
+
668
+ return list(self.get_keyspace_meta(ksname).tables)
669
+
670
+ def get_materialized_view_names(self, ksname=None):
671
+ if ksname is None:
672
+ ksname = self.current_keyspace
673
+
674
+ return list(self.get_keyspace_meta(ksname).views)
675
+
676
+ def get_index_names(self, ksname=None):
677
+ if ksname is None:
678
+ ksname = self.current_keyspace
679
+
680
+ return list(self.get_keyspace_meta(ksname).indexes)
681
+
682
+ def get_column_names(self, ksname, cfname):
683
+ if ksname is None:
684
+ ksname = self.current_keyspace
685
+ layout = self.get_table_meta(ksname, cfname)
686
+ return list(layout.columns)
687
+
688
+ def get_usertype_names(self, ksname=None):
689
+ if ksname is None:
690
+ ksname = self.current_keyspace
691
+
692
+ return list(self.get_keyspace_meta(ksname).user_types)
693
+
694
+ def get_usertype_layout(self, ksname, typename):
695
+ if ksname is None:
696
+ ksname = self.current_keyspace
697
+
698
+ ks_meta = self.get_keyspace_meta(ksname)
699
+
700
+ try:
701
+ user_type = ks_meta.user_types[typename]
702
+ except KeyError:
703
+ raise UserTypeNotFound("User type {!r} not found".format(typename))
704
+
705
+ return list(zip(user_type.field_names, user_type.field_types))
706
+
707
+ def get_userfunction_names(self, ksname=None):
708
+ if ksname is None:
709
+ ksname = self.current_keyspace
710
+
711
+ return [f.name for f in list(self.get_keyspace_meta(ksname).functions.values())]
712
+
713
+ def get_useraggregate_names(self, ksname=None):
714
+ if ksname is None:
715
+ ksname = self.current_keyspace
716
+
717
+ return [f.name for f in list(self.get_keyspace_meta(ksname).aggregates.values())]
718
+
719
+ def get_cluster_name(self):
720
+ return self.conn.metadata.cluster_name
721
+
722
+ def get_partitioner(self):
723
+ return self.conn.metadata.partitioner
724
+
725
+ def get_keyspace_meta(self, ksname):
726
+ if ksname in self.conn.metadata.keyspaces:
727
+ return self.conn.metadata.keyspaces[ksname]
728
+
729
+ raise KeyspaceNotFound('Keyspace %r not found.' % ksname)
730
+
731
+ def get_keyspaces(self):
732
+ return list(self.conn.metadata.keyspaces.values())
733
+
734
+ def get_ring(self, ks):
735
+ self.conn.metadata.token_map.rebuild_keyspace(ks, build_if_absent=True)
736
+ return self.conn.metadata.token_map.tokens_to_hosts_by_ks[ks]
737
+
738
+ def get_table_meta(self, ksname, tablename):
739
+ if ksname is None:
740
+ ksname = self.current_keyspace
741
+ ksmeta = self.get_keyspace_meta(ksname)
742
+ if tablename not in ksmeta.tables:
743
+ if ksname == 'system_auth' and tablename in ['roles', 'role_permissions']:
744
+ self.get_fake_auth_table_meta(ksname, tablename)
745
+ else:
746
+ raise ColumnFamilyNotFound("Column family {} not found".format(tablename))
747
+ else:
748
+ return ksmeta.tables[tablename]
749
+
750
+ def get_fake_auth_table_meta(self, ksname, tablename):
751
+ # may be using external auth implementation so internal tables
752
+ # aren't actually defined in schema. In this case, we'll fake
753
+ # them up
754
+ if tablename == 'roles':
755
+ ks_meta = KeyspaceMetadata(ksname, True, None, None)
756
+ table_meta = TableMetadata(ks_meta, 'roles')
757
+ table_meta.columns['role'] = ColumnMetadata(table_meta, 'role', cassandra.cqltypes.UTF8Type)
758
+ table_meta.columns['is_superuser'] = ColumnMetadata(table_meta, 'is_superuser', cassandra.cqltypes.BooleanType)
759
+ table_meta.columns['can_login'] = ColumnMetadata(table_meta, 'can_login', cassandra.cqltypes.BooleanType)
760
+ elif tablename == 'role_permissions':
761
+ ks_meta = KeyspaceMetadata(ksname, True, None, None)
762
+ table_meta = TableMetadata(ks_meta, 'role_permissions')
763
+ table_meta.columns['role'] = ColumnMetadata(table_meta, 'role', cassandra.cqltypes.UTF8Type)
764
+ table_meta.columns['resource'] = ColumnMetadata(table_meta, 'resource', cassandra.cqltypes.UTF8Type)
765
+ table_meta.columns['permission'] = ColumnMetadata(table_meta, 'permission', cassandra.cqltypes.UTF8Type)
766
+ else:
767
+ raise ColumnFamilyNotFound("Column family {} not found".format(tablename))
768
+
769
+ def get_index_meta(self, ksname, idxname):
770
+ if ksname is None:
771
+ ksname = self.current_keyspace
772
+ ksmeta = self.get_keyspace_meta(ksname)
773
+
774
+ if idxname not in ksmeta.indexes:
775
+ raise IndexNotFound("Index {} not found".format(idxname))
776
+
777
+ return ksmeta.indexes[idxname]
778
+
779
+ def get_view_meta(self, ksname, viewname):
780
+ if ksname is None:
781
+ ksname = self.current_keyspace
782
+ ksmeta = self.get_keyspace_meta(ksname)
783
+
784
+ if viewname not in ksmeta.views:
785
+ raise MaterializedViewNotFound("Materialized view '{}' not found".format(viewname))
786
+ return ksmeta.views[viewname]
787
+
788
+ def get_object_meta(self, ks, name):
789
+ if name is None:
790
+ if ks and ks in self.conn.metadata.keyspaces:
791
+ return self.conn.metadata.keyspaces[ks]
792
+ elif self.current_keyspace is None:
793
+ raise ObjectNotFound("'{}' not found in keyspaces".format(ks))
794
+ else:
795
+ name = ks
796
+ ks = self.current_keyspace
797
+
798
+ if ks is None:
799
+ ks = self.current_keyspace
800
+
801
+ ksmeta = self.get_keyspace_meta(ks)
802
+
803
+ if name in ksmeta.tables:
804
+ return ksmeta.tables[name]
805
+ elif name in ksmeta.indexes:
806
+ return ksmeta.indexes[name]
807
+ elif name in ksmeta.views:
808
+ return ksmeta.views[name]
809
+
810
+ raise ObjectNotFound("'{}' not found in keyspace '{}'".format(name, ks))
811
+
812
+ def get_trigger_names(self, ksname=None):
813
+ if ksname is None:
814
+ ksname = self.current_keyspace
815
+
816
+ return [trigger.name
817
+ for table in list(self.get_keyspace_meta(ksname).tables.values())
818
+ for trigger in list(table.triggers.values())]
819
+
820
+ def reset_statement(self):
821
+ self.reset_prompt()
822
+ self.statement.truncate(0)
823
+ self.statement.seek(0)
824
+ self.empty_lines = 0
825
+
826
+ def reset_prompt(self):
827
+ if self.current_keyspace is None:
828
+ self.set_prompt(self.default_prompt, True)
829
+ else:
830
+ self.set_prompt(self.keyspace_prompt.format(self.current_keyspace), True)
831
+
832
+ def set_continue_prompt(self):
833
+ if self.empty_lines >= 3:
834
+ self.set_prompt("Statements are terminated with a ';'. You can press CTRL-C to cancel an incomplete statement.")
835
+ self.empty_lines = 0
836
+ return
837
+ if self.current_keyspace is None:
838
+ self.set_prompt(self.continue_prompt)
839
+ else:
840
+ spaces = ' ' * len(str(self.current_keyspace))
841
+ self.set_prompt(self.keyspace_continue_prompt.format(spaces))
842
+ self.empty_lines = self.empty_lines + 1 if not self.lastcmd else 0
843
+
844
+ @contextmanager
845
+ def prepare_loop(self):
846
+ readline = None
847
+ if self.tty and self.completekey:
848
+ try:
849
+ import readline
850
+ except ImportError:
851
+ pass
852
+ else:
853
+ old_completer = readline.get_completer()
854
+ readline.set_completer(self.complete)
855
+ if readline.__doc__ is not None and 'libedit' in readline.__doc__:
856
+ readline.parse_and_bind("bind -e")
857
+ readline.parse_and_bind("bind '" + self.completekey + "' rl_complete")
858
+ readline.parse_and_bind("bind ^R em-inc-search-prev")
859
+ else:
860
+ readline.parse_and_bind(self.completekey + ": complete")
861
+ # start coverage collection if requested, unless in subshell
862
+ if self.coverage and not self.is_subshell:
863
+ # check for coveragerc file, write it if missing
864
+ if os.path.exists(CQL_DIR):
865
+ self.coveragerc_path = os.path.join(CQL_DIR, '.coveragerc')
866
+ covdata_path = os.path.join(CQL_DIR, '.coverage')
867
+ if not os.path.isfile(self.coveragerc_path):
868
+ with open(self.coveragerc_path, 'w') as f:
869
+ f.writelines(["[run]\n",
870
+ "concurrency = multiprocessing\n",
871
+ "data_file = {}\n".format(covdata_path),
872
+ "parallel = true\n"]
873
+ )
874
+ # start coverage
875
+ import coverage
876
+ self.cov = coverage.Coverage(config_file=self.coveragerc_path)
877
+ self.cov.start()
878
+ try:
879
+ yield
880
+ finally:
881
+ if readline is not None:
882
+ readline.set_completer(old_completer)
883
+ if self.coverage and not self.is_subshell:
884
+ self.stop_coverage()
885
+
886
+ def get_input_line(self, prompt=''):
887
+ if self.tty:
888
+ self.lastcmd = input(str(prompt))
889
+ line = self.lastcmd + '\n'
890
+ else:
891
+ self.lastcmd = self.stdin.readline()
892
+ line = self.lastcmd
893
+ if not len(line):
894
+ raise EOFError
895
+ self.lineno += 1
896
+ return line
897
+
898
+ def use_stdin_reader(self, until='', prompt=''):
899
+ until += '\n'
900
+ while True:
901
+ try:
902
+ newline = self.get_input_line(prompt=prompt)
903
+ except EOFError:
904
+ return
905
+ if newline == until:
906
+ return
907
+ yield newline
908
+
909
+ def cmdloop(self, intro=None):
910
+ """
911
+ Adapted from cmd.Cmd's version, because there is literally no way with
912
+ cmd.Cmd.cmdloop() to tell the difference between "EOF" showing up in
913
+ input and an actual EOF.
914
+ """
915
+ with self.prepare_loop():
916
+ while not self.stop:
917
+ try:
918
+ if self.single_statement:
919
+ line = self.single_statement
920
+ self.stop = True
921
+ else:
922
+ line = self.get_input_line(self.prompt)
923
+ self.statement.write(line)
924
+ if self.onecmd(self.statement.getvalue()):
925
+ self.reset_statement()
926
+ except EOFError:
927
+ self.handle_eof()
928
+ except CQL_ERRORS as cqlerr:
929
+ self.printerr(cqlerr.message)
930
+ except KeyboardInterrupt:
931
+ self.reset_statement()
932
+ print('')
933
+
934
+ def strip_comment_blocks(self, statementtext):
935
+ comment_block_in_literal_string = re.search('["].*[/][*].*[*][/].*["]', statementtext)
936
+ if not comment_block_in_literal_string:
937
+ result = re.sub('[/][*].*[*][/]', "", statementtext)
938
+ if '*/' in result and '/*' not in result and not self.in_comment:
939
+ raise SyntaxError("Encountered comment block terminator without being in comment block")
940
+ if '/*' in result:
941
+ result = re.sub('[/][*].*', "", result)
942
+ self.in_comment = True
943
+ if '*/' in result:
944
+ result = re.sub('.*[*][/]', "", result)
945
+ self.in_comment = False
946
+ if self.in_comment and not re.findall('[/][*]|[*][/]', statementtext):
947
+ result = ''
948
+ return result
949
+ return statementtext
950
+
951
+ def onecmd(self, statementtext):
952
+ """
953
+ Returns true if the statement is complete and was handled (meaning it
954
+ can be reset).
955
+ """
956
+ statementtext = self.strip_comment_blocks(statementtext)
957
+ try:
958
+ statements, endtoken_escaped = cqlruleset.cql_split_statements(statementtext)
959
+ except pylexotron.LexingError as e:
960
+ if self.show_line_nums:
961
+ self.printerr('Invalid syntax at line {0}, char {1}'
962
+ .format(e.linenum, e.charnum))
963
+ else:
964
+ self.printerr('Invalid syntax at char {0}'.format(e.charnum))
965
+ statementline = statementtext.split('\n')[e.linenum - 1]
966
+ self.printerr(' {0}'.format(statementline))
967
+ self.printerr(' {0}^'.format(' ' * e.charnum))
968
+ return True
969
+
970
+ while statements and not statements[-1]:
971
+ statements = statements[:-1]
972
+ if not statements:
973
+ return True
974
+ if endtoken_escaped or statements[-1][-1][0] != 'endtoken':
975
+ self.set_continue_prompt()
976
+ return
977
+ for st in statements:
978
+ try:
979
+ self.handle_statement(st, statementtext)
980
+ except Exception as e:
981
+ if self.debug:
982
+ traceback.print_exc()
983
+ else:
984
+ self.printerr(e)
985
+ return True
986
+
987
+ def handle_eof(self):
988
+ if self.tty:
989
+ print('')
990
+ statement = self.statement.getvalue()
991
+ if statement.strip():
992
+ if not self.onecmd(statement):
993
+ self.printerr('Incomplete statement at end of file')
994
+ self.do_exit()
995
+
996
+ def handle_statement(self, tokens, srcstr):
997
+ # Concat multi-line statements and insert into history
998
+ if readline is not None:
999
+ nl_count = srcstr.count("\n")
1000
+
1001
+ new_hist = srcstr.replace("\n", " ").rstrip()
1002
+
1003
+ if nl_count > 1 and self.last_hist != new_hist:
1004
+ readline.add_history(new_hist)
1005
+
1006
+ self.last_hist = new_hist
1007
+ cmdword = tokens[0][1]
1008
+ if cmdword == '?':
1009
+ cmdword = 'help'
1010
+
1011
+ cmdword_lower = cmdword.lower()
1012
+
1013
+ # Describe statements get special treatment: we first want to
1014
+ # send the request to the server and only when it fails will
1015
+ # we attempt to perform it locally. That's why we don't want
1016
+ # to follow the logic below that starts with parsing.
1017
+ #
1018
+ # The reason for that is changes in Scylla may need to be reflected
1019
+ # in the grammar used in cqlsh. We want Scylla to be "independent"
1020
+ # in that regard, so unless necessary, we don't want to do the parsing
1021
+ # here.
1022
+ if cmdword_lower == 'describe' or cmdword_lower == 'desc':
1023
+ return self.perform_describe(cmdword, tokens, srcstr)
1024
+
1025
+ custom_handler = getattr(self, 'do_' + cmdword.lower(), None)
1026
+ if custom_handler:
1027
+ parsed = cqlruleset.cql_whole_parse_tokens(tokens, srcstr=srcstr,
1028
+ startsymbol='cqlshCommand')
1029
+ if parsed and not parsed.remainder:
1030
+ # successful complete parse
1031
+ return custom_handler(parsed)
1032
+ else:
1033
+ return self.handle_parse_error(cmdword, tokens, parsed, srcstr)
1034
+ return self.perform_statement(cqlruleset.cql_extract_orig(tokens, srcstr))
1035
+
1036
+ def handle_parse_error(self, cmdword, tokens, parsed, srcstr):
1037
+ if cmdword.lower() in ('select', 'insert', 'update', 'delete', 'truncate',
1038
+ 'create', 'drop', 'alter', 'grant', 'revoke',
1039
+ 'batch', 'list'):
1040
+ # hey, maybe they know about some new syntax we don't. type
1041
+ # assumptions won't work, but maybe the query will.
1042
+ return self.perform_statement(cqlruleset.cql_extract_orig(tokens, srcstr))
1043
+ if parsed:
1044
+ self.printerr('Improper %s command (problem at %r).' % (cmdword, parsed.remainder[0]))
1045
+ else:
1046
+ self.printerr(f'Improper {cmdword} command.')
1047
+
1048
+ def do_use(self, parsed):
1049
+ ksname = parsed.get_binding('ksname')
1050
+ success, _ = self.perform_simple_statement(SimpleStatement(parsed.extract_orig()))
1051
+ if success:
1052
+ if ksname[0] == '"' and ksname[-1] == '"':
1053
+ self.current_keyspace = self.cql_unprotect_name(ksname)
1054
+ else:
1055
+ self.current_keyspace = ksname.lower()
1056
+
1057
+ def do_select(self, parsed):
1058
+ tracing_was_enabled = self.tracing_enabled
1059
+ ksname = parsed.get_binding('ksname')
1060
+ stop_tracing = ksname == 'system_traces' or (ksname is None and self.current_keyspace == 'system_traces')
1061
+ self.tracing_enabled = self.tracing_enabled and not stop_tracing
1062
+ statement = parsed.extract_orig()
1063
+ self.perform_statement(statement)
1064
+ self.tracing_enabled = tracing_was_enabled
1065
+
1066
+ def perform_statement(self, statement):
1067
+
1068
+ stmt = SimpleStatement(statement, consistency_level=self.consistency_level, serial_consistency_level=self.serial_consistency_level, fetch_size=self.page_size if self.use_paging else None)
1069
+ success, future = self.perform_simple_statement(stmt)
1070
+
1071
+ if future:
1072
+ if future.warnings:
1073
+ self.print_warnings(future.warnings)
1074
+
1075
+ if self.tracing_enabled:
1076
+ try:
1077
+ for trace in future.get_all_query_traces(max_wait_per=self.max_trace_wait, query_cl=self.consistency_level):
1078
+ print_trace(self, trace)
1079
+ except TraceUnavailable:
1080
+ msg = "Statement trace did not complete within %d seconds; trace data may be incomplete." % (self.session.max_trace_wait,)
1081
+ self.writeresult(msg, color=RED)
1082
+ for trace_id in future.get_query_trace_ids():
1083
+ self.show_session(trace_id, partial_session=True)
1084
+ except Exception as err:
1085
+ self.printerr("Unable to fetch query trace: %s" % (str(err),))
1086
+
1087
+ return success
1088
+
1089
+ def parse_for_select_meta(self, query_string):
1090
+ try:
1091
+ parsed = cqlruleset.cql_parse(query_string)[1]
1092
+ except IndexError:
1093
+ return None
1094
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1095
+ name = self.cql_unprotect_name(parsed.get_binding('cfname', None))
1096
+ try:
1097
+ return self.get_table_meta(ks, name)
1098
+ except ColumnFamilyNotFound:
1099
+ try:
1100
+ return self.get_view_meta(ks, name)
1101
+ except MaterializedViewNotFound:
1102
+ raise ObjectNotFound("'{}' not found in keyspace '{}'".format(name, ks))
1103
+
1104
+ def parse_for_update_meta(self, query_string):
1105
+ try:
1106
+ parsed = cqlruleset.cql_parse(query_string)[1]
1107
+ except IndexError:
1108
+ return None
1109
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1110
+ cf = self.cql_unprotect_name(parsed.get_binding('cfname'))
1111
+ return self.get_table_meta(ks, cf)
1112
+
1113
+ def perform_simple_statement(self, statement):
1114
+ if not statement:
1115
+ return False, None
1116
+
1117
+ future = self.session.execute_async(statement, trace=self.tracing_enabled)
1118
+ result = None
1119
+ try:
1120
+ result = future.result()
1121
+ except CQL_ERRORS as err:
1122
+ err_msg = err.message if hasattr(err, 'message') else str(err)
1123
+ self.printerr(str(err.__class__.__name__) + ": " + err_msg)
1124
+ except Exception:
1125
+ import traceback
1126
+ self.printerr(traceback.format_exc())
1127
+
1128
+ # Even if statement failed we try to refresh schema if not agreed (see CASSANDRA-9689)
1129
+ if not future.is_schema_agreed:
1130
+ try:
1131
+ self.conn.refresh_schema_metadata(5) # will throw exception if there is a schema mismatch
1132
+ except Exception:
1133
+ self.printwarn("Warning: schema version mismatch detected; check the schema versions of your "
1134
+ "nodes in system.local and system.peers.")
1135
+ self.conn.refresh_schema_metadata(-1)
1136
+
1137
+ if result is None:
1138
+ return False, None
1139
+
1140
+ if statement.query_string[:6].lower() == 'select':
1141
+ self.print_result(result, self.parse_for_select_meta(statement.query_string))
1142
+ elif statement.query_string.lower().startswith("list users") or statement.query_string.lower().startswith("list roles"):
1143
+ self.print_result(result, self.get_table_meta('system_auth', 'roles'))
1144
+ elif statement.query_string.lower().startswith("list"):
1145
+ self.print_result(result, self.get_table_meta('system_auth', 'role_permissions'))
1146
+ elif result:
1147
+ # CAS INSERT/UPDATE
1148
+ self.writeresult("")
1149
+ self.print_static_result(result, self.parse_for_update_meta(statement.query_string), with_header=True, tty=self.tty)
1150
+ self.flush_output()
1151
+ return True, future
1152
+
1153
+ def print_result(self, result, table_meta):
1154
+ self.decoding_errors = []
1155
+
1156
+ self.writeresult("")
1157
+
1158
+ def print_all(result, table_meta, tty):
1159
+ # Return the number of rows in total
1160
+ num_rows = 0
1161
+ is_first = True
1162
+ while True:
1163
+ # Always print for the first page even it is empty
1164
+ if result.current_rows or is_first:
1165
+ with_header = is_first or tty
1166
+ self.print_static_result(result, table_meta, with_header, tty, num_rows)
1167
+ num_rows += len(result.current_rows)
1168
+ if result.has_more_pages:
1169
+ if self.shunted_query_out is None and tty:
1170
+ # Only pause when not capturing.
1171
+ input("---MORE---")
1172
+ result.fetch_next_page()
1173
+ else:
1174
+ if not tty:
1175
+ self.writeresult("")
1176
+ break
1177
+ is_first = False
1178
+ return num_rows
1179
+
1180
+ num_rows = print_all(result, table_meta, self.tty)
1181
+ self.writeresult("(%d rows)" % num_rows)
1182
+
1183
+ if self.decoding_errors:
1184
+ for err in self.decoding_errors[:2]:
1185
+ self.writeresult(err.message(), color=RED)
1186
+ if len(self.decoding_errors) > 2:
1187
+ self.writeresult('%d more decoding errors suppressed.'
1188
+ % (len(self.decoding_errors) - 2), color=RED)
1189
+
1190
+ def print_static_result(self, result, table_meta, with_header, tty, row_count_offset=0):
1191
+ if not result.column_names and not table_meta:
1192
+ return
1193
+
1194
+ column_names = result.column_names or list(table_meta.columns.keys())
1195
+ formatted_names = [self.myformat_colname(name, table_meta) for name in column_names]
1196
+ if not result.current_rows:
1197
+ # print header only
1198
+ self.print_formatted_result(formatted_names, None, with_header=True, tty=tty)
1199
+ return
1200
+
1201
+ cql_types = []
1202
+ if result.column_types:
1203
+ ks_name = table_meta.keyspace_name if table_meta else self.current_keyspace
1204
+ ks_meta = self.conn.metadata.keyspaces.get(ks_name, None)
1205
+ cql_types = [CqlType(cql_typename(t), ks_meta) for t in result.column_types]
1206
+
1207
+ formatted_values = [list(map(self.myformat_value, [row[c] for c in column_names], cql_types)) for row in result.current_rows]
1208
+
1209
+ if self.expand_enabled:
1210
+ self.print_formatted_result_vertically(formatted_names, formatted_values, row_count_offset)
1211
+ else:
1212
+ self.print_formatted_result(formatted_names, formatted_values, with_header, tty)
1213
+
1214
+ def print_formatted_result(self, formatted_names, formatted_values, with_header, tty):
1215
+ # determine column widths
1216
+ widths = [n.displaywidth for n in formatted_names]
1217
+ if formatted_values is not None:
1218
+ for fmtrow in formatted_values:
1219
+ for num, col in enumerate(fmtrow):
1220
+ widths[num] = max(widths[num], col.displaywidth)
1221
+
1222
+ # print header
1223
+ if with_header:
1224
+ header = ' | '.join(hdr.ljust(w, color=self.color) for (hdr, w) in zip(formatted_names, widths))
1225
+ self.writeresult(' ' + header.rstrip())
1226
+ self.writeresult('-%s-' % '-+-'.join('-' * w for w in widths))
1227
+
1228
+ # stop if there are no rows
1229
+ if formatted_values is None:
1230
+ self.writeresult("")
1231
+ return
1232
+
1233
+ # print row data
1234
+ for row in formatted_values:
1235
+ line = ' | '.join(col.rjust(w, color=self.color) for (col, w) in zip(row, widths))
1236
+ self.writeresult(' ' + line)
1237
+
1238
+ if tty:
1239
+ self.writeresult("")
1240
+
1241
+ def print_formatted_result_vertically(self, formatted_names, formatted_values, row_count_offset):
1242
+ max_col_width = max([n.displaywidth for n in formatted_names])
1243
+ max_val_width = max([n.displaywidth for row in formatted_values for n in row])
1244
+
1245
+ # for each row returned, list all the column-value pairs
1246
+ for i, row in enumerate(formatted_values):
1247
+ self.writeresult("@ Row %d" % (row_count_offset + i + 1))
1248
+ self.writeresult('-%s-' % '-+-'.join(['-' * max_col_width, '-' * max_val_width]))
1249
+ for field_id, field in enumerate(row):
1250
+ column = formatted_names[field_id].ljust(max_col_width, color=self.color)
1251
+ value = field.ljust(field.displaywidth, color=self.color)
1252
+ self.writeresult(' ' + " | ".join([column, value]))
1253
+ self.writeresult('')
1254
+
1255
+ def print_warnings(self, warnings):
1256
+ if warnings is None or len(warnings) == 0:
1257
+ return
1258
+
1259
+ self.writeresult('')
1260
+ self.writeresult('Warnings :')
1261
+ for warning in warnings:
1262
+ self.writeresult(warning)
1263
+ self.writeresult('')
1264
+
1265
+ def emptyline(self):
1266
+ pass
1267
+
1268
+ def parseline(self, line):
1269
+ # this shouldn't be needed
1270
+ raise NotImplementedError
1271
+
1272
+ def complete(self, text, state):
1273
+ if readline is None:
1274
+ return
1275
+ if state == 0:
1276
+ try:
1277
+ self.completion_matches = self.find_completions(text)
1278
+ except Exception:
1279
+ if debug_completion:
1280
+ import traceback
1281
+ traceback.print_exc()
1282
+ else:
1283
+ raise
1284
+ try:
1285
+ return self.completion_matches[state]
1286
+ except IndexError:
1287
+ return None
1288
+
1289
+ def find_completions(self, text):
1290
+ curline = readline.get_line_buffer()
1291
+ prevlines = self.statement.getvalue()
1292
+ wholestmt = prevlines + curline
1293
+ begidx = readline.get_begidx() + len(prevlines)
1294
+ stuff_to_complete = wholestmt[:begidx]
1295
+ return cqlruleset.cql_complete(stuff_to_complete, text, cassandra_conn=self,
1296
+ debug=debug_completion, startsymbol='cqlshCommand')
1297
+
1298
+ def set_prompt(self, prompt, prepend_user=False):
1299
+ if prepend_user and self.username:
1300
+ self.prompt = "{0}@{1}".format(self.username, prompt)
1301
+ return
1302
+ self.prompt = prompt
1303
+
1304
+ def cql_unprotect_name(self, namestr):
1305
+ if namestr is None:
1306
+ return
1307
+ return cqlruleset.dequote_name(namestr)
1308
+
1309
+ def cql_unprotect_value(self, valstr):
1310
+ if valstr is not None:
1311
+ return cqlruleset.dequote_value(valstr)
1312
+
1313
+ def print_recreate_keyspace(self, ksdef, out):
1314
+ out.write(ksdef.export_as_string())
1315
+ out.write("\n")
1316
+
1317
+ def print_recreate_columnfamily(self, ksname, cfname, out):
1318
+ """
1319
+ Output CQL commands which should be pasteable back into a CQL session
1320
+ to recreate the given table.
1321
+
1322
+ Writes output to the given out stream.
1323
+ """
1324
+ out.write(self.get_table_meta(ksname, cfname).export_as_string())
1325
+ out.write("\n")
1326
+
1327
+ def print_recreate_index(self, ksname, idxname, out):
1328
+ """
1329
+ Output CQL commands which should be pasteable back into a CQL session
1330
+ to recreate the given index.
1331
+
1332
+ Writes output to the given out stream.
1333
+ """
1334
+ out.write(self.get_index_meta(ksname, idxname).export_as_string())
1335
+ out.write("\n")
1336
+
1337
+ def print_recreate_materialized_view(self, ksname, viewname, out):
1338
+ """
1339
+ Output CQL commands which should be pasteable back into a CQL session
1340
+ to recreate the given materialized view.
1341
+
1342
+ Writes output to the given out stream.
1343
+ """
1344
+ out.write(self.get_view_meta(ksname, viewname).export_as_string())
1345
+ out.write("\n")
1346
+
1347
+ def print_recreate_object(self, ks, name, out):
1348
+ """
1349
+ Output CQL commands which should be pasteable back into a CQL session
1350
+ to recreate the given object (ks, table or index).
1351
+
1352
+ Writes output to the given out stream.
1353
+ """
1354
+ out.write(self.get_object_meta(ks, name).export_as_string())
1355
+ out.write("\n")
1356
+
1357
+ def describe_keyspaces_client(self):
1358
+ print('')
1359
+ cmd.Cmd.columnize(self, protect_names(self.get_keyspace_names()))
1360
+ print('')
1361
+
1362
+ def describe_keyspace_client(self, ksname):
1363
+ print('')
1364
+ self.print_recreate_keyspace(self.get_keyspace_meta(ksname), sys.stdout)
1365
+ print('')
1366
+
1367
+ def describe_columnfamily_client(self, ksname, cfname):
1368
+ if ksname is None:
1369
+ ksname = self.current_keyspace
1370
+ if ksname is None:
1371
+ raise NoKeyspaceError("No keyspace specified and no current keyspace")
1372
+ print('')
1373
+ self.print_recreate_columnfamily(ksname, cfname, sys.stdout)
1374
+ print('')
1375
+
1376
+ def describe_index_client(self, ksname, idxname):
1377
+ print('')
1378
+ self.print_recreate_index(ksname, idxname, sys.stdout)
1379
+ print('')
1380
+
1381
+ def describe_materialized_view_client(self, ksname, viewname):
1382
+ if ksname is None:
1383
+ ksname = self.current_keyspace
1384
+ if ksname is None:
1385
+ raise NoKeyspaceError("No keyspace specified and no current keyspace")
1386
+ print('')
1387
+ self.print_recreate_materialized_view(ksname, viewname, sys.stdout)
1388
+ print('')
1389
+
1390
+ def describe_object_client(self, ks, name):
1391
+ print('')
1392
+ self.print_recreate_object(ks, name, sys.stdout)
1393
+ print('')
1394
+
1395
+ def describe_columnfamilies_client(self, ksname):
1396
+ print('')
1397
+ if ksname is None:
1398
+ for k in self.get_keyspaces():
1399
+ name = protect_name(k.name)
1400
+ print('Keyspace %s' % (name,))
1401
+ print('---------%s' % ('-' * len(name)))
1402
+ cmd.Cmd.columnize(self, protect_names(self.get_columnfamily_names(k.name)))
1403
+ print('')
1404
+ else:
1405
+ cmd.Cmd.columnize(self, protect_names(self.get_columnfamily_names(ksname)))
1406
+ print('')
1407
+
1408
+ def describe_functions_client(self, ksname):
1409
+ print('')
1410
+ if ksname is None:
1411
+ for ksmeta in self.get_keyspaces():
1412
+ name = protect_name(ksmeta.name)
1413
+ print('Keyspace %s' % (name,))
1414
+ print('---------%s' % ('-' * len(name)))
1415
+ self._columnize_unicode(list(ksmeta.functions.keys()))
1416
+ else:
1417
+ ksmeta = self.get_keyspace_meta(ksname)
1418
+ self._columnize_unicode(list(ksmeta.functions.keys()))
1419
+
1420
+ def describe_function_client(self, ksname, functionname):
1421
+ if ksname is None:
1422
+ ksname = self.current_keyspace
1423
+ if ksname is None:
1424
+ raise NoKeyspaceError("No keyspace specified and no current keyspace")
1425
+ print('')
1426
+ ksmeta = self.get_keyspace_meta(ksname)
1427
+ functions = [f for f in list(ksmeta.functions.values()) if f.name == functionname]
1428
+ if len(functions) == 0:
1429
+ raise FunctionNotFound("User defined function {} not found".format(functionname))
1430
+ print("\n\n".join(func.export_as_string() for func in functions))
1431
+ print('')
1432
+
1433
+ def describe_aggregates_client(self, ksname):
1434
+ print('')
1435
+ if ksname is None:
1436
+ for ksmeta in self.get_keyspaces():
1437
+ name = protect_name(ksmeta.name)
1438
+ print('Keyspace %s' % (name,))
1439
+ print('---------%s' % ('-' * len(name)))
1440
+ self._columnize_unicode(list(ksmeta.aggregates.keys()))
1441
+ else:
1442
+ ksmeta = self.get_keyspace_meta(ksname)
1443
+ self._columnize_unicode(list(ksmeta.aggregates.keys()))
1444
+
1445
+ def describe_aggregate_client(self, ksname, aggregatename):
1446
+ if ksname is None:
1447
+ ksname = self.current_keyspace
1448
+ if ksname is None:
1449
+ raise NoKeyspaceError("No keyspace specified and no current keyspace")
1450
+ print('')
1451
+ ksmeta = self.get_keyspace_meta(ksname)
1452
+ aggregates = [f for f in list(ksmeta.aggregates.values()) if f.name == aggregatename]
1453
+ if len(aggregates) == 0:
1454
+ raise FunctionNotFound("User defined aggregate {} not found".format(aggregatename))
1455
+ print("\n\n".join(aggr.export_as_string() for aggr in aggregates))
1456
+ print('')
1457
+
1458
+ def describe_usertypes_client(self, ksname):
1459
+ print('')
1460
+ if ksname is None:
1461
+ for ksmeta in self.get_keyspaces():
1462
+ name = protect_name(ksmeta.name)
1463
+ print('Keyspace %s' % (name,))
1464
+ print('---------%s' % ('-' * len(name)))
1465
+ self._columnize_unicode(list(ksmeta.user_types.keys()), quote=True)
1466
+ else:
1467
+ ksmeta = self.get_keyspace_meta(ksname)
1468
+ self._columnize_unicode(list(ksmeta.user_types.keys()), quote=True)
1469
+
1470
+ def describe_usertype_client(self, ksname, typename):
1471
+ if ksname is None:
1472
+ ksname = self.current_keyspace
1473
+ if ksname is None:
1474
+ raise NoKeyspaceError("No keyspace specified and no current keyspace")
1475
+ print('')
1476
+ ksmeta = self.get_keyspace_meta(ksname)
1477
+ try:
1478
+ usertype = ksmeta.user_types[typename]
1479
+ except KeyError:
1480
+ raise UserTypeNotFound("User type {} not found".format(typename))
1481
+ print(usertype.export_as_string())
1482
+
1483
+ def _columnize_unicode(self, name_list, quote=False):
1484
+ """
1485
+ Used when columnizing identifiers that may contain unicode
1486
+ """
1487
+ names = [n for n in name_list]
1488
+ if quote:
1489
+ names = protect_names(names)
1490
+ cmd.Cmd.columnize(self, names)
1491
+ print('')
1492
+
1493
+ def describe_cluster_client(self):
1494
+ print('\nCluster: %s' % self.get_cluster_name())
1495
+ p = trim_if_present(self.get_partitioner(), 'org.apache.cassandra.dht.')
1496
+ print('Partitioner: %s\n' % p)
1497
+ # TODO: snitch?
1498
+ # snitch = trim_if_present(self.get_snitch(), 'org.apache.cassandra.locator.')
1499
+ # print 'Snitch: %s\n' % snitch
1500
+ if self.current_keyspace is not None and self.current_keyspace != 'system':
1501
+ print("Range ownership:")
1502
+ ring = self.get_ring(self.current_keyspace)
1503
+ for entry in list(ring.items()):
1504
+ print(' %39s [%s]' % (str(entry[0].value), ', '.join([host.address for host in entry[1]])))
1505
+ print('')
1506
+
1507
+ def describe_schema_client(self, include_system=False):
1508
+ print('')
1509
+ for k in self.get_keyspaces():
1510
+ if include_system or k.name not in cql3handling.SYSTEM_KEYSPACES:
1511
+ self.print_recreate_keyspace(k, sys.stdout)
1512
+ print('')
1513
+
1514
+ # Precondition: the first token in `srcstr.lower()` is either `describe` or `desc`.
1515
+ def perform_describe(self, cmdword, tokens, srcstr):
1516
+ """
1517
+ DESCRIBE [cqlsh only]
1518
+
1519
+ (DESC may be used as a shorthand.)
1520
+
1521
+ Outputs information about the connected Cassandra cluster, or about
1522
+ the data objects stored in the cluster. Use in one of the following ways:
1523
+
1524
+ DESCRIBE KEYSPACES
1525
+
1526
+ Output the names of all keyspaces.
1527
+
1528
+ DESCRIBE KEYSPACE [<keyspacename>]
1529
+
1530
+ Output CQL commands that could be used to recreate the given keyspace,
1531
+ and the objects in it (such as tables, types, functions, etc.).
1532
+ In some cases, as the CQL interface matures, there will be some metadata
1533
+ about a keyspace that is not representable with CQL. That metadata will not be shown.
1534
+ The '<keyspacename>' argument may be omitted, in which case the current
1535
+ keyspace will be described.
1536
+
1537
+ DESCRIBE TABLES
1538
+
1539
+ Output the names of all tables in the current keyspace, or in all
1540
+ keyspaces if there is no current keyspace.
1541
+
1542
+ DESCRIBE TABLE [<keyspace>.]<tablename>
1543
+
1544
+ Output CQL commands that could be used to recreate the given table.
1545
+ In some cases, as above, there may be table metadata which is not
1546
+ representable and which will not be shown.
1547
+
1548
+ DESCRIBE INDEX <indexname>
1549
+
1550
+ Output the CQL command that could be used to recreate the given index.
1551
+ In some cases, there may be index metadata which is not representable
1552
+ and which will not be shown.
1553
+
1554
+ DESCRIBE MATERIALIZED VIEW <viewname>
1555
+
1556
+ Output the CQL command that could be used to recreate the given materialized view.
1557
+ In some cases, there may be materialized view metadata which is not representable
1558
+ and which will not be shown.
1559
+
1560
+ DESCRIBE CLUSTER
1561
+
1562
+ Output information about the connected Cassandra cluster, such as the
1563
+ cluster name, and the partitioner and snitch in use. When you are
1564
+ connected to a non-system keyspace, also shows endpoint-range
1565
+ ownership information for the Cassandra ring.
1566
+
1567
+ DESCRIBE [FULL] SCHEMA
1568
+
1569
+ Output CQL commands that could be used to recreate the entire (non-system) schema.
1570
+ Works as though "DESCRIBE KEYSPACE k" was invoked for each non-system keyspace
1571
+ k. Use DESCRIBE FULL SCHEMA to include the system keyspaces.
1572
+
1573
+ DESCRIBE TYPES
1574
+
1575
+ Output the names of all user-defined-types in the current keyspace, or in all
1576
+ keyspaces if there is no current keyspace.
1577
+
1578
+ DESCRIBE TYPE [<keyspace>.]<type>
1579
+
1580
+ Output the CQL command that could be used to recreate the given user-defined-type.
1581
+
1582
+ DESCRIBE FUNCTIONS
1583
+
1584
+ Output the names of all user-defined-functions in the current keyspace, or in all
1585
+ keyspaces if there is no current keyspace.
1586
+
1587
+ DESCRIBE FUNCTION [<keyspace>.]<function>
1588
+
1589
+ Output the CQL command that could be used to recreate the given user-defined-function.
1590
+
1591
+ DESCRIBE AGGREGATES
1592
+
1593
+ Output the names of all user-defined-aggregates in the current keyspace, or in all
1594
+ keyspaces if there is no current keyspace.
1595
+
1596
+ DESCRIBE AGGREGATE [<keyspace>.]<aggregate>
1597
+
1598
+ Output the CQL command that could be used to recreate the given user-defined-aggregate.
1599
+
1600
+ DESCRIBE <objname>
1601
+
1602
+ Output CQL commands that could be used to recreate the entire object schema,
1603
+ where object can be either a keyspace or a table or an index or a materialized
1604
+ view (in this order).
1605
+ """
1606
+
1607
+ def perform_describe_locally(parsed):
1608
+ what = parsed.matched[1][1].lower()
1609
+ if what == 'functions':
1610
+ self.describe_functions_client(self.current_keyspace)
1611
+ elif what == 'function':
1612
+ ksname = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1613
+ functionname = self.cql_unprotect_name(parsed.get_binding('udfname'))
1614
+ self.describe_function_client(ksname, functionname)
1615
+ elif what == 'aggregates':
1616
+ self.describe_aggregates_client(self.current_keyspace)
1617
+ elif what == 'aggregate':
1618
+ ksname = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1619
+ aggregatename = self.cql_unprotect_name(parsed.get_binding('udaname'))
1620
+ self.describe_aggregate_client(ksname, aggregatename)
1621
+ elif what == 'keyspaces':
1622
+ self.describe_keyspaces_client()
1623
+ elif what == 'keyspace':
1624
+ ksname = self.cql_unprotect_name(parsed.get_binding('ksname', ''))
1625
+ if not ksname:
1626
+ ksname = self.current_keyspace
1627
+ if ksname is None:
1628
+ self.printerr('Not in any keyspace.')
1629
+ return
1630
+ self.describe_keyspace_client(ksname)
1631
+ elif what in ('columnfamily', 'table'):
1632
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1633
+ cf = self.cql_unprotect_name(parsed.get_binding('cfname'))
1634
+ self.describe_columnfamily_client(ks, cf)
1635
+ elif what == 'index':
1636
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1637
+ idx = self.cql_unprotect_name(parsed.get_binding('idxname', None))
1638
+ self.describe_index_client(ks, idx)
1639
+ elif what == 'materialized' and parsed.matched[2][1].lower() == 'view':
1640
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1641
+ mv = self.cql_unprotect_name(parsed.get_binding('mvname'))
1642
+ self.describe_materialized_view_client(ks, mv)
1643
+ elif what in ('columnfamilies', 'tables'):
1644
+ self.describe_columnfamilies_client(self.current_keyspace)
1645
+ elif what == 'types':
1646
+ self.describe_usertypes_client(self.current_keyspace)
1647
+ elif what == 'type':
1648
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1649
+ ut = self.cql_unprotect_name(parsed.get_binding('utname'))
1650
+ self.describe_usertype_client(ks, ut)
1651
+ elif what == 'cluster':
1652
+ self.describe_cluster_client()
1653
+ elif what == 'schema':
1654
+ self.describe_schema_client(False)
1655
+ elif what == 'full' and parsed.matched[2][1].lower() == 'schema':
1656
+ self.describe_schema_client(True)
1657
+ elif what:
1658
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1659
+ name = self.cql_unprotect_name(parsed.get_binding('cfname'))
1660
+ if not name:
1661
+ name = self.cql_unprotect_name(parsed.get_binding('idxname', None))
1662
+ if not name:
1663
+ name = self.cql_unprotect_name(parsed.get_binding('mvname', None))
1664
+ self.describe_object_client(ks, name)
1665
+
1666
+ stmt = SimpleStatement(srcstr, consistency_level=cassandra.ConsistencyLevel.LOCAL_ONE,
1667
+ fetch_size=self.page_size if self.use_paging else None)
1668
+ future = self.session.execute_async(stmt)
1669
+ try:
1670
+ result = future.result()
1671
+
1672
+ # The second token in the statement indicates which
1673
+ # kind of DESCRIBE we're performing.
1674
+ what = srcstr.split()[1].lower().rstrip(';')
1675
+
1676
+ if what in ('columnfamilies', 'tables', 'types', 'functions', 'aggregates'):
1677
+ self.describe_list(result)
1678
+ elif what == 'keyspaces':
1679
+ self.describe_keyspaces(result)
1680
+ elif what == 'cluster':
1681
+ self.describe_cluster(result)
1682
+ elif what:
1683
+ self.describe_element(result)
1684
+
1685
+ except cassandra.protocol.SyntaxException:
1686
+ # Server doesn't support DESCRIBE query, retry with
1687
+ # client-side DESCRIBE implementation
1688
+ parsed = cqlruleset.cql_whole_parse_tokens(tokens, srcstr=srcstr,
1689
+ startsymbol='cqlshCommand')
1690
+ if parsed and not parsed.remainder:
1691
+ return perform_describe_locally(parsed)
1692
+ else:
1693
+ return self.handle_parse_error(cmdword, tokens, parsed, srcstr)
1694
+ except CQL_ERRORS as err:
1695
+ err_msg = err.message if hasattr(err, 'message') else str(err)
1696
+ self.printerr(err_msg.partition("message=")[2].strip('"'))
1697
+ except Exception:
1698
+ import traceback
1699
+ self.printerr(traceback.format_exc())
1700
+
1701
+ if future:
1702
+ if future.warnings:
1703
+ self.print_warnings(future.warnings)
1704
+
1705
+ def describe_keyspaces(self, rows):
1706
+ """
1707
+ Print the output for a DESCRIBE KEYSPACES query
1708
+ """
1709
+ names = [r['name'] for r in rows]
1710
+
1711
+ print('')
1712
+ cmd.Cmd.columnize(self, names)
1713
+ print('')
1714
+
1715
+ def describe_list(self, rows):
1716
+ """
1717
+ Print the output for all the DESCRIBE queries for element names (e.g DESCRIBE TABLES, DESCRIBE FUNCTIONS ...)
1718
+ """
1719
+ keyspace = None
1720
+ names = list()
1721
+ for row in rows:
1722
+ if row['keyspace_name'] != keyspace:
1723
+ if keyspace is not None:
1724
+ self.print_keyspace_element_names(keyspace, names)
1725
+
1726
+ keyspace = row['keyspace_name']
1727
+ names = list()
1728
+
1729
+ names.append(str(row['name']))
1730
+
1731
+ if keyspace is not None:
1732
+ self.print_keyspace_element_names(keyspace, names)
1733
+ print('')
1734
+
1735
+ def print_keyspace_element_names(self, keyspace, names):
1736
+ print('')
1737
+ if self.current_keyspace is None:
1738
+ print('Keyspace %s' % (keyspace))
1739
+ print('---------%s' % ('-' * len(keyspace)))
1740
+ cmd.Cmd.columnize(self, names)
1741
+
1742
+ def describe_element(self, rows):
1743
+ """
1744
+ Print the output for all the DESCRIBE queries where an element name as been specified (e.g DESCRIBE TABLE, DESCRIBE INDEX ...)
1745
+ """
1746
+ for row in rows:
1747
+ print('')
1748
+ self.query_out.write(row['create_statement'])
1749
+ print('')
1750
+
1751
+ def describe_cluster(self, rows):
1752
+ """
1753
+ Print the output for a DESCRIBE CLUSTER query.
1754
+
1755
+ If a specified keyspace was in use the returned ResultSet will contains a 'range_ownership' column,
1756
+ otherwise not.
1757
+ """
1758
+ for row in rows:
1759
+ print('\nCluster: %s' % row['cluster'])
1760
+ print('Partitioner: %s' % row['partitioner'])
1761
+ print('Snitch: %s\n' % row['snitch'])
1762
+ if 'range_ownership' in row:
1763
+ print("Range ownership:")
1764
+ for entry in list(row['range_ownership'].items()):
1765
+ print(' %39s [%s]' % (entry[0], ', '.join([host for host in entry[1]])))
1766
+ print('')
1767
+
1768
+ def do_copy(self, parsed):
1769
+ r"""
1770
+ COPY [cqlsh only]
1771
+
1772
+ COPY x FROM: Imports CSV data into a Cassandra table
1773
+ COPY x TO: Exports data from a Cassandra table in CSV format.
1774
+
1775
+ COPY <table_name> [ ( column [, ...] ) ]
1776
+ FROM ( '<file_pattern_1, file_pattern_2, ... file_pattern_n>' | STDIN )
1777
+ [ WITH <option>='value' [AND ...] ];
1778
+
1779
+ File patterns are either file names or valid python glob expressions, e.g. *.csv or folder/*.csv.
1780
+
1781
+ COPY <table_name> [ ( column [, ...] ) ]
1782
+ TO ( '<filename>' | STDOUT )
1783
+ [ WITH <option>='value' [AND ...] ];
1784
+
1785
+ Available common COPY options and defaults:
1786
+
1787
+ DELIMITER=',' - character that appears between records
1788
+ QUOTE='"' - quoting character to be used to quote fields
1789
+ ESCAPE='\' - character to appear before the QUOTE char when quoted
1790
+ HEADER=false - whether to ignore the first line
1791
+ NULL='' - string that represents a null value
1792
+ DATETIMEFORMAT= - timestamp strftime format
1793
+ '%Y-%m-%d %H:%M:%S%z' defaults to time_format value in cqlshrc
1794
+ MAXATTEMPTS=5 - the maximum number of attempts per batch or range
1795
+ REPORTFREQUENCY=0.25 - the frequency with which we display status updates in seconds
1796
+ DECIMALSEP='.' - the separator for decimal values
1797
+ THOUSANDSSEP='' - the separator for thousands digit groups
1798
+ BOOLSTYLE='True,False' - the representation for booleans, case insensitive, specify true followed by false,
1799
+ for example yes,no or 1,0
1800
+ NUMPROCESSES=n - the number of worker processes, by default the number of cores minus one
1801
+ capped at 16
1802
+ CONFIGFILE='' - a configuration file with the same format as .cqlshrc (see the Python ConfigParser
1803
+ documentation) where you can specify WITH options under the following optional
1804
+ sections: [copy], [copy-to], [copy-from], [copy:ks.table], [copy-to:ks.table],
1805
+ [copy-from:ks.table], where <ks> is your keyspace name and <table> is your table
1806
+ name. Options are read from these sections, in the order specified
1807
+ above, and command line options always override options in configuration files.
1808
+ Depending on the COPY direction, only the relevant copy-from or copy-to sections
1809
+ are used. If no configfile is specified then .cqlshrc is searched instead.
1810
+ RATEFILE='' - an optional file where to print the output statistics
1811
+
1812
+ Available COPY FROM options and defaults:
1813
+
1814
+ CHUNKSIZE=5000 - the size of chunks passed to worker processes
1815
+ INGESTRATE=100000 - an approximate ingest rate in rows per second
1816
+ MINBATCHSIZE=10 - the minimum size of an import batch
1817
+ MAXBATCHSIZE=20 - the maximum size of an import batch
1818
+ MAXROWS=-1 - the maximum number of rows, -1 means no maximum
1819
+ SKIPROWS=0 - the number of rows to skip
1820
+ SKIPCOLS='' - a comma separated list of column names to skip
1821
+ MAXPARSEERRORS=-1 - the maximum global number of parsing errors, -1 means no maximum
1822
+ MAXINSERTERRORS=1000 - the maximum global number of insert errors, -1 means no maximum
1823
+ ERRFILE='' - a file where to store all rows that could not be imported, by default this is
1824
+ import_ks_table.err where <ks> is your keyspace and <table> is your table name.
1825
+ PREPAREDSTATEMENTS=True - whether to use prepared statements when importing, by default True. Set this to
1826
+ False if you don't mind shifting data parsing to the cluster. The cluster will also
1827
+ have to compile every batch statement. For large and oversized clusters
1828
+ this will result in a faster import but for smaller clusters it may generate
1829
+ timeouts.
1830
+ TTL=3600 - the time to live in seconds, by default data will not expire
1831
+
1832
+ Available COPY TO options and defaults:
1833
+
1834
+ ENCODING='utf8' - encoding for CSV output
1835
+ PAGESIZE='1000' - the page size for fetching results
1836
+ PAGETIMEOUT=10 - the page timeout in seconds for fetching results
1837
+ BEGINTOKEN='' - the minimum token string to consider when exporting data
1838
+ ENDTOKEN='' - the maximum token string to consider when exporting data
1839
+ MAXREQUESTS=6 - the maximum number of requests each worker process can work on in parallel
1840
+ MAXOUTPUTSIZE='-1' - the maximum size of the output file measured in number of lines,
1841
+ beyond this maximum the output file will be split into segments,
1842
+ -1 means unlimited.
1843
+ FLOATPRECISION=5 - the number of digits displayed after the decimal point for cql float values
1844
+ DOUBLEPRECISION=12 - the number of digits displayed after the decimal point for cql double values
1845
+
1846
+ When entering CSV data on STDIN, you can use the sequence "\."
1847
+ on a line by itself to end the data input.
1848
+ """
1849
+
1850
+ ks = self.cql_unprotect_name(parsed.get_binding('ksname', None))
1851
+ if ks is None:
1852
+ ks = self.current_keyspace
1853
+ if ks is None:
1854
+ raise NoKeyspaceError("Not in any keyspace.")
1855
+ table = self.cql_unprotect_name(parsed.get_binding('cfname'))
1856
+ columns = parsed.get_binding('colnames', None)
1857
+ if columns is not None:
1858
+ columns = list(map(self.cql_unprotect_name, columns))
1859
+ else:
1860
+ # default to all known columns
1861
+ columns = self.get_column_names(ks, table)
1862
+
1863
+ fname = parsed.get_binding('fname', None)
1864
+ if fname is not None:
1865
+ fname = self.cql_unprotect_value(fname)
1866
+
1867
+ copyoptnames = list(map(str.lower, parsed.get_binding('optnames', ())))
1868
+ copyoptvals = list(map(self.cql_unprotect_value, parsed.get_binding('optvals', ())))
1869
+ opts = dict(list(zip(copyoptnames, copyoptvals)))
1870
+
1871
+ direction = parsed.get_binding('dir').upper()
1872
+ if direction == 'FROM':
1873
+ task = ImportTask(self, ks, table, columns, fname, opts, self.conn.protocol_version, CONFIG_FILE)
1874
+ elif direction == 'TO':
1875
+ task = ExportTask(self, ks, table, columns, fname, opts, self.conn.protocol_version, CONFIG_FILE)
1876
+ else:
1877
+ raise SyntaxError("Unknown direction %s" % direction)
1878
+
1879
+ task.run()
1880
+
1881
+ def do_show(self, parsed):
1882
+ """
1883
+ SHOW [cqlsh only]
1884
+
1885
+ Displays information about the current cqlsh session. Can be called in
1886
+ the following ways:
1887
+
1888
+ SHOW VERSION
1889
+
1890
+ Shows the version and build of the connected Cassandra instance, as
1891
+ well as the version of the CQL spec that the connected Cassandra
1892
+ instance understands.
1893
+
1894
+ SHOW HOST
1895
+
1896
+ Shows where cqlsh is currently connected.
1897
+
1898
+ SHOW SESSION <sessionid>
1899
+
1900
+ Pretty-prints the requested tracing session.
1901
+
1902
+ SHOW REPLICAS <token> (<keyspace>)
1903
+
1904
+ Lists the replica nodes by IP address for the given token. The current
1905
+ keyspace is used if one is not specified.
1906
+ """
1907
+ showwhat = parsed.get_binding('what').lower()
1908
+ if showwhat == 'version':
1909
+ self.get_connection_versions()
1910
+ self.get_scylla_version()
1911
+ self.show_version()
1912
+ elif showwhat == 'host':
1913
+ self.show_host()
1914
+ elif showwhat.startswith('session'):
1915
+ session_id = parsed.get_binding('sessionid').lower()
1916
+ self.show_session(UUID(session_id))
1917
+ elif showwhat.startswith('replicas'):
1918
+ token_id = parsed.get_binding('token')
1919
+ keyspace = parsed.get_binding('keyspace')
1920
+ self.show_replicas(token_id, keyspace)
1921
+ else:
1922
+ self.printerr('Wait, how do I show %r?' % (showwhat,))
1923
+
1924
+ def do_source(self, parsed):
1925
+ """
1926
+ SOURCE [cqlsh only]
1927
+
1928
+ Executes a file containing CQL statements. Gives the output for each
1929
+ statement in turn, if any, or any errors that occur along the way.
1930
+
1931
+ Errors do NOT abort execution of the CQL source file.
1932
+
1933
+ Usage:
1934
+
1935
+ SOURCE '<file>';
1936
+
1937
+ That is, the path to the file to be executed must be given inside a
1938
+ string literal. The path is interpreted relative to the current working
1939
+ directory. The tilde shorthand notation ('~/mydir') is supported for
1940
+ referring to $HOME.
1941
+
1942
+ See also the --file option to cqlsh.
1943
+ """
1944
+ fname = parsed.get_binding('fname')
1945
+ fname = os.path.expanduser(self.cql_unprotect_value(fname))
1946
+ try:
1947
+ encoding, bom_size = get_file_encoding_bomsize(fname)
1948
+ f = codecs.open(fname, 'r', encoding)
1949
+ f.seek(bom_size)
1950
+ except IOError as e:
1951
+ self.printerr('Could not open %r: %s' % (fname, e))
1952
+ return
1953
+ subshell = Shell(self.hostname, self.port, color=self.color,
1954
+ username=self.username,
1955
+ encoding=self.encoding, stdin=f, tty=False, use_conn=self.conn,
1956
+ cqlver=self.cql_version, keyspace=self.current_keyspace,
1957
+ tracing_enabled=self.tracing_enabled,
1958
+ display_nanotime_format=self.display_nanotime_format,
1959
+ display_timestamp_format=self.display_timestamp_format,
1960
+ display_date_format=self.display_date_format,
1961
+ display_float_precision=self.display_float_precision,
1962
+ display_double_precision=self.display_double_precision,
1963
+ display_timezone=self.display_timezone,
1964
+ max_trace_wait=self.max_trace_wait, ssl=self.ssl,
1965
+ request_timeout=self.session.default_timeout,
1966
+ connect_timeout=self.conn.connect_timeout,
1967
+ is_subshell=True,
1968
+ auth_provider=self.auth_provider,
1969
+ )
1970
+ # duplicate coverage related settings in subshell
1971
+ if self.coverage:
1972
+ subshell.coverage = True
1973
+ subshell.coveragerc_path = self.coveragerc_path
1974
+ subshell.cmdloop()
1975
+ f.close()
1976
+
1977
+ def do_capture(self, parsed):
1978
+ """
1979
+ CAPTURE [cqlsh only]
1980
+
1981
+ Begins capturing command output and appending it to a specified file.
1982
+ Output will not be shown at the console while it is captured.
1983
+
1984
+ Usage:
1985
+
1986
+ CAPTURE '<file>';
1987
+ CAPTURE OFF;
1988
+ CAPTURE;
1989
+
1990
+ That is, the path to the file to be appended to must be given inside a
1991
+ string literal. The path is interpreted relative to the current working
1992
+ directory. The tilde shorthand notation ('~/mydir') is supported for
1993
+ referring to $HOME.
1994
+
1995
+ Only query result output is captured. Errors and output from cqlsh-only
1996
+ commands will still be shown in the cqlsh session.
1997
+
1998
+ To stop capturing output and show it in the cqlsh session again, use
1999
+ CAPTURE OFF.
2000
+
2001
+ To inspect the current capture configuration, use CAPTURE with no
2002
+ arguments.
2003
+ """
2004
+ fname = parsed.get_binding('fname')
2005
+ if fname is None:
2006
+ if self.shunted_query_out is not None:
2007
+ print("Currently capturing query output to %r." % (self.query_out.name,))
2008
+ else:
2009
+ print("Currently not capturing query output.")
2010
+ return
2011
+
2012
+ if fname.upper() == 'OFF':
2013
+ if self.shunted_query_out is None:
2014
+ self.printerr('Not currently capturing output.')
2015
+ return
2016
+ self.query_out.close()
2017
+ self.query_out = self.shunted_query_out
2018
+ self.color = self.shunted_color
2019
+ self.shunted_query_out = None
2020
+ del self.shunted_color
2021
+ return
2022
+
2023
+ if self.shunted_query_out is not None:
2024
+ self.printerr('Already capturing output to %s. Use CAPTURE OFF'
2025
+ ' to disable.' % (self.query_out.name,))
2026
+ return
2027
+
2028
+ fname = os.path.expanduser(self.cql_unprotect_value(fname))
2029
+ try:
2030
+ f = open(fname, 'a')
2031
+ except IOError as e:
2032
+ self.printerr('Could not open %r for append: %s' % (fname, e))
2033
+ return
2034
+ self.shunted_query_out = self.query_out
2035
+ self.shunted_color = self.color
2036
+ self.query_out = f
2037
+ self.color = False
2038
+ print('Now capturing query output to %r.' % (fname,))
2039
+
2040
+ def do_tracing(self, parsed):
2041
+ """
2042
+ TRACING [cqlsh]
2043
+
2044
+ Enables or disables request tracing.
2045
+
2046
+ TRACING ON
2047
+
2048
+ Enables tracing for all further requests.
2049
+
2050
+ TRACING OFF
2051
+
2052
+ Disables tracing.
2053
+
2054
+ TRACING
2055
+
2056
+ TRACING with no arguments shows the current tracing status.
2057
+ """
2058
+ self.tracing_enabled = SwitchCommand("TRACING", "Tracing").execute(self.tracing_enabled, parsed, self.printerr)
2059
+
2060
+ def do_expand(self, parsed):
2061
+ """
2062
+ EXPAND [cqlsh]
2063
+
2064
+ Enables or disables expanded (vertical) output.
2065
+
2066
+ EXPAND ON
2067
+
2068
+ Enables expanded (vertical) output.
2069
+
2070
+ EXPAND OFF
2071
+
2072
+ Disables expanded (vertical) output.
2073
+
2074
+ EXPAND
2075
+
2076
+ EXPAND with no arguments shows the current value of expand setting.
2077
+ """
2078
+ self.expand_enabled = SwitchCommand("EXPAND", "Expanded output").execute(self.expand_enabled, parsed, self.printerr)
2079
+
2080
+ def do_consistency(self, parsed):
2081
+ """
2082
+ CONSISTENCY [cqlsh only]
2083
+
2084
+ Overrides default consistency level (default level is ONE).
2085
+
2086
+ CONSISTENCY <level>
2087
+
2088
+ Sets consistency level for future requests.
2089
+
2090
+ Valid consistency levels:
2091
+
2092
+ ANY, ONE, TWO, THREE, QUORUM, ALL, LOCAL_ONE, LOCAL_QUORUM, EACH_QUORUM, SERIAL and LOCAL_SERIAL.
2093
+
2094
+ SERIAL and LOCAL_SERIAL may be used only for SELECTs; will be rejected with updates.
2095
+
2096
+ CONSISTENCY
2097
+
2098
+ CONSISTENCY with no arguments shows the current consistency level.
2099
+ """
2100
+ level = parsed.get_binding('level')
2101
+ if level is None:
2102
+ print('Current consistency level is %s.' % (cassandra.ConsistencyLevel.value_to_name[self.consistency_level]))
2103
+ return
2104
+
2105
+ self.consistency_level = cassandra.ConsistencyLevel.name_to_value[level.upper()]
2106
+ print('Consistency level set to %s.' % (level.upper(),))
2107
+
2108
+ def do_serial(self, parsed):
2109
+ """
2110
+ SERIAL CONSISTENCY [cqlsh only]
2111
+
2112
+ Overrides serial consistency level (default level is SERIAL).
2113
+
2114
+ SERIAL CONSISTENCY <level>
2115
+
2116
+ Sets consistency level for future conditional updates.
2117
+
2118
+ Valid consistency levels:
2119
+
2120
+ SERIAL, LOCAL_SERIAL.
2121
+
2122
+ SERIAL CONSISTENCY
2123
+
2124
+ SERIAL CONSISTENCY with no arguments shows the current consistency level.
2125
+ """
2126
+ level = parsed.get_binding('level')
2127
+ if level is None:
2128
+ print('Current serial consistency level is %s.' % (cassandra.ConsistencyLevel.value_to_name[self.serial_consistency_level]))
2129
+ return
2130
+
2131
+ self.serial_consistency_level = cassandra.ConsistencyLevel.name_to_value[level.upper()]
2132
+ print('Serial consistency level set to %s.' % (level.upper(),))
2133
+
2134
+ def do_login(self, parsed):
2135
+ """
2136
+ LOGIN [cqlsh only]
2137
+
2138
+ Changes login information without requiring restart.
2139
+
2140
+ LOGIN <username> (<password>)
2141
+
2142
+ Login using the specified username. If password is specified, it will be used
2143
+ otherwise, you will be prompted to enter.
2144
+ """
2145
+ username = parsed.get_binding('username')
2146
+ password = parsed.get_binding('password')
2147
+ if password is None:
2148
+ password = getpass.getpass()
2149
+ else:
2150
+ password = password[1:-1]
2151
+
2152
+ auth_provider = PlainTextAuthProvider(username=username, password=password)
2153
+
2154
+ kwargs = {}
2155
+ kwargs['contact_points'] = (self.hostname,)
2156
+ kwargs['port'] = self.port
2157
+ kwargs['ssl_context'] = self.conn.ssl_context
2158
+ kwargs['ssl_options'] = self.conn.ssl_options
2159
+
2160
+ conn = Cluster(cql_version=self.conn.cql_version,
2161
+ protocol_version=self.conn.protocol_version,
2162
+ auth_provider=auth_provider,
2163
+ control_connection_timeout=self.conn.connect_timeout,
2164
+ connect_timeout=self.conn.connect_timeout,
2165
+ execution_profiles=self.profiles,
2166
+ **kwargs)
2167
+
2168
+ if self.current_keyspace:
2169
+ session = conn.connect(self.current_keyspace)
2170
+ else:
2171
+ session = conn.connect()
2172
+
2173
+ # Copy session properties
2174
+ session.max_trace_wait = self.session.max_trace_wait
2175
+
2176
+ # Update after we've connected in case we fail to authenticate
2177
+ self.conn = conn
2178
+ self.auth_provider = auth_provider
2179
+ self.username = username
2180
+ self.session = session
2181
+
2182
+ def do_exit(self, parsed=None):
2183
+ """
2184
+ EXIT/QUIT [cqlsh only]
2185
+
2186
+ Exits cqlsh.
2187
+ """
2188
+ self.stop = True
2189
+ if self.owns_connection:
2190
+ self.conn.shutdown()
2191
+ do_quit = do_exit
2192
+
2193
+ def do_clear(self, parsed):
2194
+ """
2195
+ CLEAR/CLS [cqlsh only]
2196
+
2197
+ Clears the console.
2198
+ """
2199
+ subprocess.call('clear', shell=True)
2200
+ do_cls = do_clear
2201
+
2202
+ def do_debug(self, parsed):
2203
+ import pdb
2204
+ pdb.set_trace()
2205
+
2206
+ def get_help_topics(self):
2207
+ topics = [t[3:] for t in dir(self) if t.startswith('do_') and getattr(self, t, None).__doc__]
2208
+ for hide_from_help in ('quit',):
2209
+ topics.remove(hide_from_help)
2210
+ return topics
2211
+
2212
+ def columnize(self, slist, *a, **kw):
2213
+ return cmd.Cmd.columnize(self, sorted([u.upper() for u in slist]), *a, **kw)
2214
+
2215
+ def do_help(self, parsed):
2216
+ """
2217
+ HELP [cqlsh only]
2218
+
2219
+ Gives information about cqlsh commands. To see available topics,
2220
+ enter "HELP" without any arguments. To see help on a topic,
2221
+ use "HELP <topic>".
2222
+ """
2223
+ topics = parsed.get_binding('topic', ())
2224
+ if not topics:
2225
+ shell_topics = [t.upper() for t in self.get_help_topics()]
2226
+ self.print_topics("\nDocumented shell commands:", shell_topics, 15, 80)
2227
+ cql_topics = [t.upper() for t in cqldocs.get_help_topics()]
2228
+ self.print_topics("CQL help topics:", cql_topics, 15, 80)
2229
+ return
2230
+ for t in topics:
2231
+ if t.lower() in self.get_help_topics():
2232
+ doc = getattr(self, 'do_' + t.lower()).__doc__
2233
+ self.stdout.write(doc + "\n")
2234
+ elif t.lower() in cqldocs.get_help_topics():
2235
+ urlpart = cqldocs.get_help_topic(t)
2236
+ if urlpart is not None:
2237
+ url = "%s#%s" % (CASSANDRA_CQL_HTML, urlpart)
2238
+ if self.browser is not None:
2239
+ opened = webbrowser.get(self.browser).open_new_tab(url)
2240
+ else:
2241
+ opened = webbrowser.open_new_tab(url)
2242
+ if not opened:
2243
+ self.printerr("*** No browser to display CQL help. URL for help topic %s : %s" % (t, url))
2244
+ else:
2245
+ self.printerr("*** No help on %s" % (t,))
2246
+
2247
+ def do_unicode(self, parsed):
2248
+ """
2249
+ Textual input/output
2250
+
2251
+ When control characters, or other characters which can't be encoded
2252
+ in your current locale, are found in values of 'text' or 'ascii'
2253
+ types, it will be shown as a backslash escape. If color is enabled,
2254
+ any such backslash escapes will be shown in a different color from
2255
+ the surrounding text.
2256
+
2257
+ Unicode code points in your data will be output intact, if the
2258
+ encoding for your locale is capable of decoding them. If you prefer
2259
+ that non-ascii characters be shown with Python-style "\\uABCD"
2260
+ escape sequences, invoke cqlsh with an ASCII locale (for example,
2261
+ by setting the $LANG environment variable to "C").
2262
+ """
2263
+
2264
+ def do_paging(self, parsed):
2265
+ """
2266
+ PAGING [cqlsh]
2267
+
2268
+ Enables or disables query paging.
2269
+
2270
+ PAGING ON
2271
+
2272
+ Enables query paging for all further queries.
2273
+
2274
+ PAGING OFF
2275
+
2276
+ Disables paging.
2277
+
2278
+ PAGING
2279
+
2280
+ PAGING with no arguments shows the current query paging status.
2281
+ """
2282
+ (self.use_paging, requested_page_size) = SwitchCommandWithValue(
2283
+ "PAGING", "Query paging", value_type=int).execute(self.use_paging, parsed, self.printerr)
2284
+ if self.use_paging and requested_page_size is not None:
2285
+ self.page_size = requested_page_size
2286
+ if self.use_paging:
2287
+ print(("Page size: {}".format(self.page_size)))
2288
+ else:
2289
+ self.page_size = self.default_page_size
2290
+
2291
+ def applycolor(self, text, color=None):
2292
+ if not color or not self.color:
2293
+ return text
2294
+ return color + text + ANSI_RESET
2295
+
2296
+ def writeresult(self, text, color=None, newline=True, out=None):
2297
+ if out is None:
2298
+ out = self.query_out
2299
+
2300
+ # convert Exceptions, etc to text
2301
+ if not isinstance(text, str):
2302
+ text = str(text)
2303
+
2304
+ to_write = self.applycolor(text, color) + ('\n' if newline else '')
2305
+ out.write(to_write)
2306
+
2307
+ def flush_output(self):
2308
+ self.query_out.flush()
2309
+
2310
+ def printerr(self, text, color=RED, newline=True, shownum=None):
2311
+ self.statement_error = True
2312
+ if shownum is None:
2313
+ shownum = self.show_line_nums
2314
+ if shownum:
2315
+ text = '%s:%d:%s' % (self.stdin.name, self.lineno, text)
2316
+ self.writeresult(text, color, newline=newline, out=sys.stderr)
2317
+
2318
+ def printwarn(self, text, color=YELLOW, newline=True, shownum=None):
2319
+ if shownum is None:
2320
+ shownum = self.show_line_nums
2321
+ if shownum:
2322
+ text = '%s:%d:%s' % (self.stdin.name, self.lineno, text)
2323
+ self.writeresult(text, color, newline=newline, out=sys.stderr)
2324
+
2325
+ def stop_coverage(self):
2326
+ if self.coverage and self.cov is not None:
2327
+ self.cov.stop()
2328
+ self.cov.save()
2329
+ self.cov = None
2330
+
2331
+
2332
+ class SwitchCommand(object):
2333
+ command = None
2334
+ description = None
2335
+
2336
+ def __init__(self, command, desc):
2337
+ self.command = command
2338
+ self.description = desc
2339
+
2340
+ def execute(self, state, parsed, printerr):
2341
+ switch = parsed.get_binding('switch')
2342
+ if switch is None:
2343
+ if state:
2344
+ print("%s is currently enabled. Use %s OFF to disable"
2345
+ % (self.description, self.command))
2346
+ else:
2347
+ print("%s is currently disabled. Use %s ON to enable."
2348
+ % (self.description, self.command))
2349
+ return state
2350
+
2351
+ if switch.upper() == 'ON':
2352
+ if state:
2353
+ printerr('%s is already enabled. Use %s OFF to disable.'
2354
+ % (self.description, self.command))
2355
+ return state
2356
+ print('Now %s is enabled' % (self.description,))
2357
+ return True
2358
+
2359
+ if switch.upper() == 'OFF':
2360
+ if not state:
2361
+ printerr('%s is not enabled.' % (self.description,))
2362
+ return state
2363
+ print('Disabled %s.' % (self.description,))
2364
+ return False
2365
+
2366
+
2367
+ class SwitchCommandWithValue(SwitchCommand):
2368
+ """The same as SwitchCommand except it also accepts a value in place of ON.
2369
+
2370
+ This returns a tuple of the form: (SWITCH_VALUE, PASSED_VALUE)
2371
+ eg: PAGING 50 returns (True, 50)
2372
+ PAGING OFF returns (False, None)
2373
+ PAGING ON returns (True, None)
2374
+
2375
+ The value_type must match for the PASSED_VALUE, otherwise it will return None.
2376
+ """
2377
+ def __init__(self, command, desc, value_type=int):
2378
+ SwitchCommand.__init__(self, command, desc)
2379
+ self.value_type = value_type
2380
+
2381
+ def execute(self, state, parsed, printerr):
2382
+ binary_switch_value = SwitchCommand.execute(self, state, parsed, printerr)
2383
+ switch = parsed.get_binding('switch')
2384
+ try:
2385
+ value = self.value_type(switch)
2386
+ binary_switch_value = True
2387
+ except (ValueError, TypeError):
2388
+ value = None
2389
+ return binary_switch_value, value
2390
+
2391
+
2392
+ def option_with_default(cparser_getter, section, option, default=None):
2393
+ try:
2394
+ return cparser_getter(section, option)
2395
+ except configparser.Error:
2396
+ return default
2397
+
2398
+
2399
+ def raw_option_with_default(configs, section, option, default=None):
2400
+ """
2401
+ Same (almost) as option_with_default() but won't do any string interpolation.
2402
+ Useful for config values that include '%' symbol, e.g. time format string.
2403
+ """
2404
+ try:
2405
+ return configs.get(section, option, raw=True)
2406
+ except configparser.Error:
2407
+ return default
2408
+
2409
+
2410
+ def should_use_color():
2411
+ if not sys.stdout.isatty():
2412
+ return False
2413
+ if os.environ.get('TERM', '') in ('dumb', ''):
2414
+ return False
2415
+ try:
2416
+ p = subprocess.Popen(['tput', 'colors'], stdout=subprocess.PIPE)
2417
+ stdout, _ = p.communicate()
2418
+ if int(stdout.strip()) < 8:
2419
+ return False
2420
+ except (OSError, ImportError, ValueError):
2421
+ # oh well, we tried. at least we know there's a $TERM and it's
2422
+ # not "dumb".
2423
+ pass
2424
+ return True
2425
+
2426
+
2427
+ def read_options(cmdlineargs, environment):
2428
+ configs = configparser.ConfigParser()
2429
+ configs.read(CONFIG_FILE)
2430
+
2431
+ rawconfigs = configparser.RawConfigParser()
2432
+ rawconfigs.read(CONFIG_FILE)
2433
+
2434
+ username_from_cqlshrc = option_with_default(configs.get, 'authentication', 'username')
2435
+ password_from_cqlshrc = option_with_default(rawconfigs.get, 'authentication', 'password')
2436
+ if username_from_cqlshrc or password_from_cqlshrc:
2437
+ if password_from_cqlshrc and not is_file_secure(os.path.expanduser(CONFIG_FILE)):
2438
+ print("\nWarning: Password is found in an insecure cqlshrc file. The file is owned or readable by other users on the system.",
2439
+ end='', file=sys.stderr)
2440
+ print("\nNotice: Credentials in the cqlshrc file is deprecated and will be ignored in the future."
2441
+ "\nPlease use a credentials file to specify the username and password.\n"
2442
+ "\nTo use basic authentication, place the username and password in the [PlainTextAuthProvider] section of the credentials file.\n", file=sys.stderr)
2443
+
2444
+ optvalues = optparse.Values()
2445
+
2446
+ optvalues.username = None
2447
+ optvalues.password = None
2448
+ optvalues.credentials = os.path.expanduser(option_with_default(configs.get, 'authentication', 'credentials',
2449
+ os.path.join(CQL_DIR, 'credentials')))
2450
+ optvalues.keyspace = option_with_default(configs.get, 'authentication', 'keyspace')
2451
+ optvalues.browser = option_with_default(configs.get, 'ui', 'browser', None)
2452
+ optvalues.completekey = option_with_default(configs.get, 'ui', 'completekey',
2453
+ DEFAULT_COMPLETEKEY)
2454
+ optvalues.color = option_with_default(configs.getboolean, 'ui', 'color')
2455
+ optvalues.time_format = raw_option_with_default(configs, 'ui', 'time_format',
2456
+ DEFAULT_TIMESTAMP_FORMAT)
2457
+ optvalues.nanotime_format = raw_option_with_default(configs, 'ui', 'nanotime_format',
2458
+ DEFAULT_NANOTIME_FORMAT)
2459
+ optvalues.date_format = raw_option_with_default(configs, 'ui', 'date_format',
2460
+ DEFAULT_DATE_FORMAT)
2461
+ optvalues.float_precision = option_with_default(configs.getint, 'ui', 'float_precision',
2462
+ DEFAULT_FLOAT_PRECISION)
2463
+ optvalues.double_precision = option_with_default(configs.getint, 'ui', 'double_precision',
2464
+ DEFAULT_DOUBLE_PRECISION)
2465
+ optvalues.field_size_limit = option_with_default(configs.getint, 'csv', 'field_size_limit', csv.field_size_limit())
2466
+ optvalues.max_trace_wait = option_with_default(configs.getfloat, 'tracing', 'max_trace_wait',
2467
+ DEFAULT_MAX_TRACE_WAIT)
2468
+ optvalues.timezone = option_with_default(configs.get, 'ui', 'timezone', None)
2469
+
2470
+ optvalues.debug = False
2471
+ optvalues.driver_debug = False
2472
+ optvalues.coverage = False
2473
+ if 'CQLSH_COVERAGE' in environment.keys():
2474
+ optvalues.coverage = True
2475
+
2476
+ optvalues.file = None
2477
+ optvalues.ssl = option_with_default(configs.getboolean, 'connection', 'ssl', DEFAULT_SSL)
2478
+ optvalues.encoding = option_with_default(configs.get, 'ui', 'encoding', UTF8)
2479
+
2480
+ optvalues.tty = option_with_default(configs.getboolean, 'ui', 'tty', sys.stdin.isatty())
2481
+ optvalues.protocol_version = option_with_default(configs.getint, 'protocol', 'version', 4)
2482
+ optvalues.cqlversion = option_with_default(configs.get, 'cql', 'version', None)
2483
+ optvalues.connect_timeout = option_with_default(configs.getint, 'connection', 'timeout', DEFAULT_CONNECT_TIMEOUT_SECONDS)
2484
+ optvalues.request_timeout = option_with_default(configs.getint, 'connection', 'request_timeout', DEFAULT_REQUEST_TIMEOUT_SECONDS)
2485
+ optvalues.execute = None
2486
+ optvalues.insecure_password_without_warning = False
2487
+
2488
+ (options, arguments) = parser.parse_args(cmdlineargs, values=optvalues)
2489
+
2490
+ # Credentials from cqlshrc will be expanded,
2491
+ # credentials from the command line are also expanded if there is a space...
2492
+ # we need the following so that these two scenarios will work
2493
+ # cqlsh --credentials=~/.cassandra/creds
2494
+ # cqlsh --credentials ~/.cassandra/creds
2495
+ options.credentials = os.path.expanduser(options.credentials)
2496
+
2497
+ if not is_file_secure(options.credentials):
2498
+ print("\nWarning: Credentials file '{0}' exists but is not used, because:"
2499
+ "\n a. the file owner is not the current user; or"
2500
+ "\n b. the file is readable by group or other."
2501
+ "\nPlease ensure the file is owned by the current user and is not readable by group or other."
2502
+ "\nOn a Linux or UNIX-like system, you often can do this by using the `chown` and `chmod` commands:"
2503
+ "\n chown YOUR_USERNAME credentials"
2504
+ "\n chmod 600 credentials\n".format(options.credentials),
2505
+ file=sys.stderr)
2506
+ options.credentials = '' # ConfigParser.read() will ignore unreadable files
2507
+
2508
+ if not options.username:
2509
+ credentials = configparser.ConfigParser()
2510
+ credentials.read(options.credentials)
2511
+
2512
+ # use the username from credentials file but fallback to cqlshrc if username is absent from the command line parameters
2513
+ options.username = option_with_default(credentials.get, 'plain_text_auth', 'username', username_from_cqlshrc)
2514
+
2515
+ if not options.password:
2516
+ rawcredentials = configparser.RawConfigParser()
2517
+ rawcredentials.read(options.credentials)
2518
+
2519
+ # handling password in the same way as username, priority cli > credentials > cqlshrc
2520
+ options.password = option_with_default(rawcredentials.get, 'plain_text_auth', 'password', password_from_cqlshrc)
2521
+ elif not options.insecure_password_without_warning:
2522
+ print("\nWarning: Using a password on the command line interface can be insecure."
2523
+ "\nRecommendation: use the credentials file to securely provide the password.\n", file=sys.stderr)
2524
+
2525
+ # Make sure some user values read from the command line are in unicode
2526
+ options.execute = maybe_ensure_text(options.execute)
2527
+ options.username = maybe_ensure_text(options.username)
2528
+ options.password = maybe_ensure_text(options.password)
2529
+ options.keyspace = maybe_ensure_text(options.keyspace)
2530
+
2531
+ hostname = option_with_default(configs.get, 'connection', 'hostname', DEFAULT_HOST)
2532
+ port = option_with_default(configs.get, 'connection', 'port', DEFAULT_PORT)
2533
+
2534
+ try:
2535
+ options.connect_timeout = int(options.connect_timeout)
2536
+ except ValueError:
2537
+ parser.error('"%s" is not a valid connect timeout.' % (options.connect_timeout,))
2538
+ options.connect_timeout = DEFAULT_CONNECT_TIMEOUT_SECONDS
2539
+
2540
+ try:
2541
+ options.request_timeout = int(options.request_timeout)
2542
+ except ValueError:
2543
+ parser.error('"%s" is not a valid request timeout.' % (options.request_timeout,))
2544
+ options.request_timeout = DEFAULT_REQUEST_TIMEOUT_SECONDS
2545
+
2546
+ hostname = environment.get('CQLSH_HOST', hostname)
2547
+ port = environment.get('CQLSH_PORT', port)
2548
+
2549
+ if len(arguments) > 0:
2550
+ hostname = arguments[0]
2551
+ if len(arguments) > 1:
2552
+ port = arguments[1]
2553
+
2554
+ if options.file or options.execute:
2555
+ options.tty = False
2556
+
2557
+ if options.execute and not options.execute.endswith(';'):
2558
+ options.execute += ';'
2559
+
2560
+ if optvalues.color in (True, False):
2561
+ options.color = optvalues.color
2562
+ else:
2563
+ if options.file is not None:
2564
+ options.color = False
2565
+ else:
2566
+ options.color = should_use_color()
2567
+
2568
+ if options.cqlversion is not None:
2569
+ options.cqlversion, cqlvertup = full_cql_version(options.cqlversion)
2570
+ if cqlvertup[0] < 3:
2571
+ parser.error('%r is not a supported CQL version.' % options.cqlversion)
2572
+ options.cqlmodule = cql3handling
2573
+
2574
+ try:
2575
+ port = int(port)
2576
+ except ValueError:
2577
+ parser.error('%r is not a valid port number.' % port)
2578
+ return options, hostname, port
2579
+
2580
+
2581
+ def setup_cqlruleset(cqlmodule):
2582
+ global cqlruleset
2583
+ cqlruleset = cqlmodule.CqlRuleSet
2584
+ cqlruleset.append_rules(cqlshhandling.cqlsh_extra_syntax_rules)
2585
+ for rulename, termname, func in cqlshhandling.cqlsh_syntax_completers:
2586
+ cqlruleset.completer_for(rulename, termname)(func)
2587
+ cqlruleset.commands_end_with_newline.update(cqlshhandling.my_commands_ending_with_newline)
2588
+
2589
+
2590
+ def setup_cqldocs(cqlmodule):
2591
+ global cqldocs
2592
+ cqldocs = cqlmodule.cqldocs
2593
+
2594
+
2595
+ def init_history():
2596
+ if readline is not None:
2597
+ try:
2598
+ readline.read_history_file(HISTORY)
2599
+ except IOError:
2600
+ pass
2601
+ delims = readline.get_completer_delims()
2602
+ delims.replace("'", "")
2603
+ delims += '.'
2604
+ readline.set_completer_delims(delims)
2605
+
2606
+
2607
+ def save_history():
2608
+ if readline is not None:
2609
+ try:
2610
+ readline.write_history_file(HISTORY)
2611
+ except IOError:
2612
+ pass
2613
+
2614
+
2615
+ def main(options, hostname, port):
2616
+ if options.driver_debug:
2617
+ logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
2618
+ stream=sys.stdout)
2619
+ logging.getLogger("cassandra").setLevel(logging.DEBUG)
2620
+
2621
+ setup_cqlruleset(options.cqlmodule)
2622
+ setup_cqldocs(options.cqlmodule)
2623
+ init_history()
2624
+ csv.field_size_limit(options.field_size_limit)
2625
+
2626
+ if options.file is None:
2627
+ stdin = None
2628
+ else:
2629
+ try:
2630
+ encoding, bom_size = get_file_encoding_bomsize(options.file)
2631
+ stdin = codecs.open(options.file, 'r', encoding)
2632
+ stdin.seek(bom_size)
2633
+ except IOError as e:
2634
+ sys.exit("Can't open %r: %s" % (options.file, e))
2635
+
2636
+ if options.debug:
2637
+ sys.stderr.write("Using CQL driver: %s\n" % (cassandra,))
2638
+ sys.stderr.write("Using connect timeout: %s seconds\n" % (options.connect_timeout,))
2639
+ sys.stderr.write("Using '%s' encoding\n" % (options.encoding,))
2640
+ sys.stderr.write("Using ssl: %s\n" % (options.ssl,))
2641
+
2642
+ # create timezone based on settings, environment or auto-detection
2643
+ timezone = None
2644
+ if options.timezone or 'TZ' in os.environ:
2645
+ try:
2646
+ import pytz
2647
+ if options.timezone:
2648
+ try:
2649
+ timezone = pytz.timezone(options.timezone)
2650
+ except Exception:
2651
+ sys.stderr.write("Warning: could not recognize timezone '%s' specified in cqlshrc\n\n" % (options.timezone))
2652
+ if 'TZ' in os.environ:
2653
+ try:
2654
+ timezone = pytz.timezone(os.environ['TZ'])
2655
+ except Exception:
2656
+ sys.stderr.write("Warning: could not recognize timezone '%s' from environment value TZ\n\n" % (os.environ['TZ']))
2657
+ except ImportError:
2658
+ sys.stderr.write("Warning: Timezone defined and 'pytz' module for timezone conversion not installed. Timestamps will be displayed in UTC timezone.\n\n")
2659
+
2660
+ # try auto-detect timezone if tzlocal is installed
2661
+ if not timezone:
2662
+ try:
2663
+ from tzlocal import get_localzone
2664
+ timezone = get_localzone()
2665
+ except ImportError:
2666
+ # we silently ignore and fallback to UTC unless a custom timestamp format (which likely
2667
+ # does contain a TZ part) was specified
2668
+ if options.time_format != DEFAULT_TIMESTAMP_FORMAT:
2669
+ sys.stderr.write("Warning: custom timestamp format specified in cqlshrc, "
2670
+ + "but local timezone could not be detected.\n"
2671
+ + "Either install Python 'tzlocal' module for auto-detection "
2672
+ + "or specify client timezone in your cqlshrc.\n\n")
2673
+
2674
+ try:
2675
+ shell = Shell(hostname,
2676
+ port,
2677
+ color=options.color,
2678
+ username=options.username,
2679
+ stdin=stdin,
2680
+ tty=options.tty,
2681
+ completekey=options.completekey,
2682
+ browser=options.browser,
2683
+ protocol_version=options.protocol_version,
2684
+ cqlver=options.cqlversion,
2685
+ keyspace=options.keyspace,
2686
+ display_timestamp_format=options.time_format,
2687
+ display_nanotime_format=options.nanotime_format,
2688
+ display_date_format=options.date_format,
2689
+ display_float_precision=options.float_precision,
2690
+ display_double_precision=options.double_precision,
2691
+ display_timezone=timezone,
2692
+ max_trace_wait=options.max_trace_wait,
2693
+ ssl=options.ssl,
2694
+ single_statement=options.execute,
2695
+ request_timeout=options.request_timeout,
2696
+ connect_timeout=options.connect_timeout,
2697
+ encoding=options.encoding,
2698
+ auth_provider=authproviderhandling.load_auth_provider(
2699
+ config_file=CONFIG_FILE,
2700
+ cred_file=options.credentials,
2701
+ username=options.username,
2702
+ password=options.password),
2703
+ )
2704
+ except KeyboardInterrupt:
2705
+ sys.exit('Connection aborted.')
2706
+ except CQL_ERRORS as e:
2707
+ sys.exit('Connection error: %s' % (e,))
2708
+ except VersionNotSupported as e:
2709
+ sys.exit('Unsupported CQL version: %s' % (e,))
2710
+ if options.debug:
2711
+ shell.debug = True
2712
+ if options.coverage:
2713
+ shell.coverage = True
2714
+ import signal
2715
+
2716
+ def handle_sighup():
2717
+ shell.stop_coverage()
2718
+ shell.do_exit()
2719
+
2720
+ signal.signal(signal.SIGHUP, handle_sighup)
2721
+
2722
+ shell.cmdloop()
2723
+ save_history()
2724
+
2725
+ if shell.batch_mode and shell.statement_error:
2726
+ sys.exit(2)
2727
+
2728
+
2729
+ # always call this regardless of module name: when a sub-process is spawned
2730
+ # on Windows then the module name is not __main__, see CASSANDRA-9304 (Windows support was dropped in CASSANDRA-16956)
2731
+ insert_driver_hooks()
2732
+
2733
+ if __name__ == '__main__':
2734
+ main(*read_options(sys.argv[1:], os.environ))
2735
+
2736
+ # vim: set ft=python et ts=4 sw=4 :