<!--#include file="te_config.asp"-->
<%

	sub ShowFieldForm
		if sName = "" then sName = sNewName
%>
	<!--#include file="te_header.asp"-->
	
	<table border=0 cellspacing=1 cellpadding=2 bgcolor = "#ffe4b5" width=100%>
		<tr>
			<td class="smallertext">
				<a href="index.asp">管理首页</a> <a href="te_admin.asp">编辑页面</a> <a href="te_listtables.asp?cid=<%=request("cid")%>"><%=arrDesc(request("cid"))%></a> <a href="te_tablecreate.asp?cid=<%=request("cid")%>&tablename=<%=server.urlencode(request("tablename"))%>">创建表格 [<%=request("tablename")%>]</a> 创建数据
			</td>
			<td class="smallerheader" width=130 align=right>
			<%
			if bProtected then 
				response.write session("teFullName")
				response.write "<a href=""te_logout.asp""> (退出)</a>" 
			end if
			%>
			</td>
		</tr>
	</table>
	<br>

	<p class="smallertext"><%=sErr%></p>
	<form action="te_fieldedit.asp?cid=<%=lConnID%>&tablename=<%=sTableName%>&fldname=<%=sName%>" method="post">
		<table border=0 width=400>
			<tr>
				<td class="smallerheader">数据名称</td>
				<td><input type="text" name="txtName" class="tbflat" value="<%=sName%>"></td>
			</tr>
			<tr>
				<td class="smallerheader">数据类型</td>
				<td>
					<select name="cboType" class="tbflat">
					<%
					for iTemp = 1 to 10
						response.write "<option value=""" & aT(iTemp,1) & """"
						if lType = aT(iTemp,1) then
							response.write " selected>"
						else
							response.write ">" 
						end if
						response.write aT(iTemp,2)
					next
					%>
					</select>
				</td>
			</tr>
			<tr>
				<td class="smallerheader">大小 (如果是文本)</td>
				<td><input type="text" name="txtSize" value="<%=lSize%>" size="5" maxlength="10" class="tbflat"></td>
			</tr>
			<tr>
				<td class="smallerheader" valign=top>Attributes</td>
				<td class="smallertext">
					<input type="checkbox" name="chkAcceptNulls" value="1"<%=sAcceptNulls%>>接受空格
					<input type="checkbox" name="chkIndexed" value="1"<%=sIndexed%>>编入索引<br>
					<input type="checkbox" name="chkIndexUnique" value="1"<%=sIndexUnique%>>唯一的
					<input type="checkbox" name="chkPrimaryKey" value="1"<%=sPrimaryKey%>>是否主键<br>
					<input type="checkbox" name="chkAutoIncrement" value="1"<%=sAutoIncrement%>>自动编号(如果是数字)<br>
				</td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" name="cmdSave" value=" 写入 " class="cmdflat"> 
        <input type="reset" name="cmdSave" value=" 重设 " class="cmdflat"></td>
			</tr>
		</table>
		</form>
<%
	end sub

	dim aT(10,2)

	aT(1,1) = adVarWChar		:aT(1,2) = "text"
	aT(2,1) = adInteger			:aT(2,2) = "long"
	aT(3,1) = adSmallInt		:aT(3,2) = "integer"
	aT(4,1) = adBoolean			:aT(4,2) = "boolean"
	aT(5,1) = adDate			:aT(5,2) = "date"
	aT(6,1) = adCurrency		:aT(6,2) = "currency"
	aT(7,1) = adLongVarWChar	:aT(7,2) = "memo"
	aT(8,1) = adLongVarBinary	:aT(8,2) = "ole"
	aT(9,1) = adGUID			:aT(9,2) = "guid"
	aT(10,1)= adUnsignedTinyInt	:aT(10,2)= "byte"
	
	lConnID = request("cid")
	sTableName = request("tablename")
	sName = request("fldname")
	if sName = "" then bNewField = True
	
	on error resume next	

	if request("cmdSave") <> "" then
		'Submitted new field definition
		
		OpenRS arrConn(lConnID)
		
		sNewName = request("txtName")
		sBefore = request("txtBefore")
		lType = CLng(request("cboType"))
		if request("txtSize") <> "" then lSize = CLng(request("txtSize"))
		if request("chkAutoIncrement") <> "" then sAutoIncrement = " checked" 
		if request("chkAcceptNulls") <> "" then sAcceptNulls = " checked"
		if request("chkPrimaryKey") <> "" then sPrimaryKey = " checked"
		if request("chkIndexed") <> "" then sIndexed = " checked"
		if request("chkIndexUnique") <> "" then sIndexUnique = " checked"
		
		sSQL = "ALTER TABLE [" & sTableName & "] "
		
		if bNewField then
			'We are adding a new field
			sSQL = sSQL & " ADD COLUMN [" & sNewName & "] "
		else
			'We are editing a field
			sSQL = sSQL & " ALTER COLUMN [" & sName & "] "
		end if
		
		select case lType
			case adSmallInt			: sFieldType = "SMALLINT"
			case adInteger			: sFieldType = "INTEGER"
			case adBoolean			: sFieldType = "BOOLEAN"
			case adDate				: sFieldType = "DATE"
			case adCurrency			: sFieldType = "CURRENCY"
			case adVarWChar			: sFieldType = "VARCHAR"
			case adLongVarWChar		: sFieldType = "LONGVARCHAR"
			case adLongVarBinary	: sFieldType = "LONGVARBINARY"
			case adGUID				: sFieldType = "GUID"
			case adUnsignedTinyInt	: sFieldType = "TINYINT"
		end select
		
		sSQL = sSQL & sFieldType
		
		if lType = adVarWChar then
			'Set field size
			if lSize = "" then lSize = 50
			sSQL = sSQL & "(" & lSize & ") "
		end if
		
		if sAcceptNulls <> "" then
			sSQL = sSQL & " NULL "
		else
			sSQL = sSQL & " NOT NULL "
		end if
		
		if sAutoIncrement <> "" then
			sSQL = sSQL & " IDENTITY "
		end if

		if sPrimaryKey <> "" then
			'If the field has a primary key index
			sSQL = sSQL & " CONSTRAINT [pk" & sNewName & "] PRIMARY KEY "
		else
			'If the field is indexed (not PK)
			if sIndexed <> "" then
				if sIndexUnique <> "" then
					sSQL = sSQL & " CONSTRAINT [idx" & sNewName & "] UNIQUE"
				else
					'Index is no constraint, so another sql stat required
					sSQL2 = "CREATE INDEX [idx" & sNewName & "] ON [" & sTableName & "] ([" & sNewName & "]) "
				end if
			end if
		end if
		
		if (sName <> "") and (sNewName <> sName) then
			'Rename field
			'sSQL = "ALTER TABLE [" & sTableName & "] RENAME COLUMN [" & sName & "] TO [" & sNewName & "]"
			sErr = "Cannot rename field."
			bErr = True
		end if
		
		if bErr = True then
			conn.close
			set rs = nothing
			set conn = nothing
			ShowFieldForm
		else

			conn.execute sSQL 
			if sSQL2 <> "" then
				conn.execute sSQL2
			end if
	
			conn.close
			set rs = nothing
			set conn = nothing
			if err <> 0 then
				%><!--#include file="te_header.asp"--><%
				response.write err.description & "<br>" & sSQL
			else
				response.redirect "te_tableedit.asp?cid=" & lConnID & "&tablename=" & server.urlencode(request("tablename"))
			end if
		end if

	else
		if sName <> "" then
			'We are editing a field
			'Get the attributes for the field
		
			OpenRS arrConn(lConnID)
			set AdoX = server.createobject("adox.catalog")
			set fld = server.createobject("adox.column")
	
			AdoX.ActiveConnection = conn
			set fld = AdoX.Tables(sTableName).Columns(sName)
			
			lType = fld.Type
			lSize = fld.DefinedSize
			
			if (fld.attributes AND adFldIsNullable) = adFldIsNullable then
				sAcceptNulls = " checked"
			end if
			
			'Enumerate the indices to check that if this field has a primary key'd index
			set rs = conn.openSchema(adSchemaIndexes)
			do while not rs.eof
				if rs("table_name") = sTableName then
					if (rs("column_name") = fld.Name) then
						sIndexed = " checked"
						if (rs("unique") = True) then
							sIndexUnique = " checked"
						end if
						if (rs("primary_key") = True) then 
							sPrimaryKey = " checked"
						end if
					end if
				end if
				rs.movenext
			loop

			if fld.properties("AutoIncrement") = True then
				sAutoIncrement = " checked"
			end if

		end if
		
		'Show form for a new field definition
		if bNewField then 
			sErr = "添加新区域."
		else
			sErr = "编辑区域解释."
		end if
		ShowFieldForm
	end if
%>
<!--#include file="te_footer.asp"-->
 

