88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
from sqlalchemy import Table, Column, String, MetaData, ForeignKey, JSON
|
|
from sqlalchemy.sql import select, func
|
|
from sqlalchemy.types import Integer, BigInteger, String, Boolean, TIMESTAMP, Numeric, Text
|
|
from sqlalchemy.dialects.postgresql import JSONB
|
|
from biopro.db.base import Session
|
|
|
|
from .base import Base
|
|
|
|
# build a model class with a specific table name
|
|
def get_raw_model(channel):
|
|
tablename = str(channel) + '_recording_data_raws' # dynamic table name
|
|
class_name = 'RECORDING_DATA_RAWS' # dynamic class name
|
|
print('get_raw_model', tablename)
|
|
|
|
for mapper in Base.registry.mappers:
|
|
cls = mapper.class_
|
|
classname = cls.__name__
|
|
tblname = cls.__tablename__
|
|
print(cls, classname, tblname)
|
|
if (classname == class_name):
|
|
if tblname == tablename:
|
|
return cls
|
|
|
|
Model = type(class_name, (RECORDING_DATA_RAWS,), {
|
|
'__tablename__': tablename
|
|
})
|
|
return Model
|
|
|
|
|
|
class RECORDING_DATA_RAWS(Base):
|
|
__abstract__ = True
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String(255))
|
|
parent = Column(Integer)
|
|
size = Column(String(255))
|
|
path = Column(JSONB)
|
|
uuid = Column(String(255))
|
|
serial_number = Column(String(255))
|
|
data_format = Column(String(255))
|
|
channel = Column(Integer)
|
|
start_time = Column(String(255))
|
|
end_time = Column(String(255))
|
|
data = Column(Text)
|
|
compressed = Column(Boolean)
|
|
deleted = Column(Boolean)
|
|
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now())
|
|
updated_at = Column(TIMESTAMP(timezone=True), onupdate=func.now())
|
|
|
|
# @classmethod
|
|
# def create_subject_data(cls, subject_id, project, meta, data):
|
|
# with Session() as session:
|
|
# subject = Subject(
|
|
# subject_id = subject_id,
|
|
# project = project,
|
|
# meta= meta,
|
|
# data = data
|
|
# )
|
|
# session.add(subject)
|
|
# session.commit()
|
|
# return subject
|
|
|
|
# @classmethod
|
|
# def check_name_duplicate(cls, collection_name, parent, n):
|
|
# with Session() as session:
|
|
# result = session.query(Collection).filter(Collection.name == cls.generate_name(collection_name, n), Collection.parent == parent).first()
|
|
# if result is None:
|
|
# return cls.generate_name(collection_name, n)
|
|
# else:
|
|
# new_num = n + 1
|
|
# # new_name = f"{collection_name}({new_num})"
|
|
# return cls.check_name_duplicate(collection_name, parent, new_num)
|
|
|
|
# @classmethod
|
|
# def generate_name(cls, collection_name, n):
|
|
# if n==0:
|
|
# return collection_name
|
|
# else:
|
|
# return f"{collection_name}({n})"
|
|
|
|
# @classmethod
|
|
# def find_data(cls, id):
|
|
# with Session() as session:
|
|
|
|
# result = session.query(RECORDING_DATA_RAWS).first()
|
|
# return result
|
|
|
|
|