File: demo/demo.js

Recommend this page to a friend!
  Classes of Alexey Znaev   XBSDB   demo/demo.js   Download  
File: demo/demo.js
Role: Example script
Content type: text/plain
Description: Demo script
Class: XBSDB
Manipulate arrays with an SQL-like language
Author: By
Last change: changed access
Date: 14 years ago
Size: 21,739 bytes
 

Contents

Class file image Download
/////////////////////////////////////////////////////////////////////////// // // XBSDB - Cross-Browser JavaScript Database library // Copyright (C) 2010 Alexey A.Znayev // // This file is part of XBSDB demo. // // Alexey A.Znayev, znaeff@mail.ru // /////////////////////////////////////////////////////////////////////////// //global variable containing database, initiated in function 'InitDemoPage()' var DB1; //global variables containing current table related info var sTableName; var sTableDescription; var aTableStructureDescription; var sTableQueryFieldsValue; var sTableQueryWhereValue; var sTableQueryOrderByValue; var sTableQueryLimitValue; var sTableDump2Load; //functions function InitDemoPage(sLocale){ var t0 = new Date(); DB1 = new XBSDB(sLocale); if(DB1.sResultCode != 'OK'){ alert(DB1.ResultText() + "\nCurrent locale is '" + DB1.GetLocale() + "'"); } document.getElementById('query_fields').value = ''; document.getElementById('query_where').value = ''; document.getElementById('query_order_by').value = ''; document.getElementById('query_limit').value = ''; MakeTableActionActive(-1); var t1 = new Date(); var sResultTime = (t1.getTime()-t0.getTime())/1000 + ' sec.'; ShowResultInfo(sResultTime,'Database created','','',''); } function ButtonTableLoadClick(sTableName){ if(DB1.IsTable(sTableName) && !confirm('Table ' + sTableName + " exists\nConfirm to overload")){ return; } switch(sTableName){ case 'table1' : sTableDump2Load = sTable1Dump; break; case 'table2': sTableDump2Load = sTable2Dump; break; case 'table3': sTableDump2Load = sTable3Dump; break; default : return; } MakeDatabaseChanged('load', sTableName); } function ButtonTableDropClick(sTableName){ MakeDatabaseChanged('drop', sTableName); } function ButtonTableDumpClick(sTableName){ MakeDatabaseChanged('dump', sTableName); } function MakeDatabaseChanged(sTableAction,sTableName){ var vResult, sResultOper = "Table '" + sTableName + "' ", sResultHeader = '', sResultBody = '', sResultAlert = '', sDone; var t0 = new Date(); switch(sTableAction){ case 'load' : sDone = 'loaded'; sResultHeader = '<tr><th class="r">String to load "' + sTableName + '" (length = ' + sTableDump2Load.length + ')</th></tr>'; sResultBody = '<tr><td class="r"><textarea cols="100" rows="10" readonly="1">' + sTableDump2Load + '</textarea></td></tr>'; vResult = DB1.LoadTable(sTableName,sTableDump2Load); sTableDump2Load = ''; break; case 'drop' : sDone = 'dropped'; vResult = DB1.DropTable(sTableName); break; case 'dump' : sDone = 'dumped'; vResult = DB1.DumpTable(sTableName,'string'); if(typeof vResult == 'string'){ sResultHeader = '<tr><th class="r">Dump of "' + sTableName + '" (length = ' + vResult.length + ')</th></tr>'; sResultBody = '<tr><td class="r"><textarea cols="100" rows="10" readonly="1">' + vResult + '</textarea></td></tr>'; } break; } var t1 = new Date(); var sResultTime = (t1.getTime()-t0.getTime())/1000 + ' sec.'; var sTableNewName = ''; if(vResult){ ShowResultInfo(sResultTime,sResultOper + sDone,sResultHeader,sResultBody,sResultAlert); switch(sTableAction){ case 'load' : document.getElementById('drop_' + sTableName).disabled = false; document.getElementById('dump_' + sTableName).disabled = false; sTableNewName = sTableName; break; case 'drop': document.getElementById('drop_' + sTableName).disabled = true; document.getElementById('dump_' + sTableName).disabled = true; break; } }else{ sResultAlert = DB1.ResultText(); ShowResultInfo(sResultTime,sResultOper + "not " + sDone,sResultHeader,sResultBody,sResultAlert); return; } var aTables = DB1.ShowTables(); var sSelectedTable = ''; if(Object.prototype.toString.call(aTables) === '[object Array]'){ document.getElementById('tables_number').innerHTML = 'Tables in database <B>' + aTables.length + '</B>'; var oTableSelect = document.getElementById('table_select'); if(aTables.length > 0){ oTableSelect.disabled = false; oTableSelect.options.length = 0; if(sTableNewName != ''){ sSelectedTable = sTableNewName; } for(var i=0; i<aTables.length; i++){ if(sSelectedTable == ''){ sSelectedTable = (aTables[i] == sTableName) ? sTableName : ''; } oTableSelect.options[i] = new Option(aTables[i],aTables[i],false,(aTables[i] == sSelectedTable) ? true : false); } if(sSelectedTable == ''){ oTableSelect.selectedIndex = 0; sSelectedTable = aTables[0]; } }else{ oTableSelect.disabled = true; oTableSelect.options.length = 0; oTableSelect.options[0] = new Option('no tables available','no tables available',false,false); } MakeTableChanged(sSelectedTable); } } function MakeTableChanged(sTableNewName){ switch(sTableNewName){ case 'table1' : sTableName = sTable1Name; sTableDescription = sTable1Description; aTableStructureDescription = aTable1StructureDescription; sTableQueryFieldsValue = sTable1QueryFieldsValue; sTableQueryWhereValue = sTable1QueryWhereValue; sTableQueryOrderByValue = sTable1QueryOrderByValue; sTableQueryLimitValue = sTable1QueryLimitValue; document.getElementById('table_descr').innerHTML = ' - ' + sTableDescription; ShowTableStructurePlace(true); ShowTableIndexPlace(true); ShowQueryPlace(true); break; case 'table2': sTableName = sTable2Name; sTableDescription = sTable2Description; aTableStructureDescription = aTable2StructureDescription; sTableQueryFieldsValue = sTable2QueryFieldsValue; sTableQueryWhereValue = sTable2QueryWhereValue; sTableQueryOrderByValue = sTable2QueryOrderByValue; sTableQueryLimitValue = sTable2QueryLimitValue; document.getElementById('table_descr').innerHTML = ' - ' + sTableDescription; ShowTableStructurePlace(true); ShowTableIndexPlace(true); ShowQueryPlace(true); break; case 'table3': sTableName = sTable3Name; sTableDescription = sTable3Description; aTableStructureDescription = aTable3StructureDescription; sTableQueryFieldsValue = sTable3QueryFieldsValue; sTableQueryWhereValue = sTable3QueryWhereValue; sTableQueryOrderByValue = sTable3QueryOrderByValue; sTableQueryLimitValue = sTable3QueryLimitValue; document.getElementById('table_descr').innerHTML = ' - ' + sTableDescription; ShowTableStructurePlace(true); ShowTableIndexPlace(true); ShowQueryPlace(true); break; default : sTableName = ''; sTableDescription = ''; aTableStructureDescription = []; sTableQueryFieldsValue = ''; sTableQueryWhereValue = ''; sTableQueryOrderByValue = ''; sTableQueryLimitValue = ''; document.getElementById('table_descr').innerHTML = ''; ShowTableStructurePlace(false); ShowTableIndexPlace(false); ShowQueryPlace(false); } } function ShowTableStructurePlace(bFlag){ var sStructureBody = ''; if(bFlag){ var aStructure = DB1.ShowFields(sTableName); if(Object.prototype.toString.call(aStructure) === '[object Array]'){ sStructureBody = '<TABLE width="100%" class="r">'; sStructureBody += '<TR><TH width="10%" class="r">Field</TH><TH width="10%" class="r">Type</TH><TH class="r">Description</TH></TR>'; for(var i=0; i<aStructure.length; i++){ sStructureBody += '<TR><TD class="r">' + aStructure[i][0] + '</TD><TD class="r">' + aStructure[i][1] + '</TD><TD class="r">' + aTableStructureDescription[i] + '</TD></TR>'; } sStructureBody += '</TABLE>'; } } document.getElementById('structure_table_place').innerHTML = sStructureBody; } function ShowTableIndexPlace(bFlag){ if(bFlag){ document.getElementById('index_add').disabled = false; document.getElementById('button_index_add').disabled = false; var aIndexIDs = DB1.ShowIndexes(sTableName); if(aIndexIDs){ document.getElementById('indexes_number').innerHTML = 'Indexes ' + aIndexIDs.length; var sIndexesBody = '<TABLE width="100%">'; // var sIndexesBody = '<TABLE width="100%" class="r">'; for(var i = 0; i < aIndexIDs.length; i++){ sIndexesBody += '<TR><TD><INPUT id="index_del_' + i + '" type="text" size="70" readonly value="' + aIndexIDs[i] + '"></TD><TD><INPUT type="button" value="Del" onclick="ButtonDelClick(' + i +");\"></TD></TR>\n"; // sIndexesBody += '<TR><TD width="80%" class="r" id="index_del_' + i + '">'+ aIndexIDs[i] + '</TD><TD width="20%" class="r"><INPUT type="button" value="Del" onclick="ButtonDelClick(' + i +");\"></TD></TR>\n"; } sIndexesBody += '</TABLE>'; document.getElementById('indexes_table_place').innerHTML = sIndexesBody; document.getElementById('index_add').value = ''; }else{ alert(DB1.ResultText()); } }else{ document.getElementById('indexes_number').innerHTML = 'Indexes'; document.getElementById('indexes_table_place').innerHTML = ''; document.getElementById('index_add').disabled = true; document.getElementById('button_index_add').disabled = true; } } function ShowQueryPlace(bFlag){ document.getElementById('query_group_by').value = ''; if(bFlag){ document.getElementById('query_fields').value = sTableQueryFieldsValue; document.getElementById('query_where').value = sTableQueryWhereValue; document.getElementById('query_order_by').value = sTableQueryOrderByValue; document.getElementById('query_limit').value = sTableQueryLimitValue; MakeTableActionActive(0); }else{ document.getElementById('query_fields').value = ''; document.getElementById('query_where').value = ''; document.getElementById('query_order_by').value = ''; document.getElementById('query_limit').value = ''; MakeTableActionActive(-1); } } function MakeTableActionActive(nActionIndex){ if(nActionIndex >= 0 && nActionIndex <= 4){ document.getElementById('button_go').disabled = false; document.getElementById('action_select').disabled = false; document.getElementById('action_select').selectedIndex = nActionIndex; switch(nActionIndex){ case 0 : //Select document.getElementById('query_fields').disabled = false; document.getElementById('query_values').disabled = true; document.getElementById('query_where').disabled = false; document.getElementById('query_order_by').disabled = false; document.getElementById('query_order_by_desc').disabled = false; document.getElementById('query_group_by').disabled = false; document.getElementById('query_limit').disabled = true; break; case 1 : //SelectToRecordSet document.getElementById('query_fields').disabled = false; document.getElementById('query_values').disabled = true; document.getElementById('query_where').disabled = false; document.getElementById('query_order_by').disabled = false; document.getElementById('query_order_by_desc').disabled = false; document.getElementById('query_group_by').disabled = false; document.getElementById('query_limit').disabled = false; break; case 2 : //Update document.getElementById('query_fields').disabled = false; document.getElementById('query_values').disabled = false document.getElementById('query_where').disabled = false; document.getElementById('query_order_by').disabled = true; document.getElementById('query_order_by_desc').disabled = true; document.getElementById('query_group_by').disabled = true; document.getElementById('query_limit').disabled = true; break; case 3 : //Insert document.getElementById('query_fields').disabled = false; document.getElementById('query_values').disabled = false; document.getElementById('query_where').disabled = true; document.getElementById('query_order_by').disabled = true; document.getElementById('query_order_by_desc').disabled = true; document.getElementById('query_group_by').disabled = true; document.getElementById('query_limit').disabled = true; break; case 4 : //Delete document.getElementById('query_fields').disabled = true; document.getElementById('query_values').disabled = true; document.getElementById('query_where').disabled = false; document.getElementById('query_order_by').disabled = true; document.getElementById('query_order_by_desc').disabled = true; document.getElementById('query_group_by').disabled = true; document.getElementById('query_limit').disabled = true; break; } }else{ document.getElementById('button_go').disabled = true; document.getElementById('action_select').disabled = true; document.getElementById('query_fields').disabled = true; document.getElementById('query_values').disabled = true; document.getElementById('query_where').disabled = true; document.getElementById('query_order_by').disabled = true; document.getElementById('query_order_by_desc').disabled = true; document.getElementById('query_group_by').disabled = true; document.getElementById('query_limit').disabled = true; } } function ButtonGoClick(){ var aTableCols = DB1.ShowFields(sTableName); var nActionIndex = document.getElementById('action_select').selectedIndex; var vSelectResult, vUpdateResult, bInsertResult, vDeleteResult; var sFieldNames = document.getElementById('query_fields').value.replace(/^\s+/,'').replace(/\s+$/,'').replace(/\s+/g,' '); var sFieldValues = document.getElementById('query_values').value.replace(/^\s+/,'').replace(/\s+$/,'').replace(/\s+/g,' '); var sWhere = document.getElementById('query_where').value.replace(/^\s+/,'').replace(/\s+$/,''); var sOrderBy = document.getElementById('query_order_by').value.replace(/^\s+/,'').replace(/\s+$/,''); var sGroupBy = document.getElementById('query_group_by').value.replace(/^\s+/,'').replace(/\s+$/,''); var nLimit = parseInt(document.getElementById('query_limit').value.replace(/^\s+/,'').replace(/\s+$/,'')); if(isNaN(nLimit) || nLimit < 0) nLimit = 0; document.getElementById('query_fields').value = sFieldNames; document.getElementById('query_values').value = sFieldValues; document.getElementById('query_where').value = sWhere; document.getElementById('query_order_by').value = sOrderBy; document.getElementById('query_group_by').value = sGroupBy; document.getElementById('query_limit').value = nLimit; var aFieldNames = sFieldNames.length > 0 ? sFieldNames.split(/\s+/) : null; var aFieldValues = sFieldValues.length > 0 ? sFieldValues.split(/\s+/) : null; var nNumber; if(aFieldValues){ if(aFieldNames){ for(var i=0; i<aFieldValues.length; i++){ for(var j=0; j<aTableCols.length; j++){ if(aTableCols[j][0] == aFieldNames[i] && aTableCols[j][1] == 'number'){ nNumber = Number(aFieldValues[i]); if(!isNaN(nNumber)) aFieldValues[i] = nNumber; } } } }else{ aFieldValues.length = aTableCols.length; for(var i=0; i<aFieldValues.length; i++){ if(aTableCols[i][1] == 'number'){ nNumber = Number(aFieldValues[i]); if(!isNaN(nNumber)) aFieldValues[i] = nNumber; } } } } var t0 = new Date(); var sResultTime = '', sResultOper = '', sResultHeader = '', sResultBody = '', sResultAlert = ''; window.DB1_RecordSet1 = []; switch(nActionIndex){ case 0 : //Select vSelectResult = DB1.Select(sTableName, aFieldNames, sWhere, sOrderBy, document.getElementById('query_order_by_desc').checked, sGroupBy ); break; case 1 : //SelectToRecordSet vSelectResult = DB1.SelectToRecordSet(sTableName, sWhere, sOrderBy, document.getElementById('query_order_by_desc').checked, sGroupBy ); break; case 2 : //Update vUpdateResult = DB1.Update(sTableName,aFieldValues,aFieldNames,sWhere); break; case 3 : //Insert bInsertResult = DB1.InsertOne(sTableName,aFieldValues,aFieldNames); break; case 4 : //Delete vDeleteResult = DB1.Delete(sTableName,sWhere); break; } var t1 = new Date(); sResultTime = (t1.getTime()-t0.getTime())/1000 + ' sec.'; switch(nActionIndex){ case 0 : //Select if(Object.prototype.toString.call(vSelectResult) === '[object Array]'){ sResultOper = 'Selected ' + vSelectResult.length + ' record(s)'; sResultHeader = '<tr>'; if(aFieldNames){ for(var i = 0; i < aFieldNames.length; i++){ sResultHeader += '<th class="r">' + aFieldNames[i] + '</th>'; } }else{ for(var i = 0; i < aTableCols.length; i++){ sResultHeader += '<th class="r">' + aTableCols[i][0] + '</th>'; } } sResultHeader += "</tr>\n"; sResultBody = DB1.Rows2String(vSelectResult,'<tr>',"</tr>\n",'<td class="r">','</td>','&nbsp;'); }else{ sResultAlert = DB1.ResultText(); } break; case 1 : //SelectToRecordSet if(Object.prototype.toString.call(vSelectResult) === '[object Array]'){ window.DB1_RecordSet1 = vSelectResult; sResultOper = 'Selected ' + vSelectResult.length + ' record(s)'; sResultHeader = '<tr><th class="r">Records</th></tr>'; sResultBody = "<tr><td class=\"r\">\n"; var nPages = 1; if(isNaN(nLimit) || nLimit <= 0 || nLimit > vSelectResult.length){ nLimit = vSelectResult.length; }else{ nPages = Math.floor((vSelectResult.length - 1) / nLimit) + 1; } var nRecStart, nRecEnd; for(var i = 0; i < nPages; i++){ nRecStart = i * nLimit; nRecEnd = (i + 1) * nLimit - 1; if(nRecEnd > vSelectResult.length - 1){ nRecEnd = vSelectResult.length - 1; } sResultBody += '&nbsp;<span id="span_pager_' + i + '" class="pager_link" onclick="ShowPagerInfo(' + nRecStart + ',' + nLimit + '); this.className=' + "'pager_link_a'" + '; return false;">' + nRecStart + '-' + nRecEnd + "</span>&nbsp;\n"; } sResultBody += "</td></tr>\n"; }else{ sResultAlert = DB1.ResultText(); } break; case 2 : //Update if(typeof vUpdateResult == 'number'){ sResultOper = 'Updated ' + vUpdateResult + ' record(s)'; }else{ sResultAlert = DB1.ResultText(); } break; case 3 : //Insert if(bInsertResult){ sResultOper = 'Inserted 1 record'; }else{ sResultAlert = DB1.ResultText(); } break; case 4 : //Delete if(typeof vDeleteResult == 'number'){ sResultOper = 'Deleted ' + vDeleteResult + ' record(s)'; }else{ sResultAlert = DB1.ResultText(); } break; } ShowResultInfo(sResultTime,sResultOper,sResultHeader,sResultBody,sResultAlert); } function ButtonAddClick(){ var bRes = DB1.CreateIndex(sTableName,document.getElementById('index_add').value); if(!bRes){ alert(DB1.ResultText()); return; } ShowTableIndexPlace(true); } function ButtonDelClick(nIndexNumber){ var bRes = DB1.DropIndex(sTableName,document.getElementById('index_del_' + nIndexNumber).value); // var bRes = DB1.DropIndex(sTableName,document.getElementById('index_del_' + nIndexNumber).innerHTML); if(!bRes){ alert(DB1.ResultText()); return; } ShowTableIndexPlace(true); } function ShowResultInfo(sTime,sNum,sHeader,sBody,sAlert){ document.getElementById('result_time').innerHTML = sTime; document.getElementById('result_text').innerHTML = sNum; if(sBody != ''){ document.getElementById('result_info_place').innerHTML = '<table class="r">' + sHeader + sBody + '</table>'; }else{ document.getElementById('result_info_place').innerHTML = ''; } document.getElementById('pager_info_place').innerHTML = ''; if(sAlert.length > 0){ alert(sAlert); } } function ShowPagerInfo(nOffset,nLimit){ var aTableCols = DB1.ShowFields(sTableName); var sFieldNames = document.getElementById('query_fields').value.replace(/^\s+/,'').replace(/\s+$/,''); var aFieldNames = sFieldNames.length > 0 ? sFieldNames.split(/\s+/) : null; var sPagerHeader = '', sPagerBody = '', sPagerAlert = ''; document.getElementById('query_fields').value = sFieldNames; for(var i = 0; i < 65535; i++){ var element = document.getElementById('span_pager_' + i); if(element){ element.className = 'pager_link'; }else{ break; } } var vSelectResult = DB1.SelectFromRecordSet(sTableName,window.DB1_RecordSet1,aFieldNames,nOffset,nLimit); if(Object.prototype.toString.call(vSelectResult) === '[object Array]'){ sPagerHeader = '<tr>'; if(aFieldNames){ for(var i = 0; i < aFieldNames.length; i++){ sPagerHeader += '<th class="r">' + aFieldNames[i] + '</th>'; } }else{ for(var i = 0; i < aTableCols.length; i++){ sPagerHeader += '<th class="r">' + aTableCols[i][0] + '</th>'; } } sPagerHeader += "</tr>\n"; sPagerBody = DB1.Rows2String(vSelectResult,'<tr>',"</tr>\n",'<td class="r">','</td>',' '); }else{ sPagerAlert = DB1.ResultText(); } if(sPagerBody != ''){ document.getElementById('pager_info_place').innerHTML = '<table class="r">' + sPagerHeader + sPagerBody + '</table>'; }else{ document.getElementById('pager_info_place').innerHTML = ''; } if(sPagerAlert.length > 0){ alert(sPagerAlert); } }