def _encode_ssm(value): builder = deque() prefix = "{" for k, v in value.iteritems(): builder.extend((prefix, cql_quote(k), ":", cql_quote(v))) prefix = "," builder.append("}") return "".join(builder)
def __unicode__(self): from cassandra.encoder import cql_quote if isinstance(self.value, (list, tuple)): return '[' + ', '.join([cql_quote(v) for v in self.value]) + ']' elif isinstance(self.value, dict): return '{' + ', '.join([cql_quote(k) + ':' + cql_quote(v) for k, v in self.value.items()]) + '}' elif isinstance(self.value, set): return '{' + ', '.join([cql_quote(v) for v in self.value]) + '}' return cql_quote(self.value)
def _encode_ssm(value): builder = collections.deque() prefix = '{' for k, v in value.iteritems(): builder.extend((prefix, encoder.cql_quote(k), ":", encoder.cql_quote(v))) prefix = "," builder.append('}') return "".join(builder)
def __glob_names(self, table, glob): components = self._components_from_name(glob) if len(components) > _COMPONENTS_MAX_LEN: msg = "Metric globs can have a maximum of %d dots" % _COMPONENTS_MAX_LEN - 2 raise bg_accessor.InvalidGlobError(msg) where = [ "component_%d = %s" % (n, c_encoder.cql_quote(s)) for n, s in enumerate(components) if s != "*" ] query = " ".join([ "SELECT name FROM \"%s\".\"%s\" WHERE" % (self.keyspace_metadata, table), " AND ".join(where), "LIMIT %d ALLOW FILTERING;" % (self.MAX_METRIC_PER_GLOB + 1), ]) try: metrics_names = [r.name for r in self.__session.execute(query)] except Exception as e: raise RetryableCassandraError(e) if len(metrics_names) > self.MAX_METRIC_PER_GLOB: msg = "%s yields more than %d results" % (glob, self.MAX_METRIC_PER_GLOB) raise TooManyMetrics(msg) metrics_names.sort() return metrics_names
def _encode_bsm(value): builder = collections.deque() prefix = '{' for k, v in value.iteritems(): builder.extend((prefix, _encode_b(k), ":", encoder.cql_quote(v))) prefix = "," builder.append('}') return "".join(builder)
def _encode_nsm(value): builder = deque() prefix = '{' for k, v in value.iteritems(): builder.extend((prefix, str(k), ":", cql_quote(v))) prefix = "," builder.append('}') return "".join(builder)
def __unicode__(self): from cassandra.encoder import cql_quote vals = [] for v in self.value: if isinstance(v, bool): vals.append('true' if v else 'false') else: vals.append(cql_quote(v)) return '(' + ', '.join(vals) + ')'
def __unicode__(self): from cassandra.encoder import cql_quote vals = [] for v in self.value: if isinstance(v, bool): vals.append("true" if v else "false") else: vals.append(cql_quote(v)) return "(" + ", ".join(vals) + ")"
def among(self, column, values: List[Any] = None): """Select list of values to query on a specified column. This column must be an optional column in the table yaml. If `values` parameter is missing or set to `None`, it will be set as a named parameter with the column name. :param column: column name :param values: list of values to filter :return: self """ if values is None: replacement = f"{column} IN (?)" else: values_str = ', '.join([cql_quote(v) for v in values]) replacement = f"{column} IN ({values_str})" statements = self._prepare_column_clause(column) self._current_statements = \ [self._replace(s, column, replacement) for s in statements] return self
def test_cql_quote(self): self.assertEqual(cql_quote(u'test'), "'test'") self.assertEqual(cql_quote('test'), "'test'") self.assertEqual(cql_quote(0), '0')
def __unicode__(self): from cassandra.encoder import cql_quote return '(' + ', '.join([cql_quote(v) for v in self.value]) + ')'
def __build_select_names_query(self, table, components): query_select = "SELECT name FROM \"%s\".\"%s\"" % ( self.keyspace_metadata, table, ) query_limit = "LIMIT %d" % (self.max_metrics_per_pattern + 1) if len(components) == 0: return "%s %s;" % (query_select, query_limit) # If all components are constant values we can search by exact name. # If all but the last component are constant values we can search by # exact parent, in which case we may benefit from filtering the last # component by prefix when we have one. (Code refers to the previous-to # -last component because of the __END__ suffix we use). # # We are not using prefix search on the parent because it appears to be # too slow/costly at the moment (see #174 for details). if (components[-1] == [_LAST_COMPONENT] and # Not a prefix globstar all( len(c) == 1 and bg_glob.is_fixed_sequence(c[0]) for c in components[:-2])): last = components[-2] if len(last) == 1 and bg_glob.is_fixed_sequence(last[0]): # XXX(d.forest): do not try to optimize by passing the raw glob # and using it here; because this is invalid in # cases where the glob contains braces. name = DIRECTORY_SEPARATOR.join( itertools.chain.from_iterable(components[:-1])) return "%s WHERE name = %s %s;" % ( query_select, c_encoder.cql_quote(name), query_limit, ) else: if len(last) > 0 and bg_glob.is_fixed_sequence(last[0]): prefix_filter = "AND component_%d LIKE %s" % ( len(components) - 2, c_encoder.cql_quote(last[0] + '%'), ) allow_filtering = "ALLOW FILTERING" else: prefix_filter = '' allow_filtering = '' parent = itertools.chain.from_iterable(components[:-2]) parent = DIRECTORY_SEPARATOR.join(parent) + DIRECTORY_SEPARATOR return "%s WHERE parent = %s %s %s %s;" % ( query_select, c_encoder.cql_quote(parent), prefix_filter, query_limit, allow_filtering, ) where_clauses = [] for n, component in enumerate(components): if len(component) == 0: continue # We are currently using prefix indexes, so if we do not have a # prefix value (i.e. it is a wildcard), then the current component # cannot be constrained inside the request. value = component[0] if not bg_glob.is_fixed_sequence(value): continue if len(component) == 1: op = '=' else: op = "LIKE" value += '%' clause = "component_%d %s %s" % (n, op, c_encoder.cql_quote(value)) where_clauses.append(clause) if len(where_clauses) == 0: return "%s %s;" % (query_select, query_limit) return "%s WHERE %s %s ALLOW FILTERING;" % ( query_select, " AND ".join(where_clauses), query_limit)
def __build_select_names_query(self, table, components): query_select = 'SELECT name FROM "%s"."%s"' % (self.keyspace_metadata, table) query_limit = "LIMIT %d" % (self.max_metrics_per_pattern + 1) if len(components) == 0: return "%s %s;" % (query_select, query_limit) # If all components are constant values we can search by exact name. # If all but the last component are constant values we can search by # exact parent, in which case we may benefit from filtering the last # component by prefix when we have one. (Code refers to the previous-to # -last component because of the __END__ suffix we use). # # We are not using prefix search on the parent because it appears to be # too slow/costly at the moment (see #174 for details). if components[-1] == [_LAST_COMPONENT] and all( # Not a prefix globstar len(c) == 1 and bg_glob.is_fixed_sequence(c[0]) for c in components[:-2] ): last = components[-2] if len(last) == 1 and bg_glob.is_fixed_sequence(last[0]): # XXX(d.forest): do not try to optimize by passing the raw glob # and using it here; because this is invalid in # cases where the glob contains braces. name = DIRECTORY_SEPARATOR.join( itertools.chain.from_iterable(components[:-1]) ) return "%s WHERE name = %s %s;" % ( query_select, c_encoder.cql_quote(name), query_limit, ) else: if len(last) > 0 and bg_glob.is_fixed_sequence(last[0]): prefix_filter = "AND component_%d LIKE %s" % ( len(components) - 2, c_encoder.cql_quote(last[0] + "%"), ) allow_filtering = "ALLOW FILTERING" else: prefix_filter = "" allow_filtering = "" parent = itertools.chain.from_iterable(components[:-2]) parent = DIRECTORY_SEPARATOR.join(parent) + DIRECTORY_SEPARATOR return "%s WHERE parent = %s %s %s %s;" % ( query_select, c_encoder.cql_quote(parent), prefix_filter, query_limit, allow_filtering, ) where_clauses = [] for n, component in enumerate(components): if len(component) == 0: continue # We are currently using prefix indexes, so if we do not have a # prefix value (i.e. it is a wildcard), then the current component # cannot be constrained inside the request. value = component[0] if not bg_glob.is_fixed_sequence(value): continue if len(component) == 1: op = "=" else: op = "LIKE" value += "%" clause = "component_%d %s %s" % (n, op, c_encoder.cql_quote(value)) where_clauses.append(clause) if len(where_clauses) == 0: return "%s %s;" % (query_select, query_limit) return "%s WHERE %s %s ALLOW FILTERING;" % ( query_select, " AND ".join(where_clauses), query_limit, )