Update REGEXP function for sqlite
This commit is contained in:
@ -1,11 +1,10 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import List, Optional
|
||||
from typing import List
|
||||
import argparse
|
||||
import sqlite3
|
||||
import os
|
||||
import re
|
||||
import datetime
|
||||
|
||||
import utils
|
||||
@ -39,12 +38,6 @@ def get_sql_files() -> List[str]:
|
||||
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:
|
||||
rows = utils.csv_parse(f'{DIR}/{table_name}.csv')
|
||||
names = next(rows)
|
||||
@ -117,7 +110,7 @@ def main() -> None:
|
||||
sqlite3.register_adapter(datetime.time, lambda t: t.strftime('%H:%M:%S'))
|
||||
|
||||
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:
|
||||
for file_name in get_sql_files():
|
||||
|
@ -2,7 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
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 datetime
|
||||
import csv
|
||||
@ -13,6 +13,10 @@ RE_STR_START = 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:
|
||||
if value == '':
|
||||
return None
|
||||
|
Reference in New Issue
Block a user