Difference between revisions of "SQLdb Tutorial4/ja"

From Free Pascal wiki
Jump to navigationJump to search
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
{{Infobox databases/ja}}
 
{{Infobox databases/ja}}
== Introduction ==
+
== 概要 ==
This tutorial is an attempt to demonstrate the use of [[Data module|Lazarus Data Modules]] to isolate the data access components of a project from the program logic associated with the access.  Such isolation makes program maintenance and debugging easier.
+
このチュートリアルは、[[Data module|Lazarus Data Modules]] を使用して、プロジェクトのデータ アクセス コンポーネントを、アクセスに関連付けられたプログラム ロジックから分離する方法をデモンストレーションすることを試みる。 このような分離により、プログラムのメンテナンスとデバッグが容易となる。
  
The tutorial was developed using Windows 7 and SQLite3 as the database, with Lazarus 1.0.8 with FPC 2.6.2; however it should work with earlier versions. Similarly, other DBMS and Operating Systems should require minimal change, if any.
+
このチュートリアルは、Windows 7 SQLite3 をデータベースとして使用し、Lazarus 1.0.8 FPC 2.6.2 を使用して開発された。 ただし、以前のバージョンでも動作するはずである。 同様に、他の DBMS およびオペレーティング システムも、必要な場合は最小限の変更が必要である。
  
== Why use datamodules? ==
+
== なぜデータモジュールを用いるのか? ==
Simple - after following the Lazarus Tutorials:
+
以下のチュートリアルの後では簡単である:
* [[SQLdb Tutorial0]]
+
* [[SQLdb Tutorial0/ja]]
* [[SQLdb Tutorial1]]
+
* [[SQLdb Tutorial1/ja]]
* [[SQLdb Tutorial2]]
+
* [[SQLdb Tutorial2/ja]]
* [[SQLdb Tutorial3]]
+
* [[SQLdb Tutorial3/ja]]
developing a 'real' application becomes harder.  'Form1' grows to an exponential size handling the different Events and Database Queries. 
+
'本物' のアプリケーションを開発することはより困難である。 'Form1' は異なるイベントとデータベースクエリを扱うことで指数的に大きくなる。
  
Isolating each table access into a single datamodule makes debugging and maintenance so much easier.  An application may have any number of datamodules - a small application with just one or 2 tables can probably suffice with just one Datamodule - a larger application could probably benefit from having a data module for each table or view.
+
各テーブルへのアクセスを単一のデータモジュールに分離すると、デバッグとメンテナンスが非常に簡単になる。
 +
  アプリケーションは多くのデータモジュールを持つかもしれない - テーブルが 1 つまたは 2 つしかない小規模なアプリケーションは、おそらく 1 つのデータモジュールだけで十分だ - より大きなアプリケーションはそれぞれのテーブルもしくはビューに対してデータモジュールを持つことで恩恵を受けるかもしれない。
  
The sample shown here uses just 2 tables with simple queries, but it can be expanded to include more options with each table. 
+
ここに示すサンプルでは、単純なクエリを含む 2 つのテーブルのみを使用してるが、各テーブルにさらに多くのオプションを含めるように拡張できる。
  
== Getting started ==
+
== 始めてみる ==
In the Lazarus IDE, create a new Application and then click File --> New --> Data Module
+
Lazarus IDEで、新しいアプリケーションを作り、次いで、ファイル --> 新規 --> データモジュールをクリックする。
  
 
[[file:newDM.jpg]]
 
[[file:newDM.jpg]]
  
You will be presented with a window as if you selected 'New Form':  
+
'New Form'を選択した場合、新しいウインドウが開く:  
  
 
[[file:dm1.jpg]]
 
[[file:dm1.jpg]]
  
The difference is this window/form will only accept non-visual components.  In fact, look at your Component Palette: it has been greatly reduced to only allow selection of ONLY non-visual components.
+
その差は、このウインドウ/フォームが見ることのできないコンポーネントのみを配置できる点である。 実際には、コンポーネントパレットを見ると、見ることのできないコンポーネントのみが選択可能なようにその数が減っている。
  
 
+
== 皆 それぞれの仕方を持っている ==
== Everyone has their own way ==
+
データ モジュールの使用方法は、必要に応じて異なる。 ただし、例として、少なくとも 2 つのデータ モジュールがあるとする:
Your use of data modules will vary to suit your own needs. but as an example, I have at least 2 data modules:
 
  
 
Unit: DataModule1   
 
Unit: DataModule1   
On this module I 'drop' a ''T*Connection'' and a ''TSQLTransaction''.
+
このモジュールに ''T*Connection'' ''TSQLTransaction'' を配置する。
  
 
[[file:conn.jpg]]
 
[[file:conn.jpg]]
  
DataModule1 is used as the connection for all queries.
+
DataModule1 はすべてのクエリに対するコネクションとして用いられる。
  
I then create a DataModuleN for each table or view.
+
そこでそれぞれのテーブル、もしくはビューに対して DataModuleN を作る。
  
 
[[file:dm2.jpg]]
 
[[file:dm2.jpg]]
  
Each DataModuleN will need to have the DataModule1 unit (unit2 in this example) added to the USES clause to connect to the database.
+
それぞれの DataModuleN はデータベースに接続するためUSES節にDataModule1(この例ではunit2)と記述することが必要である。
  
From here everything is the same as stated in [[SQLdb Tutorial1]].  The components are connected in the same way and access is identical.
+
ここからは [[SQLdb Tutorial1/ja]] と同じである。コンポーネントは同様のやり方で、同一のアクセスで接続される。
  
==More uses of data modules==
+
==データモジュールのさらなる用途==
=== Additional components ===
+
=== Additional コンポーネント ===
In this example, DataModule1 had nothing more than a Connection and Transaction, but in a 'real' application, this container would typically also hold global non-visual components to be used by the application. 
+
この例では、DataModule1はConnectionとTransactionを持つだけであるが、「現実」のアプリケーションでは、このコンポーネントはアプリケーションによって用いられる、見ることのできないコンポーネントも持つのが普通である。
  
For example, Load and Save INI settings, ''TOpenDialog'', ''TSaveDialog'', etc.  The concept here is to isolate data access from the business logic of an application.  A change in data source for any application is never a minimal task, but having the datasources isolated will make the change much easier.
+
例えば、INI設定を開いて、保存する''TOpenDialog''''TSaveDialog''などである。ここで念頭に置いているのは、アプリケーションの固有の機能とデータアクセスを切り離すことである。アプリケーションにとってデータソースの変更は、決して小さな仕事ではないが、データソースと切り離すことは変更をより簡単にするだろう。
  
=== Debugging ===
+
=== デバッグ ===
Debugging a program is also a difficult task. By separating data access and business logic, the code to be viewed is halved. Data access and business logic can be tested separately to at least halve the problem.
+
また、プログラムをデバッグすることは困難なことである。データアクセスと、仕事のためのロジックを切り離すことで、少なくとも問題を半減するまで独立してテストすることができる。
  
The importance of the ''DataModule'' will become even more obvious when developing other applications using the same database and tables.  The data module can of course be reused in the new application.
+
''DataModule'' の重要性は同じデータベースとテーブルを用いた他のアプリケーションを開発するときにより明らかとなる。もちろんデータモジュールは新しいアプリケーションでも利用可能である。

Latest revision as of 06:23, 30 March 2024

English (en) français (fr) 日本語 (ja)

データベースのポータル

参照:

チュートリアル/練習となる記事:

各種データベース

Advantage - MySQL - MSSQL - Postgres - Interbase - Firebird - Oracle - ODBC - Paradox - SQLite - dBASE - MS Access - Zeos

概要

このチュートリアルは、Lazarus Data Modules を使用して、プロジェクトのデータ アクセス コンポーネントを、アクセスに関連付けられたプログラム ロジックから分離する方法をデモンストレーションすることを試みる。 このような分離により、プログラムのメンテナンスとデバッグが容易となる。

このチュートリアルは、Windows 7 と SQLite3 をデータベースとして使用し、Lazarus 1.0.8 と FPC 2.6.2 を使用して開発された。 ただし、以前のバージョンでも動作するはずである。 同様に、他の DBMS およびオペレーティング システムも、必要な場合は最小限の変更が必要である。

なぜデータモジュールを用いるのか?

以下のチュートリアルの後では簡単である:

'本物' のアプリケーションを開発することはより困難である。 'Form1' は異なるイベントとデータベースクエリを扱うことで指数的に大きくなる。

各テーブルへのアクセスを単一のデータモジュールに分離すると、デバッグとメンテナンスが非常に簡単になる。

 アプリケーションは多くのデータモジュールを持つかもしれない - テーブルが 1 つまたは 2 つしかない小規模なアプリケーションは、おそらく 1 つのデータモジュールだけで十分だ - より大きなアプリケーションはそれぞれのテーブルもしくはビューに対してデータモジュールを持つことで恩恵を受けるかもしれない。

ここに示すサンプルでは、単純なクエリを含む 2 つのテーブルのみを使用してるが、各テーブルにさらに多くのオプションを含めるように拡張できる。

始めてみる

Lazarus IDEで、新しいアプリケーションを作り、次いで、ファイル --> 新規 --> データモジュールをクリックする。

newDM.jpg

'New Form'を選択した場合、新しいウインドウが開く:

dm1.jpg

その差は、このウインドウ/フォームが見ることのできないコンポーネントのみを配置できる点である。 実際には、コンポーネントパレットを見ると、見ることのできないコンポーネントのみが選択可能なようにその数が減っている。

皆 それぞれの仕方を持っている

データ モジュールの使用方法は、必要に応じて異なる。 ただし、例として、少なくとも 2 つのデータ モジュールがあるとする:

Unit: DataModule1 このモジュールに T*ConnectionTSQLTransaction を配置する。

conn.jpg

DataModule1 はすべてのクエリに対するコネクションとして用いられる。

そこでそれぞれのテーブル、もしくはビューに対して DataModuleN を作る。

dm2.jpg

それぞれの DataModuleN はデータベースに接続するためUSES節にDataModule1(この例ではunit2)と記述することが必要である。

ここからは SQLdb Tutorial1/ja と同じである。コンポーネントは同様のやり方で、同一のアクセスで接続される。

データモジュールのさらなる用途

Additional コンポーネント

この例では、DataModule1はConnectionとTransactionを持つだけであるが、「現実」のアプリケーションでは、このコンポーネントはアプリケーションによって用いられる、見ることのできないコンポーネントも持つのが普通である。

例えば、INI設定を開いて、保存するTOpenDialogTSaveDialogなどである。ここで念頭に置いているのは、アプリケーションの固有の機能とデータアクセスを切り離すことである。アプリケーションにとってデータソースの変更は、決して小さな仕事ではないが、データソースと切り離すことは変更をより簡単にするだろう。

デバッグ

また、プログラムをデバッグすることは困難なことである。データアクセスと、仕事のためのロジックを切り離すことで、少なくとも問題を半減するまで独立してテストすることができる。

DataModule の重要性は同じデータベースとテーブルを用いた他のアプリケーションを開発するときにより明らかとなる。もちろんデータモジュールは新しいアプリケーションでも利用可能である。