-
Notifications
You must be signed in to change notification settings - Fork 0
/
HireFirm.py
84 lines (75 loc) · 2.29 KB
/
HireFirm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import datetime
import os
from dotenv import load_dotenv
from db_connect import *
from dataclasses import dataclass
@dataclass
class HireFirm:
OrgId: int
FirmId: int
StartDate: str
EndDate: str
load_dotenv()
connection = db_connect(os.getenv('CALACCESSDB'))
cursor = connection.cursor()
# get all hired firms
firm_hires = []
cursor.execute("""
WITH FilerIdTable AS (
SELECT DISTINCT FILER_ID, NAML
FROM FILERNAME_CD
),
OrgIdTable AS (
SELECT DISTINCT
organization.FILER_ID as OrgId,
SUB_NAME,
LEMP_CD.EFF_DATE AS StartDate,
LEMP_CD.CON_PERIOD AS EndDate
FROM
LEMP_CD
LEFT JOIN
FilerIdTable as organization ON organization.NAML = LEMP_CD.CLI_NAML
WHERE LEMP_CD.CLI_NAML != '' AND LEMP_CD.SUB_NAME != ''
)
SELECT DISTINCT
OrgId,
lobbyingfirm.FILER_ID as FirmId,
StartDate,
EndDate
FROM
OrgIdTable
LEFT JOIN
FilerIdTable as lobbyingfirm ON lobbyingfirm.NAML = SUB_NAME
WHERE OrgId IS NOT NULL AND lobbyingfirm.FILER_ID IS NOT NULL
""")
db_firm_hires = cursor.fetchall()
print("Unique Firm Hires: ", cursor.rowcount)
for hire in db_firm_hires:
firm_hires.append(
HireFirm(
OrgId=hire[0],
FirmId=hire[1],
StartDate=hire[2],
EndDate=hire[3]
)
)
cursor.close()
connection.close()
# insert HireFirms into db
connection = db_connect(os.getenv('DEVDB'))
cursor = connection.cursor()
insert_query = """
INSERT IGNORE INTO HireFirm (OrgId, FirmId, StartDate, EndDate)
VALUES (%s, %s, %s, %s)
"""
data_to_insert = [
(hire.OrgId, hire.FirmId, hire.StartDate, hire.EndDate)
for hire in firm_hires
]
batch_size = 1000
for i in range(0, len(data_to_insert), batch_size):
batch_data = data_to_insert[i:i + batch_size]
cursor.executemany(insert_query, batch_data)
connection.commit()
cursor.close()
connection.close()