Skip to content

Commit

Permalink
Modify .null_text_to_empty_string() and .add_primary_keys()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeqfu committed Jun 13, 2022
1 parent 3e01242 commit 7e2797d
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions pyhelpers/dbms.py
Expand Up @@ -204,7 +204,7 @@ def _import_data(cls, data, table_name, schema_name=None, if_exists='fail',
if confirmation_required:
log_msg = f"Importing the data into the table {table_name_}"
else:
log_msg = f"Importing data into the table {table_name_} at {cls.address}"
log_msg = f"Importing data into the table {table_name_}\n\tat {cls.address}"
print(log_msg, end=" ... ")

to_sql_args = {
Expand Down Expand Up @@ -1375,7 +1375,8 @@ def get_column_info(self, table_name, schema_name=None, as_dict=True):
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.PostgreSQL.create_table`.
- Examples for the method
:meth:`PostgreSQL.create_table()<pyhelpers.dbms.PostgreSQL.create_table>`.
"""

schema_name_ = self._schema_name(schema_name=schema_name)
Expand Down Expand Up @@ -1410,7 +1411,8 @@ def get_column_dtype(self, table_name, column_names=None, schema_name=None):
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.PostgreSQL.create_table`.
- Examples for the method
:meth:`PostgreSQL.create_table()<pyhelpers.dbms.PostgreSQL.create_table>`.
"""

schema_name_ = self._schema_name(schema_name=schema_name)
Expand Down Expand Up @@ -1660,10 +1662,10 @@ def null_text_to_empty_string(self, table_name, column_names=None, schema_name=N
text_columns = [k for k, v in column_dtypes.items() if v == 'text']

if len(text_columns) > 0:
tbl = self._table_name(table_name=table_name, schema_name=schema_name)
cols_query = ', '.join(f'"{x}"=COALESCE("{x}", \'\')' for x in text_columns)
table_name_ = self._table_name(table_name=table_name, schema_name=schema_name)

self.engine.execute(f'UPDATE {tbl} SET {cols_query};')
self.engine.execute(f'UPDATE {table_name_} SET {cols_query};')

def add_primary_keys(self, primary_keys, table_name, schema_name=None):
"""
Expand All @@ -1678,11 +1680,10 @@ def add_primary_keys(self, primary_keys, table_name, schema_name=None):
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.PostgreSQL.get_primary_keys`.
- Examples for the method
:meth:`PostgreSQL.get_primary_keys()<pyhelpers.dbms.PostgreSQL.get_primary_keys>`.
"""

table_name_ = self._table_name(table_name=table_name, schema_name=schema_name)

if primary_keys is not None:
# If any null values in 'text' columns, replace null values with empty strings
self.null_text_to_empty_string(
Expand All @@ -1695,6 +1696,8 @@ def add_primary_keys(self, primary_keys, table_name, schema_name=None):
else:
primary_keys_ = str(tuple(pri_keys)).replace("'", '"')

table_name_ = self._table_name(table_name=table_name, schema_name=schema_name)

self.engine.execute(f'ALTER TABLE {table_name_} ADD PRIMARY KEY {primary_keys_};')

def get_primary_keys(self, table_name, schema_name=None, names_only=True):
Expand Down Expand Up @@ -1802,7 +1805,8 @@ def drop_table(self, table_name, schema_name=None, confirmation_required=True, v
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.PostgreSQL.create_table`.
- Examples for the method
:meth:`PostgreSQL.create_table()<pyhelpers.dbms.PostgreSQL.create_table>`.
"""

table_name_ = self._table_name(table_name=table_name, schema_name=schema_name)
Expand Down Expand Up @@ -1915,7 +1919,8 @@ def import_data(self, data, table_name, schema_name=None, if_exists='fail',
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.PostgreSQL.read_sql_query`.
- Examples for the method
:meth:`PostgreSQL.read_sql_query()<pyhelpers.dbms.PostgreSQL.read_sql_query>`.
"""

_import_data(
Expand Down Expand Up @@ -2165,7 +2170,8 @@ def read_table(self, table_name, schema_name=None, conditions=None, chunk_size=N
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.PostgreSQL.read_sql_query`.
- Examples for the method
:meth:`PostgreSQL.read_sql_query()<pyhelpers.dbms.PostgreSQL.read_sql_query>`.
"""

table_name_ = self._table_name(table_name=table_name, schema_name=schema_name)
Expand Down Expand Up @@ -3613,7 +3619,7 @@ def import_data(self, data, table_name, schema_name=None, if_exists='fail',
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.MSSQL.read_table`.
- Examples for the method :meth:`MSSQL.read_table()<pyhelpers.dbms.MSSQL.read_table>`.
"""

if index is True:
Expand Down Expand Up @@ -3671,7 +3677,7 @@ def read_columns(self, table_name, column_names, dtype=None, schema_name=None, c
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.MSSQL.read_table`.
- Examples for the method :meth:`MSSQL.read_table()<pyhelpers.dbms.MSSQL.read_table>`.
"""

col_names = [column_names] if isinstance(column_names, str) else copy.copy(column_names)
Expand Down Expand Up @@ -3797,7 +3803,7 @@ def read_table(self, table_name, schema_name=None, column_names=None, conditions
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.MSSQL.import_data`.
- Examples for the method :meth:`MSSQL.import_data()<pyhelpers.dbms.MSSQL.import_data>`.
"""

if column_names is None:
Expand Down Expand Up @@ -3866,7 +3872,7 @@ def drop_table(self, table_name, schema_name=None, confirmation_required=True, v
.. seealso::
- Examples for the method :py:meth:`pyhelpers.dbms.MSSQL.create_table`.
- Examples for the method :meth:`MSSQL.create_table()<pyhelpers.dbms.MSSQL.create_table>`.
"""

table_name_ = self._table_name(table_name=table_name, schema_name=schema_name)
Expand Down

0 comments on commit 7e2797d

Please sign in to comment.