forked from taoyds/test-suite-sql-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_subprocess.py
47 lines (39 loc) · 1.3 KB
/
exec_subprocess.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
import sys
sys.path.append('./')
import os
import pickle as pkl
from typing import Tuple, Any
import sqlite3
import re
def replace_cur_year(query: str) -> str:
return re.sub('YEAR\s*\(\s*CURDATE\s*\(\s*\)\s*\)\s*', '2020', query, flags=re.IGNORECASE)
# get the database cursor for a sqlite database path
def get_cursor_from_path(sqlite_path: str):
try:
if not os.path.exists(sqlite_path):
print('Openning a new connection %s' % sqlite_path)
connection = sqlite3.connect(sqlite_path)
except Exception as e:
print(sqlite_path)
raise e
connection.text_factory = lambda b: b.decode(errors='ignore')
cursor = connection.cursor()
return cursor
def exec_on_db_(sqlite_path: str, query: str) -> Tuple[str, Any]:
query = replace_cur_year(query)
cursor = get_cursor_from_path(sqlite_path)
try:
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
cursor.connection.close()
return 'result', result
except Exception as e:
cursor.close()
cursor.connection.close()
return 'exception', e
f_prefix = sys.argv[1]
func_args = pkl.load(open(f_prefix + '.in', 'rb'))
sqlite_path, query = func_args
result = exec_on_db_(sqlite_path, query)
pkl.dump(result, open(f_prefix + '.out', 'wb'))