00001 ' Attribute VB_Name = "DbAccess" 00002 Option Explicit 00003 '---------------------------------------------- 00004 '! @brief DB接続のためのショートカットクラス 00005 '! @require common.vbs 00006 '! @require Configuration.cls 00007 '! @require Logger.cls 00008 '! @note *(config.properties)にdatabase.xxx.connectionStringの設定が必要 00009 '---------------------------------------------- 00010 Class DbAccess 00011 Dim mConnection 00012 00013 '------------------------------------------------------------------- 00014 '* @brief 接続を開く 00015 '* @param dbname 使用するデータベースの設定名を指定。 00016 '* database.hoge.connectionStringを設定した場合、指定するのは"hoge" 00017 '------------------------------------------------------------------- 00018 Public Sub open(dbname) 00019 Dim connectionString 00020 connectionString = ScriptProperties.Item("database." & dbname & ".connectionString") 00021 00022 Set mConnection = CreateObject("ADODB.Connection") 00023 mConnection.ConnectionString = connectionString 00024 mConnection.Open 00025 If Err.Number <> 0 Then 00026 Logger.Error Err.Description 00027 Wscript.Quit 99 00028 End If 00029 Logger.Info "Database connected " & connectionString 00030 00031 00032 End Sub 00033 '------------------------------------------------------------------- 00034 '* @brief 接続を閉じる 00035 '------------------------------------------------------------------- 00036 Public Sub close() 00037 mConnection.Close 00038 Set mConnection = Nothing 00039 End Sub 00040 00041 '------------------------------------------------------------------- 00042 '* @brief 指定クエリーからレコードセットを作成 00043 '* @remarks queryはSQLQuot等を使用してエスケープしておくべき 00044 '------------------------------------------------------------------- 00045 Public Function openRecordset(query) 00046 dim rs 00047 Logger.Debug query 00048 Set rs = mConnection.Execute(query) 00049 Set openRecordset = rs 00050 End Function 00051 '------------------------------------------------------------------- 00052 '* @brief 指定クエリーを実行 00053 '* @remarks queryはSQLQuot等を使用してエスケープしておくべき 00054 '------------------------------------------------------------------- 00055 Public Function execute(query) 00056 Logger.Debug query 00057 Dim result 00058 mConnection.Execute query,result 00059 execute = result 00060 End Function 00061 '------------------------------------------------------------------- 00062 '* @brief 指定クエリーからDbCommandのインスタンスを作成 00063 '* @remarks commantType が adCmdTextの場合、パラメータのプレースホルダは "?"<br /> 00064 '* commantType が adCmdStoredProcの場合、パラメータのプレースホルダは ストアドプロシージャのパラメータ名 00065 '------------------------------------------------------------------- 00066 Public Function createCommand(query,commantType) 00067 Dim command 00068 Set command = new DbCommand 00069 command.newCommand commantType,query,mConnection 00070 00071 Set createCommand = command 00072 End Function 00073 '------------------------------------------------------------------- 00074 '* tran start 00075 '------------------------------------------------------------------- 00076 Public Function beginTransaction() 00077 mConnection.BeginTrans 00078 End Function 00079 '------------------------------------------------------------------- 00080 '* tran end 00081 '------------------------------------------------------------------- 00082 Public Function commitTransaction() 00083 mConnection.CommitTrans 00084 End Function 00085 '------------------------------------------------------------------- 00086 '* tran end 00087 '------------------------------------------------------------------- 00088 Public Function rollbackTransaction() 00089 mConnection.RollbackTrans 00090 End Function 00091 00092 Public Sub Class_Initialize 00093 End Sub 00094 Public Sub Class_Terminate 00095 End Sub 00096 00097 End Class