MCPcopy
hub / github.com/HelloZeroNet/ZeroNet / Db

Class Db

src/Db/Db.py:56–432  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54 self.table = table
55
56class Db(object):
57
58 def __init__(self, schema, db_path, close_idle=False):
59 self.db_path = db_path
60 self.db_dir = os.path.dirname(db_path) + "/"
61 self.schema = schema
62 self.schema["version"] = self.schema.get("version", 1)
63 self.conn = None
64 self.cur = None
65 self.progress_sleeping = False
66 self.log = logging.getLogger("Db:%s" % schema["db_name"])
67 self.table_names = None
68 self.collect_stats = False
69 self.foreign_keys = False
70 self.need_commit = False
71 self.query_stats = {}
72 self.db_keyvalues = {}
73 self.delayed_queue = []
74 self.delayed_queue_thread = None
75 self.close_idle = close_idle
76 self.last_query_time = time.time()
77 self.last_sleep_time = time.time()
78 self.num_execute_since_sleep = 0
79
80 def __repr__(self):
81 return "<Db#%s:%s close_idle:%s>" % (id(self), self.db_path, self.close_idle)
82
83 def connect(self):
84 if self not in opened_dbs:
85 opened_dbs.append(self)
86 s = time.time()
87 if not os.path.isdir(self.db_dir): # Directory not exist yet
88 os.makedirs(self.db_dir)
89 self.log.debug("Created Db path: %s" % self.db_dir)
90 if not os.path.isfile(self.db_path):
91 self.log.debug("Db file not exist yet: %s" % self.db_path)
92 self.conn = sqlite3.connect(self.db_path, isolation_level="DEFERRED")
93 self.conn.row_factory = sqlite3.Row
94 self.conn.set_progress_handler(self.progress, 5000000)
95 self.cur = self.getCursor()
96 self.log.debug(
97 "Connected to %s in %.3fs (opened: %s, sqlite version: %s)..." %
98 (self.db_path, time.time() - s, len(opened_dbs), sqlite3.version)
99 )
100
101 def progress(self, *args, **kwargs):
102 self.progress_sleeping = True
103 time.sleep(0.001)
104 self.progress_sleeping = False
105
106 # Execute query using dbcursor
107 def execute(self, query, params=None):
108 if not self.conn:
109 self.connect()
110 return self.cur.execute(query, params)
111
112 def commit(self, reason="Unknown"):
113 if self.progress_sleeping:

Callers 2

openDbMethod · 0.90
Db.pyFile · 0.85

Calls

no outgoing calls

Tested by 1

openDbMethod · 0.72