-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
71 lines (51 loc) · 2.07 KB
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from sqlalchemy import Table, MetaData, Column, Index, Integer, String, Boolean, DateTime, Float
from sqlalchemy.orm import mapper
from sqlalchemy.orm.attributes import *
from sqlalchemy import create_engine, desc
from sqlalchemy.orm import sessionmaker
from enum import Enum
class MerchantNumber:
def __init__(self, number=None):
self.id = None
self.number = number
def __repr__(self):
return "<MerchantNumber(number='%s')>" % (self.number,)
class MerchantHistory:
def __init__(self, id=None):
self.id = id
self.merchant_id = 0
self.history = ''
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData()
merchant_number_table = Table('MerchantNumber', metadata,
Column('Id', Integer, primary_key=True, nullable=False),
Column('Number', String(32), nullable=False),
Index('MerchantNumber_Idx', 'Number', unique=True),
sqlite_autoincrement=True)
mapper(MerchantNumber, merchant_number_table, properties={
'id': merchant_number_table.columns.Id,
'number': merchant_number_table.columns.Number
})
merchant_history_table = Table('MerchantHistory', metadata,
Column('Id', Integer, primary_key=True, nullable=False),
Column('MerchantId', Integer, nullable=True),
Column('History', String),
Index('MerchantId_Idx', 'MerchantId', unique=False),
sqlite_autoincrement=True)
metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
mn = MerchantNumber(number='123456799765')
session.add(mn)
session.flush()
session.expunge_all()
mn.number='31432432'
session.add(mn)
session.flush()
mh = MerchantHistory(id=mn.id)
mh.history = 'dsadasdasdas'
session.commit()
query = session.query(MerchantNumber).filter(MerchantNumber.number.like('31223%')).order_by(MerchantNumber.number.desc())
r = query.all()
print(r)
session.close()