ozh / sqltableextractor
Extract table names from SQL queries (SELECT, INSERT, UPDATE, JOIN, subqueries)
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/ozh/sqltableextractor
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2026-01-22 04:49:51 UTC
README
A lightweight PHP library to extract table names from SQL queries.
Features
- ✅ Supports SELECT, INSERT, UPDATE queries
- ✅ Handles all JOIN types (INNER, LEFT, RIGHT, FULL, CROSS)
- ✅ Extracts tables from subqueries
- ✅ Removes SQL comments and string literals
- ✅ Cleans table names (backticks, quotes, database prefixes)
- ✅ Manages table aliases
- ✅ Zero dependencies
Installation
composer require --prefer-dist ozh/sqltableextractor
Usage
use Ozh\SQLTableExtractor\SqlTableExtractor; $extractor = new SqlTableExtractor(); $query = "SELECT u.*, o.* FROM users u LEFT JOIN orders o ON u.id = o.user_id"; $tables = $extractor->extractTables($query); ## Examples print_r($tables); // Output: ['users', 'orders']
$extractor = new SqlTableExtractor(); $tables = $extractor->extractTables( // Simple SELECT $tables = $extractor->extractTables("SELECT * FROM users WHERE id = 1"); // ['users'] // Multiple JOINs $tables = $extractor->extractTables( "SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id LEFT JOIN table3 t3 ON t2.id = t3.id" ); // ['table1', 'table2', 'table3'] // INSERT "INSERT INTO customers (name, email) VALUES ('John', 'john@example.com')" ); // ['customers'] // UPDATE with subquery $tables = $extractor->extractTables( "UPDATE products SET price = 100 WHERE category_id IN (SELECT id FROM categories WHERE name = 'Electronics')" ); // ['products', 'categories'] // Database prefix $tables = $extractor->extractTables("SELECT * FROM `mydb`.`users`"); // ['users']
Requirements
- PHP 7.4 or higher
License
Do whatever the hell you want with it. Or,
MIT License - See LICENSE file for details