-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathte_readDB.asp
314 lines (279 loc) · 9.91 KB
/
te_readDB.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<%
'==============================================================
' TableEditoR 0.81 Beta
' http://www.2enetworx.com/dev/projects/tableeditor.asp
'--------------------------------------------------------------
' File: te_readDB.asp
' Description: Generate CSV file for te_showtable.asp (IE mode)
' Initiated By Hakan Eskici on Nov 07, 2000
'--------------------------------------------------------------
' Copyright (c) 2002, 2eNetWorX/dev.
'
' TableEditoR is distributed with General Public License.
' Any derivatives of this software must remain OpenSource and
' must be distributed at no charge.
' (See license.txt for additional information)
'
' See Credits.txt for the list of contributors.
'
' Change Log:
'--------------------------------------------------------------
' # Mar 26, 2001 by Hakan Eskici
' Added support for automatic primary key detection
' Added support for multiple primary keys
' # Mar 28, 2001 by Hakan Eskici
' Modified the recordset paging control
' # Mar 29, 2001 by Hakan Eskici
' Added support for SQL Server boolean values
' Modified request's to .form or .querystring
' Added support for deleting multiple records
' # April 18, 2002 by Rami Kattan
' this file generate only CSV text file to be read by the
' te_showtable.asp and view the records
'-------------------------------------------------------------
Response.ContentType = "text/csv"
' Get the requested number of records per page
cPerPage = CLng(request.QueryString("cPerPage"))
If cPerPage = 0 or cPerPage= "" then cPerPage = iDefaultPerPage
%>
<!--#include file="te_config.asp"-->
<%
lConnID = request("cid")
sTableName = request("tablename")
sQuery = request("q")
'------------------------------
'added 8/10/01 by j.wilkinson, [email protected]
'added a check for nonAdmin users trying to view the admin table
'This is just checking that the connection ID = 0, assumes that
'non-admin users have no legitimate reason to get to that db at all.
' note that this may not protect against using queries to view
' this db and table
if lConnID=0 and not bAdmin then
response.end
end if
'------------------------------
const csvchar = ","
if sQuery <> "" then
bQuery = True
sTableName = replace(sTableName, """", "'")
end if
function CheckData(inData)
if not isnull(inData) then
' inData = replace(inData, vbCrLf, "")
' inData = replace(inData, vbCr, "")
' inData = replace(inData, vbLf, "")
inData = replace(inData, "\", "\\")
inData = replace(inData, ",", "\,")
inData = replace(inData, ";", "\;")
inData = replace(inData, "<form", "<form")
inData = replace(inData, "</form", "</form")
inData = replace(inData, """", "\""")
end if
CheckData = inData
end function
function isPrimaryKey(sFieldName)
bPrimaryKey = False
for iPK = 0 to ubound(aPrimaryKeys)
if LCase(sFieldName) = LCase(aPrimaryKeys(iPK)) then
bPrimaryKey = True
exit for
end if
next
isPrimaryKey = bPrimaryKey
end function
OpenRS arrConn(lConnID)
'Added by Hakan
'Find the primary key of the given table
dim aPrimaryKeys
if arrType(lConnID) = tedbDsn then
'response.write "Automatic primary key detection is not possible for DSN Connections. " & sSoWhat & "<br><br>"
else
set rsX = conn.openSchema(adSchemaPrimaryKeys)
do while not rsX.eof
if (rsX("table_name") = sTableName) then
if sPrimaryKeyFieldName = "" then
sPrimaryKeyFieldName = rsX("column_name")
else
sPrimaryKeyFieldName = sPrimaryKeyFieldName & "," & rsX("column_name")
end if
end if
rsX.movenext
loop
rsX.close
end if
'Set the primary key field to first field in the list by default
if sPrimaryKeyFieldName = "" then sPrimaryKeyFieldName = 0
if request.querystring("orderby") <> "" then
sOrderBy = " ORDER BY [" & request.querystring("orderby") & "] "
sOrderByLink = "&orderby=" & request.querystring("orderby")
select case request.querystring("dir")
case "desc"
sOrderBy = sOrderBy & " DESC"
sOrderByLink = sOrderByLink & "&dir=desc"
case "asc"
sOrderBy = sOrderBy & " ASC"
sOrderByLink = sOrderByLink & "&dir=asc"
case else
sOrderBy = sOrderBy & " ASC"
sOrderByLink = sOrderByLink & "&dir=asc"
end select
end if
if instr(lcase(sTableName), "order by") <> 0 then
sOrderBy = ""
end if
'Added by Danival
'Modified by Hakan
bProc = request.querystring("proc")
if instr(1, ucase(sTableName), "SELECT") then
sSQL = sTableName & sOrderBy
else
if bProc <> "" then
bRecAdd = False
bRecEdit = False
bRecDel = False
sParamString = request.querystring("paramstring")
sProcURL = "&proc=1¶mstring=" & sParamString
sSQL = "EXEC [" & sTableName & "] " & sParamString
else
sSQL = "SELECT * FROM [" & sTableName & "]" & sWhere & sOrderBy
end if
end if
on error resume next
rs.CursorLocation = adUseServer
rs.Open sSQL, conn, adOpenStatic
if err <> 0 then
response.write "Error: " & err.description & "<br><br>"
if bQuery then
response.write "SQL : " & sSQL & "<br><br>"
end if
response.write "Click here to <a href=""javascript:history.back()"">go back</a>.<br><br>"
CloseRS
%><!--#include file="te_footer.asp"--><%
response.end
end if
on error goto 0
'Performance Issue:
'Getting the recordset properties may take long time for tables with many records
lRecs = rs.RecordCount
lFields = rs.Fields.Count
if isNumeric(request("ipage")) then iPage = CLng(request("ipage"))
rs.PageSize = cPerPage
rs.CacheSize = cPerPage
iPageCount = rs.PageCount
if iPage < 1 then iPage = 1
if lRecs > 0 then rs.AbsolutePage = iPage
' if bQuery then
' response.write sSQL & ""
' end if
response.write "Action"
for each fld in rs.fields
' Type Description
' -------------------------
' String Text data
' Date Calendar date
' Boolean Logical data
' Int Integer number
' Float Floating-point number
select case fld.Type
case adSmallInt, adInteger, adCurrency : tdcType = "Int"
case adVarWChar, adLongVarWChar : tdcType = "String"
case adBoolean : tdcType = "Boolean"
case adDate : tdcType = "date"
case else : tdcType = ""
end select
response.write csvchar & fld.name
if tdcType <> "" then response.write ":" & tdcType
'Added by Hakan
'Support for automatic primary key detection
'Support for multiple primary keys
aPrimaryKeys = split(sPrimaryKeyFieldName, ",")
sPKFieldNames = ""
sPKFieldValues = ""
sPKFieldTypes = ""
for iPK = 0 to ubound(aPrimaryKeys)
if isNumeric(aPrimaryKeys(iPK)) then aPrimaryKeys(iPK) = 0
set fld = rs.fields(aPrimaryKeys(iPK))
if sPKFieldNames = "" then sPKFieldNames = fld.name else sPKFieldNames = sPKFieldNames & ";" & fld.name
'if sPKFieldValues = "" then sPKFieldValues = fld.value else sPKFieldValues = sPKFieldValues & ";" & fld.value
if sPKFieldTypes = "" then sPKFieldTypes = fld.type else sPKFieldTypes = sPKFieldTypes & ";" & fld.type
next
next
response.write ";"
iRecCount = 0
'Key Field form elements for Multiple delete
do while not rs.eof
if (iRecCount = cPerPage) or (not Response.IsClientConnected) then exit do
if (iRecCount MOD 15) = 0 then Response.Flush
response.write vbCrLf
if arrType(lConnID) = tedbAccess or arrType(lConnID) = tedbSQLServer then
'Only Access and SQL can do this
if rs.AbsolutePage <> iPage then exit do
end if
sPKFieldValues = ""
for iPK = 0 to ubound(aPrimaryKeys)
if isNumeric(aPrimaryKeys(iPK)) then aPrimaryKeys(iPK) = 0
set fld = rs.fields(aPrimaryKeys(iPK))
'if sPKFieldNames = "" then sPKFieldNames = fld.name else sPKFieldNames = sPKFieldNames & ";" & fld.name
if sPKFieldValues = "" then sPKFieldValues = fld.value else sPKFieldValues = sPKFieldValues & ";" & fld.value
'if sPKFieldTypes = "" then sPKFieldTypes = fld.type else sPKFieldTypes = sPKFieldTypes & ";" & fld.type
next
sPKURL = "<a href=""javascript:EW('" & server.URLEncode(sPKFieldValues) & "')"">"
if bRecEdit then response.write "<img src=""images/edit.gif"" alt=""edit"" class=""smimg"" onclick=""EW('" & server.URLEncode(sPKFieldValues) & "')""> \;"
if bRecDel then
'One click delete link
response.write "<img src=""images/del.gif"" alt=""delete"" onclick=""DW('" & server.URLEncode(sPKFieldValues) & "')"" class=""smimg"">"
'Multi Delete Check box
response.write "<input type=""checkbox"" name=""chkDel"" value=""" & sPKFieldValues & """>"
end if
' response.write csvchar
iFieldCount = 0
for each fld in rs.fields
response.write csvchar
iFieldCount = iFieldCount + 1
if isPrimaryKey(fld.name) = True then
response.write sPKURL & rs(fld.name) & "</a>"' & csvchar
else
select case fld.type
case adSmallInt, adInteger
response.write rs(fld.name)
case adDate
if isdate(rs(fld.name)) then
response.write """" & rs(fld.name) & """"
end if
case adBoolean
response.write """<input type=\""checkbox\"" name=\""chk\"" disabled"
if rs(fld.name)=true then
response.write " checked>"""
else
response.write ">"""
end if
case adLongVarBinary
If Not isNull(rs(fld.name)) Then
response.write "<img align=""absmiddle"" src=""te_imagesdb.asp?cid=" & lConnID & "&tablename=" & server.UrlEncode(sTableName) & "&fld=" & sPKFieldNames & "&val=" & sPKFieldValues & "&fldtype=" & sPKFieldTypes & "&olefield=" & server.UrlEncode(fld.name) & """>"
End if
case adVarWChar, adLongVarWChar 'Text, Memo
if lMaxShowLen > 0 then
'If max # of chars is specified
sVal = left(rs(fld.name), lMaxShowLen)
else
sVal = rs(fld.name)
end if
sVal = CheckData(sVal)
sVal = MakeURL(sVal)
if (bEncodeHTML) and (len(sVal) > 0)then
response.write """" & server.htmlencode(sVal) & """"
else
response.write """" & sVal & """"
end if
case else
response.write """" & CheckData(rs(fld.name)) & """"
end select
' response.write csvchar
end if
next
response.write ";" '& VbCrLf
rs.movenext
iRecCount = iRecCount + 1
loop
CloseRS
%>