DE-Lib 0.0.20__tar.gz → 0.0.36__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {de_lib-0.0.20 → de_lib-0.0.36}/DE_LIB.egg-info/PKG-INFO +3 -2
  2. {de_lib-0.0.20 → de_lib-0.0.36}/DE_LIB.egg-info/SOURCES.txt +3 -0
  3. de_lib-0.0.36/DE_Lib/DataBase/Azure.py +55 -0
  4. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/Cache.py +8 -2
  5. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/Firebird.py +15 -6
  6. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/Informix.py +14 -4
  7. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/Metadata.py +1 -1
  8. de_lib-0.0.20/DE_Lib/DataBase/MySql.py → de_lib-0.0.36/DE_Lib/DataBase/MsSql.py +11 -8
  9. de_lib-0.0.36/DE_Lib/DataBase/MySql.py +68 -0
  10. de_lib-0.0.36/DE_Lib/DataBase/Oracle.py +208 -0
  11. de_lib-0.0.36/DE_Lib/DataBase/OracleDDL.py +89 -0
  12. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/Postgres.py +15 -7
  13. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/RedShift.py +15 -7
  14. de_lib-0.0.36/DE_Lib/DataBase/SQCipher.py +46 -0
  15. de_lib-0.0.36/DE_Lib/DataBase/SQLite.py +72 -0
  16. de_lib-0.0.36/DE_Lib/Files/Zip.py +119 -0
  17. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Log/Level.py +13 -13
  18. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Log/Log.py +61 -40
  19. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Fernet.py +26 -5
  20. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Colors.py +1 -1
  21. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/DateUtils.py +3 -3
  22. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Generic.py +30 -9
  23. de_lib-0.0.36/DE_Lib/Utils/Sql.py +78 -0
  24. de_lib-0.0.36/DE_Lib/Utils/System.py +103 -0
  25. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/WebHook.py +3 -2
  26. {de_lib-0.0.20 → de_lib-0.0.36}/PKG-INFO +3 -2
  27. {de_lib-0.0.20 → de_lib-0.0.36}/setup.py +1 -1
  28. de_lib-0.0.20/DE_Lib/DataBase/Azure.py +0 -44
  29. de_lib-0.0.20/DE_Lib/DataBase/MsSql.py +0 -39
  30. de_lib-0.0.20/DE_Lib/DataBase/Oracle.py +0 -111
  31. de_lib-0.0.20/DE_Lib/DataBase/SQCipher.py +0 -42
  32. de_lib-0.0.20/DE_Lib/DataBase/SQLite.py +0 -48
  33. de_lib-0.0.20/DE_Lib/Utils/SQL.py +0 -34
  34. de_lib-0.0.20/DE_Lib/Utils/System.py +0 -50
  35. {de_lib-0.0.20 → de_lib-0.0.36}/DE_LIB.egg-info/dependency_links.txt +0 -0
  36. {de_lib-0.0.20 → de_lib-0.0.36}/DE_LIB.egg-info/requires.txt +0 -0
  37. {de_lib-0.0.20 → de_lib-0.0.36}/DE_LIB.egg-info/top_level.txt +0 -0
  38. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Cloud/__init__.py +0 -0
  39. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/DataBase/__init__.py +0 -0
  40. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/Avro.py +0 -0
  41. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/Csv.py +0 -0
  42. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/JSon.py +0 -0
  43. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/Parquet.py +0 -0
  44. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/Txt.py +0 -0
  45. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/Xlsx.py +0 -0
  46. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Files/__init__.py +0 -0
  47. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Log/DE_LogEventos.py +0 -0
  48. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Log/__init__.py +0 -0
  49. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Aes.py +0 -0
  50. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Argon.py +0 -0
  51. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Base64.py +0 -0
  52. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Cipher.py +0 -0
  53. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Gcm.py +0 -0
  54. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Pbkdf2.py +0 -0
  55. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/Rsa.py +0 -0
  56. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/Cipher/__init__.py +0 -0
  57. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/Utils/__init__.py +0 -0
  58. {de_lib-0.0.20 → de_lib-0.0.36}/DE_Lib/__init__.py +0 -0
  59. {de_lib-0.0.20 → de_lib-0.0.36}/LICENCE +0 -0
  60. {de_lib-0.0.20 → de_lib-0.0.36}/README.md +0 -0
  61. {de_lib-0.0.20 → de_lib-0.0.36}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: DE_Lib
3
- Version: 0.0.20
3
+ Version: 0.0.36
4
4
  Summary: Biblioteca de funcionalidades
5
5
  Home-page: https://github.com/DE-DATAx/DAX_DB.git
6
6
  Author: Almir J Gomes
@@ -22,6 +22,7 @@ Dynamic: author-email
22
22
  Dynamic: description
23
23
  Dynamic: description-content-type
24
24
  Dynamic: home-page
25
+ Dynamic: license-file
25
26
  Dynamic: requires-dist
26
27
  Dynamic: summary
27
28
 
@@ -21,6 +21,7 @@ DE_Lib/DataBase/Metadata.py
21
21
  DE_Lib/DataBase/MsSql.py
22
22
  DE_Lib/DataBase/MySql.py
23
23
  DE_Lib/DataBase/Oracle.py
24
+ DE_Lib/DataBase/OracleDDL.py
24
25
  DE_Lib/DataBase/Postgres.py
25
26
  DE_Lib/DataBase/RedShift.py
26
27
  DE_Lib/DataBase/SQCipher.py
@@ -32,6 +33,7 @@ DE_Lib/Files/JSon.py
32
33
  DE_Lib/Files/Parquet.py
33
34
  DE_Lib/Files/Txt.py
34
35
  DE_Lib/Files/Xlsx.py
36
+ DE_Lib/Files/Zip.py
35
37
  DE_Lib/Files/__init__.py
36
38
  DE_Lib/Log/DE_LogEventos.py
37
39
  DE_Lib/Log/Level.py
@@ -41,6 +43,7 @@ DE_Lib/Utils/Colors.py
41
43
  DE_Lib/Utils/DateUtils.py
42
44
  DE_Lib/Utils/Generic.py
43
45
  DE_Lib/Utils/SQL.py
46
+ DE_Lib/Utils/Sql.py
44
47
  DE_Lib/Utils/System.py
45
48
  DE_Lib/Utils/WebHook.py
46
49
  DE_Lib/Utils/__init__.py
@@ -0,0 +1,55 @@
1
+ """
2
+ Faltam testes mais conclusivos
3
+ """
4
+ import redshift_connector as redshift
5
+ from azure.storage.filedatalake import DataLakeServiceClient
6
+ import os
7
+ import json
8
+
9
+ from DE_Lib.Utils import Generic
10
+
11
+ gen = Generic.GENERIC()
12
+
13
+
14
+ class AZURE:
15
+ def __init__(self):
16
+ self._connection_is_valid = None
17
+ self._nome_database = None
18
+ self._cnn = None
19
+ self.__database_error = None
20
+
21
+ def Connect(self, string_connect: dict):
22
+ conn, result = None, None
23
+ try:
24
+ conn = DataLakeServiceClient(host=string_connect["host"],
25
+ database=string_connect["instance"],
26
+ user=string_connect["username"],
27
+ password=string_connect["password"]
28
+ )
29
+ self._connection_is_valid = True
30
+ self._cnn = result
31
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
32
+ self._nome_database = gen.nvl(string_connect["database"], "")
33
+ except Exception as error:
34
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados AZURE\nException Error: {error} """
35
+ result = msg
36
+ self._connection_is_valid = False
37
+ self.__database_error = msg
38
+ finally:
39
+ return result
40
+
41
+ @property
42
+ def CONNECTION(self):
43
+ return self._cnn
44
+
45
+ @property
46
+ def CONNECTION_VALID(self):
47
+ return self._connection_is_valid
48
+
49
+ @property
50
+ def NOME_DATABASE(self):
51
+ return self._nome_database.upper()
52
+
53
+ @property
54
+ def DATABASE_ERROR(self):
55
+ return self.__database_error
@@ -3,7 +3,11 @@ import jpype as jp
3
3
  import os
4
4
  import json
5
5
 
6
- class Cache:
6
+ from DE_Lib.Utils import Generic
7
+
8
+ gen = Generic.GENERIC()
9
+
10
+ class CACHE:
7
11
  def __init__(self):
8
12
  self._connection_is_valid = None
9
13
  self._nome_database = None
@@ -48,7 +52,7 @@ class Cache:
48
52
  self._connection_is_valid = True
49
53
  self._cnn = result
50
54
  self._database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["pwd"], "******")}\nConexao bem sucedida!"""
51
- self._nome_database = string_connect["database"]
55
+ self._nome_database = gen.nvl(string_connect["database"], "")
52
56
  except Exception as error:
53
57
  msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["pwd"], "******")}\nFalha ao tentar se conectar com o banco de dados CACHÉ (intersystem)\nException Error: {error} """
54
58
  result = msg
@@ -57,6 +61,8 @@ class Cache:
57
61
  finally:
58
62
  return result
59
63
 
64
+
65
+
60
66
  @property
61
67
  def CONNECTION(self):
62
68
  return self._cnn
@@ -1,6 +1,12 @@
1
1
  import fbd
2
+ import os
3
+ import json
2
4
 
3
- class Firebird:
5
+ from DE_Lib.Utils import Generic
6
+
7
+ gen = Generic.GENERIC()
8
+
9
+ class FIREBIRD:
4
10
  def __init__(self):
5
11
  self._connection_is_valid = None
6
12
  self._nome_database = None
@@ -10,7 +16,7 @@ class Firebird:
10
16
  # ----------------------------------------------------------------
11
17
  # Falta driver - maquina local não permite
12
18
  def Connect(self, string_connect: dict):
13
- msg, conn = None, None
19
+ msg, conn, result = None, None, None
14
20
  try:
15
21
  user = string_connect["username"]
16
22
  pwd = string_connect["password"]
@@ -19,13 +25,16 @@ class Firebird:
19
25
  instance = string_connect["instance"]
20
26
  conn = fbd.connect(host=host, database=instance, user=user, password=pwd, port=port)
21
27
  self._connection_is_valid = True
22
- self._nome_database = string_connect["database"]
23
- self._cnn = conn
28
+ self._nome_database = gen.nvl(string_connect["database"], "")
29
+ self._cnn = result
30
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
24
31
  except Exception as error:
25
- conn = error
32
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados ORACLE\nException Error: {error} """
33
+ result = msg
26
34
  self._connection_is_valid = False
27
- self._DATABASE_ERROR = conn.DatabaseError
35
+ self.__database_error = msg
28
36
  finally:
37
+ return result
29
38
  return conn
30
39
 
31
40
  @property
@@ -1,6 +1,11 @@
1
+ import os
2
+ import json
1
3
 
4
+ from DE_Lib.Utils import Generic
2
5
 
3
- class Informix:
6
+ gen = Generic.GENERIC()
7
+
8
+ class INFORMIX:
4
9
  def __init__(self):
5
10
  self._connection_is_valid = None
6
11
  self._nome_database = None
@@ -10,15 +15,20 @@ class Informix:
10
15
  # ----------------------------------------------------------------
11
16
  # Falta tudo (Instalar driver ODBC) Maquina local não permite
12
17
  def Connect(self, string_connect: dict):
18
+ msg, result = None, None
13
19
  try:
14
20
  pass
15
21
  self._connection_is_valid = True
16
- self._cnn = None
22
+ self._nome_database = gen.nvl(string_connect["database"], "")
23
+ self._cnn = result
24
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
17
25
  except Exception as error:
26
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados ORACLE\nException Error: {error} """
27
+ result = msg
18
28
  self._connection_is_valid = False
19
- self._DATABASE_ERROR = True
29
+ self.__database_error = msg
20
30
  finally:
21
- pass
31
+ return result
22
32
 
23
33
  @property
24
34
  def CONNECTION(self):
@@ -1,6 +1,6 @@
1
1
  import pandas as pd
2
2
 
3
- class Metadata:
3
+ class METADATA:
4
4
  def __init__(self):
5
5
  ...
6
6
 
@@ -1,7 +1,10 @@
1
- import mysql.connector as mysql
2
- import json
1
+ import pymssql as mssql
3
2
 
4
- class MySql:
3
+ from DE_Lib.Utils import Generic
4
+
5
+ gen = Generic.GENERIC()
6
+
7
+ class MSSQL:
5
8
  def __init__(self):
6
9
  self._connection_is_valid = None
7
10
  self._nome_database = None
@@ -12,16 +15,16 @@ class MySql:
12
15
  msg, conn = None, None
13
16
  try:
14
17
  # Efetuando a conexao com a instancia do BANCO
15
- result = mysql.connect(user=string_connect["username"], password=string_connect["password"], database=string_connect["instance"], host=string_connect["host"])
18
+ conn = mssql.connect(user=string_connect["username"], password=string_connect["password"], database=string_connect["instance"], server=string_connect["host"])
16
19
  self._connection_is_valid = True
17
20
  self._cnn = result
18
- self._DATABASE_ERROR = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
19
- self._nome_database = string_connect["database"]
21
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
22
+ self._nome_database = gen.nvl(string_connect["database"], "")
20
23
  except Exception as error:
21
- msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados MYSQL\nException Error: {error} """
24
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados MSSQL (SqlAlchemy)\nException Error: {error} """
22
25
  result = msg
23
26
  self._connection_is_valid = False
24
- self._DATABASE_ERROR = msg
27
+ self.__database_error = msg
25
28
  finally:
26
29
  return result
27
30
 
@@ -0,0 +1,68 @@
1
+ import mysql.connector as mysql
2
+ import pymysql
3
+ import sqlalchemy as sqa
4
+ import json
5
+
6
+ from DE_Lib.Utils import Generic
7
+
8
+ gen = Generic.GENERIC()
9
+
10
+ class MYSQL:
11
+ def __init__(self):
12
+ self._connection_is_valid = None
13
+ self._nome_database = None
14
+ self.__database_driver = None
15
+ self._cnn = None
16
+ self.__database_error = None
17
+
18
+ def Connect(self, conn: dict):
19
+ msg, result = None, True
20
+ try:
21
+ # Efetuando a conexao com a instancia do BANCO
22
+ __conn = None
23
+ if conn["driver_conexao"].upper() == "SQLALCHEMY":
24
+ __dns = f"""{conn["database"].lower()}+pymysql://{conn["username"]}:{conn["password"]}@{conn["host"]}:{conn["port"]}/{conn["instance"]}"""
25
+ __engine = sqa.create_engine(url=__dns)
26
+ __conn = __engine.connect().connection
27
+ #__conn = __conn.connection
28
+ elif conn["driver_conexao"].upper() == "MYSQL":
29
+ __conn = mysql.connect(user=conn["username"], password=conn["password"], database=conn["instance"], host=conn["host"])
30
+ elif conn["driver_conexao"].upper() == "PYMYSQL":
31
+ __conn = pymysql.connect(user=conn["username"], password=conn["password"], database=conn["instance"], host=conn["host"])#, cursorclass=pymysql.cursors.DictCursor)
32
+ self._connection_is_valid = True
33
+ self._nome_database = gen.nvl(conn["database"], "")
34
+ self.__database_driver = conn["driver_conexao"]
35
+ self._cnn = __conn
36
+ self.__database_error = result
37
+ result = True
38
+ except Exception as error:
39
+ msg = f"""{json.dumps(conn, indent=4).replace(conn["password"], "******")}\nFalha ao tentar se conectar com o banco de dados MYSQL\nException Error: {error} """
40
+ result = msg
41
+ self._connection_is_valid = False
42
+ self.__database_error = msg
43
+ finally:
44
+ return result
45
+
46
+ @property
47
+ def CONNECTION(self):
48
+ return self._cnn
49
+
50
+ @property
51
+ def CONNECTION_VALID(self):
52
+ return self._connection_is_valid
53
+
54
+ @property
55
+ def NOME_DATABASE(self):
56
+ return self._nome_database
57
+
58
+ @property
59
+ def DATABASE_ERROR(self):
60
+ return self.__database_error
61
+
62
+ @property
63
+ def DATABASE_DRIVER(self):
64
+ return self.__database_driver
65
+
66
+ @DATABASE_DRIVER.setter
67
+ def DATABASE_DRIVER(self, value):
68
+ self._DATABASE_DRIVER = value
@@ -0,0 +1,208 @@
1
+ import os
2
+ import cx_Oracle as ora
3
+ import oracledb as odb
4
+ import sqlalchemy as sqa
5
+ import json
6
+
7
+ from DE_Lib.Utils import Generic
8
+
9
+ gen = Generic.GENERIC()
10
+
11
+ class ORACLE:
12
+ def __init__(self):
13
+ self._connection_is_valid = None
14
+ self._nome_database = None
15
+ self._cnn = None
16
+ self.__database_error = None
17
+
18
+ # ---------------------------------
19
+ def Connect(self, conn: dict):
20
+ msg, result = None, True
21
+ try:
22
+
23
+ #region Definindo o tipo de instancia SID/SERVICE_NAME
24
+ if conn["type_conection"].upper() == "SID":
25
+ __dnsName = odb.makedsn(host=conn["host"], port=conn["port"],
26
+ sid=conn["instance"])
27
+ else:
28
+ __dnsName = odb.makedsn(host=conn["host"], port=conn["port"],
29
+ service_name=conn["instance"])
30
+ #endregion
31
+
32
+ #region Tipo de driver de conexao
33
+ """
34
+ Oracle Thin Mode vs Thick Mode no oracledb
35
+ O driver oracledb do Python pode operar em dois modos de conexão com o Oracle Database:
36
+ . Thin Mode (Padrão) → Conexão nativa, sem necessidade do Oracle Client.
37
+ . Thick Mode → Usa o Oracle Client para recursos avançados e maior desempenho.
38
+ 🔹 Comparação Geral
39
+ Característica Thin Mode (Padrão) Thick Mode
40
+ Requer Oracle Client? ❌ Não ✅ Sim
41
+ Performance Média Alta
42
+ Suporta TNS (tnsnames.ora)? ❌ Não ✅ Sim
43
+ Suporta Connection Pooling? ❌ Limitado ✅ Sim
44
+ Suporte a Banco de Dados Antigos? ❌ Não ✅ Sim
45
+ Uso recomendado Ambientes simples Produção, conexões complexas
46
+ ,Cloud, Containers
47
+ """
48
+ if conn["driver_mode"].upper() == "THICK":
49
+ try:
50
+ # region LIBRARY
51
+ if conn["path_library"] is None:
52
+ __pathlib = os.getenv("ORACLE_LIB")
53
+ else:
54
+ __pathlib = conn["path_library"]
55
+ # endregion
56
+ ora.init_oracle_client(lib_dir=__pathlib)
57
+ except Exception as error:
58
+ ...
59
+ # Modo THIN apenas do banco 12 em diante
60
+ #endregion
61
+
62
+ #region Conexao via SQLALCHEMY | CX_ORACLE | ORACLEDB
63
+ __conn = None
64
+ if conn["driver_conexao"].upper() == "SQLALCHEMY":
65
+ # driver oracledb compativel com banco 12 em diante
66
+ if not conn["driver_library"]:
67
+ # driver default caso não seja explicitado
68
+ # para versões do oracle anterior a 12, este driver é o mais recomendado.
69
+ __driver = "cx_oracle"
70
+ else:
71
+ __driver = conn["driver_library"].lower()
72
+ conn_cnn = f"""{conn["database"].lower()}+{__driver}://{conn["username"]}:{conn["password"]}@{__dnsName}"""
73
+ __engine = sqa.create_engine(conn_cnn)
74
+ __conn = __engine.connect().connection
75
+ elif conn["driver_conexao"].upper() == "CX_ORACLE":
76
+ __conn = ora.connect(conn["username"], conn["password"], __dnsName, threaded=True)
77
+ elif conn["driver_conexao"].upper() == "ORACLEDB" or conn is None:
78
+ # Conexao via ORACLEDB (Novo driver em substituicao do CX_ORACLE)
79
+ __conn = ora.connect(conn["username"], conn["password"], __dnsName, threaded=True)
80
+ #endregion
81
+
82
+ #region Populando propriedadas da classe
83
+ self.__connection_is_valid = True
84
+ self.__nome_database = gen.nvl(conn["database"], "")
85
+ self.__database_driver = conn["driver_conexao"]
86
+ self.__cnn = __conn
87
+ self.__database_error = result
88
+ #endregion
89
+ except Exception as error:
90
+ msg = f"""{json.dumps(conn, indent=4).replace(conn["password"], "******")}\nFalha ao tentar se conectar com o banco de dados ORACLE (SqlAlchemy)\nException Error: {error} """
91
+ self._connection_is_valid = False
92
+ self.__database_error = msg
93
+ result = self._connection_is_valid
94
+ finally:
95
+ return result
96
+
97
+ #region METODOS qQUE FICARÃO DEPRECIADOS A PARTIR DE 18/03/2025
98
+ def Connect_ORA(self, string_connect: dict):
99
+ pathlib, msg, result = None, None, None
100
+ try:
101
+ # Definindo a Library ORACLE
102
+ if "library" in string_connect.keys():
103
+ if string_connect["library"] is None:
104
+ pathlib = os.getenv("ORACLE_LIB")
105
+ else:
106
+ pathlib = string_connect["library"]
107
+ else:
108
+ pathlib = os.getenv("ORACLE_LIB")
109
+
110
+ # Consistindo se a biblioteca do oracle ja esta iniciada
111
+ try:
112
+ ora.init_oracle_client(lib_dir=pathlib)
113
+ except:
114
+ pass
115
+ # não faz nada (e para deixar assim se nao da erro)
116
+
117
+ # Definindo o tipo de instancia SID/SERVICE_NAME
118
+ if string_connect["type_conection"].upper() == "SID":
119
+ dnsName = ora.makedsn(host=string_connect["host"], port=string_connect["port"], sid=string_connect["instance"])
120
+ else:
121
+ dnsName = ora.makedsn(host=string_connect["host"], port=string_connect["port"], service_name=string_connect["instance"])
122
+
123
+ # Efetuando a conexao com a instancia do BANCO
124
+ result = ora.connect(string_connect["username"], string_connect["password"], dnsName, threaded=True)
125
+ self._connection_is_valid = True
126
+ self._nome_database = gen.nvl(string_connect["database"], "")
127
+ self._cnn = result
128
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
129
+ except Exception as error:
130
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados ORACLE\nException Error: {error} """
131
+ result = msg
132
+ self._connection_is_valid = False
133
+ self.__database_error = msg
134
+ finally:
135
+ return result
136
+
137
+ def Connect_SQLA(self, string_connect: dict):
138
+ conn = None
139
+ try:
140
+ # Definindo a Library ORACLE
141
+ if string_connect["path_library"] is None:
142
+ # ORACLE_LIB tem que estar previamente criada
143
+ pathlib = os.getenv("ORACLE_LIB")
144
+ else:
145
+ pathlib = string_connect["path_library"]
146
+
147
+ # Consistindo se a biblioteca do oracle ja esta iniciada
148
+ try:
149
+ ora.init_oracle_client(lib_dir=pathlib)
150
+ except:
151
+ pass
152
+ # não faz nada (e para deixar assim se nao da erro)
153
+ # Validando se foi passado um driver para conexao
154
+ if string_connect["driver_conexao"] is None:
155
+ string_connect["driver_conexao"] = "cx_oracle"
156
+ database = string_connect["database"]
157
+ driver = string_connect["driver_conexao"]
158
+ user = string_connect["username"]
159
+ pwd = string_connect["password"]
160
+ host = string_connect["host"]
161
+ port = string_connect["port"]
162
+ string_connect["instance"] = ora.makedsn(host, port, string_connect["instance"])
163
+ # Validando o tipo de conexao (SID ou SERVICE_NAME) apenas oracle
164
+ if string_connect["type_conection"].upper() == "SERVICE_NAME":
165
+ string_connect["instance"] = string_connect["instance"].replace("SID", "SERVICE_NAME")
166
+ dnsName = string_connect["instance"]
167
+ str_cnn = f"""{database.lower()}{driver}://{user}:{pwd}@{dnsName}"""
168
+ engine = sqa.create_engine(str_cnn)
169
+ result = engine.connect()
170
+ self._connection_is_valid = True
171
+ self._cnn = result
172
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
173
+ self._nome_database = gen.nvl(string_connect["database"], "")
174
+ except Exception as error:
175
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados ORACLE (SqlAlchemy)\nException Error: {error} """
176
+ result = msg
177
+ self._connection_is_valid = False
178
+ self.__database_error = msg
179
+ finally:
180
+ return result
181
+ #endregion
182
+
183
+ #region PROPRIEDADES
184
+ @property
185
+ def CONNECTION(self):
186
+ return self.__cnn
187
+
188
+ @property
189
+ def CONNECTION_VALID(self):
190
+ return self.__connection_is_valid
191
+
192
+ @property
193
+ def NOME_DATABASE(self):
194
+ return self.__nome_database.upper()
195
+
196
+ @property
197
+ def DATABASE_ERROR(self):
198
+ return self.__database_error
199
+
200
+ @property
201
+ def DATABASE_DRIVER(self):
202
+ return self.__database_driver
203
+
204
+ @DATABASE_DRIVER.setter
205
+ def DATABASE_DRIVER(self, value):
206
+ self._DATABASE_DRIVER = value
207
+
208
+ #endregion
@@ -0,0 +1,89 @@
1
+ import pandas as pd
2
+
3
+ class OracleDDL:
4
+ def __init__(self):
5
+ msg = result = None, None
6
+ try:
7
+ ...
8
+ except Exception as error:
9
+ msg = error
10
+ result = msg
11
+ finally:
12
+ print(result)
13
+
14
+
15
+ def map_dtype_to_oracle(self, dtype):
16
+ msg = result = None, None
17
+ try:
18
+ if pd.api.types.is_integer_dtype(dtype):
19
+ result = "NUMBER"
20
+ elif pd.api.types.is_float_dtype(dtype):
21
+ result = "FLOAT"
22
+ elif pd.api.types.is_bool_dtype(dtype):
23
+ result = "CHAR(1)" # True = 'Y', False = 'N'
24
+ elif pd.api.types.is_string_dtype(dtype):
25
+ result = "VARCHAR2(255)"
26
+ elif pd.api.types.is_datetime64_any_dtype(dtype):
27
+ result = "DATE"
28
+ else:
29
+ result = "VARCHAR2(4000)"
30
+ except Exception as error:
31
+ msg = error
32
+ result = msg
33
+ finally:
34
+ return result
35
+
36
+ def generate_create_table_sql(self, table_name, df):
37
+ msg = result = None, None
38
+ try:
39
+ columns = []
40
+ for col in df.columns:
41
+ colname = col.lower()
42
+ oracle_type = self.map_dtype_to_oracle(df[col].dtype)
43
+ columns.append(f"{colname} {oracle_type}")
44
+ columns_sql = ",\n ".join(columns)
45
+ result = f"CREATE TABLE {table_name} (\n {columns_sql}\n)"
46
+ except Exception as error:
47
+ msg = error
48
+ result = msg
49
+ finally:
50
+ return result
51
+
52
+ def prepare_data_for_insert(self, df):
53
+ msg = result = None, None
54
+ try:
55
+ def convert_row(row):
56
+ return tuple(
57
+ 'Y' if isinstance(val, bool) and val else 'N' if isinstance(val, bool) else val for val in row)
58
+ result = [convert_row(row) for row in df.itertuples(index=False, name=None)]
59
+ except Exception as error:
60
+ msg = error
61
+ result = msg
62
+ finally:
63
+ return result
64
+
65
+ def create_table_and_insert_data(self, df, table_name, conn):
66
+ msg = result = None, None
67
+ try:
68
+ cur = conn.cursor()
69
+ try:
70
+ cur.execute(f"DROP TABLE {table_name}")
71
+ except:
72
+ pass # Ignora se a tabela não existir
73
+
74
+ create_sql = self.generate_create_table_sql(table_name, df)
75
+ cur.execute(create_sql)
76
+
77
+ placeholders = ', '.join([f":{i+1}" for i in range(len(df.columns))])
78
+ insert_sql = f"INSERT INTO {table_name} VALUES({placeholders})"
79
+ data = self.prepare_data_for_insert(df)
80
+ cur.executemany(insert_sql, data)
81
+ conn.commit()
82
+ cur.close()
83
+ conn.close()
84
+
85
+ except Exception as error:
86
+ msg = error
87
+ result = msg
88
+ finally:
89
+ return result
@@ -1,6 +1,12 @@
1
1
  import psycopg2 as ps2
2
+ import os
3
+ import json
2
4
 
3
- class Postgres:
5
+ from DE_Lib.Utils import Generic
6
+
7
+ gen = Generic.GENERIC()
8
+
9
+ class POSTGRES:
4
10
  def __init__(self):
5
11
  self._connection_is_valid = None
6
12
  self._nome_database = None
@@ -8,19 +14,21 @@ class Postgres:
8
14
  self.__database_error = None
9
15
 
10
16
  def Connect(self, string_connect: dict):
11
- msg, conn = None, None
17
+ msg, conn, result = None, None, None
12
18
  try:
13
19
  # Efetuando a conexao com a instancia do BANCO
14
20
  conn = ps2.connect(user=string_connect["username"], password=string_connect["password"], database=string_connect["instance"], host=string_connect["host"])
15
21
  self._connection_is_valid = True
16
- self._nome_database = string_connect["database"]
17
- self._cnn = conn
22
+ self._cnn = result
23
+ self.__database_error = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nConexao bem sucedida!"""
24
+ self._nome_database = gen.nvl(string_connect["database"], "")
18
25
  except Exception as error:
19
- conn = f"""Falha ao tentar se conectar com o banco de dados POSTGRES.\n """
26
+ msg = f"""{json.dumps(string_connect, indent=4).replace(string_connect["password"], "******")}\nFalha ao tentar se conectar com o banco de dados POSTGRES\nException Error: {error} """
27
+ result = msg
20
28
  self._connection_is_valid = False
21
- self._DATABASE_ERROR = conn.DatabaseError
29
+ self.__database_error = msg
22
30
  finally:
23
- return conn
31
+ return result
24
32
 
25
33
  @property
26
34
  def CONNECTION(self):