From bed619e3403ddbba2797c21a1c277c0bd63efac7 Mon Sep 17 00:00:00 2001
From: Mesharo <Hecko97@seznam.cz>
Date: Sat, 21 Sep 2024 09:33:27 +0200
Subject: [PATCH] filtering postgresql questions

---
 README.md | 10 ++++++-
 main.py   | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 main.py

diff --git a/README.md b/README.md
index f9aed02..3fa9297 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,10 @@
 # SQL-schemas
-Bachelor's thesis - Database schema extraction from a collection of questions on StackOverflow using SQL analysis.
+
+Extrakce schématu databáze z kolekce otázek na StackOverflow s využitím analýzy SQL
+
+StackOverflow je jedna z největších question and answer sítí. Je zde statisíce otázek s tématem SQL a velké množství SQL dotazů. Pro další analýzu SQL dotazů by byla vhodná znalost schématu databáze, kterých se dotazy týkají. Schéma bývá v textu uvedeno v různých formách. Cílem této práce je zautomatizovat čtení schémat pro co největší množství SQL.
+
+- Seznámení se s možnostmi parsrování SQL a extrakce schématu z SQL dotazu.
+- Implementace knihovny, která pro vstupní SQL dotaz vrátí seznam tabulek spolu se seznamem atributů a případně i s návrhy datových typů jednotlivých atributů.
+- Příprava trénovací kolekce pro umělou inteligenci, kde pro StackOverflow dotaz bude uveden očekávaný DDL skript. Kolekce bude mít alespoň sto prvků.
+- Implementace ověřovací aplikace, která pro DDL skript vykoná dotaz.
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..5933ae8
--- /dev/null
+++ b/main.py
@@ -0,0 +1,80 @@
+import re
+from bigxml import Parser, xml_handle_element
+import time
+
+# wanna find postgresql and postgresql-version tags
+def get_postgresql_tags(file_name: str) -> list:
+    result = []
+    file = open(file_name, 'r', encoding='utf8')
+
+    for row in file:
+        id_tagname = re.search('<row Id="(.+?)" TagName="(.+?)"', row)
+
+        if not id_tagname:
+            # Either meta data or <tags> element.
+            continue
+        
+        tagname = id_tagname.group(2)
+        
+        if tagname != 'postgresql':
+            postgresql_version = re.search('postgresql-(.+?)', tagname)
+
+            if not postgresql_version:
+                continue
+
+            try:
+                float(postgresql_version.group(1))
+            except ValueError:
+                # Not a version of postgresql.
+                continue
+
+        result.append(tagname)
+
+    file.close()
+    return result
+
+
+@xml_handle_element('posts', 'row')
+def handler(node):
+    id = node.attributes['Id']
+    body = node.attributes['Body']
+
+    try:
+        tags = node.attributes['Tags']
+    except KeyError:
+        tags = 'N/A'
+
+    try:
+        accepted_answer_id = node.attributes['AcceptedAnswerId']
+    except KeyError:
+        accepted_answer_id = 'N/A'
+    
+    yield {
+        'id': id,
+        'body': body,
+        'tags': tags,
+        'accepted_answer_id': accepted_answer_id
+    }
+
+
+# filter out wanted questions and save them in a separate file for quicker access
+def filter_postgresql_questions(file_name: str) -> None:
+    postgresql_tags = get_postgresql_tags('D:\stackoverflow.com\Tags.xml')
+    output_file = open('D:\postgresql_questions.txt', 'a', encoding='utf8')
+    count = 0
+
+    start = time.time()
+    with open(file_name, 'rb') as XML_file:
+        for item in Parser(XML_file).iter_from(handler):
+            for correct_tag in postgresql_tags:
+                if correct_tag in item['tags']:
+                    output_file.write(str(item) + '\n')
+            print(count)
+            count += 1
+    end = time.time()
+    print(f'Time elapsed: {(end - start) / 60}')
+
+    output_file.close()
+
+if __name__ == "__main__":
+    filter_postgresql_questions('D:\stackoverflow.com\Posts.xml')
\ No newline at end of file
-- 
GitLab