-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_features_info.py
106 lines (94 loc) · 3.13 KB
/
write_features_info.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from connect_db import get_db_connection
import logging
def feature_check_in_db(pid, sid, name):
cnxn = get_db_connection()
cursor = cnxn.cursor()
try:
row = cursor.execute("""
select *
from (
select id,
row_number() OVER (order by id desc) row_num
from property_features
where property_id = ?
and source_id = ?
and name = ?) t
where row_num = 1
""", pid, sid, name)
row = cursor.fetchone()
cursor.close()
cnxn.close()
if row:
return row[0]
else:
return None
except Exception as e:
logging.warning('Could not query db: {}, {}, {}: {}'.format(pid,
sid,
name,
e))
cursor.close()
cnxn.close()
def feature_details_changed(id, name, value):
cnxn = get_db_connection()
cursor = cnxn.cursor()
row = None
try:
row = cursor.execute("""
select property_id,
source_id,
name,
value
from property_features
where id = ?
""", id)
row = cursor.fetchone()
except Exception as e:
logging.warning('Could not query db: {}, {}, {}: {}'.format(id,
name,
value,
e))
_new_name = name.lower()
_new_value = value
_old_name = row[2]
_old_value = row[3]
if _old_name != _new_name or \
_old_value != _new_value:
cursor.execute("""
update property_features
set details_end_date = getdate()
where id = ?
""", id)
cursor.commit()
cursor.close()
cnxn.close()
feature_write_to_db(row[0], row[1], _new_name, _new_value)
def feature_write_to_db(pid, sid, name, value):
cnxn = get_db_connection()
cursor = cnxn.cursor()
try:
cursor.execute("""
INSERT INTO property_features (
property_id,
source_id,
name,
value)
VALUES (
?, ?, ?, ?)""",
pid,
sid,
name.lower(),
value
)
cursor.commit()
cursor.close()
cnxn.close()
except Exception as e:
logging.warning('Could not query db: {}, {}, {}, {}: {}'.format(pid,
sid,
name,
value,
e))
cursor.close()
cnxn.close()
pass