| |||||||||||||||||
Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
Seasar.Dao.Dbms\DbmsManager.cs | 50.0% | 71.4% | 75.0% | 68.2% |
|
1 | #region Copyright | |
2 | /* | |
3 | * Copyright 2005 the Seasar Foundation and the Others. | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
14 | * either express or implied. See the License for the specific language | |
15 | * governing permissions and limitations under the License. | |
16 | */ | |
17 | #endregion | |
18 | ||
19 | using System; | |
20 | using System.Data; | |
21 | using System.Data.OleDb; | |
22 | using System.Data.Odbc; | |
23 | using System.Reflection; | |
24 | using System.Resources; | |
25 | using Seasar.Extension.ADO; | |
26 | ||
27 | namespace Seasar.Dao.Dbms | |
28 | { | |
29 | public sealed class DbmsManager | |
30 | { | |
31 | private static ResourceManager resourceManager; | |
32 | ||
33 | 1 | static DbmsManager() |
34 | { | |
35 | 1 | resourceManager = new ResourceManager( |
36 | "Dbms", Assembly.GetExecutingAssembly()); | |
37 | } | |
38 | ||
39 | 0 | private DbmsManager() |
40 | { | |
41 | } | |
42 | ||
43 | 25 | public static IDbms GetDbms(IDataSource dataSource) |
44 | { | |
45 | // IDbConnectionをDataSourceから取得する | |
46 | 25 | IDbConnection cn = dataSource.GetConnection(); |
47 | ||
48 | //IDbmsの実装クラスを取得するためのKey | |
49 | 25 | string dbmsKey = null; |
50 | ||
51 | 25 | if(cn is OleDbConnection) |
52 | { | |
53 | // OleDbConnectionの場合はKeyをType名とProvider名から作成する | |
54 | 0 | OleDbConnection oleDbCn = cn as OleDbConnection; |
55 | 0 | dbmsKey = cn.GetType().Name + "_" + oleDbCn.Provider; |
56 | } | |
57 | 25 | else if(cn is OdbcConnection) |
58 | { | |
59 | // OdbcConnectionの場合はKeyをType名とDriver名から作成する | |
60 | 0 | OdbcConnection odbcCn = cn as OdbcConnection; |
61 | 0 | dbmsKey = cn.GetType().Name + "_" + odbcCn.Driver; |
62 | } | |
63 | else | |
64 | { | |
65 | 25 | dbmsKey = cn.GetType().Name; |
66 | } | |
67 | ||
68 | // KeyからIDbms実装クラスのインスタンスを取得する | |
69 | 25 | return GetDbms(dbmsKey); |
70 | } | |
71 | ||
72 | /// <summary> | |
73 | /// Dbms.resxをdbmsKeyで探し、IDbms実装クラスのインスタンスを取得する | |
74 | /// </summary> | |
75 | /// <param name="dbmsKey">Dbms.resxを検索する為のKey</param> | |
76 | /// <returns>IDbms実装クラスのインスタンス</returns> | |
77 | /// <remarks>dbmsKeyに対応するものが見つからない場合は、 | |
78 | /// 標準のStandardを使用する</remarks> | |
79 | 28 | public static IDbms GetDbms(string dbmsKey) |
80 | { | |
81 | // Dbms.resxからIDbmsの実装クラス名を取得する | |
82 | 28 | string typeName = resourceManager.GetString(dbmsKey); |
83 | ||
84 | // IDbms実装クラスのTypeを取得する | |
85 | // Dbms.resxに対応するIDbms実装クラスが無い場合は、標準のStandardを使用する | |
86 | 28 | Type type = typeName == null ? typeof(Standard) : Type.GetType(typeName); |
87 | ||
88 | // IDbms実装クラスのインスタンスを作成して返す | |
89 | 28 | return (IDbms) Activator.CreateInstance(type, false); |
90 | } | |
91 | } | |
92 | } | |
93 |
|