Stig Johansen wrote:
> On Dec 20, 2:26 pm, "Erik Ginnerskov" <
erik.ginners...@live.dk> wrote:
>> Jow. Men præcis hvor skal der så indsættes et array af nøgleord og
>> hvordan skal det formateres?
>
> Det skal jeg nok beskrive (når jeg kommer til et ordentligt
> interface) ;-)
I'M BACK!! :-)
Jeg skulle lige søge i 'arkiverne', og komme til min foretrukne VM.
Jeg har samlet hele 'skidtet' til et enkelt dokument, så man blot kan gemme
og afprøve (kode til sidst).
Jeg har klippet de forskellige js-filer ind i selve html'et, og forsøgt at
adskille det med kommentarer.
'Principskitsen' er:
1) Events.js - bruges globalt og alle steder hvor der behandles events.
2) Highlight.js - bruges alle steder hvor der ønskes highlight.
Disse 2 filer ligger for sig selv, da de er generelle.
Den sidste, og lokale er så storm.monitor.highlight.js, som angiver _data_
for netop dette dokument.
Denne fil (eller 'funktion' ) består af 2 dele.
1) definition af ord og klasser (klasser til css'et).
Dvs. denne her:
addarray(jsarray,['pubs','database']);
fortæller at ordet 'pubs' skal have tilføjet en class='database', hvor
præsentationen af class='database' angives i css'et.
2) funktionen 'do_highlight' er i princippet ikke nødvendig men IE har
åbenbart den kedelige egenskab at lave om på whitespace i en pre ved
udlæsning og genindlæsning.
I princippet betyder det ikke så meget, men netop denne side var lavet til
at man kan copy/paste 'koden' ind i Query Analyzer.
'koden' er et (T)SQL statement, og skal være 100% _præcist_, så det er
årsagen til min warning om IE, da forkert 'kode' til en database kan have
uoverskuelige konsekvenser.
Da man formentlig ikke ønsker highligt på _alt_, kaldes highligt til en
given id:
if (confirmed) highlight_ids(['triggercode'],jsarray);
Kig efter 'triggercode' i dokumentet.
Har man flere afsnit, eller id'er, der skal highlightes, kaldes den blot med
de forskellige id'er
*********************************************
* Samlet kode hvor JS er i selve dokumentet
*********************************************
(PS: Jeg bliver squ nødt til at sige det: Det virker _naturligvis_ i min
'gamle' Konqueror :-))
<%@ LANGUAGE = VBScript%><%Option Explicit%><%
Dim pagetitle
pagetitle = "W.O.P.R STORM monitor - MS SQLServer - Creation of the
triggers"
%><!--#include virtual="/include_files/doctype.inc.asp"-->
<style type="text/css">
.database {color:yellowgreen;}
.trigger {color:green;}
.table {color:blue;}
.field {color:red;}
.keyfield {color:Magenta;}
.textholder {white-space: pre; display:none; }
#iewarning {color:red; white-space: pre; display:none; font-size:
12pt;}
</style>
<!--script type="text/javascript"
src="/javascript/jsrepository.asp?f=events,highlight"></script-->
<script type="text/javascript"> <!-- events.js kan udskilles i fil -->
function addEvents (eventobject,eventtype,eventfunction) {
if ( eventobject.addEventListener ) {
eventobject.addEventListener (eventtype,eventfunction,false) ;
} else if( eventobject.attachEvent ) {
eventobject.attachEvent ('on'+eventtype,eventfunction) ;
} else {
eventobject['on' + eventtype] = eventfunction ;
}
}
function removeEvents (eventobject,eventtype,eventfunction) {
if ( eventobject.removeEventListener ) {
eventobject.removeEventListener (eventtype,eventfunction,false) ;
} else if( eventobject.detachEvent ) {
eventobject.detachEvent ('on'+eventtype,eventfunction) ;
} else {
eventobject['on' + eventtype] = null ;
}
}
function eventsource(e) {
var src ;
if (window.event) {
src = window.event.srcElement; // For IE
} else {
src = e.target; // For Firefox
}
if ( src.nodeName == '#text') src = src.parentNode ;
return src ;
}
function keynbr(e) {
e = e || window.event;
return e.keyCode || e.which ;
}
function mousecoords(ev) {
var e = ev || window.event ;
if(e.pageX || e.pageY){
return {x:e.pageX,y:e.pageY };
} else {
return {x:e.clientX,y:e.clientY} ;
}
}
function getXY(element) {
var top = 0;
var left = 0;
while (element.offsetParent){
top += element.offsetTop;
left += element.offsetLeft;
element = element.offsetParent;
}
top += element.offsetTop;
left += element.offsetLeft;
return {x:left,y:top} ;
}
function scroll () {
if (document.all) {
var iebody=(document.compatMode && document.compatMode !=
"BackCompat")? document.documentElement : document.body
var X = iebody.scrollLeft ;
var Y = iebody.scrollTop ;
} else {
var X = window.pageXOffset ;
var Y = window.pageYOffset ;
}
return {x:X,y:Y} ;
}
</script>
<script type="text/javascript"> <!-- highlight.js kan udskilles i fil -->
function replaceWord(instring, rfrom, rto) {
var rstring = instring;
var i = instring.indexOf(rfrom);
if (i > -1) {
var charbefore = instring.charAt(i-1) ;
var charafter = instring.charAt(i+rfrom.length) ;
var isword = (' -/()[].,"\x27*%=&|+<>\r\n'.indexOf(charbefore) > -1 &&
' -/()[].,"\x27*%=&|+<>\r\n'.indexOf(charafter) > -1 ) ;
if (isword) {
rstring = instring.substring(0, i);
rstring += rto + replaceWord(instring.substring(i +
rfrom.length),rfrom,rto);
} else {
rstring = instring.substring(0,i+rfrom.length) +
replaceWord(instring.substring(i + rfrom.length),rfrom,rto);
}
}
return rstring;
}
function addarray(to_array,item) {
to_array[to_array.length] = item ;
}
function highlight_ids(idarray,wordarray) {
for (var i = 0 ; i< idarray.length ; i++ ) {
var string = document.getElementById(idarray[i]).innerHTML ;
for (var w=0; w<wordarray.length;w++){
string = replaceWord(string, wordarray[w][0],'<span class="'+
wordarray[w][1]+'">'+wordarray[w][0]+'</span>') ;
}
document.getElementById(idarray[i]).innerHTML = string ;
}
}
</script>
<!--script type="text/javascript"
src="storm.monitor.highlight.js"></script-->
<script type="text/javascript"> <!-- storm.monitor.highlight.js kan
udskilles i fil -->
var jsarray = new Array ;
addarray(jsarray,['pubs','database']);
addarray(jsarray,['tr_storm','trigger']);
addarray(jsarray,['pub_info','table']);
addarray(jsarray,['pr_info','field']);
addarray(jsarray,['pub_id','keyfield']);
function do_highlight () {
var confirmed = true ;
if (document.all) {
confirmed = confirm('WARNING!\r\nYou are using some kind of Internet
Explorer.\r\nIf you continue here, the text will be highlighted to better
read it\r\n*BUT* the line formatting will be lost.\r\nPress OK to get
*Readability* and Cancel to get *Copy/Paste* ability.\r\n\r\n*FULL*
functionality can be obtained by installing, and using Firefox.')
if (confirmed) {
var s = document.getElementById('triggercode').innerHTML ;
s = s.replace(/\n/g,'<br/>');
document.getElementById('triggercode').innerHTML = s ;
document.getElementById('iewarning').style.display='block';
}
}
if (confirmed) highlight_ids(['triggercode'],jsarray);
}
addEvents (window,'load',do_highlight);
</script>
</head>
<body>
<!--#include virtual="/include_files/body.start.inc.asp"-->
<h3><u>Creation of the trigger.</u></h3>
<div class="ppre">Requires <a href="mssql.create.udf.asp" title="Creating
the helper function">Helper function</a> and <a
href="mssql.create.table.asp" title="Create the monitor table">monitor
table</a>
This is a sample code of creating a trigger on the demo database <span
class="database">pubs</span> which is included with MS SQLServer.
To customize it to your database, you need to change the various entities.
The legends used in this example are:
<span class="database">database</span>, which in this example is <span
class="database">pubs</span>
The statement, use <span class="database">pubs</span>, however should be
removed.
It is written to illustrate the database which is used.
Creation of a <span class="trigger">trigger</span> requires the first
statement to be CREATE <span class="trigger">trigger</span>, so connect to
the right <span class="database">database</span> and comment out this line.
Comment by preceeding --
<span class="trigger">trigger</span>, which in this example is <span
class="trigger">tr_storm</span>
A trigger name must be unique to a database, so you may use your own
mnemonics.
Could be tr_ followed by the tablename.
<span class="table">table</span>, which in this example is <span
class="table">pub_info</span>
Replace <span class="table">pub_info</span> to the tablename on which you
would establish monitoring.
<span class="field">field</span>, which in this example is <span
class="field">pr_info</span>
Replace <span class="field">pr_info</span> to the fieldname you want to
monitor.
Multiple fields can be monitored, in which case you can repeat the block
from
-- ******* Start field block ************
to
-- ******* End field block, repeat for each field in table ************
<span class="keyfield">keyfield</span>, which in this example is <span
class="keyfield">pub_id</span>
This is the name of the field which identifies each record.
In the pubs example it is pub_id, which is a unique key on the pub_info
table.
Notice it is a Varchar, and if you use some kind of integer/identity you
need to change the definitions of @cpub_id.
<span id="iewarning">Warning!<br/>The line and space formatting may be
corrupted due to a bug in Internet Explorer.<br/>Copy paste may not work as
expected.</span>
The code to create the trigger copy/pasting in Query Analyzer ( or similar
tool ):
<pre><code id="triggercode">-- This is an example of a trigger on the
pub_info table in the pubs database checking the pr_info field logging the
pub_id key value
use pubs
CREATE TRIGGER tr_storm ON pub_info
FOR INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON
DECLARE @cpr_info INT, @cpub_id VARCHAR (20) -- pub_id is the key/identifier
field
DECLARE inserted_cursor CURSOR LOCAL FOR
SELECT I.pub_id,dbo.CrC32(P.pr_info)
FROM INSERTED I
INNER JOIN pub_info P
ON I.pub_id=P.pub_id
OPEN inserted_cursor
FETCH NEXT FROM inserted_cursor INTO @cpub_id,@cpr_info
WHILE @@FETCH_STATUS = 0
BEGIN
-- ******* Start field block ************
IF NOT Exists(SELECT id FROM StormMonitor WHERE Tablename = 'pub_info'
AND ColoumName = 'pr_info' AND KeyField = CAST(@cpub_id AS Varchar) ) BEGIN
insert into stormmonitor VALUES(CAST('pub_info' AS
Varbinary),CAST('pr_info' AS
Varbinary),CONVERT(float,GetDate()),0,@cpr_info,CAST(@cpub_id AS
Varbinary))
END ELSE BEGIN
UPDATE StormMonitor SET
LastCrc=CurrentCrc,CurrentCrc=@cpr_info,LastRun=CONVERT(float,GetDate())
WHERE Tablename = 'pub_info' AND ColoumName = 'pr_info' AND KeyField =
@cpub_id AND CurrentCrc <> @cpr_info
END
-- ******* End field block, repeat for each field in table ************
FETCH NEXT FROM inserted_cursor INTO @cpub_id,@cpr_info
END
CLOSE inserted_cursor
DEALLOCATE inserted_cursor
</code></pre>
<div class="textholder" id="database">The actual database where the trigger
should be installed. In this example we use pubs, but you must change it to
the actual database you are installing on.</div>
<div class="textholder" id="trigger">The trigger name. Ir it unique in the
database, so you might consider some semanthics where the tablename is part
of it. tr_storm_pub_info could be a guess.</div>
<div class="textholder" id="table">The name of the table on which you will
monitor changes.</div>
<div class="textholder" id="field">The field name inside the table where
changes will be recorded.</div>
<div class="textholder" id="keyfield">Key field, which identifies the very
record inside the table. It is used to identify the entry to backtrace
changes. </div>
</div>
</body>
</html>
*****************************
* End of dokument
*****************************