おまけ
セットアップ上の変更点、および各RDBMS用NHibernateコンフィグファイルについて。SQLite
標準で導入されているsqlite3パッケージの使用で問題ありません。PostgreSQL
セットアップ上の変更点
「PostgreSQL 8.3.7」はAPTから導入可能です。$ sudo apt-get install postgresql
リモート接続用の設定を行います。
[/etc/postgresql/8.3/main/postgresql.conf の修正例]
- ...
- # 修正
- #listen_addresses = 'localhost'
- listen_addresses = '*'
- ...
[/etc/postgresql/8.3/main/pg_hba.conf の修正例]
- ...
- # 修正、なければ追加
- # IPv4 remote connections:
- host all all 192.168.1.0/24 md5
- ...
[実行例]
$ su - postgres
$ createuser -P sta
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
$ exit
$ createdb TestData
$ psql TestData -f ~/src/sql/postgresql/Products.sql
$ createuser -P sta
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
$ exit
$ createdb TestData
$ psql TestData -f ~/src/sql/postgresql/Products.sql
NHibernateコンフィグファイル
[PostgreSQL.cfg.xml(PostgreSQL用)]
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernateExample.Test">
- <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
- <property name="connection.connection_string">
- Server=192.168.1.5;
- Port=5432;
- User Id=sta;
- Password=password;
- Database=TestData;
- </property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
- <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
- <mapping file="Product.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
$ cd ~/lib
$ ln -s /opt/mono/2.4/lib/mono/2.0/Npgsql.dll .
$ ln -s /opt/mono/2.4/lib/mono/2.0/Npgsql.dll .
MySQL
セットアップ上の変更点
「MySQL 5.1」は、APTから導入可能です。$ sudo apt-get install mysql-server-5.1
キャラクタセット、デェフォルトストレージエンジンの設定は「/etc/mysql/my.cnf」で行えます。
[/etc/mysql/my.cnf の設定例]
- ...
- [client]
- # 追加
- default-character-set=utf8
- [mysqld]
- # 追加
- default-character-set=utf8
- default-storage-engine=INNODB
- # コメント化
- #bind-address = 127.0.0.1
- ...
[Connection.cs の一時的な修正]
- ...
- //protected override DbProviderFactory DbProviderFactory
- protected DbProviderFactory DbProviderFactory
- ...
NHibernateコンフィグファイル
[MySQL.cfg.xml(MySQL用)]
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernateExample.Test">
- <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
- <property name="connection.connection_string">
- Server=192.168.1.5;
- Database=TestData;
- Uid=sta;
- Pwd=password;
- CharSet=utf8
- </property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
- <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
- <mapping file="Product.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
$ cd ~/lib
$ ln -s /opt/mono/2.4/lib/mono/2.0/MySql.Data.dll .
$ ln -s /opt/mono/2.4/lib/mono/2.0/MySql.Data.dll .
SQL Server
セットアップ上の変更点
「SQL Server 2008 Express Edition SP1」はダウンロードセンターからダウンロード可能です。データベース権限の付与の際、「ON データベース名」の指定が不要でした。
[ユーザーに権限(例:CONTROL)の付与]
> sqlcmd -S localhost\SQLEXPRESS
1> USE TestData;
2> GRANT CONTROL TO sta
3> GO
1> USE TestData;
2> GRANT CONTROL TO sta
3> GO
Windows 7のファイアウォールが有効な場合、リモート接続のためのポートの登録が必要になります。
[コントロールパネル]-[システムとセキュリティ]-[Windows ファイアウォール]-[詳細設定]-[受信の規則]-[新しい規則...]を選択し、
- 規則の種類: ポート
- プロトコル: TCP
- ローカルポート: 49174
- 名前: SQL Server 2008 Express
を設定します。
NHibernateコンフィグファイル
[SQLServer.cfg.xml(SQL Server用)]
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernateExample.Test">
- <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
- <property name="connection.connection_string">
- Data Source=192.168.1.6,49174;
- Initial Catalog=TestData;
- User ID=sta;
- Password=passw0rd;
- Trusted_Connection=False
- </property>
- <property name="adonet.batch_size">0</property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
- <property name="use_outer_join">true</property>
- <property name="command_timeout">60</property>
- <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
- <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
- <mapping file="Product.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
Oracle
セットアップ上の変更点
「Oracle Database 10g Express」をUbuntuにインストールする場合、APTのソースリストにリポジトリを追加することで、APTから導入可能になります。[/etc/apt/sources.list へ追加]
- ...
- deb http://oss.oracle.com/debian unstable main non-free
- ...
[導入例]
$ wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install oracle-xe-universal # ゲストOS側で
$ sudo apt-get install oracle-xe-client # ホストOS側で
$ sudo apt-get update
$ sudo apt-get install oracle-xe-universal # ゲストOS側で
$ sudo apt-get install oracle-xe-client # ホストOS側で
導入時にスワップ領域の不足からインストールに失敗することがあります。その場合、スワップ領域を増やす必要があるので、次を参考に対応して下さい。
Mono 2.4で提供されている「System.Data.OracleClient」に以前の記事の様な修正対応は必要ありません。
NHibernateコンフィグファイル
[Oracle.cfg.xml(Oracle用)]
- <?xml version="1.0" encoding="utf-8"?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernateExample.Test">
- <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
- <property name="connection.connection_string">
- Server=192.168.1.5;
- User ID=TESTDATA;
- Password=password;
- </property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.OracleDialect</property>
- <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
- <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
- <mapping file="Product.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
Firebird
セットアップ上の変更点
変更点はなく、「Firebird編」の環境をそのまま使用しました。NHibernateコンフィグファイル
[Firebird.cfg.xml(Firebird用)]
- <?xml version="1.0" encoding="utf-8" ?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
- <session-factory name="NHibernateExample.Test">
- <property name="connection.driver_class">NHibernate.Driver.FirebirdClientDriver</property>
- <property name="connection.connection_string">
- Server=192.168.1.5;
- Database=/home/sta/data/firebird/TestData.fdb;
- User=sta;
- Password=password;
- Dialect=3;
- </property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.FirebirdDialect</property>
- <property name="command_timeout">60</property>
- <property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
- <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
- <mapping file="Product.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
$ cd ~/lib
$ ln -s /opt/mono/2.4/lib/mono/2.0/FirebirdSql.Data.FirebirdClient.dll .
$ ln -s /opt/mono/2.4/lib/mono/2.0/FirebirdSql.Data.FirebirdClient.dll .
DB2
セットアップ上の変更点
変更点はなく、「DB2編」の環境をそのまま使用しました。NHibernateコンフィグファイル
[DB2.cfg.xml(DB2用)]
- <?xml version="1.0" encoding="utf-8" ?>
- <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
- <session-factory name="NHibernateExample.Test">
- <property name="connection.driver_class">NHibernate.Driver.OdbcDriver</property>
- <property name="connection.connection_string">
- DSN=TestData_DB2;
- UID=sta;
- PWD=password
- </property>
- <property name="show_sql">false</property>
- <property name="dialect">NHibernate.Dialect.DB2Dialect</property>
- <property name="command_timeout">60</property>
- <property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
- <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
- <mapping file="Product.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
9.まとめ
Mono環境上でのFirebird、DB2、db4o、NHibernateを使用したDBアクセスについて解説を行いました。「Monoで他のDBも使ってみたよ!」シリーズでは、次のことが確認できました。
DBMS | データプロバイダ | プロバイダファクトリ(ADO.NET 2.0)対応 | NHibernate(2.1.0)対応 |
---|---|---|---|
SQLite | Mono.Data.Sqlite | ◯ | ◯:NHibernate SQLiteドライバの変更が必要 |
PostgreSQL | Npgsql | ◯:Npgsql から提供されているデータプロバイダを使用 | ◯:同左 |
MySQL | MySql.Data | ◯:Connector/Net から提供されているデータプロバイダを使用 | ◯:同左 |
SQL Server | System.Data.SqlClient | ◯ | ◯ |
Oracle | System.Data.OracleClient | ◯:Mono 1.9.1 では実装対応が必要(Mono 2.4 ではその必要はない) | ◯ |
Firebird | FirebirdSql.Data.FirebirdClient | ◯:Firebird から提供されているデータプロバイダを使用 | ◯:同左 |
DB2 | System.Data.Odbc | ◯:プロバイダファクトリ対応とは別だが、「動作確認」で解説した System.Data.Odbc の DB2 CLI/ODBC ドライバ対応が必要になる場合がある | ◯:System.Data.OdbcのDB2 CLI/ODBCドライバ対応が必要 |
基本的なCRUD操作レベルですが、プロバイダファクトリ、NHibernateを使用した汎用的なDB操作を試すことができました。
db4oでは、.NETの汎用的なクエリ機能であるLINQを試すことができました。db4oは、Javaおよび.NETで使用可能ですが、Linux上のMono環境でも問題なく使用できました。
本稿で「Monoで他のDBも使ってみたよ!」シリーズは終了です。読んでいただいた方のなんらかの役に立ったなら幸いです。では、また。
参考資料
おしまい。
0 件のコメント:
コメントを投稿