MCPcopy
hub / github.com/spaceandtimefdn/SxT-Python-SDK / prep_sql

Method prep_sql

src/spaceandtime/sxtbaseapi.py:92–135  ·  view source on GitHub ↗

------------------- Cleans and prepares sql_text for transmission and execution on-network. Args: sql_text (str): SQL text to prepare. Returns: sql: slightly modified / cleansed SQL text Examples: >>> api = SXTBaseAPI()

(self, sql_text:str)

Source from the content-addressed store, hash-verified

90
91
92 def prep_sql(self, sql_text:str) -> str:
93 """-------------------
94 Cleans and prepares sql_text for transmission and execution on-network.
95
96 Args:
97 sql_text (str): SQL text to prepare.
98
99 Returns:
100 sql: slightly modified / cleansed SQL text
101
102 Examples:
103 >>> api = SXTBaseAPI()
104 >>> sql = "Select 'complex \nstring ' as A \n \t from \n\t TableName \n Where A=1;"
105 >>> newsql = api.prep_sql(sql)
106 >>> newsql == "Select 'complex \nstring ' as A from TableName Where A=1"
107 True
108 """
109 if sql_text == None or len(sql_text.strip()) == 0: return ''
110 insinglequote = False
111 indoublequote = False
112 rtn = []
113 char = prevchar = ''
114 for char in list(sql_text.strip()):
115
116 # escape anything in quotes
117 if char == "'": insinglequote = not insinglequote
118 elif char == '"': indoublequote = not indoublequote
119 if insinglequote or indoublequote:
120 rtn.append(char)
121 prevchar = ''
122 continue
123
124 # replace newlines and tabs with spaces
125 if char in ['\n', '\t']: char = ' '
126
127 # remove double-spaces
128 if char == ' ' and prevchar == ' ': continue
129
130 rtn.append(char)
131 prevchar = char
132
133 # remove ; if last character
134 if char == ';': rtn = rtn[:-1]
135 return str(''.join(rtn)).strip()
136
137
138 def call_api(self, endpoint: str,

Callers 3

sql_execMethod · 0.95
sql_exec_tamperproofMethod · 0.95

Calls

no outgoing calls

Tested by 1