Đang chuẩn bị liên kết để tải về tài liệu:
Web to py enterprise web framework - p 20
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
OTHER OPERATORS 175 6.10 Other Operators web2py has other operators that provide an API to access equivalent SQL operators. Let’s define another table "log" to store security events, their timestamp and severity, where the severity is an integer number. 1 2 3 db.define_table('log', Field('event'), Field('timestamp', 'datetime'), Field('severity', 'integer')) As before, insert a few events, a "port scan", an "xss injection" and an "unauthorized login". For the sake of the example, you can log events with the same timestamp but with different severities (1, 2, 3 respectively). 1 2 3 4 5 1 import datetime now = datetime.datetime.now() print db.log.insert(event='port scan', timestamp=now, severity=1) print db.log.insert(event='xss injection',. | OTHER OPERATORS 175 6.10 Other Operators 1 2 3 1 2 3 4 5 6 7 8 1 2 3 1 2 3 web2py has other operators that provide an API to access equivalent SQL operators. Let s define another table log to store security events their timestamp and severity where the severity is an integer number. db.define_table log Field event Field timestamp datetime Field severity integer As before insert a few events a port scan an xss injection and an unauthorized login . For the sake of the example you can log events with the same timestamp but with different severities 1 2 3 respectively . import datetime now datetime.datetime.now print db.log.insert event port scan timestamp now severity 1 1 print db.log.insert event xss injection timestamp now severity 2 2 print db.log.insert event unauthorized login timestamp now severity 3 3 like upper lower Fields have a like operator that you can use to match strings for row in db db.log.event.like port .select print row.event port scan Here port indicates a string starting with port . The percent sign character is a wild-card character that means any sequence of characters . Similarly you can use upper and lower methods to convert the value of the field to upper or lower case and you can also combine them with the like operator. for row in db db.log.event.upper .like PORT .select print row.event port scan year month day hour minutes seconds The date and datetime fields have day month and year methods. The datetime and time fields have hour minutes and seconds methods. Here is an example 176 THE DATABASE ABSTRACTION LAYER 1 2 3 4 5 1 2 3 4 1 2 3 4 5 6 1 2 3 for row in db db.log.timestamp.year 2009 .select print row.event port scan xss inj ection unauthorized login belongs The SQL IN operator is realized via the belongs method which returns true when the field value belongs to the specified set list of tuples for row in db db.log.severity.belongs 1 2 .select print row.event port scan xss injection The DAL also allows a nested select as the argument .