bsition/backend/main.py

133 lines
3.0 KiB
Python

import psycopg2
from bson import ObjectId
from psycopg2 import sql
from pymongo import MongoClient
def get_database():
client = MongoClient("mongodb://root:password@localhost:27017")
return client['documents']
def create_document(document):
dbname = get_database()
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} (row_number SERIAL PRIMARY KEY)").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, data):
cur = conn.cursor()
str = "(" + "DEFAULT, %s," * (len(data) - 1) + "%s" + ")" # TODO: change.
# cur.execute(sql.SQL("INSERT INTO {table} VALUES %s").format(
cur.execute(sql.SQL("INSERT INTO {table} VALUES" + str).format(
table=sql.Identifier(name)),
data
)
def edit_columns(name, columns, data, id):
cur = conn.cursor()
i = 0
for column in columns:
cur.execute(sql.SQL("UPDATE {table} SET {col} = %s WHERE row_number = " + id).format(
table=sql.Identifier(name),
col=sql.Identifier(column)),
[data[i]]
)
i += 1
def remove_column(name, column):
cur = conn.cursor()
cur.execute(sql.SQL("ALTER TABLE {table} DROP COLUMN {column}").format(
table=sql.Identifier(name),
column=sql.Identifier(column))
)
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")
conn.commit()