Update REGEXP function for sqlite
This commit is contained in:
@ -1,11 +1,10 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from typing import List, Optional
|
from typing import List
|
||||||
import argparse
|
import argparse
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import utils
|
import utils
|
||||||
@ -39,12 +38,6 @@ def get_sql_files() -> List[str]:
|
|||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
def sqlite_regexp(pattern: str, value: Optional[str]) -> Optional[bool]:
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
return re.match(pattern, value) is not None
|
|
||||||
|
|
||||||
|
|
||||||
def import_csv(cur: sqlite3.Cursor, table_name: str, verbose: bool = False) -> None:
|
def import_csv(cur: sqlite3.Cursor, table_name: str, verbose: bool = False) -> None:
|
||||||
rows = utils.csv_parse(f'{DIR}/{table_name}.csv')
|
rows = utils.csv_parse(f'{DIR}/{table_name}.csv')
|
||||||
names = next(rows)
|
names = next(rows)
|
||||||
@ -117,7 +110,7 @@ def main() -> None:
|
|||||||
sqlite3.register_adapter(datetime.time, lambda t: t.strftime('%H:%M:%S'))
|
sqlite3.register_adapter(datetime.time, lambda t: t.strftime('%H:%M:%S'))
|
||||||
|
|
||||||
cnx = sqlite3.connect(args.db)
|
cnx = sqlite3.connect(args.db)
|
||||||
cnx.create_function('REGEXP', 2, sqlite_regexp)
|
cnx.create_function('REGEXP', 2, utils.sqlite_regexp, deterministic=True)
|
||||||
|
|
||||||
if not args.keep:
|
if not args.keep:
|
||||||
for file_name in get_sql_files():
|
for file_name in get_sql_files():
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Iterator, Dict, Any, Tuple, TextIO, List
|
from typing import Iterator, Dict, Any, Tuple, TextIO, List, Optional
|
||||||
import re
|
import re
|
||||||
import datetime
|
import datetime
|
||||||
import csv
|
import csv
|
||||||
@ -13,6 +13,10 @@ RE_STR_START = re.compile(r'.*,"[^"]*$')
|
|||||||
RE_STR_END = re.compile(r'^[^"]*",.*')
|
RE_STR_END = re.compile(r'^[^"]*",.*')
|
||||||
|
|
||||||
|
|
||||||
|
def sqlite_regexp(pattern: str, value: Optional[str]) -> Optional[bool]:
|
||||||
|
return re.match(pattern, value) is not None if value is not None else None
|
||||||
|
|
||||||
|
|
||||||
def cast_value(value: str) -> Any:
|
def cast_value(value: str) -> Any:
|
||||||
if value == '':
|
if value == '':
|
||||||
return None
|
return None
|
||||||
|
Reference in New Issue
Block a user