From fb2de5746c9bbfd1e66ba0b59bb6949bb3ebc5e9 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 1 Jan 2019 17:43:56 -0800 Subject: [PATCH] Add function sqlhelpers.literal. --- voussoirkit/sqlhelpers.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/voussoirkit/sqlhelpers.py b/voussoirkit/sqlhelpers.py index 9a19964..5ae55f8 100644 --- a/voussoirkit/sqlhelpers.py +++ b/voussoirkit/sqlhelpers.py @@ -107,3 +107,45 @@ def update_filler(pairs, where_key): qmarks = 'SET {setters} WHERE {where_key} == ?' qmarks = qmarks.format(setters=setters, where_key=where_key) return (qmarks, bindings) + +def hex_byte(byte): + if byte not in range (0, 256): + raise ValueError(byte) + return hex(byte)[2:].rjust(2, '0') + +def literal(item): + ''' + Return a string depicting the SQL literal for this item. + + Example: + 0 -> "0" + 'hello' -> "'hello'" + b'hello' -> "X'68656c6c6f'" + [3, 'hi'] -> "(3, 'hi')" + ''' + if item is None: + return 'NULL' + + elif isinstance(item, bool): + return f'{int(item)}' + + elif isinstance(item, int): + return f'{item}' + + elif isinstance(item, float): + return f'{item:f}' + + elif isinstance(item, str): + return f"'{item}'" + + elif isinstance(item, bytes): + item = ''.join(hex_byte(byte) for byte in item) + return f"X'{item}'" + + elif isinstance(item, (list, tuple, set)): + output = ', '.join(literal(element) for element in item) + output = f'({output})' + return output + + else: + raise ValueError(f'Unrecognized type {type(item)} {item}.')