依存関係

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

依存関係

web2pyは、データベース抽象化層(DAL)を備えています。これは、Pythonオブジェクトを、クエリやテーブル、レコードなどのデーターベース・オブジェクトに対応付けするAPIです。DAL は、データベース・バックエンド固有の方言を用いてSQLをリアルタイムで動的に生成します。そのため、開発者はSQLコードを書く必要がなく、また、異 なるSQL方言を学ぶ必要もありません(SQLという言葉は総称的に用いています)。そして、アプリケーションは異なるタイプのデータベース間でポータブ ルになります。こ の文書の執筆時点で、サポートされているデータベースは、SQLite (Pythonに備わっています。したがってweb2pyにも備わっています)、PostgreSQL、MySQL、Oracle、MSSQL、 FireBird、DB2、Informix、Ingres、(部分的に) the Google App Engine (GAE)です。GAEは、第11章で具体的事例として扱います。

web2py comes with a Database Abstraction Layer (DAL), an API that maps Python objects into database objects such as queries, tables, and records. The DAL dynamically generates the SQL in real time using the specified dialect for the database back end, so that you do not have to write SQL code or learn different SQL dialects (the term SQL is used generically), and the application will be portable among different types of databases. At the time of this writing, the supported databases are SQLite (which comes with Python and thus web2py), PostgreSQL, MySQL, Oracle, MSSQL, FireBird, DB2, Informix, and Ingres and (partially) the Google App Engine (GAE). GAE is treated as a particular case in Chapter 11.

Windowsのバイナリ配布は、SQLiteとMySQLとともに、すぐに利用できます。 Macのバイナリ配布は、SQLiteとともにすぐに利用できます。他のデータベースバックエンドを使うには、ソース配布から実行し、バックエンドに必要な適切なドライバをインストールしてください。

The Windows binary distribution works out of the box with SQLite and MySQL. The Mac binary distribution works out of the box with SQLite. To use any other database back-end, run from the source distribution and install the appropriate driver for the required back end.

適切なドライバをインストールしたら、web2pyをソースから起動してください。web2pyはドライバを見つけることになります。以下はドライバのリストです:

Once the proper driver is installed, start web2py from source, and it will find the driver. Here is a list of drivers:

web2pyは、DALを構成する次のクラスを定義しています:

web2py defines the following classes that make up the DAL:

DALは、データベース接続を表します。例:

DAL represents a database connection. For example:

1.

db = DAL('sqlite://storage.db')

Tableはデータベースのテーブルを表します。Tableを直接インスタンスかすることはなく、代わりに、DAL.define_tableによってインスタンス化します。

Table represents a database table. You do not directly instantiate Table; instead,DAL.define_table instantiates it.

1.

db.define_table('mytable', Field('myfield'))

Tableの中で最も重要なメソッドは以下のものです:

The most important methods of a Table are:

.insert, .truncate, .drop, and .import_from_csv_file.

Fieldはデータベースのフィールドを表します。インスタンス化して、DAL.define_tableへ引数として渡すことができます。

Field represents a database field. It can be instantiated and passed as an argument toDAL.define_table.

DAL Rowsはデータベースの選択によって返されるオブジェクトです。Rowの行からなるリストとして考えることができます:

DAL Rows is the object returned by a database select. It can be thought of as a list of Rowrows:

1.

rows = db(db.mytable.myfield!=None).select()

Rowはフィールドの値を保持します。

Row contains field values.

1.

2.

for row in rows:

print row.myfield

QueryはSQLのwhere句を表現するオブジェクトです。

Query is an object that represents a SQL "where" clause:

1.

myquery = (db.mytable.myfield != None) | (db.mytable.myfield > 'A')

Setはレコードのセットを表します。最も重要なメソッドは、count、select、update、deleteです。例:

Set is an object that represents a set of records. Its most important methods are count,select, update, and delete. For example:

1.

2.

3.

4.

myset = db(myquery)

rows = myset.select()

myset.update(myfield='somevalue')

myset.delete()

Expressionはorderbyやgroupby式のようなものです。Fieldクラスは、Expressionから派生しています。下に例を示します。

Expression is something like an orderby or groupby expression. The Field class is derived from the Expression. Here is an example.

1.

2.

myorder = db.mytable.myfield.upper() | db.mytable.id

db().select(db.table.ALL, orderby=myorder)