<!--METADATA TYPE= "typelib"  NAME= "ADODB Type Library" FILE="C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll"  --> 
<%
Function getConnectString()
	Dim strConnect
	strConnect = "Provider=SQLOLEDB.1;Data Source="&Application("url")&";Initial catalog="&Application("DB")&";user ID="&Application("DB_id")&";Password="&Application("DB_pwd")
	'strConnect = "Provider=OraOLEDB.Oracle;Data Source="&Application("url")&";User Id="&Application("DB_id")&";Password="&Application("DB_pwd")
	getConnectString = strConnect
End function

'select
class SelectTable
	public connectString
	Private dbcon
	Private rs

	'클래스 초기화
	Private Sub Class_Initialize
		connectString = getConnectString()
		Set dbcon = Server.CreateObject("ADODB.Connection")
	End Sub


	'select 쿼리(단일필드)
	Public function selectQueryColumn( sql )
		Dim ret_value : ret_value = null

		selectQuery( sql )
		If Not( rs.bof Or rs.eof ) Then
			ret_value = rs(0)
			If isnull(ret_value) Then ret_value = ""
			ret_value = CStr( ret_value )
		End if

		dbClose()
		selectQueryColumn = ret_value
	End function


	'select 쿼리(단일행,배열)
	Public function selectQueryRecord( sql )
		Dim ret_value : Set ret_value = Server.CreateObject("Scripting.Dictionary")

		selectQuery( sql )
		If Not( rs.bof Or rs.eof ) Then
			For each k in rs.Fields
				value = k.value
				If isnull(value) Then value = ""
				value = CStr( value )

				ret_value(LCase(k.name)) = value
			Next
		End If
	
		dbClose()
		Set selectQueryRecord = ret_value
		Set ret_value = nothing
	End Function
	

	'select 쿼리(2차원배열)
	Public function selectQueryTable( sql )
		Dim ret_value(), ret_null, i

		selectQuery( sql )
		If rs.bof Or rs.eof Then
			selectQueryTable = null
		else
			redim ret_value(rs.recordcount-1)
			i = 0
			Do until rs.eof
				Set ret_value(i) = Server.CreateObject("Scripting.Dictionary")
				For each k in rs.Fields
					value = k.value
					If isnull(value) Then value = ""
					value = CStr( value )
					ret_value(i)( LCase(k.name) ) = value
				Next
				rs.movenext
				i = i + 1
			Loop
			selectQueryTable = ret_value
		End If
	
		dbClose()
	End function


	Public sub arr2Value( rs )
		For each item in rs
			execute(item & " = rs(""" & item & """)")
		next
	End sub

	Private sub selectQuery( sql )
		dbcon.Open connectString
		Set Rs = Server.CreateObject("ADODB.RecordSet")
		rs.open sql, dbcon, 3
	End sub

	Private sub dbClose()
		rs.close()
		Set rs = Nothing
		dbcon.close
	End sub

	' 클래스 종료
	Private Sub Class_Terminate
		Set dbcon = Nothing
	End Sub

end Class


'insert
class InsertTable
	public connectString
	public tableName
	Private dbcon

	'db에 저장할 데이타 저장 리스트
	private insertValues
	private addValues

	'클래스 초기화
	Private Sub Class_Initialize
		tableName = ""
		Set insertValues = Server.CreateObject("Scripting.Dictionary")
		Set addValues = Server.CreateObject("Scripting.Dictionary")

		connectString = getConnectString()
		Set dbcon = Server.CreateObject("ADODB.Connection")
	End Sub


	'세팅된 insertValues로 insert 쿼리 생성 실행
	Public function execute()
		Dim sql, i_fields, i_values, i

		If tableName = "" then
			respons.wirte "tableName is none."
			Response.end
		End if

		sql = "insert into " & tableName & "("
		i_fields = ""
		If addValues.count > 0 Then
			i = 0
			For each k in addValues
				If i > 0 Then i_fields = i_fields & ","
				i_fields = i_fields & k
				i = i + 1
			next
		End If

		For each k in insertValues
			If i_fields <> "" Then i_fields = i_fields & ","
			i_fields = i_fields & k
		next
		sql =  sql & i_fields & ") values("

		i_values = ""
		If addValues.count > 0 Then
			i = 0
			For each v in addValues
				If i > 0 Then i_values = i_values & ","
				i_values = i_values & addValues(v)
				i = i + 1
			next
		End If
		For each v in insertValues
			If i_values <> "" Then i_values = i_values & ","
			i_values = i_values & "'" & insertValues(v) & "'"
		next
		sql = sql & i_values & ")"
		insertQuery(sql)

		'연관배열 초기화
		insertValues.removeall
		addValues.removeall
		
		execute = sql
	End function

	'insert 쿼리 직접받아서 실행
	private sub insertQuery(sql)
		dbcon.Open connectString
		dbcon.execute sql
		dbcon.close()
	End sub

	'연관배열 추가
	sub setValues(keyname, value)
		insertValues( keyname ) = value
	End sub

	'연관배열 추가
	sub setAddValues(keyname, value)
		addValues(keyname) = value
	End sub

	'필드명으로 연관배열 추가
	sub setFieldsValues( str_fileds )
		Dim str_fileds_arr, z
		
		str_fileds = replace(str_fileds, " ", "")
		str_fileds_arr = split(str_fileds, ",")

		For z=0 To ubound( str_fileds_arr )
			Call setValues(str_fileds_arr(z), eval(str_fileds_arr(z)))
		next
	End sub


	' 클래스 종료
	Private Sub Class_Terminate
		Set dbcon = nothing
		Set insertValues = Nothing
		Set addValues = nothing
	End Sub

end Class


'update
class UpdateTable
	public connectString
	public tableName
	public whereQuery
	Private dbcon

	'db에 저장할 데이타 저장 리스트
	private updateValues
	private addValues

	'클래스 초기화
	Private Sub Class_Initialize
		tableName = ""
		whereQuery = ""
		Set updateValues = Server.CreateObject("Scripting.Dictionary")
		Set addValues = Server.CreateObject("Scripting.Dictionary")

		connectString = getConnectString()
		Set dbcon = Server.CreateObject("ADODB.Connection")
	End Sub


	'세팅된 updateValues로 insert 쿼리 생성 실행
	Public function execute()
		Dim sql, u_values, i

		If tableName = "" then
			respons.wirte "tableName is none."
			Response.end
		End if

		sql = "update " & tableName & " set "
		u_values = ""
		If addValues.count > 0 Then
			i = 0
			For each k in addValues
				If i > 0 Then u_values = u_values & ","
				u_values = u_values & k & "=" & addValues(k)
				i = i + 1
			next
		End If

		If updateValues.count > 0 Then
			i = 0
			For each k in updateValues
				If u_values <> "" Then u_values = u_values & ","
				u_values = u_values & k & "= '" & updateValues(k) & "'"
				i = i + 1
			next
		End If

		sql = sql & u_values & " " & whereQuery
		updateQuery( sql )
		
		'연관배열 초기화
		updateValues.removeall
		addValues.removeall
		
		execute = sql
	End function

	'update 쿼리 직접받아서 실행
	private sub updateQuery(sql)
		dbcon.Open connectString
		dbcon.execute sql
		dbcon.close()
	End sub

	'연관배열 추가
	sub setValues(keyname, value)
		updateValues( keyname ) = value
	End sub

	'연관배열 추가
	sub setAddValues(keyname, value)
		addValues(keyname) = value
	End sub

	'필드명으로 연관배열 추가
	sub setFieldsValues( str_fileds )
		Dim str_fileds_arr, z
		
		str_fileds = replace(str_fileds, " ", "")
		str_fileds_arr = split(str_fileds, ",")

		For z=0 To ubound( str_fileds_arr )
			Call setValues(str_fileds_arr(z), eval(str_fileds_arr(z)))
		next
	End sub


	' 클래스 종료
	Private Sub Class_Terminate
		Set dbcon = nothing
		Set updateValues = Nothing
		Set addValues = nothing
	End Sub

end Class


class UpdateClob
	public connectString
	public tableName
	public whereQuery

	public field
	public value

	Private dbcon

	'db에 저장할 데이타 저장 리스트
	private updateValues

	'클래스 초기화
	Private Sub Class_Initialize
		tableName = ""
		whereQuery = ""
		Set updateValues = Server.CreateObject("Scripting.Dictionary")
		connectString = getConnectString()
		Set dbcon = Server.CreateObject("ADODB.Connection")
	End Sub


	'세팅된 updateValues로 insert 쿼리 생성 실행
	Public function execute()
		Dim sql, u_values, i, ocmd

		If tableName = "" then
			respons.wirte "tableName is none."
			Response.end
		End if

		sql = "update " & tableName & " set "
		u_values = ""
		If updateValues.count > 0 Then
			i = 0
			For each k in updateValues
				If u_values <> "" Then u_values = u_values & ","
				u_values = u_values & k & "= empty_clob()"
				i = i + 1
			next
		End If

		If field <> "" And value <> "" Then
			If u_values <> "" Then u_values = u_values & ","
			u_values = u_values & field & " = empty_clob() "
		End if

		sql = sql & u_values & " " & whereQuery
		updateQuery( sql )

		sql = "UPDATE " & tableName & " SET "
		u_values = ""
		If updateValues.count > 0 Then
			i = 0
			For each k in updateValues
				If u_values <> "" Then u_values = u_values & ","
				u_values = u_values & k & "= ?"
				i = i + 1
			next
		End If

		If field <> "" And value <> "" Then
			If u_values <> "" Then u_values = u_values & ","
			u_values = u_values & field & " = ?"
		End if

		sql = sql & u_values & " " & whereQuery
		set ocmd = Server.CreateObject("ADODB.Command")

		With ocmd
			.ActiveConnection = connectString
			.CommandType = adCmdText
			.CommandText = sql
			For each k in updateValues
				.Parameters.Append .CreateParameter("@"&field, adLongVarWChar, adParamInput, 40000, updateValues(k)) 
			Next
			If field <> "" And value <> "" Then
				.Parameters.Append .CreateParameter("@"&field, adLongVarWChar, adParamInput, 40000, value) 
			End if
			.Execute
		End With

		Set ocmd = nothing

		'연관배열 초기화
		updateValues.removeall
		execute = sql
	End function

	'update 쿼리 직접받아서 실행
	private sub updateQuery(sql)
		dbcon.Open connectString
		dbcon.execute sql
		dbcon.close()
	End sub

	'연관배열 추가
	sub setValues(keyname, value)
		updateValues( keyname ) = value
	End sub

	'필드명으로 연관배열 추가
	sub setFieldsValues( str_fileds )
		Dim str_fileds_arr, z
		
		str_fileds = replace(str_fileds, " ", "")
		str_fileds_arr = split(str_fileds, ",")

		For z=0 To ubound( str_fileds_arr )
			Call setValues(str_fileds_arr(z), eval(str_fileds_arr(z)))
		next
	End sub


	' 클래스 종료
	Private Sub Class_Terminate
		Set dbcon = nothing
		Set updateValues = Nothing
	End Sub

end Class



Sub executeQuery( sql )
	Dim dbcon
	Set dbcon = Server.CreateObject("ADODB.Connection")
	dbcon.Open getConnectString()
	dbcon.execute sql

	dbcon.close
	Set dbcon = nothing
End sub
%>