Metadata-Version: 2.4
Name: sqlgrep
Version: 0.0.14
Summary: Find string anywhere in database
Project-URL: Homepage, https://github.com/yaroslaff/sqlgrep
Project-URL: Issues, https://github.com/yaroslaff/sqlgrep/issues
Author-email: Yaroslav Polyakov <yaroslaff@gmail.com>
License: MIT License
        
        Copyright (c) 2022 Yaroslav Polyakov
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Requires-Dist: rich
Requires-Dist: sqlalchemy
Provides-Extra: mysql
Requires-Dist: mysqlclient; extra == 'mysql'
Provides-Extra: postgresql
Requires-Dist: psycopg2; extra == 'postgresql'
Description-Content-Type: text/markdown

# SQLGrep: Grep in MySQL database tables / fields

*If you do not know db schema (drank a lot yesterday, first day on new project or hacking alien starhip database)*

SQLGrep will examine db schema and search (`SELECT ... WHERE ...`) for specified text/number/regex/like (needle) in all fields of all tables.

## Install

To install mysql version on debian you may need to:
~~~
apt install pkg-config libmariadb-dev python3-dev gcc
~~~

Installation:
~~~
# new way, use pipx if you can (with mysql packages)
pipx install sqlgrep[mysql]

# old-fashioned way  (and postgresql support)
pip install sqlgrep[postgresql]
~~~


## Examples
I use test db with just one table, so all found records will be in "libro" table.

~~~shell
# We want to know where year is stored and we know, at least one book published in 1991
# found in table libro, fields: masterid, id, anno, peso
$ sqlgrep mysql://localhost/libro --limit 1 1991
libro(id=1991) masterId: 1991
libro(id=1991) id: 1991
libro(id=80) anno: '1991'
libro(id=169601) peso: 1991

# LIKE search, I'm only interested in mice...
$ sqlgrep mysql://localhost/libro --like %mice%
libro(id=149894) autore: 'Primicerj Giulio'
libro(id=37004) title: 'ECONOMICESKAIA GHEOGRAFIA SSSR. - Lialikov H.I. - 1961'
libro(id=1359) titolo: 'STUDI MICENEI ED EGEO-ANATOLICI. Fascicolo ottavo.'
libro(id=1367) titolo: 'IL TORO DI MINOSSE. Creta, il Minotauro e la civiltà micenea.'
~~~

To look in database via socket connection use URL `mysql:///dbname` .

## Speed
sqlgrep does one SQL SELECT ... WHERE query for each field in database. So, for db of 5 tables and 10 fields in each, there will be 50 queries (sending query to db is very simple and fast operation). All filtering are performed on database side (not in our slow python code), so it goes with maximal speed.

## Database credentials
Specify database as SQLAlchemy URL like `mysql://user:password@host/db_name` (or `postgresql://...`)

See SQLAlchemy [Engine Configuration](https://docs.sqlalchemy.org/en/20/core/engines.html) for details.

## Usage
~~~
usage: sqlgrep [-h] [--host HOST] [-t TABLES [TABLES ...]] [--like] [--float] [--int] [--limit N] [-c] [--all]
               DB URL needle

database search (SQL grep), version: 0.0.6

positional arguments:
  DB URL                example: mysql://user:password@host/db_name
  needle

options:
  -h, --help            show this help message and exit
  --host HOST
  -t TABLES [TABLES ...], --tables TABLES [TABLES ...]
                        tables (default: all)
  --like                use SQL LIKE instead of =

Types (default - string):
  --float               coerce to float
  --int                 coerce to integer

Output:
  --limit N             Limit to N results for each column
  -c, --count           Count only
  --all                 display ALL fields from matching rows
~~~