Skip to content

Commit

Permalink
update oracle to datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixstone committed Feb 28, 2024
1 parent 1f3d30a commit 80e0b53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
clean_column_name,
convert_column_to_type,
convert_column_type,
convert_datetime,
)
from mage_integrations.destinations.sql.base import Destination, main
from mage_integrations.destinations.sql.utils import (
Expand Down Expand Up @@ -161,6 +162,7 @@ def build_insert_commands(
columns=columns,
records=records,
convert_column_to_type_func=convert_column_to_type,
convert_datetime_func=convert_datetime,
string_parse_func=lambda x, y: x.replace("'", "''").replace('\\', '\\\\')
if COLUMN_TYPE_OBJECT == y['type'] else x,
use_lowercase=self.use_lowercase,
Expand All @@ -178,7 +180,11 @@ def build_insert_commands(
# Build update command
insert_value_without_left_parenthesis = re.sub(r"^\(", "", insert_value)
insert_value_strip = re.sub(r"\)$", "", insert_value_without_left_parenthesis)
updated_values = insert_value_strip.split(',')
insert_value_strip = re.sub(r"\,$", "", insert_value_strip)
splitted_values = insert_value_strip.split('),')
updated_values = [s + ")" if i < len(splitted_values) - 1 else s
for i, s in enumerate(splitted_values)]

updated_command = ', '.join([f'{col} = {updated_values[idx].strip()}'
for idx, col in enumerate(columns_cleaned)])
update_command_constraint = ""
Expand Down
24 changes: 22 additions & 2 deletions mage_integrations/mage_integrations/destinations/oracledb/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Dict, List

from mage_integrations.destinations.sql.constants import SQL_RESERVED_WORDS
Expand Down Expand Up @@ -54,6 +55,24 @@ def convert_column_to_type(value, column_type) -> str:
return f"CAST('{value}' AS {column_type})"


def is_valid_datetime_format(date_string, date_format):
try:
datetime.strptime(date_string, date_format)
return True
except ValueError:
return False


def convert_datetime(value: str, column_type_dict: Dict) -> str:
if is_valid_datetime_format(value, "%Y-%m-%d %H:%M:%S.%f"):
# Oracle stores only the fractions up to second in a DATE field.
# Use TO_TIMESTAMP for milliseconds and microseconds.
return f"TO_TIMESTAMP('{value}', 'yyyy-mm-dd hh24:mi:ss.ff6')"
if is_valid_datetime_format(value, "%Y-%m-%d"):
return f"TO_DATE('{value}', 'yyyy-mm-dd')"
return 'CHAR(255)'


def build_create_table_command(
column_type_mapping: Dict,
columns: List[str],
Expand Down Expand Up @@ -108,7 +127,8 @@ def convert_column_type(column_type: str, column_settings: Dict, **kwargs) -> st
return 'NCLOB'
elif COLUMN_TYPE_STRING == column_type:
if COLUMN_FORMAT_DATETIME == column_settings.get('format'):
# Twice as long as the number of characters in ISO date format
return 'CHAR(52)'
return 'DATE'
elif COLUMN_FORMAT_DATETIME == column_type:
return 'DATE'

return 'CHAR(255)'

0 comments on commit 80e0b53

Please sign in to comment.