-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchepy_sqlite.py
70 lines (54 loc) · 1.86 KB
/
chepy_sqlite.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
from pathlib import Path
# TODO move sql import to lazy
import sqlite3
import chepy.core
class Chepy_SQLite(chepy.core.ChepyCore):
"""This plugin allows interacting with SQLite3 database files"""
def _conn(self):
p = Path(self._convert_to_str())
if not p.is_file():
logging.error("State is not a valid file path")
return sqlite3.connect(f"file:{str(p)}?mode=rw", uri=True)
@chepy.core.ChepyDecorators.call_stack
def sqlite_get_tables(self):
"""Get all table names from db
Returns:
ChepyPlugin: The Chepy object.
"""
curr = self._conn().execute('select name from sqlite_master where type="table"')
self.state = [c for [c] in curr]
return self
@chepy.core.ChepyDecorators.call_stack
def sqlite_get_columns(self, table: str):
"""List all columns of a table
Args:
table (str): A valid table name
Returns:
ChepyPlugin: The Chepy object.
"""
curr = self._conn().execute(f"select * from {table};")
self.state = [c[0] for c in curr.description]
return self
@chepy.core.ChepyDecorators.call_stack
def sqlite_dump_table(self, table: str):
"""Dump all data from a table
Args:
table (str): A valid table name
Returns:
ChepyPlugin: The Chepy object.
"""
curr = self._conn().execute(f"select * from {table};")
self.state = [c for c in curr]
return self
@chepy.core.ChepyDecorators.call_stack
def sqlite_query(self, query: str):
"""Run a raw sql query
Args:
query (str): The sql query string
Returns:
ChepyPlugin: The Chepy object.
"""
curr = self._conn().execute(query)
self.state = [c for c in curr]
return self