<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import re <EOL> import dataproperty <EOL> import six <EOL> from six . moves import map <EOL> import simplesqlite as sql <EOL> class SqlQuery : <EOL> """<STR_LIT>""" <EOL> __RE_SANITIZE = re . compile ( "<STR_LIT>" % ( re . escape ( "<STR_LIT>" ) ) ) <EOL> __RE_TABLE_STR = re . compile ( "<STR_LIT>" % ( re . escape ( "<STR_LIT>" ) ) ) <EOL> __RE_TO_ATTR_STR = re . compile ( "<STR_LIT>" % ( re . escape ( "<STR_LIT>" ) ) ) <EOL> __RE_SPACE = re . compile ( "<STR_LIT>" ) <EOL> __VALID_WHERE_OPERATION_LIST = [ <EOL> "<STR_LIT:=>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:>>" , "<STR_LIT>" , "<STR_LIT:<>" , "<STR_LIT>" , <EOL> ] <EOL> @ classmethod <EOL> def sanitize ( cls , query_item ) : <EOL> """<STR_LIT>""" <EOL> return cls . __RE_SANITIZE . sub ( "<STR_LIT>" , query_item ) <EOL> @ classmethod <EOL> def to_table_str ( cls , name ) : <EOL> """<STR_LIT>""" <EOL> if cls . __RE_TABLE_STR . search ( name ) : <EOL> return "<STR_LIT>" % ( name ) <EOL> if cls . __RE_SPACE . search ( name ) : <EOL> return "<STR_LIT>" % ( name ) <EOL> return name <EOL> @ classmethod <EOL> def to_attr_str ( cls , name , operation_query = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if cls . __RE_TO_ATTR_STR . search ( name ) : <EOL> sql_name = "<STR_LIT>" % ( name ) <EOL> elif name == "<STR_LIT>" : <EOL> sql_name = "<STR_LIT>" % ( name ) <EOL> else : <EOL> sql_name = name <EOL> if dataproperty . is_not_empty_string ( operation_query ) : <EOL> sql_name = "<STR_LIT>" % ( operation_query , sql_name ) <EOL> return sql_name <EOL> @ classmethod <EOL> def to_attr_str_list ( cls , name_list , operation_query = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if dataproperty . is_empty_string ( operation_query ) : <EOL> return map ( cls . to_attr_str , name_list ) <EOL> return [ <EOL> "<STR_LIT>" % ( operation_query , cls . to_attr_str ( name ) ) <EOL> for name in name_list <EOL> ] <EOL> @ classmethod <EOL> def to_value_str ( cls , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return "<STR_LIT>" <EOL> if dataproperty . is_integer ( value ) or dataproperty . is_float ( value ) : <EOL> return str ( value ) <EOL> return "<STR_LIT>" % ( value ) <EOL> @ classmethod <EOL> def to_value_str_list ( cls , value_list ) : <EOL> """<STR_LIT>""" <EOL> return map ( cls . to_value_str , value_list ) <EOL> @ classmethod <EOL> def make_select ( cls , select , table , where = None , extra = None ) : <EOL> """<STR_LIT>""" <EOL> sql . validate_table_name ( table ) <EOL> if dataproperty . is_empty_string ( select ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> query_list = [ <EOL> "<STR_LIT>" + select , <EOL> "<STR_LIT>" + cls . to_table_str ( table ) , <EOL> ] <EOL> if dataproperty . is_not_empty_string ( where ) : <EOL> query_list . append ( "<STR_LIT>" + where ) <EOL> if dataproperty . is_not_empty_string ( extra ) : <EOL> query_list . append ( extra ) <EOL> return "<STR_LIT:U+0020>" . join ( query_list ) <EOL> @ classmethod <EOL> def make_insert ( cls , table , insert_tuple , is_insert_many = False ) : <EOL> """<STR_LIT>""" <EOL> sql . validate_table_name ( table ) <EOL> table = cls . to_table_str ( table ) <EOL> if dataproperty . is_empty_list_or_tuple ( insert_tuple ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if is_insert_many : <EOL> value_list = [ '<STR_LIT:?>' for _i in insert_tuple ] <EOL> else : <EOL> value_list = [ <EOL> "<STR_LIT>" % ( value ) <EOL> if isinstance ( value , six . string_types ) and value != "<STR_LIT>" <EOL> else str ( value ) <EOL> for value in insert_tuple <EOL> ] <EOL> return "<STR_LIT>" % ( <EOL> table , "<STR_LIT:U+002C>" . join ( value_list ) ) <EOL> @ classmethod <EOL> def make_update ( cls , table , set_query , where = None ) : <EOL> """<STR_LIT>""" <EOL> sql . validate_table_name ( table ) <EOL> if dataproperty . is_empty_string ( set_query ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> query_list = [ <EOL> "<STR_LIT>" + cls . to_table_str ( table ) , <EOL> "<STR_LIT>" + set_query , <EOL> ] <EOL> if dataproperty . is_not_empty_string ( where ) : <EOL> query_list . append ( "<STR_LIT>" + where ) <EOL> return "<STR_LIT:U+0020>" . join ( query_list ) <EOL> @ classmethod <EOL> def make_where ( cls , key , value , operation = "<STR_LIT:=>" ) : <EOL> """<STR_LIT>""" <EOL> if operation not in cls . __VALID_WHERE_OPERATION_LIST : <EOL> raise ValueError ( "<STR_LIT>" + str ( operation ) ) <EOL> return "<STR_LIT>" % ( <EOL> cls . to_attr_str ( key ) , operation , cls . to_value_str ( value ) ) <EOL> @ classmethod <EOL> def make_where_in ( cls , key , value_list ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( <EOL> cls . to_attr_str ( key ) , "<STR_LIT:U+002CU+0020>" . join ( cls . to_value_str_list ( value_list ) ) ) <EOL> @ classmethod <EOL> def make_where_not_in ( cls , key , value_list ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( <EOL> cls . to_attr_str ( key ) , "<STR_LIT:U+002CU+0020>" . join ( cls . to_value_str_list ( value_list ) ) ) </s>
