dict cursor e pool conn
This commit is contained in:
@@ -1,37 +1,36 @@
|
||||
from utils.database.connection import connetti_db
|
||||
|
||||
import aiomysql
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def get_nodes_type(cfg: object, tool: str, unit: str) -> tuple:
|
||||
async def get_nodes_type(cfg: object, tool: str, unit: str, pool) -> tuple:
|
||||
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor(dictionary=True)
|
||||
query = f"""
|
||||
SELECT t.name AS name, n.seq AS seq, n.num AS num, n.channels AS channels, y.type AS type, n.ain AS ain, n.din AS din
|
||||
FROM {cfg.dbname}.{cfg.dbnodes} AS n
|
||||
INNER JOIN tools AS t ON t.id = n.tool_id
|
||||
INNER JOIN units AS u ON u.id = t.unit_id
|
||||
INNER JOIN nodetypes AS y ON n.nodetype_id = y.id
|
||||
WHERE y.type NOT IN ('Anchor Link', 'None') AND t.name = '{tool}' AND u.name = '{unit}'
|
||||
ORDER BY n.num;
|
||||
"""
|
||||
#logger.info(f"{unit} - {tool}: Executing query: {query}")
|
||||
cur.execute(query)
|
||||
results = cur.fetchall()
|
||||
logger.info(f"{unit} - {tool}: {cur.rowcount} rows selected to get node type/Ain/Din/channels.")
|
||||
cur.close()
|
||||
conn.close()
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(f"""
|
||||
SELECT t.name AS name, n.seq AS seq, n.num AS num, n.channels AS channels, y.type AS type, n.ain AS ain, n.din AS din
|
||||
FROM {cfg.dbname}.{cfg.dbnodes} AS n
|
||||
INNER JOIN tools AS t ON t.id = n.tool_id
|
||||
INNER JOIN units AS u ON u.id = t.unit_id
|
||||
INNER JOIN nodetypes AS y ON n.nodetype_id = y.id
|
||||
WHERE y.type NOT IN ('Anchor Link', 'None') AND t.name = '{tool}' AND u.name = '{unit}'
|
||||
ORDER BY n.num;
|
||||
""")
|
||||
|
||||
results = await cur.fetchall()
|
||||
logger.info(f"{unit} - {tool}: {cur.rowcount} rows selected to get node type/Ain/Din/channels.")
|
||||
|
||||
if not results:
|
||||
logger.info(f"{unit} - {tool}: Node/Channels/Ain/Din not defined.")
|
||||
return None, None, None, None
|
||||
else:
|
||||
channels, types, ains, dins = [], [], [], []
|
||||
for row in results:
|
||||
channels.append(row['channels'])
|
||||
types.append(row['type'])
|
||||
ains.append(row['ain'])
|
||||
dins.append(row['din'])
|
||||
return channels, types, ains, dins
|
||||
|
||||
if not results:
|
||||
logger.info(f"{unit} - {tool}: Node/Channels/Ain/Din not defined.")
|
||||
return None, None, None, None
|
||||
else:
|
||||
channels, types, ains, dins = [], [], [], []
|
||||
for row in results:
|
||||
channels.append(row['channels'])
|
||||
types.append(row['type'])
|
||||
ains.append(row['ain'])
|
||||
dins.append(row['din'])
|
||||
return channels, types, ains, dins
|
||||
|
||||
|
||||
Reference in New Issue
Block a user