その他の演算子

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

その他の演算子

web2pyには、同等なSQL演算子にアクセスするためのAPIを提供する演算子があります。"log"という別のテーブルを定義してみます。そのテーブルでは、セキュリティ・イベントとそのevent_timeと重大度(severity)を保存するようにします。ここで重大度(severity)は整数です。

web2py has other operators that provide an API to access equivalent SQL operators. Let's define another table "log" to store security events, their event_time and severity, where the severity is an integer number.

1.

2.

3.

>>> db.define_table('log', Field('event'),

Field('event_time', 'datetime'),

Field('severity', 'integer'))

前回と同様、イベントとして、"port scan"と "xss injection"と"unauthorized login"を数個挿入します。例として、同じevent_timeを持つが重大度(それぞれ1,2,3)は異なるイベントをログとして記録します。

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 event_time but with different severities (1, 2, 3 respectively).

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

>>> import datetime

>>> now = datetime.datetime.now()

>>> print db.log.insert(

event='port scan', event_time=now, severity=1)

1

>>> print db.log.insert(

event='xss injection', event_time=now, severity=2)

2

>>> print db.log.insert(

event='unauthorized login', event_time=now, severity=3)

3

like, startswith, contains, upper, lower

フィールドは、文字列を照合するためのlike演算子を持っています:

Fields have a like operator that you can use to match strings:

1.

2.

3.

>>> for row in db(db.log.event.like('port%')).select():

print row.event

port scan

ここで、"port%"は"port"から始まる文字列を示しています。パーセント記号文字"%"は、"任意の文字列"を意味するワイルドカード文字です。

Here "port%" indicates a string starting with "port". The percent sign character, "%", is a wild-card character that means "any sequence of characters".

web2pyはまた、いくつかのショートカットを提供しています:

web2py also provides some shortcuts:

1.

2.

db.mytable.myfield.startswith('value')

db.mytable.myfield.contains('value')

これは、それぞれに以下に相当します

which are equivalent respectively to

1.

2.

db.mytable.myfield.like('value%')

db.mytable.myfield.like('%value%')

containsは、前節で説明したように、list:<type>フィールドに対して特別な意味を持つことに注意してください。

Notice that contains has a special meaning for list:<type> fields and it was discussed in a previous section.

同様に、フィールドの値を大文字、または、小文字に変換するためにupperとlowerメソッドを使用することができます。さらに、like演算子と組み合わせることができます。

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.

1.

2.

3.

>>> for row in db(db.log.event.upper().like('PORT%')).select():

print row.event

port scan

year, month, day, hour, minutes, seconds

dateとdatetimeフィールドはday、month、yearメソッドを持ちます。datetimeおよびtimeフィールドは、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:

1.

2.

3.

4.

5.

>>> for row in db(db.log.event_time.year()==2009).select():

print row.event

port scan

xss injection

unauthorized login

belongs

SQLのIN演算子はbelongsメソッドを介して実現されます。このメソッドは、フィールドの値が指定したセット(タプルのリスト)に所属しているときtrueを返します。

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):

1.

2.

3.

4.

>>> for row in db(db.log.severity.belongs((1, 2))).select():

print row.event

port scan

xss injection

DALはまた、belongs演算子の引数においてネストした選択をできるようにしています。唯一の注意点は、ネストした選択はselectではなく_selectでなければならず、セットを定義する唯一つのフィールドが明示的に選択されなければなりません。

The DAL also allows a nested select as the argument of the belongs operator. The only caveat is that the nested select has to be a _select, not a select, and only one field has to be selected explicitly, the one that defines the set.

1.

2.

3.

4.

5.

6.

>>> bad_days = db(db.log.severity==3)._select(db.log.event_time)

>>> for row in db(db.log.event_time.belongs(bad_days)).select():

print row.event

port scan

xss injection

unauthorized login

前回は、カウント演算子をレコードのカウントに使用しました。同様に、サム(sum)演算子を、レコードのグループから特定のフィールドの値を足す(sum)ことに用いることができます。カウントの場合と同様に、サムの結果は格納オブジェクトから取り出すことができます:

Previously, you have used the count operator to count records. Similarly, you can use the sum operator to add (sum) the values of a specific field from a group of records. As in the case of count, the result of a sum is retrieved via the store object:

1.

2.

3.

>>> sum = db.log.severity.sum()

>>> print db().select(sum).first()[sum]

6