diff --git a/backend/main.py b/backend/main.py index 1bc3a38..b56ff48 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,4 +1,6 @@ import psycopg2 +from bson import ObjectId +from psycopg2 import sql from pymongo import MongoClient @@ -7,14 +9,108 @@ def get_database(): return client['documents'] -if __name__ == "__main__": +def create_document(document): dbname = get_database() - print(dbname['docs'].find_one()) + docs_coll = dbname['docs'] + docs_coll.insert_one(document) + + +def get_document_by_id(id): + dbname = get_database() + docs_coll = dbname['docs'] + return docs_coll.find({"_id": id}) + + +def get_document_by_name(name): + dbname = get_database() + docs_coll = dbname['docs'] + return docs_coll.find({"name": name}) + + +def edit_data(id, data): + dbname = get_database() + docs_coll = dbname['docs'] + docs_coll.update_one({"_id": id}, {"$set": {"data": data}}) + + +def edit_access(id, access): + dbname = get_database() + docs_coll = dbname['docs'] + docs_coll.update_one({"_id": id}, {"$set": {"access": access}}) + + +def edit_name(id, name): + dbname = get_database() + docs_coll = dbname['docs'] + docs_coll.update_one({"_id": id}, {"$set": {"name": name}}) + + +def create_table(name): + cur = conn.cursor() + cur.execute(sql.SQL("CREATE TABLE {table} ()").format(table=sql.Identifier(name))) + + +def add_column(name, column, type): + cur = conn.cursor() + cur.execute(sql.SQL("ALTER TABLE {table} ADD {column}" + type).format( + table=sql.Identifier(name), + column=sql.Identifier(column)) + ) + + +def insert_column(name, column, data): + pass + + +def insert_columns(name, columns, data): + pass + + +def edit_column(name, column, data): + pass + + +def edit_columns(name, columns, data): + pass + + +def remove_column(name, column): + pass + + +def create_sort(name): + cur = conn.cursor() + cur.execute( + sql.SQL("CREATE TABLE {table} (property TEXT, _order CHAR(3), priority int)").format( + table=sql.Identifier(name + "_sort") + ) + ) + + +def add_sort(name, property, order, priority): + cur = conn.cursor() + cur.execute( + sql.SQL("INSERT INTO {table} VALUES (%s, %s, %s)").format(table=sql.Identifier(name + "_sort")), + (property, order, priority) + ) + + +def sort(): + pass + + +def add_filter(): + pass + + +conn = None + +if __name__ == "__main__": conn = psycopg2.connect( host="localhost", database="bd2", user="root", password="password") - cur = conn.cursor() - cur.execute('SELECT version()') - print(cur.fetchone()) + + conn.commit() +