def RAISE_APPLICATION_ERROR(error_number, message): error_number = extract_value(error_number) message = extract_value(message) message = f"ORA{error_number}: {message}" PLGLOBALS.SQLCODE = m(error_number) PLGLOBALS.SQLERRM = m(message) raise _PL_EXCEPTION
def REPLACE(char, search, replacement=None): if replacement is None: replacement = NULL() if ISNULL(char) or ISNULL(search): return char char: str = extract_value(char) search: str = extract_value(search) if not ISNULL(replacement): replacement = extract_value(replacement) else: replacement = "" value = char.replace(search, replacement) return m(value)
def SUBSTR(string, position, length=None): if ISNULL(string) or ISNULL(position): return NULL() string = extract_value(string) position = extract_value(position) length = extract_value(length) if position == 0: position = 1 if position > 0: position -= 1 if length is not None: value = string[position:position+length] else: value = string[position:] return m(value)
def LOWER(string): if ISNULL(string): return NULL() string = extract_value(string) string = str(string) string = string.lower() return m(string)
def UPPER(string): if ISNULL(string): return NULL() string = extract_value(string) string = str(string) string = string.upper() return m(string)
def CHR(n): if ISNULL(n): return NULL() value = extract_value(n) if isinstance(value, str): value = int(value) value = chr(value) return m(value)
def TO_NUMBER(value): if ISNULL(value): return NULL() value = extract_value(value) value = float(value) if value.is_integer(): value = int(value) return m(value)
def padstr(to_left: bool, value, count, fill): if ISNULL(value) or ISNULL(count) or ISNULL(fill) or count <= m(0): return NULL() if fill is None: fill = m(' ') value = extract_value(value) count = extract_value(count) fill = extract_value(fill) valuelen = len(value) if count <= valuelen: value = value[:count] return value charsleft = count - valuelen filllen = len(fill) charsfill = fill * int(charsleft / filllen) charsfill += fill[:(charsleft % filllen)] if to_left: value = charsfill + value else: value = value + charsfill return value
def execute(self, sql: str, params=None): sql = extract_value(sql) if not params: params = {} for key in _sqls_mocked: if not re.search(key, sql, re.IGNORECASE): continue self.mocksql = _sqls_mocked[key] del _sqls_mocked[key] self.mocksql.cursor = self self.rowcount = self.mocksql.rowcount return raise RuntimeError("unable to find valid sql mock")
def OPEN(self, cursor_params: list, the_locals: Dict): params = {} for i, param_name in enumerate(self.cursor_params_names): value = cursor_params[i] value = extract_value(value) params[f'"{param_name}"'] = value for sql_bind in self.sql_binds: if sql_bind in self.cursor_params_names: continue if not sql_bind in the_locals: raise RuntimeError(f"expected variable {sql_bind} to be defined in the locals") value = the_locals[sql_bind] value = extract_value(value) if not isinstance(value, PLRECORD): params[f'"{sql_bind}"'] = value continue for key in value.keys(): record_bind = f"{sql_bind}.{key}" field = value.__getattr__(key) field = extract_value(field) params[f'"{record_bind}"'] = field self.cursor = PLCURSOR.getConn().cursor() self.cursor.execute(self.sql, params) PLCURSOR.rowcount = self.cursor.rowcount
def __setitem__(self, index, value): index = extract_value(index) index -= 1 self._safe_access_index(index) self.inner_list[index] = value
def NEXT(self, index): index = extract_value(index) if index >= len(self.inner_list): return NULL() return m(index + 1)
def PRIOR(self, index): index = extract_value(index) if index == 1: return NULL() return m(index - 1)
def MOD(n, mod): n = extract_value(n) mod = extract_value(mod) return m(n % mod)
def __getitem__(self, index): index = extract_value(index) index -= 1 self._safe_access_index(index) return self.inner_list[index]
def LENGTH(string): if ISNULL(string): return NULL() string = extract_value(string) return len(string)
def INSTR(st, sub): st: str = extract_value(st) sub: str = extract_value(sub) index = st.find(sub) return m(index + 1)
def __init__(self, sql): sql = extract_value(sql) self.datasource: Deque[List] = None self.cursor = None self.rowcount = None _sqls_mocked[sql] = self
def RTRIM(value): if ISNULL(value): return NULL() value = extract_value(value) value = value.rstrip(" ") return m(value)
def EXISTS(self, index): index = extract_value(index) index -= 1 return index >= 0 and index < len(self.inner_list)
def TO_CHAR(value): if ISNULL(value): return NULL() value = extract_value(value) value = str(value) return m(value)