diff options
author | Grant Lanham <contact@grantlanham.com> | 2024-09-29 20:56:59 -0400 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2024-10-03 13:04:06 +0200 |
commit | 2a29e16d25ae19d9216b959f177cdcd71c43511e (patch) | |
tree | e41231c4d32c3a9ebca3b21a488d4cfd38cc0578 /searx/engines | |
parent | d48f04e8094c8b9ddb7808a9798f71549146a38e (diff) | |
download | searxng-2a29e16d25ae19d9216b959f177cdcd71c43511e.tar.gz searxng-2a29e16d25ae19d9216b959f177cdcd71c43511e.zip |
[feat] implement mariadb engine
Diffstat (limited to 'searx/engines')
-rw-r--r-- | searx/engines/mariadb_server.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/searx/engines/mariadb_server.py b/searx/engines/mariadb_server.py new file mode 100644 index 000000000..7cf7eec33 --- /dev/null +++ b/searx/engines/mariadb_server.py @@ -0,0 +1,79 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +"""MariaDB is a community driven fork of MySQL. Before enabling MariaDB engine, +you must the install the pip package ``mariadb`` along with the necessary +prerequities. + +`See the following documentation for more details +<https://mariadb.com/docs/server/connect/programming-languages/c/install/>`_ + +Example +======= + +This is an example configuration for querying a MariaDB server: + +.. code:: yaml + + - name: my_database + engine: mariadb_server + database: my_database + username: searxng + password: password + limit: 5 + query_str: 'SELECT * from my_table WHERE my_column=%(query)s' + +""" + +from typing import TYPE_CHECKING + +try: + import mariadb +except ImportError: + # import error is ignored because the admin has to install mysql manually to use + # the engine + pass + +if TYPE_CHECKING: + import logging + + logger = logging.getLogger() + + +engine_type = 'offline' +host = "127.0.0.1" +port = 3306 +database = "" +username = "" +password = "" +query_str = "" +limit = 10 +paging = True +result_template = 'key-value.html' +_connection = None + + +def init(engine_settings): + global _connection # pylint: disable=global-statement + + if 'query_str' not in engine_settings: + raise ValueError('query_str cannot be empty') + + if not engine_settings['query_str'].lower().startswith('select '): + raise ValueError('only SELECT query is supported') + + _connection = mariadb.connect(database=database, user=username, password=password, host=host, port=port) + + +def search(query, params): + query_params = {'query': query} + query_to_run = query_str + ' LIMIT {0} OFFSET {1}'.format(limit, (params['pageno'] - 1) * limit) + logger.debug("SQL Query: %s", query_to_run) + + with _connection.cursor() as cur: + cur.execute(query_to_run, query_params) + results = [] + col_names = [i[0] for i in cur.description] + for res in cur: + result = dict(zip(col_names, map(str, res))) + result['template'] = result_template + results.append(result) + return results |