生のSQL

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

生のSQL

executesql

DALは、SQL文を明示的に発行することを可能にします。

The DAL allows you to explicitly issue SQL statements.

1.

2.

>>> print db.executesql('SELECT * FROM person;')

[(1, u'Massimo'), (2, u'Massimo')]

この場合、戻り値は、DALによって構文解析や変換されることはなく、その形式は特定のデータベース・ドライバに依存します。選 択において、このような利用は必要ありませんが、インデックスにおいてより一般的です。executesqlは2つのオプション引数をとりま す:placeholdersとas_dictです。placeholdersは、SQLにおいて置換されるオプション的な値の配列、もしくは、DBドラ イバによってサポートされいれば、SQLにおいて名前付きのプレースホルダーにマッチするキーを持つ辞書です。

In this case, the return values are not parsed or transformed by the DAL, and the format depends on the specific database driver. This usage with selects is normally not needed, but it is more common with indexes. executesql takes two optional arguments: placeholders andas_dict placeholders is an optional sequence of values to be substituted in or, if supported by the DB driver, a dictionary with keys matching named placeholders in your SQL.

as_dictがTrueに設定されていると、DBドライバによって返される結果のカーソルは、dbフィールド名をキーとして持つ辞書の配列に変換されます。as_dict = Trueとして返された結果は、通常の選択時に.as_list()を適用したときに返されるものと同様のものになります。

If as_dict is set to True, and the results cursor returned by the DB driver will be converted to a sequence of dictionaries keyed with the db field names. Results returned with as_dict = True are the same as those returned when applying .as_list() to a normal select.

1.

[{field1: value1, field2: value2}, {field1: value1b, field2: value2b}]

_lastsql

SQLがexecutesqlを用いて手動で実行されたものなのか、DALによって生成されたSQLなのかにしても、db._lastsqlにおいてSQLのコードを常に見ることができます。これは、デバッグに便利です:

Whether SQL was executed manually using executesql or was SQL generated by the DAL, you can always find the SQL code in db._lastsql. This is useful for debugging purposes:

1.

2.

3.

>>> rows = db().select(db.person.ALL)

>>> print db._lastsql

SELECT person.id, person.name FROM person;

web2pyは"*"演算子を利用したクエリを生成することはありません。web2pyでは常に、フィールドを選択するときは明示的です。

web2py never generates queries using the "*" operator. web2py is always explicit when selecting fields.