From 1d01884fba47bbc0b63f17938ef5a744e6c2cb90 Mon Sep 17 00:00:00 2001 From: Stephan Kulla Date: Wed, 22 Nov 2023 09:29:52 +0100 Subject: [PATCH] Add src/2023/2023-11-11-analysis-of-used-tables-in-serlo.ipynb --- requirements.txt | 1 + ...-11-analysis-of-used-tables-in-serlo.ipynb | 1446 +++++++++++++++++ 2 files changed, 1447 insertions(+) create mode 100644 src/2023/2023-11-11-analysis-of-used-tables-in-serlo.ipynb diff --git a/requirements.txt b/requirements.txt index 867d0cc..6d37ed0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ mysql-connector-python gsutil psycopg2-binary SQLAlchemy +sqlglot plotly seaborn kaleido diff --git a/src/2023/2023-11-11-analysis-of-used-tables-in-serlo.ipynb b/src/2023/2023-11-11-analysis-of-used-tables-in-serlo.ipynb new file mode 100644 index 0000000..59bac15 --- /dev/null +++ b/src/2023/2023-11-11-analysis-of-used-tables-in-serlo.ipynb @@ -0,0 +1,1446 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "35d51ec1-8b42-4c80-916d-28817f203386", + "metadata": {}, + "source": [ + "# Repos using the database" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e6ea4b37-4650-449b-9984-cdc71d9ecaa3", + "metadata": {}, + "outputs": [], + "source": [ + "repos = [\"database-layer\", \"notification-mail-service\" ]" + ] + }, + { + "cell_type": "markdown", + "id": "13bb6e9d-ea21-4736-9a07-6769e7d00f8a", + "metadata": {}, + "source": [ + "# Download repos using the database" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "139e3de0-9080-4339-b1eb-5bd32681c14c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/tmp/database-layer'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "\n", + "from tempfile import gettempdir\n", + "\n", + "def get_repo_dir(repo):\n", + " return os.path.join(gettempdir(), repo)\n", + "\n", + "get_repo_dir(repos[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "bac1c347-0e2d-4c4d-b4dd-2592aacd2669", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cloning into '/tmp/database-layer'...\n", + "remote: Enumerating objects: 949, done.\u001b[K\n", + "remote: Counting objects: 100% (949/949), done.\u001b[K\n", + "remote: Compressing objects: 100% (832/832), done.\u001b[K\n", + "remote: Total 949 (delta 144), reused 821 (delta 107), pack-reused 0\u001b[K\n", + "Receiving objects: 100% (949/949), 104.49 MiB | 11.41 MiB/s, done.\n", + "Resolving deltas: 100% (144/144), done.\n", + "Cloning into '/tmp/notification-mail-service'...\n", + "remote: Enumerating objects: 1032, done.\u001b[K\n", + "remote: Counting objects: 100% (1032/1032), done.\u001b[K\n", + "remote: Compressing objects: 100% (1019/1019), done.\u001b[K\n", + "remote: Total 1032 (delta 8), reused 982 (delta 5), pack-reused 0\u001b[K\n", + "Receiving objects: 100% (1032/1032), 44.82 MiB | 11.81 MiB/s, done.\n", + "Resolving deltas: 100% (8/8), done.\n" + ] + } + ], + "source": [ + "for repo in repos:\n", + " !git clone --depth 1 \"https://github.com/serlo/{repo}.git\" {get_repo_dir(repo)}" + ] + }, + { + "cell_type": "markdown", + "id": "9146f1c9-5813-42a3-a4ed-a980724afd3a", + "metadata": {}, + "source": [ + "# Extract SQL statements" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "dc340e89-b4b9-4d10-8575-7c18d6d4ac03", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['/tmp/database-layer/scripts/changelog.ts',\n", + " '/tmp/database-layer/scripts/deploy.ts',\n", + " '/tmp/database-layer/scripts/mysql/mysql-import-anonymous-data.ts',\n", + " '/tmp/database-layer/server/tests/thread.rs',\n", + " '/tmp/database-layer/server/tests/uuid.rs']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import fnmatch\n", + "\n", + "def find_files(root_dir, extensions=['ts', 'tsx', 'rs']):\n", + " found_files = []\n", + "\n", + " for root, _, files in os.walk(root_dir):\n", + " for file in files:\n", + " # Check if the file extension matches any of the specified extensions\n", + " if any(fnmatch.fnmatch(file, f'*.{ext}') for ext in extensions):\n", + " found_files.append(os.path.join(root, file))\n", + "\n", + " return found_files\n", + "\n", + "find_files(get_repo_dir(repos[0]))[:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "63a13901-d599-4220-98ee-f34e5da7644b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'sql_query': 'SELECT user_id\\n FROM role_user\\n WHERE user_id = ?\\n and role_id = ?',\n", + " 'file': '/tmp/database-layer/server/tests/user.rs'},\n", + " {'sql_query': 'SELECT user_id\\n FROM role_user\\n WHERE user_id = ?\\n and role_id = ?',\n", + " 'file': '/tmp/database-layer/server/tests/user.rs'}]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import re\n", + "\n", + "sql_operators = (\"select\", \"insert\", \"update\", \"delete\")\n", + "raw_sql_statements = []\n", + "\n", + "def sql_query_matches(operator, sql_query):\n", + " return sql_query.lower().startswith(operator + \" \")\n", + "\n", + "for repo in repos:\n", + " source_files = find_files(get_repo_dir(repo))\n", + "\n", + " for source_file in source_files:\n", + " if source_file.endswith(\".rs\"):\n", + " regex = r\"r#\\\"([^\\\"]*)\\\"#\"\n", + " else:\n", + " regex = r\"`([^`]*)`\"\n", + "\n", + " with open(source_file, \"r\") as file:\n", + " source_content = file.read()\n", + "\n", + " matches = re.findall(regex, source_content)\n", + "\n", + " for untrimmed_match in matches:\n", + " sql_query = untrimmed_match.strip()\n", + " \n", + " # Delete comments in database-layer/server/src/navigation/model/navigation_child.rs\n", + " sql_query = re.sub(\"# Level \\d\", \"\", sql_query)\n", + "\n", + " # Fix insert\n", + " # INSERT INTO entity_link (parent_id, child_id, type_id, entity_link.order) VALUES (42, 42, 9, 42)\n", + " # in database-layer/server/src/uuid/model/entity/mod.rs\n", + " #\n", + " # -> Should be done in a PR\n", + " sql_query = sql_query.replace(\"(parent_id, child_id, type_id, entity_link.order)\", \"(parent_id, child_id, type_id, order)\")\n", + "\n", + " # Fix\n", + " # UPDATE notification\n", + " # SET email_sent = true\n", + " # WHERE id in (${notificationsIds.join(',')});\n", + " # in notification-mail-service/src/mail-service/db-connection.ts\n", + " sql_query = sql_query.replace(\"(${notificationsIds.join(',')})\", \"(42)\")\n", + "\n", + " if any(sql_query_matches(operator, sql_query) for operator in sql_operators):\n", + " raw_sql_statements.append({\n", + " \"sql_query\": sql_query,\n", + " \"file\": source_file\n", + " })\n", + "\n", + "raw_sql_statements[:2]" + ] + }, + { + "cell_type": "markdown", + "id": "2e76582b-1d15-433b-87fa-815a926f8858", + "metadata": {}, + "source": [ + "# Parse SQL statements and add data about it" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "e1f99204-0cf7-4c9d-b34a-6781abcd1041", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "SELECT user_id\n", + " FROM role_user\n", + " WHERE user_id = ?\n", + " and role_id = ?\n", + "tables ['role_user']\n", + "columns ['user_id', 'user_id', 'role_id']\n", + "\n", + "SELECT user_id\n", + " FROM role_user\n", + " WHERE user_id = ?\n", + " and role_id = ?\n", + "tables ['role_user']\n", + "columns ['user_id', 'user_id', 'role_id']\n", + "\n", + "SELECT LAST_INSERT_ID() AS id\n", + " FROM uuid;\n", + "tables ['uuid']\n", + "columns []\n", + "\n", + "select author_id from ad where id = 1\n", + "tables ['ad']\n", + "columns ['author_id', 'id']\n", + "\n", + "select author_id from blog_post where id = 1199\n", + "tables ['blog_post']\n", + "columns ['author_id', 'id']\n", + "\n", + "select author_id from comment where id = 16740\n", + "tables ['comment']\n", + "columns ['author_id', 'id']\n", + "\n", + "select * from comment_vote where user_id = 10\n", + "tables ['comment_vote']\n", + "columns ['user_id']\n", + "\n", + "select author_id from entity_revision where id = 16114\n", + "tables ['entity_revision']\n", + "columns ['author_id', 'id']\n", + "\n", + "select actor_id from event_log where id = 38383\n", + "tables ['event_log']\n", + "columns ['actor_id', 'id']\n", + "\n", + "select * from notification where user_id = 10\n", + "tables ['notification']\n", + "columns ['user_id']\n", + "\n", + "select author_id from page_revision where id = 16283\n", + "tables ['page_revision']\n", + "columns ['author_id', 'id']\n", + "\n", + "select * from role_user where user_id = 10\n", + "tables ['role_user']\n", + "columns ['user_id']\n", + "\n", + "select * from subscription where user_id = 10\n", + "tables ['subscription']\n", + "columns ['user_id']\n", + "\n", + "select * from subscription where uuid_id = 10\n", + "tables ['subscription']\n", + "columns ['uuid_id']\n", + "\n", + "select * from uuid where id = 10\n", + "tables ['uuid']\n", + "columns ['id']\n", + "\n", + "select * from flag where reporter_id = 10\n", + "tables ['flag']\n", + "columns ['reporter_id']\n", + "\n", + "INSERT INTO role_user (user_id, role_id)\n", + " VALUES (?, 3)\n", + "tables ['role_user']\n", + "columns []\n", + "\n", + "SELECT *\n", + " FROM role_user\n", + " WHERE user_id = ?\n", + " and role_id = ?\n", + "tables ['role_user']\n", + "columns ['user_id', 'role_id']\n", + "\n", + "SELECT i.id as instance_id\n", + " FROM uuid\n", + " JOIN (\n", + " SELECT id, instance_id FROM attachment_container\n", + " UNION ALL\n", + " SELECT id, instance_id FROM blog_post\n", + " UNION ALL\n", + " SELECT id, instance_id FROM comment\n", + " UNION ALL\n", + " SELECT id, instance_id FROM entity\n", + " UNION ALL\n", + " SELECT er.id, e.instance_id FROM entity_revision er JOIN entity e ON er.repository_id = e.id\n", + " UNION ALL\n", + " SELECT id, instance_id FROM page_repository\n", + " UNION ALL\n", + " SELECT pr.id, p.instance_id FROM page_revision pr JOIN page_repository p ON pr.page_repository_id = p.id\n", + " UNION ALL\n", + " SELECT ta.id, t.instance_id FROM term_taxonomy ta JOIN term t ON t.id = ta.term_id\n", + " UNION ALL\n", + " SELECT user.id, 1 FROM user) u\n", + " JOIN instance i ON i.id = u.instance_id\n", + " WHERE u.id = ?\n", + "tables ['uuid', 'instance', 'attachment_container', 'blog_post', 'comment', 'entity', 'entity_revision', 'entity', 'page_repository', 'page_revision', 'page_repository', 'term_taxonomy', 'term', 'user']\n", + "columns ['id', 'id', 'instance_id', 'id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'repository_id', 'id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'page_repository_id', 'id', 'id', 'term_id']\n", + "\n", + "INSERT INTO comment\n", + " (id, date, archived, title, content, uuid_id, parent_id,\n", + " author_id, instance_id)\n", + " VALUES (LAST_INSERT_ID(), ?, 0, ?, ?, ?, NULL, ?, ?)\n", + "tables ['comment']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO comment\n", + " (id, date, archived, title, content, uuid_id,\n", + " parent_id, author_id, instance_id )\n", + " VALUES (LAST_INSERT_ID(), ?, 0, NULL, ?, NULL, ?, ?, ?)\n", + "tables ['comment']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "UPDATE comment\n", + " SET archived = ?\n", + " WHERE id = ?\n", + "tables ['comment']\n", + "columns ['archived', 'id']\n", + "\n", + "UPDATE comment\n", + " SET comment_status_id = (SELECT id from comment_status where name = ?)\n", + " WHERE comment.id = ?\n", + "tables ['comment', 'comment_status']\n", + "columns ['comment_status_id', 'id', 'id', 'name']\n", + "\n", + "SELECT content, author_id, archived, trashed\n", + " FROM comment JOIN uuid ON uuid.id = comment.id\n", + " WHERE comment.id = ?\n", + "tables ['comment', 'uuid']\n", + "columns ['content', 'author_id', 'archived', 'trashed', 'id', 'id', 'id']\n", + "\n", + "UPDATE comment SET content = ? WHERE id = ?\n", + "tables ['comment']\n", + "columns ['content', 'id']\n", + "\n", + "SELECT date FROM event_log WHERE id = 1\n", + "tables ['event_log']\n", + "columns ['date', 'id']\n", + "\n", + "SELECT p.id\n", + " FROM navigation_page p\n", + " WHERE p.parent_id = ?\n", + " ORDER BY p.position, p.id\n", + "tables ['navigation_page']\n", + "columns ['id', 'parent_id', 'position', 'id']\n", + "\n", + "SELECT name, value FROM\n", + " (\n", + " \n", + " SELECT k.name, p.value, p.page_id\n", + " FROM navigation_parameter p\n", + " JOIN navigation_parameter_key k ON k.id = p.key_id\n", + " WHERE p.parent_id IS NULL AND value != ''\n", + " UNION ALL\n", + " \n", + " SELECT CONCAT(k1.name, '.', k2.name) as name, p2.value, p2.page_id\n", + " FROM navigation_parameter p1\n", + " JOIN navigation_parameter p2 ON p2.parent_id = p1.id\n", + " JOIN navigation_parameter_key k1 ON k1.id = p1.key_id\n", + " JOIN navigation_parameter_key k2 ON k2.id = p2.key_id\n", + " WHERE p1.parent_id IS NULL AND p2.value != ''\n", + " UNION ALL\n", + " \n", + " SELECT CONCAT(k1.name, '.', k2.name, '.', k3.name) as name, p3.value, p3.page_id\n", + " FROM navigation_parameter p1\n", + " JOIN navigation_parameter p2 ON p2.parent_id = p1.id\n", + " JOIN navigation_parameter p3 ON p3.parent_id = p2.id\n", + " JOIN navigation_parameter_key k1 ON k1.id = p1.key_id\n", + " JOIN navigation_parameter_key k2 ON k2.id = p2.key_id\n", + " JOIN navigation_parameter_key k3 ON k3.id = p3.key_id\n", + " WHERE p1.parent_id IS NULL AND p3.value != ''\n", + " ) u\n", + " WHERE page_id = ?\n", + "tables ['navigation_parameter', 'navigation_parameter_key', 'navigation_parameter', 'navigation_parameter', 'navigation_parameter_key', 'navigation_parameter_key', 'navigation_parameter', 'navigation_parameter', 'navigation_parameter', 'navigation_parameter_key', 'navigation_parameter_key', 'navigation_parameter_key']\n", + "columns ['name', 'value', 'page_id', 'name', 'value', 'page_id', 'value', 'page_id', 'value', 'page_id', 'id', 'key_id', 'parent_id', 'value', 'name', 'name', 'parent_id', 'id', 'id', 'key_id', 'id', 'key_id', 'name', 'name', 'name', 'parent_id', 'id', 'parent_id', 'id', 'id', 'key_id', 'id', 'key_id', 'id', 'key_id', 'parent_id', 'value', 'parent_id', 'value']\n", + "\n", + "SELECT p.id\n", + " FROM navigation_page p\n", + " JOIN navigation_container c ON c.id = p.container_id\n", + " JOIN instance i ON i.id = c.instance_id\n", + " JOIN type t ON t.id = c.type_id\n", + " WHERE i.subdomain = ? AND t.name = 'default' AND p.parent_id IS NULL\n", + " ORDER BY p.position, p.id\n", + "tables ['navigation_page', 'navigation_container', 'instance', 'type']\n", + "columns ['id', 'id', 'container_id', 'id', 'instance_id', 'id', 'type_id', 'position', 'id', 'parent_id', 'subdomain', 'name']\n", + "\n", + "SELECT id FROM event_log WHERE uuid_id = ? ORDER BY date DESC\n", + "tables ['event_log']\n", + "columns ['id', 'uuid_id', 'date']\n", + "\n", + "SELECT instance_id FROM comment WHERE id = ?\n", + "tables ['comment']\n", + "columns ['instance_id', 'id']\n", + "\n", + "SELECT l.id, l.actor_id, l.uuid_id, l.date, i.subdomain, e.name\n", + " FROM event_log l\n", + " LEFT JOIN event_parameter p ON l.id = p.log_id\n", + " JOIN instance i ON l.instance_id = i.id\n", + " JOIN event e ON l.event_id = e.id\n", + " WHERE l.id = ?\n", + "tables ['event_log', 'event_parameter', 'instance', 'event']\n", + "columns ['id', 'actor_id', 'uuid_id', 'date', 'subdomain', 'name', 'id', 'log_id', 'instance_id', 'id', 'event_id', 'id', 'id']\n", + "\n", + "SELECT n.name, s.value\n", + " FROM event_parameter p\n", + " JOIN event_parameter_name n ON n.id = p.name_id\n", + " JOIN event_parameter_string s ON s.event_parameter_id = p.id\n", + " WHERE p.name_id = n.id AND p.log_id = ?\n", + "tables ['event_parameter', 'event_parameter_name', 'event_parameter_string']\n", + "columns ['name', 'value', 'id', 'name_id', 'event_parameter_id', 'id', 'name_id', 'id', 'log_id']\n", + "\n", + "SELECT n.name, u.uuid_id\n", + " FROM event_parameter p\n", + " JOIN event_parameter_name n ON n.id = p.name_id\n", + " JOIN event_parameter_uuid u ON u.event_parameter_id = p.id\n", + " WHERE p.name_id = n.id AND p.log_id = ?\n", + "tables ['event_parameter', 'event_parameter_name', 'event_parameter_uuid']\n", + "columns ['name', 'uuid_id', 'id', 'name_id', 'event_parameter_id', 'id', 'name_id', 'id', 'log_id']\n", + "\n", + "SELECT *\n", + " FROM user\n", + " WHERE id = ?\n", + "tables ['user']\n", + "columns ['id']\n", + "\n", + "INSERT INTO event_log (actor_id, event_id, uuid_id, instance_id, date)\n", + " SELECT ?, id, ?, ?, ?\n", + " FROM event\n", + " WHERE name = ?\n", + "tables ['event_log', 'event']\n", + "columns ['id', 'name']\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO event_parameter (log_id, name_id)\n", + " SELECT ?, id\n", + " FROM event_parameter_name\n", + " WHERE name = ?\n", + "tables ['event_parameter', 'event_parameter_name']\n", + "columns ['id', 'name']\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO event_parameter_string (value, event_parameter_id)\n", + " VALUES (?, ?)\n", + "tables ['event_parameter_string']\n", + "columns []\n", + "\n", + "INSERT INTO event_parameter (log_id, name_id)\n", + " SELECT ?, id\n", + " FROM event_parameter_name\n", + " WHERE name = ?\n", + "tables ['event_parameter', 'event_parameter_name']\n", + "columns ['id', 'name']\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO event_parameter_uuid (uuid_id, event_parameter_id)\n", + " VALUES (?, ?)\n", + "tables ['event_parameter_uuid']\n", + "columns []\n", + "\n", + "SELECT u.trashed, f.name\n", + " FROM attachment_file f\n", + " JOIN attachment_container c ON c.id = f.attachment_id\n", + " JOIN uuid u ON u.id = c.id\n", + " WHERE c.id = ?\n", + "tables ['attachment_file', 'attachment_container', 'uuid']\n", + "columns ['trashed', 'name', 'id', 'attachment_id', 'id', 'id', 'id']\n", + "\n", + "SELECT discriminator FROM uuid WHERE id = ?\n", + "tables ['uuid']\n", + "columns ['discriminator', 'id']\n", + "\n", + "SELECT u.trashed, i.subdomain, u.discriminator\n", + " FROM uuid u\n", + " JOIN (\n", + " SELECT id, instance_id FROM attachment_container\n", + " UNION ALL\n", + " SELECT id, instance_id FROM blog_post\n", + " UNION ALL\n", + " SELECT id, instance_id FROM comment\n", + " UNION ALL\n", + " SELECT id, instance_id FROM entity\n", + " UNION ALL\n", + " SELECT id, instance_id FROM page_repository\n", + " UNION ALL\n", + " SELECT pr.id, p.instance_id FROM page_revision pr JOIN page_repository p ON pr.page_repository_id = p.id\n", + " UNION ALL\n", + " SELECT term_taxonomy.id, instance_id FROM term_taxonomy JOIN term ON term.id = term_taxonomy.term_id\n", + " ) c ON c.id = u.id\n", + " JOIN instance i ON i.id = c.instance_id\n", + " WHERE u.id = ?\n", + "tables ['uuid', 'instance', 'attachment_container', 'blog_post', 'comment', 'entity', 'page_repository', 'page_revision', 'page_repository', 'term_taxonomy', 'term']\n", + "columns ['trashed', 'subdomain', 'discriminator', 'id', 'id', 'id', 'instance_id', 'id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'id', 'instance_id', 'page_repository_id', 'id', 'id', 'term_id']\n", + "\n", + "SELECT trashed FROM uuid WHERE id = ?\n", + "tables ['uuid']\n", + "columns ['trashed', 'id']\n", + "\n", + "SELECT trashed FROM uuid WHERE id = ?\n", + "tables ['uuid']\n", + "columns ['trashed', 'id']\n", + "\n", + "SELECT trashed, username, date, last_login, description\n", + " FROM user\n", + " JOIN uuid ON user.id = uuid.id\n", + " WHERE user.id = ?\n", + "tables ['user', 'uuid']\n", + "columns ['trashed', 'username', 'date', 'last_login', 'description', 'id', 'id', 'id']\n", + "\n", + "SELECT r.name\n", + " FROM role r\n", + " JOIN role_user ru on r.id = ru.role_id\n", + " WHERE ru.user_id = ?\n", + "tables ['role', 'role_user']\n", + "columns ['name', 'id', 'role_id', 'user_id']\n", + "\n", + "SELECT u.trashed, c.author_id, c.title, c.date, c.archived, c.content, c.parent_id,\n", + " c.uuid_id, p.title as parent_title, comment_status.name as status\n", + " FROM comment c\n", + " LEFT JOIN comment p ON p.id = c.parent_id\n", + " LEFT JOIN comment_status ON comment_status.id = c.comment_status_id\n", + " JOIN uuid u ON u.id = c.id\n", + " WHERE c.id = ?\n", + "tables ['comment', 'comment', 'comment_status', 'uuid']\n", + "columns ['trashed', 'author_id', 'title', 'date', 'archived', 'content', 'parent_id', 'uuid_id', 'title', 'name', 'id', 'parent_id', 'id', 'comment_status_id', 'id', 'id', 'id']\n", + "\n", + "SELECT id\n", + " FROM comment\n", + " WHERE parent_id = ?\n", + "tables ['comment']\n", + "columns ['id', 'parent_id']\n", + "\n", + "SELECT uuid_id as id\n", + " FROM (\n", + " SELECT id, uuid_id FROM comment c\n", + " UNION ALL\n", + " SELECT c.id, p.uuid_id FROM comment p LEFT JOIN comment c ON c.parent_id = p.id\n", + " ) t\n", + " WHERE id = ? AND uuid_id IS NOT NULL\n", + "tables ['comment', 'comment', 'comment']\n", + "columns ['uuid_id', 'id', 'id', 'uuid_id', 'id', 'uuid_id', 'uuid_id', 'parent_id', 'id']\n", + "\n", + "SELECT u.trashed, r.title, r.content, r.date, r.author_id, r.page_repository_id\n", + " FROM page_revision r\n", + " JOIN uuid u ON u.id = r.id\n", + " WHERE r.id = ?\n", + "tables ['page_revision', 'uuid']\n", + "columns ['trashed', 'title', 'content', 'date', 'author_id', 'page_repository_id', 'id', 'id', 'id']\n", + "\n", + "SELECT u.trashed, b.title\n", + " FROM blog_post b\n", + " JOIN uuid u ON u.id = b.id\n", + " WHERE b.id = ?\n", + "tables ['blog_post', 'uuid']\n", + "columns ['trashed', 'title', 'id', 'id', 'id']\n", + "\n", + "SELECT u.trashed, i.subdomain, p.current_revision_id, p.license_id, r.title\n", + " FROM page_repository p\n", + " JOIN uuid u ON u.id = p.id\n", + " JOIN instance i ON i.id = p.instance_id\n", + " LEFT JOIN page_revision r ON r.id = p.current_revision_id\n", + " WHERE p.id = ?\n", + "tables ['page_repository', 'uuid', 'instance', 'page_revision']\n", + "columns ['trashed', 'subdomain', 'current_revision_id', 'license_id', 'title', 'id', 'id', 'id', 'instance_id', 'id', 'current_revision_id', 'id']\n", + "\n", + "SELECT id, date FROM page_revision WHERE page_repository_id = ?\n", + "tables ['page_revision']\n", + "columns ['id', 'date', 'page_repository_id']\n", + "\n", + "SELECT id FROM page_repository WHERE id = ?\n", + "tables ['page_repository']\n", + "columns ['id', 'id']\n", + "\n", + "INSERT INTO uuid (trashed, discriminator)\n", + " VALUES (0, 'pageRevision')\n", + "tables ['uuid']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO page_revision (id, author_id, page_repository_id, title, content, date)\n", + " VALUES (?, ?, ?, ?, ?, ?)\n", + "tables ['page_revision']\n", + "columns []\n", + "\n", + "SELECT instance_id\n", + " FROM page_repository\n", + " WHERE id = ?\n", + "tables ['page_repository']\n", + "columns ['instance_id', 'id']\n", + "\n", + "UPDATE page_repository\n", + " SET current_revision_id = ?\n", + " WHERE id = ?\n", + "tables ['page_repository']\n", + "columns ['current_revision_id', 'id']\n", + "\n", + "INSERT INTO uuid (trashed, discriminator)\n", + " VALUES (0, 'page')\n", + "tables ['uuid']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO page_repository (id, instance_id, license_id, discussions_enabled)\n", + " VALUES (?, ?, ?, ?)\n", + "tables ['page_repository']\n", + "columns []\n", + "\n", + "UPDATE page_repository\n", + " SET forum_id = ?\n", + " WHERE id = ?\n", + "tables ['page_repository']\n", + "columns ['forum_id', 'id']\n", + "\n", + "SELECT page_repository.id\n", + " FROM instance, page_repository\n", + " JOIN page_revision ON page_repository.id = page_revision.page_repository_id\n", + " WHERE page_repository.instance_id = instance.id\n", + " AND (? is null or instance.subdomain = ?)\n", + " AND page_repository.current_revision_id = page_revision.id\n", + " ORDER BY page_repository.id\n", + "tables ['instance', 'page_repository', 'page_revision']\n", + "columns ['id', 'id', 'page_repository_id', 'id', 'current_revision_id', 'id', 'instance_id', 'id', 'subdomain']\n", + "\n", + "SELECT t.name, u.trashed, i.subdomain, e.date, e.current_revision_id, e.license_id, f1.value as title, f2.value as fallback_title\n", + " FROM entity e\n", + " JOIN uuid u ON u.id = e.id\n", + " JOIN instance i ON i.id = e.instance_id\n", + " JOIN type t ON t.id = e.type_id\n", + " LEFT JOIN entity_revision_field f1 ON f1.entity_revision_id = e.current_revision_id AND f1.field = 'title'\n", + " LEFT JOIN entity_revision_field f2 on f2.entity_revision_id = (SELECT id FROM entity_revision WHERE repository_id = ? LIMIT 1) AND f2.field = 'title'\n", + " WHERE e.id = ?\n", + "tables ['entity', 'uuid', 'instance', 'type', 'entity_revision_field', 'entity_revision_field', 'entity_revision']\n", + "columns ['name', 'trashed', 'subdomain', 'date', 'current_revision_id', 'license_id', 'value', 'value', 'id', 'id', 'id', 'instance_id', 'id', 'type_id', 'id', 'entity_revision_id', 'current_revision_id', 'field', 'entity_revision_id', 'field', 'id', 'repository_id']\n", + "\n", + "SELECT id FROM entity_revision WHERE repository_id = ?\n", + "tables ['entity_revision']\n", + "columns ['id', 'repository_id']\n", + "\n", + "SELECT term_taxonomy_id as id FROM term_taxonomy_entity WHERE entity_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['term_taxonomy_id', 'entity_id']\n", + "\n", + "SELECT term_taxonomy_id as id\n", + " FROM (\n", + " SELECT term_taxonomy_id, entity_id FROM term_taxonomy_entity\n", + " UNION ALL\n", + " SELECT t.term_taxonomy_id, l.child_id as entity_id\n", + " FROM term_taxonomy_entity t\n", + " JOIN entity_link l ON t.entity_id = l.parent_id\n", + " UNION ALL\n", + " SELECT t.term_taxonomy_id, l2.child_id as entity_id\n", + " FROM term_taxonomy_entity t\n", + " JOIN entity_link l1 ON t.entity_id = l1.parent_id\n", + " JOIN entity_link l2 ON l2.parent_id = l1.child_id\n", + " ) u\n", + " WHERE entity_id = ?\n", + "tables ['term_taxonomy_entity', 'term_taxonomy_entity', 'entity_link', 'term_taxonomy_entity', 'entity_link', 'entity_link']\n", + "columns ['term_taxonomy_id', 'entity_id', 'term_taxonomy_id', 'entity_id', 'term_taxonomy_id', 'term_taxonomy_id', 'child_id', 'child_id', 'entity_id', 'parent_id', 'entity_id', 'parent_id', 'parent_id', 'child_id']\n", + "\n", + "SELECT l.parent_id as id\n", + " FROM entity_link l\n", + " WHERE l.child_id = ?\n", + "tables ['entity_link']\n", + "columns ['parent_id', 'child_id']\n", + "\n", + "SELECT c.id, uuid.trashed\n", + " FROM entity_link l\n", + " JOIN entity c on c.id = l.child_id\n", + " JOIN uuid on uuid.id = c.id\n", + " JOIN type t ON t.id = c.type_id\n", + " WHERE l.parent_id = ? AND t.name = ?\n", + " ORDER BY l.order ASC\n", + "tables ['entity_link', 'entity', 'uuid', 'type']\n", + "columns ['id', 'trashed', 'id', 'child_id', 'id', 'id', 'id', 'type_id', 'order', 'parent_id', 'name']\n", + "\n", + "SELECT er.id\n", + " FROM entity_revision er\n", + " JOIN uuid ON er.id = uuid.id\n", + " WHERE repository_id = ?\n", + " AND trashed = 0\n", + " ORDER BY date DESC\n", + " LIMIT 1\n", + "tables ['entity_revision', 'uuid']\n", + "columns ['id', 'id', 'id', 'date', 'repository_id', 'trashed']\n", + "\n", + "SELECT instance_id\n", + " FROM entity\n", + " WHERE id = ?\n", + "tables ['entity']\n", + "columns ['instance_id', 'id']\n", + "\n", + "SELECT id FROM entity WHERE id = ?\n", + "tables ['entity']\n", + "columns ['id', 'id']\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "SELECT id FROM type WHERE name = ?\n", + "tables ['type']\n", + "columns ['id', 'name']\n", + "\n", + "INSERT INTO entity (id, type_id, instance_id, license_id, date)\n", + " VALUES (?, ?, ?, ?, ?)\n", + "tables ['entity']\n", + "columns []\n", + "\n", + "SELECT IFNULL(MAX(et.order), 0) AS current_last\n", + " FROM entity_link et\n", + " WHERE et.parent_id = ?\n", + "tables ['entity_link']\n", + "columns ['parent_id', 'order']\n", + "\n", + "INSERT INTO entity_link (parent_id, child_id, type_id, order)\n", + " VALUES (?, ?, 9, ?)\n", + "tables ['entity_link']\n", + "columns []\n", + "\n", + "SELECT IFNULL(MAX(position), 0) AS current_last\n", + " FROM term_taxonomy_entity\n", + " WHERE term_taxonomy_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['term_taxonomy_id', 'position']\n", + "\n", + "INSERT INTO term_taxonomy_entity (entity_id, term_taxonomy_id, position)\n", + " VALUES (?, ?, ?)\n", + "tables ['term_taxonomy_entity']\n", + "columns []\n", + "\n", + "UPDATE entity\n", + " SET current_revision_id = ?\n", + " WHERE id = ?\n", + "tables ['entity']\n", + "columns ['current_revision_id', 'id']\n", + "\n", + "SELECT uuid_id, MAX(event_log.date) AS date\n", + " FROM event_log, uuid, instance, entity\n", + " WHERE uuid.id = event_log.uuid_id\n", + " AND event_log.date < ?\n", + " AND (? is null OR instance.subdomain = ?)\n", + " AND instance.id = entity.instance_id\n", + " AND entity.id = event_log.uuid_id\n", + " AND event_log.event_id = 10\n", + " AND uuid.trashed = 1\n", + " AND uuid.discriminator = 'entity'\n", + " AND entity.type_id NOT IN (35, 39, 40, 41, 42, 43, 44)\n", + " GROUP BY uuid_id\n", + " ORDER BY date DESC\n", + " LIMIT ?\n", + "tables ['event_log', 'uuid', 'instance', 'entity']\n", + "columns ['uuid_id', 'uuid_id', 'date', 'date', 'discriminator', 'type_id', 'trashed', 'event_id', 'id', 'uuid_id', 'id', 'instance_id', 'id', 'uuid_id', 'date', 'subdomain']\n", + "\n", + "UPDATE entity_link\n", + " SET entity_link.order = ?\n", + " WHERE parent_id = ? AND child_id = ?\n", + "tables ['entity_link']\n", + "columns ['order', 'parent_id', 'child_id']\n", + "\n", + "select * from user where id = ?\n", + "tables ['user']\n", + "columns ['id']\n", + "\n", + "select * from entity where id = ?\n", + "tables ['entity']\n", + "columns ['id']\n", + "\n", + "update entity set license_id = ? where id = ?\n", + "tables ['entity']\n", + "columns ['license_id', 'id']\n", + "\n", + "INSERT INTO uuid (trashed, discriminator)\n", + " VALUES (0, 'entityRevision')\n", + "tables ['uuid']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO entity_revision (id, author_id, repository_id, date)\n", + " VALUES (?, ?, ?, ?)\n", + "tables ['entity_revision']\n", + "columns []\n", + "\n", + "INSERT INTO entity_revision_field (field, value, entity_revision_id)\n", + " VALUES (?, ?, ?)\n", + "tables ['entity_revision_field']\n", + "columns []\n", + "\n", + "SELECT t.name, u.trashed, r.date, r.author_id, r.repository_id\n", + " FROM entity_revision r\n", + " JOIN uuid u ON u.id = r.id\n", + " JOIN entity e ON e.id = r.repository_id\n", + " JOIN type t ON t.id = e.type_id\n", + " WHERE r.id = ?\n", + "tables ['entity_revision', 'uuid', 'entity', 'type']\n", + "columns ['name', 'trashed', 'date', 'author_id', 'repository_id', 'id', 'id', 'id', 'repository_id', 'id', 'type_id', 'id']\n", + "\n", + "SELECT field, value\n", + " FROM entity_revision_field\n", + " WHERE entity_revision_id = ?\n", + "tables ['entity_revision_field']\n", + "columns ['field', 'value', 'entity_revision_id']\n", + "\n", + "SELECT repository_id FROM entity_revision WHERE id = ?\n", + "tables ['entity_revision']\n", + "columns ['repository_id', 'id']\n", + "\n", + "SELECT entity_id\n", + " FROM term_taxonomy_entity\n", + " WHERE term_taxonomy_id = ?\n", + " ORDER BY position ASC\n", + "tables ['term_taxonomy_entity']\n", + "columns ['entity_id', 'term_taxonomy_id', 'position']\n", + "\n", + "SELECT id\n", + " FROM term_taxonomy\n", + " WHERE parent_id = ?\n", + " ORDER BY weight ASC\n", + "tables ['term_taxonomy']\n", + "columns ['id', 'parent_id', 'weight']\n", + "\n", + "SELECT t.name as name, t1.id as id\n", + " FROM term_taxonomy t0\n", + " JOIN term_taxonomy t1 ON t1.parent_id = t0.id\n", + " LEFT JOIN term_taxonomy t2 ON t2.parent_id = t1.id\n", + " LEFT JOIN term_taxonomy t3 ON t3.parent_id = t2.id\n", + " LEFT JOIN term_taxonomy t4 ON t4.parent_id = t3.id\n", + " LEFT JOIN term_taxonomy t5 ON t5.parent_id = t4.id\n", + " LEFT JOIN term_taxonomy t6 ON t6.parent_id = t5.id\n", + " LEFT JOIN term_taxonomy t7 ON t7.parent_id = t6.id\n", + " LEFT JOIN term_taxonomy t8 ON t8.parent_id = t7.id\n", + " LEFT JOIN term_taxonomy t9 ON t9.parent_id = t8.id\n", + " LEFT JOIN term_taxonomy t10 ON t10.parent_id = t9.id\n", + " LEFT JOIN term_taxonomy t11 ON t11.parent_id = t10.id\n", + " LEFT JOIN term_taxonomy t12 ON t12.parent_id = t11.id\n", + " LEFT JOIN term_taxonomy t13 ON t13.parent_id = t12.id\n", + " LEFT JOIN term_taxonomy t14 ON t14.parent_id = t13.id\n", + " LEFT JOIN term_taxonomy t15 ON t15.parent_id = t14.id\n", + " LEFT JOIN term_taxonomy t16 ON t16.parent_id = t15.id\n", + " LEFT JOIN term_taxonomy t17 ON t17.parent_id = t16.id\n", + " LEFT JOIN term_taxonomy t18 ON t18.parent_id = t17.id\n", + " LEFT JOIN term_taxonomy t19 ON t19.parent_id = t18.id\n", + " LEFT JOIN term_taxonomy t20 ON t20.parent_id = t19.id\n", + " JOIN term t on t1.term_id = t.id\n", + " WHERE\n", + " (\n", + " t0.id = 146728 OR\n", + " t0.id = 106081 OR\n", + " (t0.parent_id IS NULL AND t2.id != 146728 AND t1.id != 106081)\n", + " ) AND (\n", + " t1.id = ? OR t2.id = ? OR t3.id = ? OR t4.id = ? OR t5.id = ? OR\n", + " t6.id = ? OR t7.id = ? OR t8.id = ? OR t9.id = ? OR t10.id = ? OR\n", + " t11.id = ? OR t12.id = ? OR t13.id = ? OR t14.id = ? OR t15.id = ?\n", + " OR t16.id = ? OR t17.id = ? OR t18.id = ? OR t19.id = ? OR t20.id = ?\n", + " )\n", + "tables ['term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term_taxonomy', 'term']\n", + "columns ['name', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'parent_id', 'id', 'term_id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'parent_id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id', 'id']\n", + "\n", + "SELECT term.instance_id\n", + " FROM term_taxonomy\n", + " JOIN term\n", + " ON term.id = term_taxonomy.term_id\n", + " WHERE term_taxonomy.id = ?\n", + "tables ['term_taxonomy', 'term']\n", + "columns ['instance_id', 'id', 'term_id', 'id']\n", + "\n", + "SELECT term_id AS id, instance_id\n", + " FROM term_taxonomy\n", + " JOIN term\n", + " ON term.id = term_taxonomy.term_id\n", + " WHERE term_taxonomy.id = ?\n", + "tables ['term_taxonomy', 'term']\n", + "columns ['instance_id', 'term_id', 'id', 'term_id', 'id']\n", + "\n", + "UPDATE term\n", + " SET name = ?\n", + " WHERE id = ?\n", + "tables ['term']\n", + "columns ['name', 'id']\n", + "\n", + "UPDATE term_taxonomy\n", + " SET description = ?\n", + " WHERE id = ?\n", + "tables ['term_taxonomy']\n", + "columns ['description', 'id']\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "SELECT type.id FROM type\n", + " JOIN taxonomy\n", + " ON taxonomy.type_id = type.id\n", + " WHERE type.name = ?\n", + " AND instance_id = ?\n", + "tables ['type', 'taxonomy']\n", + "columns ['id', 'type_id', 'id', 'name', 'instance_id']\n", + "\n", + "INSERT INTO taxonomy (type_id, instance_id)\n", + " VALUES (?, ?)\n", + "tables ['taxonomy']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "INSERT INTO term (name, instance_id)\n", + " VALUES (?, ?)\n", + "tables ['term']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() as id\n", + "tables []\n", + "columns []\n", + "\n", + "SELECT IFNULL(MAX(tt.weight), 0) AS current_heaviest\n", + " FROM term_taxonomy tt\n", + " WHERE tt.parent_id = ?\n", + "tables ['term_taxonomy']\n", + "columns ['parent_id', 'weight']\n", + "\n", + "INSERT INTO term_taxonomy (id, term_id, taxonomy_id, parent_id, description, weight)\n", + " VALUES (?, ?, ?, ?, ?, ?)\n", + "tables ['term_taxonomy']\n", + "columns []\n", + "\n", + "select taxonomy.instance_id, type.name as term_type\n", + " from term_taxonomy\n", + " join taxonomy on term_taxonomy.taxonomy_id = taxonomy.id\n", + " join type on type.id = taxonomy.type_id\n", + " where term_taxonomy.id = ?\n", + "tables ['term_taxonomy', 'taxonomy', 'type']\n", + "columns ['instance_id', 'name', 'taxonomy_id', 'id', 'id', 'type_id', 'id']\n", + "\n", + "SELECT id FROM term_taxonomy_entity\n", + " WHERE entity_id = ?\n", + " AND term_taxonomy_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['id', 'entity_id', 'term_taxonomy_id']\n", + "\n", + "SELECT instance_id\n", + " FROM entity\n", + " WHERE id = ?\n", + "tables ['entity']\n", + "columns ['instance_id', 'id']\n", + "\n", + "SELECT IFNULL(MAX(position), 0) AS current_last\n", + " FROM term_taxonomy_entity\n", + " WHERE term_taxonomy_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['term_taxonomy_id', 'position']\n", + "\n", + "INSERT INTO term_taxonomy_entity (entity_id, term_taxonomy_id, position)\n", + " VALUES (?, ?, ?)\n", + "tables ['term_taxonomy_entity']\n", + "columns []\n", + "\n", + "SELECT id FROM term_taxonomy_entity\n", + " WHERE entity_id = ?\n", + " AND term_taxonomy_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['id', 'entity_id', 'term_taxonomy_id']\n", + "\n", + "SELECT count(*) AS quantity FROM term_taxonomy_entity\n", + " WHERE entity_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['entity_id']\n", + "\n", + "DELETE FROM term_taxonomy_entity WHERE id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['id']\n", + "\n", + "UPDATE term_taxonomy_entity\n", + " SET position = ?\n", + " WHERE term_taxonomy_id = ?\n", + " AND entity_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['position', 'term_taxonomy_id', 'entity_id']\n", + "\n", + "UPDATE term_taxonomy\n", + " SET weight = ?\n", + " WHERE parent_id = ?\n", + " AND id = ?\n", + "tables ['term_taxonomy']\n", + "columns ['weight', 'parent_id', 'id']\n", + "\n", + "SELECT tt.id, instance_id\n", + " FROM term_taxonomy tt\n", + " JOIN taxonomy t\n", + " ON t.id = tt.taxonomy_id\n", + " WHERE instance_id = (\n", + " SELECT instance_id\n", + " FROM term_taxonomy tt\n", + " JOIN taxonomy t\n", + " ON t.id = tt.taxonomy_id\n", + " WHERE tt.id = ?\n", + " )\n", + " AND t.type_id = 17\n", + "tables ['term_taxonomy', 'taxonomy', 'term_taxonomy', 'taxonomy']\n", + "columns ['id', 'instance_id', 'id', 'taxonomy_id', 'instance_id', 'type_id', 'instance_id', 'id', 'taxonomy_id', 'id']\n", + "\n", + "SELECT uuid_id, user_id, notify_mailman FROM subscription WHERE uuid_id = ?\n", + "tables ['subscription']\n", + "columns ['uuid_id', 'user_id', 'notify_mailman', 'uuid_id']\n", + "\n", + "INSERT INTO subscription (uuid_id, user_id, notify_mailman, date)\n", + " VALUES (?, ?, ?, ?)\n", + " ON DUPLICATE KEY UPDATE notify_mailman = ?\n", + "tables ['subscription']\n", + "columns ['notify_mailman']\n", + "\n", + "DELETE FROM subscription WHERE uuid_id = ? AND user_id = ?\n", + "tables ['subscription']\n", + "columns ['uuid_id', 'user_id']\n", + "\n", + "INSERT INTO subscription (uuid_id, user_id, notify_mailman)\n", + " VALUES (?, ?, 1)\n", + "tables ['subscription']\n", + "columns []\n", + "\n", + "SELECT id\n", + " FROM user\n", + " WHERE username = ?\n", + "tables ['user']\n", + "columns ['id', 'username']\n", + "\n", + "SELECT a.uuid_id FROM url_alias a\n", + " JOIN instance i on i.id = a.instance_id\n", + " WHERE i.subdomain = ? AND a.alias = ?\n", + " ORDER BY a.timestamp DESC\n", + "tables ['url_alias', 'instance']\n", + "columns ['uuid_id', 'id', 'instance_id', 'timestamp', 'subdomain', 'alias']\n", + "\n", + "SELECT u.id\n", + " FROM user u\n", + " JOIN event_log e ON u.id = e.actor_id\n", + " WHERE e.event_id = 5 AND e.date > DATE_SUB(?, Interval 90 day)\n", + " GROUP BY u.id\n", + " HAVING count(e.event_id) > 10\n", + "tables ['user', 'event_log']\n", + "columns ['id', 'id', 'id', 'actor_id', 'event_id', 'date', 'event_id']\n", + "\n", + "SELECT u.id\n", + " FROM event_log e1\n", + " JOIN event_log e2 ON e1.uuid_id = e2.uuid_id AND (e1.event_id = 6 OR e1.event_id = 11) AND e2.event_id = 5 AND e1.date >= e2.date AND e1.actor_id != e2.actor_id\n", + " JOIN user u ON u.id = e1.actor_id\n", + " WHERE e1.date > DATE_SUB(?, Interval 90 day)\n", + " GROUP BY u.id\n", + " HAVING count(e1.event_id) > 10\n", + "tables ['event_log', 'event_log', 'user']\n", + "columns ['id', 'id', 'id', 'actor_id', 'date', 'actor_id', 'actor_id', 'event_id', 'date', 'date', 'event_id', 'uuid_id', 'uuid_id', 'event_id', 'event_id']\n", + "\n", + "SELECT id\n", + " FROM user\n", + " WHERE username = ?\n", + "tables ['user']\n", + "columns ['id', 'username']\n", + "\n", + "SELECT role_id\n", + " FROM role_user\n", + " WHERE user_id = ? AND role_id = ?\n", + "tables ['role_user']\n", + "columns ['role_id', 'user_id', 'role_id']\n", + "\n", + "INSERT INTO role_user (user_id, role_id)\n", + " VALUES (?, ?)\n", + "tables ['role_user']\n", + "columns []\n", + "\n", + "INSERT INTO uuid (discriminator)\n", + " VALUES ('user')\n", + "tables ['uuid']\n", + "columns []\n", + "\n", + "SELECT LAST_INSERT_ID() AS id\n", + " FROM uuid\n", + "tables ['uuid']\n", + "columns []\n", + "\n", + "INSERT INTO user (id, email, username, password, date, token)\n", + " VALUES (?, ?, ?, ?, ?, ?)\n", + "tables ['user']\n", + "columns []\n", + "\n", + "INSERT INTO role_user (user_id, role_id)\n", + " VALUES (?, ?)\n", + "tables ['role_user']\n", + "columns []\n", + "\n", + "DELETE FROM uuid WHERE id = ? AND discriminator = 'user'\n", + "tables ['uuid']\n", + "columns ['id', 'discriminator']\n", + "\n", + "select * from user where id = ?\n", + "tables ['user']\n", + "columns ['id']\n", + "\n", + "update ad set author_id = ? where author_id = ?\n", + "tables ['ad']\n", + "columns ['author_id', 'author_id']\n", + "\n", + "update blog_post set author_id = ? where author_id = ?\n", + "tables ['blog_post']\n", + "columns ['author_id', 'author_id']\n", + "\n", + "update comment set author_id = ? where author_id = ?\n", + "tables ['comment']\n", + "columns ['author_id', 'author_id']\n", + "\n", + "delete from comment_vote where user_id = ?\n", + "tables ['comment_vote']\n", + "columns ['user_id']\n", + "\n", + "update entity_revision set author_id = ? where author_id = ?\n", + "tables ['entity_revision']\n", + "columns ['author_id', 'author_id']\n", + "\n", + "update event_log set actor_id = ? where actor_id = ?\n", + "tables ['event_log']\n", + "columns ['actor_id', 'actor_id']\n", + "\n", + "delete from flag where reporter_id = ?\n", + "tables ['flag']\n", + "columns ['reporter_id']\n", + "\n", + "delete from notification where user_id = ?\n", + "tables ['notification']\n", + "columns ['user_id']\n", + "\n", + "update page_revision set author_id = ? where author_id = ?\n", + "tables ['page_revision']\n", + "columns ['author_id', 'author_id']\n", + "\n", + "delete from role_user where user_id = ?\n", + "tables ['role_user']\n", + "columns ['user_id']\n", + "\n", + "delete from subscription where user_id = ?\n", + "tables ['subscription']\n", + "columns ['user_id']\n", + "\n", + "delete from subscription where uuid_id = ?\n", + "tables ['subscription']\n", + "columns ['uuid_id']\n", + "\n", + "delete from uuid where id = ? and discriminator = 'user'\n", + "tables ['uuid']\n", + "columns ['id', 'discriminator']\n", + "\n", + "SELECT id\n", + " FROM user\n", + " WHERE username = ?\n", + "tables ['user']\n", + "columns ['id', 'username']\n", + "\n", + "SELECT user_id\n", + " FROM role_user\n", + " WHERE role_id = ?\n", + " AND (? IS NULL OR user_id > ?)\n", + " ORDER BY user_id\n", + " LIMIT ?\n", + "tables ['role_user']\n", + "columns ['user_id', 'user_id', 'role_id', 'user_id']\n", + "\n", + "SELECT id\n", + " FROM role\n", + " WHERE name = ?\n", + "tables ['role']\n", + "columns ['id', 'name']\n", + "\n", + "INSERT INTO notification (user_id, date, email)\n", + " VALUES (?, ?, ?)\n", + "tables ['notification']\n", + "columns []\n", + "\n", + "INSERT INTO notification_event (notification_id, event_log_id)\n", + " SELECT LAST_INSERT_ID(), ?\n", + "tables ['notification_event']\n", + "columns []\n", + "\n", + "UPDATE notification\n", + " SET seen = ?\n", + " WHERE seen != ? AND id = ?\n", + "tables ['notification']\n", + "columns ['seen', 'seen', 'id']\n", + "\n", + "SELECT seen FROM notification WHERE id = ?\n", + "tables ['notification']\n", + "columns ['seen', 'id']\n", + "\n", + "SELECT seen FROM notification WHERE id = ?\n", + "tables ['notification']\n", + "columns ['seen', 'id']\n", + "\n", + "SELECT id, seen\n", + " FROM notification\n", + " WHERE id = ?\n", + "tables ['notification']\n", + "columns ['id', 'seen', 'id']\n", + "\n", + "SELECT * FROM notification_event WHERE event_log_id = ?\n", + "tables ['notification_event']\n", + "columns ['event_log_id']\n", + "\n", + "INSERT INTO subscription (uuid_id, user_id, notify_mailman)\n", + " VALUES (?, ?, 1)\n", + "tables ['subscription']\n", + "columns []\n", + "\n", + "DELETE FROM notification_event WHERE event_log_id = ?\n", + "tables ['notification_event']\n", + "columns ['event_log_id']\n", + "\n", + "SELECT tt.id, t.name, tt.parent_id, u.trashed, type.name AS typename\n", + " FROM term t\n", + " JOIN term_taxonomy tt ON t.id = tt.term_id\n", + " JOIN taxonomy ON tt.taxonomy_id = taxonomy.id\n", + " JOIN type ON taxonomy.type_id = type.id\n", + " JOIN uuid u on tt.id = u.id\n", + " JOIN instance i ON t.instance_id = i.id\n", + " WHERE i.subdomain = ?\n", + "tables ['term', 'term_taxonomy', 'taxonomy', 'type', 'uuid', 'instance']\n", + "columns ['id', 'name', 'parent_id', 'trashed', 'name', 'id', 'term_id', 'taxonomy_id', 'id', 'type_id', 'id', 'id', 'id', 'instance_id', 'id', 'subdomain']\n", + "\n", + "INSERT INTO user (id, username, email, password, token)\n", + " VALUES (?, ?, ?, ?, ?)\n", + "tables ['user']\n", + "columns []\n", + "\n", + "SELECT COUNT(*) AS count\n", + " FROM term_taxonomy_entity\n", + " WHERE entity_id = ?\n", + " AND term_taxonomy_id = ?\n", + "tables ['term_taxonomy_entity']\n", + "columns ['entity_id', 'term_taxonomy_id']\n", + "\n", + "SELECT username, user.email, user.id\n", + " FROM notification\n", + " JOIN user ON user.id = notification.user_id\n", + " WHERE notification.email = 1 AND notification.email_sent = 0 AND notification.seen = 0\n", + " GROUP BY notification.user_id;\n", + "tables ['notification', 'user']\n", + "columns ['username', 'email', 'id', 'user_id', 'id', 'user_id', 'seen', 'email', 'email_sent']\n", + "\n", + "UPDATE notification\n", + " SET email_sent = true\n", + " WHERE id in (42);\n", + "tables ['notification']\n", + "columns ['email_sent', 'id']\n" + ] + }, + { + "data": { + "text/plain": [ + "{'file': '/tmp/database-layer/server/tests/user.rs',\n", + " 'is_test_file': True,\n", + " 'sql_operator': 'select',\n", + " 'sql_type': 'query',\n", + " 'sql_query': 'SELECT user_id\\n FROM role_user\\n WHERE user_id = ?\\n and role_id = ?'}" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sqlglot import parse, exp\n", + "\n", + "def add_data(file, sql_query):\n", + " is_test_file = \"test\" in file\n", + " sql_operator = [operator for operator in sql_operators if sql_query_matches(operator, sql_query)][0]\n", + "\n", + " print()\n", + " #print(file)\n", + " print(sql_query)\n", + " parsed_statements = parse(sql_query)\n", + "\n", + " assert len(parsed_statements) == 1\n", + "\n", + " parsed_query = parsed_statements[0]\n", + "\n", + " tables = [t.name for t in parsed_query.find_all(exp.Table)]\n", + " columns = [c.to_dot().name for c in parsed_query.find_all(exp.Column)]\n", + " print(\"tables\", tables)\n", + " print(\"columns\", columns)\n", + "\n", + " \n", + " \n", + " #for column in parse_one(sql_query).find_all(exp.Column):\n", + " \n", + " #print(column.to_dot())\n", + " \n", + " \n", + " return {\n", + " \"file\": file,\n", + " \"is_test_file\": is_test_file,\n", + " \"sql_operator\": sql_operator,\n", + " \"sql_type\": \"query\" if sql_operator == \"select\" else \"mutation\",\n", + " \"sql_query\": sql_query\n", + " }\n", + "\n", + "sql_statements = [add_data(entry[\"file\"], entry[\"sql_query\"]) for entry in raw_sql_statements]\n", + "sql_statements[0]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "598b34cc-e1cb-469a-812f-ff7db9ba27be", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Tables: set()\n", + "Columns: set()\n" + ] + } + ], + "source": [ + "from sqlalchemy import create_engine, MetaData, text\n", + "from sqlalchemy.sql import compiler\n", + "\n", + "# Replace with your database connection details\n", + "DATABASE_URI = 'mysql+mysqlconnector://root:secret@localhost:3306/serlo'\n", + "\n", + "engine = create_engine(DATABASE_URI)\n", + "metadata = MetaData()\n", + "metadata.reflect(bind=engine)\n", + "\n", + "# Your SQL statement\n", + "sql_statement = \"\"\"SELECT username, user.email, user.id\n", + " FROM notification\n", + " JOIN user ON user.id = notification.user_id\n", + " WHERE notification.email = 1 AND notification.email_sent = 0 AND notification.seen = 0\n", + " GROUP BY notification.user_id\"\"\"\n", + "\n", + "# Parse SQL statement\n", + "parsed_statement = text(sql_statement)\n", + "\n", + "# Compile the statement to the dialect specific query\n", + "compiled_statement = parsed_statement.compile(engine)\n", + "\n", + "# Introspect compiled statement for tables and columns\n", + "tables = set()\n", + "columns = set()\n", + "\n", + "print(type(parsed_statement))\n", + "\n", + "# Inspecting the compiled statement\n", + "if hasattr(compiled_statement, 'columns'):\n", + " for c in compiled_statement.columns:\n", + " columns.add(c.name)\n", + " if hasattr(c, 'table'):\n", + " tables.add(c.table.name)\n", + "\n", + "print(\"Tables:\", tables)\n", + "print(\"Columns:\", columns)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}