예제 #1
0
파일: model.py 프로젝트: arshul/DBMS
class Route(Base):
    __tablename__ = 'route'
    price = db.Column(db.Integer)
    cab_category = db.Column(db.String(20))
    source_id = db.Column(db.Integer, db.ForeignKey('location.id'), nullable=False)
    destination_id = db.Column(db.Integer, db.ForeignKey('location.id'), nullable=False)
    source = db.relationship("Location", foreign_keys=source_id)
    destination = db.relationship("Location", foreign_keys=destination_id)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __repr__(self):
        return "<{}>".format(self.price)
예제 #2
0
class Base(db.Model):
    __abstract__ = True

    id = db.Column('id', db.Integer, primary_key=True)
    created_at = db.Column('created_at', db.DateTime(timezone=True), default=db.func.current_timestamp())
    updated_at = db.Column('updated_at', db.DateTime(timezone=True),default=db.func.current_timestamp(),
                           onupdate=db.func.current_timestamp())

    def create(self):
        db.session.add(self)
        db.session.commit()
        db.session.flush()

    def update(self):
        db.session.commit()
        db.session.flush()

    @staticmethod
    def update_db():
        db.session.commit()
        db.session.flush()

    def bulk_insert(self, data):
        db.session.bulk_insert_mappings(self, data)
        db.session.commit()
        db.session.flush()

    def last_updated_id(self, class_name):
        return db.session.query(class_name).order_by(class_name.id.desc()).first()

    def save(self):
        db.session.add(self)
        db.session.commit()
        return self

    def delete(self):
        db.session.delete(self)
        db.session.commit()

    @classmethod
    def merge(cls, obj):
        db.session.merge(obj)
        db.session.commit()
예제 #3
0
class ItemModel(db.Model):
    __tablename__ = 'items'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    price = db.Column(db.Float(precision=2))

    store_id = db.Column(db.Integer, db.ForeignKey('stores.id'))
    store = db.relationship('StoreModel')

    def __init__(self, name, price, store_id):
        self.name = name
        self.price = price
        self.store_id = store_id

    def json(self):
        return {'name': self.name, 'price': self.price}

    @classmethod
    def find_by_name(cls, name):
        # connection = sqlite3.connect('data.db')
        # cursor = connection.cursor()

        # query = "SELECT * FROM items WHERE name=?"
        # result = cursor.execute(query, (name,))
        # row = result.fetchone()
        # connection.close()

        # if row:
        #     return cls(*row)     #this used for sqlite

        # let's use sqlalchemy
        return cls.query.filter_by(name=name).first(
        )  # said that SELECT * FROM items WHERE name=name limit=1

    def save_to_db(self):  # it will updating and opserting the data
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()
예제 #4
0
파일: model.py 프로젝트: arshul/DBMS
class Location(Base):
    __tablename__ = 'location'
    name = db.Column(db.String(100), nullable=False)
    src = db.relationship("Route", foreign_keys="[Route.source_id]", backref='location_source')
    dest = db.relationship("Route", foreign_keys="[Route.destination_id]", backref='location_destination')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __repr__(self):
        return "<{}>".format(self.title)
예제 #5
0
파일: user.py 프로젝트: Reema1998/codes_api
class UserModel(db.Model):
    __tablename__ = 'users'   # tells the sqlalchemy to table name
    
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80))
    password = db.Column(db.String(80))
    
    def __init__(self, username, password):
        self.username = username
        self.password = password
        
        
    def save_to_db(self):
        db.session.add(self)
        db.session.commit()
    
    @classmethod
    def find_by_username(cls, username):
        return cls.query.filter_by(username=username).first()
    
    
    @classmethod
    def find_by_id(cls, _id):
        return cls.query.filter_by(id= _id).first()