# -*- coding: utf-8 -*- import sqlite3 from collections import namedtuple from arkindex_worker import logger SQL_ELEMENTS_TABLE_CREATION = """CREATE TABLE IF NOT EXISTS elements ( id VARCHAR(32) PRIMARY KEY, parent_id VARCHAR(32), type TEXT NOT NULL, polygon TEXT, initial BOOLEAN DEFAULT 0 NOT NULL, worker_version_id VARCHAR(32) )""" CachedElement = namedtuple( "CachedElement", ["id", "type", "polygon", "worker_version_id", "parent_id", "initial"], defaults=[None, 0], ) class LocalDB(object): def __init__(self, path): self.db = sqlite3.connect(path) self.db.row_factory = sqlite3.Row self.cursor = self.db.cursor() logger.info(f"Connection to local cache {path} established.") def create_tables(self): self.cursor.execute(SQL_ELEMENTS_TABLE_CREATION) def insert(self, table, lines): if not lines: return columns = ", ".join(lines[0]._fields) placeholders = ", ".join("?" * len(lines[0])) values = [tuple(line) for line in lines] self.cursor.executemany( f"INSERT INTO {table} ({columns}) VALUES ({placeholders})", values ) self.db.commit()