function bb_sql_describe_table($query) { require_once BACKPRESS_PATH . 'class.bp-sql-schema-parser.php'; bb_log_deprecated('function', __FUNCTION__, 'BP_SQL_Schema_Parser::describe_table'); return BP_SQL_Schema_Parser::describe_table($query); }
/** * Splits grouped SQL statements into queries within a highly structured array * Only supports CREATE TABLE, INSERT and UPDATE */ function parse($sql) { // Only accept strings or arrays if (is_string($sql)) { // Just pop strings into an array to start with $queries = array($sql); } elseif (is_array($sql)) { // Flatten the array $queries = BP_SQL_Schema_Parser::_flatten_array($sql, 0, false); // Remove empty nodes $queries = array_filter($queries); } else { return false; } // Clean up the queries $_clean_queries = array(); foreach ($queries as $_query) { // Trim space and semi-colons $_query = trim($_query, "; \t\n\r\v"); // If it exists and isn't a number if ($_query && !is_numeric($_query)) { // Is it more than one query? if (strpos(';', $_query) !== false) { // Explode by semi-colon foreach (explode(';', $_query) as $_part) { $_part = trim($_part); if ($_part && !is_numeric($_part)) { // Pull out any commented code // Can't properly deal with /*!4321 FOO `bar` */ version specific inclusion, just includes it regardless of version $_part = preg_replace('@/\\*![0-9]*([^\\*]*)\\*/@', '$1', $_part); $_part = preg_replace('@/\\*[^\\*]*\\*/@', '', $_part); $_part = preg_replace('@[\\-\\-|#].*$@m', '', $_part); $_clean_queries[] = trim($_part) . ';'; } } unset($_part); } else { $_clean_queries[] = $_query . ';'; } } } unset($_query); if (!count($_clean_queries)) { return false; } $queries = $_clean_queries; unset($_clean_queries); $_queries = array(); foreach ($queries as $_query) { // Only process table creation, inserts and updates, capture the table/database name while we are at it if (!preg_match('@^(CREATE\\s+TABLE(?:\\s+IF\\s+NOT\\s+EXISTS)?|INSERT\\s+INTO|UPDATE)\\s+`?([^\\s|`]+)`?@im', $_query, $_matches)) { continue; } // Tidy up the type so we can switch it $_type = strtoupper(preg_replace('@\\s+@', ' ', trim($_matches[1]))); $_table_name = trim($_matches[2]); unset($_matches); switch ($_type) { case 'CREATE TABLE': case 'CREATE TABLE IF NOT EXISTS': $_description = BP_SQL_Schema_Parser::describe_table($_query); if (is_array($_description)) { $_queries['tables'][$_table_name] = $_description; } break; case 'INSERT INTO': // Just add the query as is for now $_queries['insert'][$_table_name][] = $_query; break; case 'UPDATE': // Just add the query as is for now $_queries['update'][$_table_name][] = $_query; break; } unset($_type, $_table_name); } unset($_query); if (!count($_queries)) { return false; } return $_queries; }