def _combine_cmd_params(cmd, params, conn): """Combine the command string and params""" if isinstance(cmd, six.text_type): cmd = cmd.encode(conn._py_enc) # Return when no argument binding is required. Note that this method is # not called from .execute() if `params` is None. if b'%' not in cmd: return cmd idx = 0 next_start = 0 param_num = 0 n_arg_values = 0 arg_values = None named_args_format = None parts = [] cmd_length = len(cmd) while idx < cmd_length: # Escape if cmd[idx:idx + 2] == b'%%': parts.append(cmd[next_start:idx]) parts.append(b'%') idx += 1 next_start = idx + 1 # Named parameters elif cmd[idx:idx + 2] == b'%(': # Validate that we don't mix formats if named_args_format is False: raise ValueError("argument formats can't be mixed") elif named_args_format is None: named_args_format = True # Check for incomplate placeholder max_lookahead = cmd.find(b'%', idx + 2) end = cmd.find(b')', idx + 2, max_lookahead) if end < 0: raise ProgrammingError( "incomplete placeholder: '%(' without ')'") key = cmd[idx + 2:end].decode(conn._py_enc) if arg_values is None: arg_values = {} if key not in arg_values: arg_values[key] = _getquoted(params[key], conn) parts.append(cmd[next_start:idx]) if six.PY3 and isinstance(arg_values[key], six.text_type): arg_values[key] = arg_values[key].encode(conn._py_enc) parts.append(arg_values[key]) n_arg_values += 1 next_start = end + 2 _check_format_char(cmd[end + 1], idx) # Indexed parameters elif cmd[idx:idx + 1] == b'%': # Validate that we don't mix formats if named_args_format is True: raise ValueError("argument formats can't be mixed") elif named_args_format is None: named_args_format = False _check_format_char(cmd[idx + 1], idx) value = _getquoted(params[param_num], conn) if six.PY3 and isinstance(value, six.text_type): value = value.encode(conn._py_enc) n_arg_values += 1 parts.append(cmd[next_start:idx]) parts.append(value) next_start = idx + 2 param_num += 1 idx += 1 idx += 1 parts.append(cmd[next_start:cmd_length]) if named_args_format is False: if n_arg_values != len(params): raise TypeError( "not all arguments converted during string formatting") return b''.join(parts)
def _combine_cmd_params(cmd, params, conn): """Combine the command string and params""" if isinstance(cmd, six.text_type): cmd = cmd.encode(conn._py_enc) # Return when no argument binding is required. Note that this method is # not called from .execute() if `params` is None. if b'%' not in cmd: return cmd idx = 0 next_start = 0 param_num = 0 n_arg_values = 0 arg_values = None named_args_format = None parts = [] cmd_length = len(cmd) while idx < cmd_length: # Escape if cmd[idx:idx+2] == b'%%': parts.append(cmd[next_start:idx]) parts.append(b'%') idx += 1 next_start = idx + 1 # Named parameters elif cmd[idx:idx+2] == b'%(': # Validate that we don't mix formats if named_args_format is False: raise ValueError("argument formats can't be mixed") elif named_args_format is None: named_args_format = True # Check for incomplate placeholder max_lookahead = cmd.find(b'%', idx + 2) end = cmd.find(b')', idx + 2, max_lookahead) if end < 0: raise ProgrammingError( "incomplete placeholder: '%(' without ')'") key = cmd[idx + 2:end].decode(conn._py_enc) if arg_values is None: arg_values = {} if key not in arg_values: arg_values[key] = _getquoted(params[key], conn) parts.append(cmd[next_start:idx]) if six.PY3 and isinstance(arg_values[key], six.text_type): arg_values[key] = arg_values[key].encode(conn._py_enc) parts.append(arg_values[key]) n_arg_values += 1 next_start = end + 2 _check_format_char(cmd[end + 1], idx) # Indexed parameters elif cmd[idx:idx+1] == b'%': # Validate that we don't mix formats if named_args_format is True: raise ValueError("argument formats can't be mixed") elif named_args_format is None: named_args_format = False _check_format_char(cmd[idx + 1], idx) value = _getquoted(params[param_num], conn) if six.PY3 and isinstance(value, six.text_type): value = value.encode(conn._py_enc) n_arg_values += 1 parts.append(cmd[next_start:idx]) parts.append(value) next_start = idx + 2 param_num += 1 idx += 1 idx += 1 parts.append(cmd[next_start:cmd_length]) if named_args_format is False: if n_arg_values != len(params): raise TypeError( "not all arguments converted during string formatting") return b''.join(parts)
def _combine_cmd_params(cmd, params, conn): """Combine the command string and params""" # Return when no argument binding is required. Note that this method is # not called from .execute() if `params` is None. if '%' not in cmd: return cmd idx = 0 param_num = 0 arg_values = None named_args_format = None def check_format_char(format_char, pos): """Raise an exception when the format_char is unsupported""" if format_char not in 's ': raise ValueError( "unsupported format character '%s' (0x%x) at index %d" % (format_char, ord(format_char), pos)) cmd_length = len(cmd) while idx < cmd_length: # Escape if cmd[idx] == '%' and cmd[idx + 1] == '%': idx += 1 # Named parameters elif cmd[idx] == '%' and cmd[idx + 1] == '(': # Validate that we don't mix formats if named_args_format is False: raise ValueError("argument formats can't be mixed") elif named_args_format is None: named_args_format = True # Check for incomplate placeholder max_lookahead = cmd.find('%', idx + 2) end = cmd.find(')', idx + 2, max_lookahead) if end < 0: raise ProgrammingError( "incomplete placeholder: '%(' without ')'") key = cmd[idx + 2:end] if arg_values is None: arg_values = {} if key not in arg_values: arg_values[key] = _getquoted(params[key], conn) check_format_char(cmd[end + 1], idx) # Indexed parameters elif cmd[idx] == '%': # Validate that we don't mix formats if named_args_format is True: raise ValueError("argument formats can't be mixed") elif named_args_format is None: named_args_format = False check_format_char(cmd[idx + 1], idx) if arg_values is None: arg_values = [] value = _getquoted(params[param_num], conn) arg_values.append(value) param_num += 1 idx += 1 idx += 1 if named_args_format is False: if len(arg_values) != len(params): raise TypeError( "not all arguments converted during string formatting") arg_values = tuple(arg_values) if not arg_values: return cmd % tuple() # Required to unescape % chars return cmd % arg_values