生SQLの生成

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

生SQLの生成

場合によっては、SQLは生成したいが実行したくないことがあります。web2pyでこれを行うのは簡単です。なぜならデータベースIOを実行するすべてのコマンドは、単純に、実行されようとしたSQLを実行せずに返す同等のコマンドを持つからです。これらのコマンドは、機能するものと同じ名前と構文を持ちますが、アンダースコアで始まります:

Sometimes you need to generate the SQL but not execute it. This is easy to do with web2py since every command that performs database IO has an equivalent command that does not, and simply returns the SQL that would have been executed. These commands have the same names and syntax as the functional ones, but they start with an underscore:

これは_insertです:

Here is _insert

1.

2.

>>> print db.person._insert(name='Alex')

INSERT INTO person(name) VALUES ('Alex');

これは_countです

Here is _count

1.

2.

>>> print db(db.person.name=='Alex')._count()

SELECT count(*) FROM person WHERE person.name='Alex';

これは_selectです

Here is _select

1.

2.

>>> print db(db.person.name=='Alex')._select()

SELECT person.id, person.name FROM person WHERE person.name='Alex';

これは_deleteです

Here is _delete

1.

2.

>>> print db(db.person.name=='Alex')._delete()

DELETE FROM person WHERE person.name='Alex';

最後に、これは_updateです

And finally, here is _update

1.

2.

>>> print db(db.person.name=='Alex')._update()

UPDATE person SET WHERE person.name='Alex';

さらに、db._lastsqlを用いて、最後のSQLコードを返すことができます。これは、executesqlを用いて手動で実行されたSQLでも、DALによって生成されたSQLでも可能です。

Moreover you can always use db._lastsql to return the most recent SQL code, whether it was executed manually using executesql or was SQL generated by the DAL.