embedded queries
This commit is contained in:
parent
c7b4e8ef85
commit
cc8adbebad
12
inex.py
12
inex.py
@ -1,23 +1,20 @@
|
||||
import pyodbc
|
||||
import os
|
||||
import logging
|
||||
import datetime
|
||||
import tomllib
|
||||
from inexLogging import inexLog
|
||||
import inexConnect
|
||||
from inexDataModel import dataTemplate
|
||||
from inexDataProcessing import processData
|
||||
import json
|
||||
import decimal
|
||||
import requests
|
||||
import inexEncoder
|
||||
|
||||
import inexSqlquery
|
||||
class Inex:
|
||||
def __init__(self):
|
||||
"""Initilize config, calls functions from inex-connect.py and inex-logging.py"""
|
||||
# assign libraries
|
||||
self.db = pyodbc
|
||||
self.tm = datetime
|
||||
self.il = logging
|
||||
self.ic = inexConnect
|
||||
self.r = requests
|
||||
@ -25,6 +22,7 @@ class Inex:
|
||||
self.os = os
|
||||
self.j = json
|
||||
self.e = inexEncoder.Encoder
|
||||
self.sq = inexSqlquery
|
||||
|
||||
if self.os.path.exists('./config.toml'):
|
||||
config_file_path = './config.toml'
|
||||
@ -63,9 +61,11 @@ class Inex:
|
||||
inexLog(self)
|
||||
|
||||
# create the connection to the database
|
||||
self.cursor = self.ic.connectDatabase(self, self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
|
||||
self.cursor = self.ic.inexSql.connectDatabase(self, self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
|
||||
|
||||
self.data = self.ic.databaseQuery(self, self.cursor, self.dbQuery)
|
||||
# self.data = self.ic.inexSql.databaseQuery(self, self.cursor, self.dbQuery)
|
||||
|
||||
self.data = self.ic.inexSql.databaseQuery(self, self.cursor, self.sq.sqlQuerymodel.queryData())
|
||||
|
||||
self.modifiedData = processData(self.data, dataTemplate, prd_instance_id=self.prdInstanceID,\
|
||||
product_guid=self.productGUID,product_name=self.productName,product_version=self.productVersion)
|
||||
|
112
inexConnect.py
112
inexConnect.py
@ -1,45 +1,79 @@
|
||||
def connectDatabase(self, lib, driver, server, database, user, password):
|
||||
"""Connects to the database. Requires a windows driver to do so.
|
||||
Typically there is one installed by default"""
|
||||
class inexSql:
|
||||
def connectDatabase(self, lib, driver, server, database, user, password):
|
||||
"""Connects to the database. Requires a windows driver to do so.
|
||||
Typically there is one installed by default"""
|
||||
|
||||
connectionString = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={user};PWD={password};TrustServerCertificate=yes'
|
||||
if self.useLog:
|
||||
self.il.debug(f"Connection String: connectionString")
|
||||
self.il.info(f"Connecting to {database}@{server} with driver[{driver}].")
|
||||
try:
|
||||
connection = lib.connect(connectionString)
|
||||
except lib.Error as ex:
|
||||
sqlstate = ex.args[1]
|
||||
connectionString = f'DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={user};PWD={password};TrustServerCertificate=yes'
|
||||
if self.useLog:
|
||||
self.il.error(sqlstate)
|
||||
if self.useLog:
|
||||
self.il.debug(f"Connected.")
|
||||
cursor = connection.cursor()
|
||||
|
||||
return cursor
|
||||
|
||||
def databaseQuery(self, cursor, query, args=()):
|
||||
if self.useLog:
|
||||
self.il.debug(f"Query:")
|
||||
self.il.debug(query)
|
||||
self.il.info(f"Sending query:{query[0:20]}...")
|
||||
|
||||
try:
|
||||
cur = cursor.execute(query, args)
|
||||
except cur.Error as ex:
|
||||
sqlstate = ex.args[1]
|
||||
self.il.debug(f"Connection String: connectionString")
|
||||
self.il.info(f"Connecting to {database}@{server} with driver[{driver}].")
|
||||
try:
|
||||
connection = lib.connect(connectionString)
|
||||
except lib.Error as ex:
|
||||
sqlstate = ex.args[1]
|
||||
if self.useLog:
|
||||
self.il.error(sqlstate)
|
||||
if self.useLog:
|
||||
self.il.error(sqlstate)
|
||||
|
||||
if self.useLog:
|
||||
self.il.debug(f"Processing database response...")
|
||||
r = [dict((cur.description[i][0], value) \
|
||||
for i, value in enumerate(row)) for row in cur.fetchall()]
|
||||
|
||||
cur.connection.close()
|
||||
if self.useLog:
|
||||
self.il.debug(f"Database connection closed")
|
||||
return r
|
||||
self.il.debug(f"Connected.")
|
||||
cursor = connection.cursor()
|
||||
|
||||
return cursor
|
||||
|
||||
def databaseQuery(self, cursor, query, args=()):
|
||||
if self.useLog:
|
||||
self.il.debug(f"Query:")
|
||||
self.il.debug(query)
|
||||
self.il.info(f"Sending query:{query[0:20]}...")
|
||||
|
||||
try:
|
||||
cur = cursor.execute(query, args)
|
||||
except cur.Error as ex:
|
||||
sqlstate = ex.args[1]
|
||||
if self.useLog:
|
||||
self.il.error(sqlstate)
|
||||
|
||||
if self.useLog:
|
||||
self.il.debug(f"Processing database response...")
|
||||
r = [dict((cur.description[i][0], value) \
|
||||
for i, value in enumerate(row)) for row in cur.fetchall()]
|
||||
|
||||
cur.connection.close()
|
||||
if self.useLog:
|
||||
self.il.debug(f"Database connection closed")
|
||||
return r
|
||||
def queryData():
|
||||
"""Embedded query data"""
|
||||
q ="""DECLARE @stopTime DATETIME2
|
||||
SET @stopTime = DATEADD(DAY, -30, GETDATE())
|
||||
SELECT p.[ProtocolCommandID]
|
||||
,t.[Time_stamp]
|
||||
,p.[RemoteIP]
|
||||
,p.[RemotePort]
|
||||
,p.[LocalIP]
|
||||
,p.[LocalPort]
|
||||
,p.[Protocol]
|
||||
,p.[SiteName]
|
||||
,p.[Command]
|
||||
,p.[CommandParameters]
|
||||
,p.[FileName]
|
||||
,p.[VirtualFolderName]
|
||||
,p.[PhysicalFolderName]
|
||||
,p.[IsInternal]
|
||||
,p.[FileSize]
|
||||
,p.[TransferTime]
|
||||
,p.[BytesTransferred]
|
||||
,p.[ResultID]
|
||||
,t.[TransactionID]
|
||||
,p.[Description]
|
||||
,p.[Actor]
|
||||
,t.ParentTransactionID
|
||||
,t.TransactionObject
|
||||
,t.NodeName
|
||||
,t.TransactionGUID
|
||||
,a.Protocol user_type
|
||||
FROM [EFTDB].[dbo].[tbl_Transactions] t Full JOIN tbl_ProtocolCommands p ON (t.TransactionID = p.TransactionID) Full join tbl_Authentications a ON (t.TransactionID = a.TransactionID)
|
||||
WHERE p.Time_stamp > @stopTime"""
|
||||
return q
|
||||
|
||||
class fortraEFC:
|
||||
def getToken(self):
|
||||
|
11
inexSqlquery.py
Normal file
11
inexSqlquery.py
Normal file
@ -0,0 +1,11 @@
|
||||
class sqlQuerymodel:
|
||||
def queryData():
|
||||
"""Embedded query data"""
|
||||
q ="""DECLARE @stopTime DATETIME2
|
||||
SET @stopTime=DATEADD(DAY, -30, GETDATE())
|
||||
SELECT p.ProtocolCommandID, t.Time_stamp, p.RemoteIP, p.RemotePort, p.LocalIP, p.LocalPort, p.Protocol, p.SiteName, p.Command, p.CommandParameters, p.FileName, p.VirtualFolderName, p.PhysicalFolderName, p.IsInternal, p.FileSize, p.TransferTime, p.BytesTransferred, p.ResultID, t.TransactionID, p.Description, p.Actor, t.ParentTransactionID, t.TransactionObject, t.NodeName, t.TransactionGUID, a.Protocol user_type
|
||||
FROM tbl_Transactions t
|
||||
Full JOIN tbl_ProtocolCommands p ON(t.TransactionID=p.TransactionID)
|
||||
Full join tbl_Authentications a ON(t.TransactionID=a.TransactionID)
|
||||
WHERE p.Time_stamp>@stopTime"""
|
||||
return q
|
Loading…
x
Reference in New Issue
Block a user