//**************************************************************** // icDatabase.js (_ic313.txt) // // Copyright (c) 2006 Clifford L. Collins // All rights are Reserved // // Collins Software // 7710 Janak Drive, Houston Texas // //---------------------------------------------------------------- // No distribution/publication/use of this code is permitted //**************************************************************** function passed(A) { if ((A == null) || (A == undefined)) return false; return true; } //=================================================================== // ic$Database //=================================================================== function ic$Database(id,receiver,database,table,column,Q,dataElement,countElement,navButtons,imgElement,onFormatField,onShowTable,onFail) { this.id = id; this.database = database; this.data = null; this.count = null; this.navButtons = null; this.img = null; if (passed(dataElement)) this.data = dataElement; if (passed(countElement)) this.count = countElement; if (passed(navButtons)) this.navButtons = navButtons if (passed(imgElement)) this.img = imgElement; this.onFormatField = onFormatField; this.onShowTable = onShowTable; this.onFail = onFail; this.isRecordModified = false; this.isTableModified = false; this.tableName = table; this.columnName = column; this.Q = Q; this.receiver = receiver + "?database=" + this.database + '&'; //----------- Methods ----------------- this.makeElements = DB$makeElements; this.makeButtons = DB$makeButtons; this.readTable = DB$readTable; this.readGraphicTable = DB$readGraphicTable; this.getX = DB$getX; this.getY = DB$getY; this.getHref = DB$getHref; this.newRecord = DB$newRecord; this.newPointRecord = DB$newPointRecord; this.resolve = DB$resolve; this.deleteRecord = DB$deleteRecord; this.deleteRecordAll = DB$deleteRecordAll; this.deleteRecord_A = DB$deleteRecord_A; this.isImg = DB$isImg; this.execute = DB$execute; this.saveRecord = DB$saveRecord; this.saveTable = DB$saveTable; this.first = DB$first; this.last = DB$last; this.next = DB$next; this.prev = DB$prev; this.put = DB$put; this.get = DB$get; } //==================================================================== // DB$put //==================================================================== function DB$put(name,value) { if (this.rs.status == 0) return false; try { f = this.rs.Fields.Item(name); f.value = value; return true; } catch (e) { return false } } //==================================================================== // DB$get //==================================================================== function DB$get(name) { var f,value; if (this.rs.status == 0) return ""; try { f = this.rs.Fields.Item(name); value = f.value; if (value == null) value = ""; if (value.length > 1000) value = value.substr(1,400); return value; } catch (e) { return "" } } //==================================================================== // DB$newRecord //==================================================================== function DB$newRecord() { if (this.rs.status == 0) return 0; this.saveRecord(); this.rs.AddNew(); this.makeElements(); return this.rs.RecordCount; } //==================================================================== // DB$newPointRecord //==================================================================== function DB$newPointRecord(x,y,href) { if (this.rs.status == 0) return 0; this.saveRecord(); this.rs.AddNew(); this.put("xmin",x); this.put("ymin",y); this.put("xmax",x); this.put("ymax",y); this.put("x",x); this.put("y",y); this.put("href",href); this.makeElements(); return this.rs.RecordCount; } //==================================================================== // DB$makeElements //==================================================================== function DB$makeElements() { var text; var v,n,xname; var i,add; text = ""; if (this.data != null) this.data.innerHTML = text; if (this.count != null) this.count.innerText = "" this.isRecordModified = false; //------------------------- No Records Found -------------------- if ((! this.rs) || (this.rs.BOF && this.rs.EOF) ) { if (this.onFail != null) text = this.onFail(); if (this.data != null) this.data.innerHTML = text; return false; } def = true; if (this.onFormatField != null) def = false; //-------------------------- Default Table ------------------------- if (def) { text = ""; } for (i = 0; i < this.rs.Fields.Count; i++) { if (this.isImg(this.rs.Fields(i))) continue; v = ""; if (this.rs.Fields(i).value) v = this.rs.Fields(i).value.toString(); if (v == null) v = ""; if (v == undefined) v = ""; while (v.indexOf("'") >= 0) { v = v.replace("'"," "); } while (v.indexOf("\"") >= 0) { v = v.replace("\""," "); } n = this.rs.Fields(i).name; if (def) { xname = n.toUpperCase(); if (xname == "X") continue; if (xname == "Y") continue; if (xname == "ID") continue; if (xname == "XMIN") continue; if (xname == "YMIN") continue; if (xname == "XMAX") continue; if (xname == "YMAX") continue; add = ""; if (xname == 'HREF' && (! ((v == null) || (v == "")) ) ) add = " "; more = " id=\"DB_" + xname + "\""; more = more + " onMouseDown=\"DBUpdate('" + this.id + "')\""; text = text + "" + ""; } else { text = text + this.onFormatField(this.rs.Fields(i),n,v,i+1,this.rs.Fields.Count); } } if (def) text = text + '
" + "" + n + "" + add + "
' //-------------------------------------------------------------------------- if (this.onShowTable != null) { this.onShowTable(text) } else { if (this.data != null) this.data.innerHTML = text;} if (this.count == null) return; if ( this.rs.RecordCount > 0) if (this.count != null) this.count.innerText = this.rs.AbsolutePosition + ' of ' + this.rs.RecordCount; } //==================================================================== // DB$getX //==================================================================== function DB$getX() { var x; try { x = this.rs.Fields('x').value; } catch (e) { x = 0} return x; } //==================================================================== // DB$getHref //==================================================================== function DB$getHref() { var h; try { h = this.rs.Fields('Href').value; } catch (e) { h = "" } return h; } //==================================================================== // DB$getY //==================================================================== function DB$getY() { var y; try { y = this.rs.Fields('y').value; } catch (e) { y = 0 } return y; } //=============================================================== // DB$first //=============================================================== function DB$first() { try { this.saveRecord(); this.rs.MoveFirst(); this.makeElements(); return true; } catch (e) { return false } } function DB$prev() { try { this.saveRecord(); this.rs.MovePrevious(); if (this.rs.BOF) this.rs.MoveFirst(); this.makeElements(); return true; } catch (e) { return false } } function DB$next() { try { this.saveRecord(); this.rs.MoveNext(); if (this.rs.EOF) this.rs.MoveLast(); this.makeElements(); return true; } catch (e) { return false } } function DB$last() { try { this.saveRecord(); this.rs.MoveLast(); this.makeElements(); return true; } catch (e) { return false } } //================================================================ // DB$readTable //================================================================ function DB$readTable(tableName) { var text; this.isModified = false; try { this.table = tableName; this.rs = new ActiveXObject("ADODB.Recordset"); this.xmlDoc = new ActiveXObject("MsXML2.DOMDocument"); this.xmlhttp = new ActiveXObject("MsXML2.XMLhttp"); text = this.receiver + "getRecordset=YES&Table=" + tableName; // window.alert (text); this.xmlhttp.Open("Get",text,false); this.xmlhttp.send(); // window.alert(this.xmlhttp.responseText); this.xmlDoc.loadXML(this.xmlhttp.responseText); //load the returned stream into the dom document this.rs.Open(this.xmlDoc); //load the dom document into the recordset if (this.navButtons != null) this.navButtons.style.display = ''; this.makeElements(); return true; } catch (e) { window.alert(e.description); return false } } //================================================================ // DB$readGraphicTable //================================================================ function DB$readGraphicTable(tableName,xmin,ymin,xmax,ymax) { var text; this.isModified = false; try { this.table = tableName; this.rs = new ActiveXObject("ADODB.Recordset"); this.xmlDoc = new ActiveXObject("MsXML2.DOMDocument"); this.xmlhttp = new ActiveXObject("MsXML2.xmlhttp"); text = this.receiver + "getRecordset=YES&Table=" + tableName + "&Graphic=YES" + "&xmin=" + xmin + "&ymin=" + ymin + "&xmax=" + xmax + "&ymax=" + ymax; this.xmlhttp.Open("Get",text,false); this.xmlhttp.send(); // window.alert(this.xmlhttp.responseText); this.xmlDoc.loadXML(this.xmlhttp.responseText); //load the returned stream into the dom document this.rs.Open(this.xmlDoc); //load the dom document into the recordset if (this.navButton != null) this.navButtons.style.display = ''; this.makeElements(); return true; } catch (e) { window.alert(e.description); return false } } //================================================================ // DB$execute //================================================================ function DB$execute(sql) { var text; if (this.rs) { if (! (this.rs.BOF && this.rs.EOF)) { if (this.rs.status != 0) this.rs.close(); } } this.isModified = false; try { this.table = ""; this.rs = new ActiveXObject("ADODB.Recordset"); this.xmlDoc = new ActiveXObject("MsXml2.DOMDocument"); this.xmlhttp = new ActiveXObject("MsXml2.xmlhttp"); text = this.receiver + "getRecordset=YES&SQL=" + sql; // toClipboard(text); this.xmlhttp.Open("Get",text,false); this.xmlhttp.send(); // window.alert(this.xmlhttp.responseText); strToFile(this.xmlhttp.responseText,'c:/xx.htm'); this.xmlDoc.loadXML(this.xmlhttp.responseText); //load the returned stream into the dom document this.rs.Open(this.xmlDoc); //load the dom document into the recordset if (this.navButtons != null) this.navButtons.style.display = ''; this.makeElements(); return true; } catch (e) { window.alert(e.description); return false } } //================================================================ // DB$saveTable //================================================================ function DB$saveTable() { var text; if (! this.isTableModified) return; if (this.rs.status == 0) return; try { var DBstream = new ActiveXObject("ADODB.Stream"); DBstream.Mode = 3; //read write DBstream.Open(); DBstream.Type = 1; // adTypeBinary this.rs.Save(DBstream,0); //adpersistadtg this.xmlhttp = new ActiveXObject("Msxml2.xmlhttp"); this.xmlhttp.Open("POST",this.receiver + "getRecordset=NO",false); this.xmlhttp.setRequestHeader("Content-Length",DBstream.Size); //set the length of the content this.xmlhttp.send(DBstream.Read(DBstream.Size)); //Send the stream alert(this.xmlhttp.responseText); return true; } catch (e) { window.alert(e.description); return false } } //================================================================ // DB$saveRecord //================================================================ function DB$saveRecord() { if (this.rs.status == 0) return; if (this.rs.RecordCount <= 0) return; if (! this.isRecordModified) return; this.lastMsg = ""; try { for (i = 0; i < this.rs.Fields.Count; i++) { n = this.rs.Fields(i).name; xname = n.toUpperCase(); if (xname == "X") continue; if (xname == "Y") continue; if (xname == "ID") continue; if (xname == "XMIN") continue; if (xname == "YMIN") continue; if (xname == "XMAX") continue; if (xname == "YMAX") continue; this.lastMsg = "failed to update column " + xname; name = "DB_" + xname; obj = eval(name); if (obj == null) continue; text = obj.value; if (text == "") text = null; this.rs.Fields(i).value = text; } this.lastMsg this.rs.Update(); this.isRecordModified = false; this.isTableModified = true; } catch (e) { window.alert(this.lastMsg + '\n' + DBError(e,this.xmlhttp,this.xmlDoc)); throw (e); } } //================================================================ // error //================================================================ function DBError(e,x,d) { var text; text = e.description; return text; } //================================================================ // DBUpdate //================================================================ function DBUpdate(name) { var obj; obj = eval(name); if (obj == null) return; obj.isRecordModified = true; } //================================================================ // DB$resolve //================================================================ function DB$resolve(text) { var temp,name; name = this.get('name'); temp = text.replace('{DB.name}',name); return temp; } //================================================================ // DB$deleteRecord //================================================================ function DB$deleteRecord() { var adAffectCurrent = 1; return this.deleteRecord_A(adAffectCurrent); } //================================================================ // DB$deleteRecordAll //================================================================ function DB$deleteRecordAll() { var adAffectAll = 3; return this.deleteRecord_A(adAffectAll); } //================================================================ // DB$deleteRecord //================================================================ function DB$deleteRecord_A(type) { if (this.rs.status == 0) return false; if (this.rs.RecordCount == 0) return false; try { this.rs.Delete(type); this.next(); this.makeElements(); return true; } catch (e) { window.alert(e.description); return false; } } //================================================================ // DB$makeButtons //================================================================ function DB$makeButtons() { var t,obj; if (this.navButtons == null) { window.alert('DB.makeButtons - Could not find Element: navButtons'); return; } t = ""; // t = t + "
"; t = t + ""; t = t + ""; t = t + "\" STYLE=\"width:20px\" onclick=\"" + this.id + ".next()\">"; t = t + ">\" onclick=\"" + this.id + ".last()\">"; t = t + "    "; t = t + ""; t = t + ""; t = t + ""; // t = t + ""; this.navButtons.innerHTML = t; this.navButtons.style.display = 'none'; } //================================================================ // DB$isImg //================================================================ function DB$isImg(f) { var b,ms,o,c; if (f.name.toUpperCase() != "PICTURE") return false; if (this.img == null) return true; if (f.value == null) return true; try { tr=this.rs.GetString(2); size = f.ActualSize; this.img.src = tr } catch (e) {window.alert(e.description) } return true; }