summaryrefslogtreecommitdiffstats
path: root/pgspecial/namedqueries.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 05:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 05:31:44 +0000
commitf77392695c09f9fef9386c112aef0e2b2f6fcd1a (patch)
tree04c428a7cfc9b7c6dbea73b5697f8c201d9106ff /pgspecial/namedqueries.py
parentInitial commit. (diff)
downloadpython-pgspecial-f77392695c09f9fef9386c112aef0e2b2f6fcd1a.tar.xz
python-pgspecial-f77392695c09f9fef9386c112aef0e2b2f6fcd1a.zip
Adding upstream version 2.1.2.upstream/2.1.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--pgspecial/namedqueries.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/pgspecial/namedqueries.py b/pgspecial/namedqueries.py
new file mode 100644
index 0000000..ef46617
--- /dev/null
+++ b/pgspecial/namedqueries.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+class NamedQueries(object):
+ section_name = "named queries"
+
+ usage = """Named Queries are a way to save frequently used queries
+with a short name. Think of them as favorites.
+Examples:
+
+ # Save a new named query.
+ > \\ns simple select * from abc where a is not Null;
+
+ # List all named queries.
+ > \\n
+ +--------+----------------------------------------+
+ | Name | Query |
+ |--------+----------------------------------------|
+ | simple | SELECT * FROM xyzb where a is not null |
+ +--------+----------------------------------------+
+
+ # Run a named query.
+ > \\n simple
+ +-----+
+ | a |
+ |-----|
+ | 50 |
+ +-----+
+
+ # Delete a named query.
+ > \\nd simple
+ simple: Deleted
+"""
+
+ # Class-level variable, for convenience to use as a singleton.
+ instance = None
+
+ def __init__(self, config):
+ self.config = config
+
+ @classmethod
+ def from_config(cls, config):
+ return NamedQueries(config)
+
+ def list(self):
+ return self.config.get(self.section_name, [])
+
+ def get(self, name):
+ return self.config.get(self.section_name, {}).get(name, None)
+
+ def save(self, name, query):
+ if self.section_name not in self.config:
+ self.config[self.section_name] = {}
+ self.config[self.section_name][name] = query
+ self.config.write()
+
+ def delete(self, name):
+ try:
+ del self.config[self.section_name][name]
+ except KeyError:
+ return "%s: Not Found." % name
+ self.config.write()
+ return "%s: Deleted" % name