home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / ServerBeh-CF-SimpRS.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  18.1 KB  |  656 lines

  1. // Copyright 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3.  
  4. // *************** GLOBALS VARS *****************
  5.  
  6. var helpDoc = MM.HELP_ssCFSimpleRecordset;
  7.  
  8.  
  9. var RECORDSET_SBOBJ;  // SBRecordset argument to the command.
  10. var CMD_FILENAME_ADV; // Command filename for advanced recordset dialog.
  11.  
  12. var _RecordsetName = new TextField("Recordset.htm", "RecordsetName");
  13. var _UserName = new TextField("Recordset.htm", "UserName");
  14. var _Password = new TextField("Recordset.htm", "Password");
  15. var _ConnectionName = new CFDataSourceMenu("Recordset.htm", "ConnectionName");
  16. var _TableName = new ConnectionTableMenu("Recordset.htm", "TableName");
  17. var _ColumnType = new RadioGroup("Recordset.htm", "ColumnType");
  18. var _ColumnList = new ConnectionColumnMenu("Recordset.htm", "ColumnList", "", false);
  19. var _FilterColumn = new ConnectionColumnMenu("Recordset.htm", "FilterColumn", "", true);
  20. var _FilterOperator = new ListMenu("Recordset.htm", "FilterOperator");
  21. var _FilterParameter = new ListMenu("Recordset.htm", "FilterParameter");
  22. var _FilterParameterValue = new TextField("Recordset.htm", "FilterParameterValue");
  23. var _SortColumn = new ConnectionColumnMenu("Recordset.htm", "SortColumn", "", true);
  24. var _SortDirection = new ListMenu("Recordset.htm", "SortDirection");
  25.  
  26.  
  27. // ******************* API **********************
  28.  
  29. //--------------------------------------------------------------------
  30. // FUNCTION:
  31. //   commandButtons
  32. //
  33. // DESCRIPTION:
  34. //   Returns the list of buttons which should appear on the right hand
  35. //   side of the dialog
  36. //
  37. // ARGUMENTS:
  38. //   none
  39. //
  40. // RETURNS:
  41. //   Array - pairs of button name and function call
  42. //--------------------------------------------------------------------
  43.  
  44. function commandButtons()
  45. {
  46.   return new Array(MM.BTN_OK,       "clickedOK()", 
  47.                    MM.BTN_Cancel,   "clickedCancel()", 
  48.                    MM.BTN_Test,     "clickedTest()", 
  49.                    MM.BTN_Advanced, "clickedAdvanced()", 
  50.                    MM.BTN_Help,     "displayHelp()"); 
  51. }
  52.  
  53.  
  54. //--------------------------------------------------------------------
  55. // FUNCTION:
  56. //   clickedOK
  57. //
  58. // DESCRIPTION:
  59. //   This function is called when the user clicks OK
  60. //
  61. // ARGUMENTS:
  62. //   none
  63. //
  64. // RETURNS:
  65. //   nothing
  66. //--------------------------------------------------------------------
  67.  
  68. function clickedOK()
  69. {
  70.   // Update RECORDSET_SBOBJ from the UI.
  71.   updateSBRecordsetObject();
  72.  
  73.   var fileName = window.document.URL;
  74.   recordsetDialog.onClickOK(window, RECORDSET_SBOBJ, fileName.substring(fileName.lastIndexOf('/')+1));
  75. }
  76.  
  77.  
  78. //--------------------------------------------------------------------
  79. // FUNCTION:
  80. //   clickedCancel
  81. //
  82. // DESCRIPTION:
  83. //   This function is called when CANCEL is clicked
  84. //
  85. // ARGUMENTS:
  86. //   none
  87. //
  88. // RETURNS:
  89. //   nothing
  90. //--------------------------------------------------------------------
  91.  
  92. function clickedCancel()
  93. {
  94.   recordsetDialog.onClickCancel(window);
  95. }
  96.  
  97.  
  98. //--------------------------------------------------------------------
  99. // FUNCTION:
  100. //   clickedTest
  101. //
  102. // DESCRIPTION:
  103. //   This function is called when the user clicks the TEST button
  104. //
  105. // ARGUMENTS:
  106. //   none
  107. //
  108. // RETURNS:
  109. //   nothing
  110. //--------------------------------------------------------------------
  111.  
  112. function clickedTest()
  113. {
  114.   // Update RECORDSET_SBOBJ from the UI.
  115.   updateSBRecordsetObject();
  116.  
  117.   if (!RECORDSET_SBOBJ.checkData(true))
  118.   {
  119.     alert(RECORDSET_SBOBJ.getErrorMessage());
  120.     return;
  121.   }
  122.  
  123.   var theSQL = RECORDSET_SBOBJ.getSQLForTest();
  124.   
  125.   if (theSQL)
  126.   {
  127.     MMDB.showResultset(dwscripts.getCFDataSourceName(RECORDSET_SBOBJ.getConnectionName()), theSQL);
  128.   }
  129. }
  130.  
  131.  
  132. //--------------------------------------------------------------------
  133. // FUNCTION:
  134. //   clickedAdvanced
  135. //
  136. // DESCRIPTION:
  137. //   This function is called when the user clicks the ADVANCED button
  138. //
  139. // ARGUMENTS:
  140. //   none
  141. //
  142. // RETURNS:
  143. //   nothing
  144. //--------------------------------------------------------------------
  145.  
  146. function clickedAdvanced()
  147. {
  148.   // Update RECORDSET_SBOBJ from the UI.
  149.   updateSBRecordsetObject();
  150.  
  151.   recordsetDialog.onClickSwitchUI(window, recordsetDialog.UI_ACTION_SWITCH_ADV, 
  152.                                   RECORDSET_SBOBJ, CMD_FILENAME_ADV);
  153. }
  154.  
  155.  
  156. //--------------------------------------------------------------------
  157. // FUNCTION:
  158. //   displayHelp
  159. //
  160. // DESCRIPTION:
  161. //   This function is called when the user clicks the HELP button
  162. //
  163. // ARGUMENTS:
  164. //   none
  165. //
  166. // RETURNS:
  167. //   nothing
  168. //--------------------------------------------------------------------
  169.  
  170. function displayHelp()
  171. {
  172.   dwscripts.displayDWHelp(helpDoc);
  173. }
  174.  
  175.  
  176. // ***************** LOCAL FUNCTIONS  ******************
  177.  
  178. //--------------------------------------------------------------------
  179. // FUNCTION:
  180. //   initializeUI
  181. //
  182. // DESCRIPTION:
  183. //   This function is called in the onLoad event.  It is responsible
  184. //   for initializing the UI.  If we are inserting a recordset, this
  185. //   is a matter of populating the connection drop down.
  186. //
  187. //   If we are modifying a recordset, this is a matter of inspecting
  188. //   the recordset tag and setting all the form elements.
  189. //
  190. // ARGUMENTS:
  191. //   none
  192. //
  193. // RETURNS:
  194. //   nothing
  195. //--------------------------------------------------------------------
  196.  
  197. function initializeUI()
  198. {
  199.   var setConnectionSuccess = true;  // return value from connectionmenu's initializeUI() 
  200.   var sqlObject = null;
  201.  
  202.   var args = dwscripts.getCommandArguments();
  203.   RECORDSET_SBOBJ = args[0];
  204.   CMD_FILENAME_ADV = args[1];
  205.   
  206.   // Get the UI elements
  207.   _RecordsetName.initializeUI();
  208.  
  209.   setConnectionSuccess = _ConnectionName.initializeUI();
  210.  
  211.   _UserName.initializeUI(); 
  212.   _Password.initializeUI(); 
  213.   _TableName.initializeUI();
  214.   _ColumnType.initializeUI();
  215.   _ColumnList.initializeUI();
  216.   _FilterColumn.initializeUI();
  217.   _FilterOperator.initializeUI();
  218.   _FilterParameter.initializeUI(dwscripts.getParameterTypeArray(), dwscripts.getParameterTypeArray());
  219.   _FilterParameterValue.initializeUI();
  220.   _SortColumn.initializeUI();
  221.   _SortDirection.initializeUI();
  222.  
  223.   var rsName = RECORDSET_SBOBJ.getRecordsetName();
  224.   if (!rsName)
  225.   {
  226.     rsName = RECORDSET_SBOBJ.getUniqueRecordsetName();
  227.   }
  228.   _RecordsetName.setValue(rsName);
  229.   
  230.   var connectionName = RECORDSET_SBOBJ.getConnectionName();
  231.   var userName = RECORDSET_SBOBJ.getUserName();
  232.   var password = RECORDSET_SBOBJ.getPassword();
  233.   
  234.   if (connectionName)
  235.   {
  236.     _ConnectionName.pickName(RECORDSET_SBOBJ.getConnectionName());
  237.   }
  238.  
  239.   if (userName)
  240.   {
  241.     _UserName.setValue(userName); 
  242.   }
  243.   
  244.   if (password)
  245.   {
  246.     _Password.setValue(password); 
  247.   }
  248.   
  249.   var sqlParams = new Array();
  250.   var sqlString = RECORDSET_SBOBJ.getDatabaseCall(sqlParams);
  251.   if (sqlString)
  252.   {
  253.     sqlObject = new SQLStatement(sqlString);
  254.   }
  255.   else
  256.   {
  257.     sqlObject = new SQLStatement("");
  258.   }
  259.  
  260.   if (sqlObject.getType() == SQLStatement.STMT_TYPE_SELECT)
  261.   {
  262.     // select the correct table
  263.     var tableNames = sqlObject.getTableNames();
  264.  
  265.     if (tableNames && tableNames.length > 0)
  266.     {
  267.       _TableName.pickValue(tableNames[0]);
  268.     }
  269.  
  270.     // selet the correct columns
  271.     var columnNames = sqlObject.getColumnNames();
  272.     if (columnNames.length == 1 && columnNames[0] == "*")
  273.     {
  274.       _ColumnType.pickValue("all");
  275.     }
  276.     else
  277.     {
  278.       _ColumnType.pickValue("some");
  279.       _ColumnList.setDisabled(false);
  280.       // multiply select the given columns
  281.       _ColumnList.pickValues(columnNames);
  282.     }
  283.  
  284.     // select the correct filter column
  285.     var filterInfo = RECORDSET_SBOBJ.getSimpleWhereInfo(sqlObject);
  286.     if (filterInfo != null)
  287.     {
  288.       if (filterInfo.lval)
  289.       {
  290.         _FilterColumn.pickValue(filterInfo.lval);
  291.       }
  292.       if (filterInfo.operator)
  293.       {
  294.         _FilterOperator.pickValue(filterInfo.operator);
  295.       }
  296.       if (filterInfo.rval)
  297.       {
  298.         var param = new Object();
  299.         // If there is not sql parameter, this is probably an entered parameter
  300.         //   value. Use the filter rval as the runtime code.
  301.         if (sqlParams.length > 0)
  302.         {
  303.           param = sqlParams[0];
  304.         }
  305.         else
  306.         {
  307.           param.varName = filterInfo.rval; 
  308.           param.varDefault = "";
  309.         }
  310.  
  311.         // The parameter name is the 'runtime' value expected in dwscripts.getParameterTypeFromCode.
  312.         var paramInfo = dwscripts.getParameterTypeFromCode(param.varName);
  313.  
  314.         _FilterParameter.pickValue(paramInfo.paramType);
  315.         _FilterParameterValue.setValue(paramInfo.paramName);
  316.       }
  317.     }
  318.     else
  319.     {
  320.       updateUI("FilterColumn", "onChange");
  321.     }
  322.  
  323.     var orderByInfo = RECORDSET_SBOBJ.getSimpleOrderByInfo(sqlObject);
  324.     if (orderByInfo != null)
  325.     {
  326.       if (orderByInfo.column)
  327.       {
  328.         _SortColumn.pickValue(orderByInfo.column);
  329.       }
  330.       if (orderByInfo.direction)
  331.       {
  332.         _SortDirection.pickValue(orderByInfo.direction);
  333.       }
  334.     }
  335.     else
  336.     {
  337.       updateUI("SortColumn", "onChange");
  338.     }
  339.   }
  340.   else 
  341.   {
  342.     updateUI("ColumnType", "onChange");
  343.     updateUI("FilterColumn", "onChange");
  344.     updateUI("SortColumn", "onChange");
  345.   }
  346.  
  347.   elts = document.forms[0].elements;
  348.   if (elts && elts.length)
  349.   {
  350.     elts[0].focus();
  351.     elts[0].select();
  352.   }
  353. }
  354.  
  355.  
  356. //--------------------------------------------------------------------
  357. // FUNCTION:
  358. //   updateUI
  359. //
  360. // DESCRIPTION:
  361. //   This function is called by the UI controls to handle UI updates
  362. //
  363. // ARGUMENTS:
  364. //   control - string - the name of the control sending the event
  365. //   event - string - the event which is being sent
  366. //
  367. // RETURNS:
  368. //   nothing
  369. //--------------------------------------------------------------------
  370.  
  371. function updateUI(control, event)
  372. {
  373.   if (control == "RecordsetName")
  374.   {
  375.   }
  376.   else if (control == "ConnectionName")
  377.   {
  378.     // check the connection, and get a username and password if needed
  379.     _ConnectionName.ensureLogin(RECORDSET_SBOBJ.getUserName(),
  380.                                 RECORDSET_SBOBJ.getPassword());
  381.  
  382.     if (event == "onChange")
  383.     {
  384.       // set the username and password for this data source
  385.       _UserName.setValue(_ConnectionName.getUsername());
  386.       _Password.setValue(_ConnectionName.getPassword());
  387.     }
  388.     
  389.     _TableName.updateUI(_ConnectionName, event);
  390.   }
  391.   else if (control == "TableName")
  392.   {
  393.     _ColumnList.updateUI(_TableName, event);
  394.     _FilterColumn.updateUI(_TableName, event);
  395.     _SortColumn.updateUI(_TableName, event);
  396.     _ColumnType.pickValue("all");
  397.   }
  398.   else if (control == "ColumnType")
  399.   {
  400.     var value = _ColumnType.getValue();
  401.     if (value == "all")
  402.     {
  403.       _ColumnList.setDisabled(true, true);
  404.     }
  405.     else
  406.     {
  407.       _ColumnList.setDisabled(false);
  408.     }
  409.   }
  410.   else if (control == "FilterColumn")
  411.   {
  412.     if (_FilterColumn.getValue() == "")
  413.     {
  414.       _FilterOperator.setDisabled(true);
  415.       _FilterParameter.setDisabled(true);
  416.       _FilterParameterValue.setValue("");
  417.       _FilterParameterValue.setDisabled(true);
  418.     }
  419.     else
  420.     {
  421.       _FilterOperator.setDisabled(false);
  422.       _FilterParameter.setDisabled(false);
  423.       _FilterParameterValue.setDisabled(false);
  424.       _FilterParameterValue.setValue(_FilterColumn.getValue());
  425.     }
  426.   }
  427.   else if (control == "SortColumn")
  428.   {
  429.     if (_SortColumn.getValue() == "")
  430.     {
  431.       _SortDirection.setDisabled(true);
  432.     }
  433.     else
  434.     {
  435.       _SortDirection.setDisabled(false);
  436.     }
  437.   }
  438.   else
  439.   {
  440.     //alert("updateUI(" + control + ", " + event + ")");
  441.   }
  442. }
  443.  
  444.  
  445. //--------------------------------------------------------------------
  446. // FUNCTION:
  447. //   updateSBRecordsetObject
  448. //
  449. // DESCRIPTION:
  450. //   Collects information from the UI and sets the SBRecordset object
  451. //
  452. // ARGUMENTS:
  453. //   none
  454. //
  455. // RETURNS:
  456. //   boolean - true if successful, false otherwise
  457. //--------------------------------------------------------------------
  458.  
  459. function updateSBRecordsetObject()
  460. {
  461.   RECORDSET_SBOBJ.setRecordsetName(_RecordsetName.getValue());
  462.   RECORDSET_SBOBJ.setConnectionName(_ConnectionName.getName());
  463.   RECORDSET_SBOBJ.setPassword(_Password.getValue());
  464.   RECORDSET_SBOBJ.setUserName(_UserName.getValue());  
  465.  
  466.   var sqlObject = new SQLStatement("");
  467.  
  468.   sqlObject.addFrom(_TableName.getValue());
  469.  
  470.   var colType = _ColumnType.getValue();
  471.   if (colType != "all")
  472.   {
  473.     var columnNames = _ColumnList.getValues();
  474.     for (var i=0; i < columnNames.length; i++)
  475.     {
  476.       sqlObject.addSelect(_TableName.getValue(), columnNames[i], true); // dont include table name
  477.     }
  478.   }
  479.  
  480.   var sqlParams = new Array();
  481.  
  482.   if (_FilterColumn.getValue() != "")
  483.   {
  484.     var paramInfo = dwscripts.getParameterCodeFromType(_FilterParameter.getValue(), 
  485.                                                        _FilterParameterValue.getValue(),
  486.                                                        "");
  487.  
  488.     // If there is no parameter, enter the filter parameter value directly to the
  489.     //   where clause.
  490.     if (paramInfo != null)
  491.     {
  492.       var paramName = dwscripts.getSQLStringForDBColumnType(paramInfo.runtimeVal, _FilterColumn.getType());
  493.   
  494.       RECORDSET_SBOBJ.addSimpleWhere(sqlObject, _FilterColumn.getValue(), _FilterOperator.getValue(), paramName);
  495.   
  496.       var sqlParam = new Object();
  497.       sqlParam.varName = paramInfo.nameVal;
  498.       sqlParam.varDefault = paramInfo.defaultVal;
  499.       sqlParam.valuePrompt = _FilterParameter.getValue() + ": " + _FilterParameterValue.getValue();
  500.   
  501.       sqlParams.push(sqlParam);
  502.     }
  503.     else
  504.     {
  505.       var paramName = dwscripts.getSQLStringForDBColumnType(_FilterParameterValue.getValue(), _FilterColumn.getType());
  506.       RECORDSET_SBOBJ.addSimpleWhere(sqlObject, _FilterColumn.getValue(), _FilterOperator.getValue(), paramName);      
  507.     }
  508.   }
  509.  
  510.   if (_SortColumn.getValue() != "")
  511.   {
  512.     sqlObject.addOrderBy(_TableName.getValue(), _SortColumn.getValue(), _SortDirection.getValue(), true); // dont include table prefix
  513.   }
  514.   
  515.   RECORDSET_SBOBJ.setDatabaseCall(sqlObject.getStatement(), sqlParams);
  516. }
  517.  
  518.  
  519. //--------------------------------------------------------------------
  520. // FUNCTION:
  521. //   canDisplayRecordset
  522. //
  523. // DESCRIPTION:
  524. //   Returns true if the given recordset can be displayed in this
  525. //   recordset dialog. Called by the recordsetDialog to determine which
  526. //   dialog to display.
  527. //
  528. // ARGUMENTS: 
  529. //   sbRecordset - SBRecordset. the recordset to check.
  530. //
  531. // RETURNS:
  532. //   boolean - true if can display the recordset.
  533. //--------------------------------------------------------------------
  534.  
  535. function canDisplayRecordset(sbRecordset) 
  536. {
  537.   var retVal = false;
  538.  
  539.   if (!sbRecordset)
  540.   {
  541.     retVal = true;
  542.   }
  543.  
  544.   if (!retVal)
  545.   {
  546.     var sqlParams = new Array();
  547.     var sqlString = sbRecordset.getDatabaseCall(sqlParams);
  548.     if (sqlString == "" && sqlParams.length == 0)
  549.     {
  550.       retVal = true;
  551.       return retVal;
  552.     }
  553.     
  554.     var sqlObject = new SQLStatement(sqlString);
  555.     
  556.     // check for select statement.
  557.     if (sqlObject.getType() == SQLStatement.STMT_TYPE_SELECT)
  558.     {
  559.       // check that only one table is specified
  560.       var tableNames = sqlObject.getTableNames();
  561.       if (tableNames.length == 1)
  562.       {
  563.         // check that the table exists for the connection.
  564.         var connectionName = sbRecordset.getConnectionName();
  565.         var bTableExistsForConn = true;
  566.         if (connectionName)
  567.         {  
  568.           bTableExistsForConn = false;
  569.           
  570.           var connName = dwscripts.getCFDataSourceName(connectionName);
  571.           var tableObjects = MMDB.getTables(connName);
  572.           
  573.           var viewObjects = MMDB.getViews(connName);
  574.           for (var i=0; i < viewObjects.length; i++)
  575.           {
  576.             // make a view object look like a table object
  577.             viewObjects[i].table = viewObjects[i].view;
  578.             // add it to the list of table objects
  579.             tableObjects.push(viewObjects[i]);
  580.           }
  581.           
  582.           if (tableObjects.length > 0)
  583.           {
  584.             for (var i=0; i < tableObjects.length; i++)
  585.             {
  586.               var tableObj = tableObjects[i];
  587.       
  588.               var schema = dwscripts.trim(tableObj.schema);
  589.               if (schema.length == 0)
  590.               {
  591.                 schema = dwscripts.trim(tableObj.catalog);
  592.               }
  593.               if (schema.length > 0)
  594.               {
  595.                 schema += ".";
  596.               }
  597.       
  598.               var connTableName = String(schema + tableObj.table).toUpperCase(); 
  599.               if (connTableName.indexOf(tableNames[0].toUpperCase()) != -1)
  600.               {
  601.                 bTableExistsForConn = true;
  602.                 break;
  603.               }
  604.             }
  605.           }
  606.         }
  607.         if (bTableExistsForConn)
  608.         {
  609.           // check that the columns are simple values
  610.           var columns = sqlObject.getColumnNames();
  611.           var bSimpleNames = true;
  612.           for (var i=0; i < columns.length; i++)
  613.           { 
  614.             if (!sbRecordset.isSimpleColumnName(columns[i]))
  615.             {
  616.               bSimpleNames = false;
  617.               break;
  618.             }
  619.           }
  620.           if (bSimpleNames)
  621.           {
  622.             // check for no filter column (no parameter), or filter is single param, or filter
  623.             //   is single entered value (no parameter).
  624.             var whereInfo = sbRecordset.getSimpleWhereInfo(sqlObject);
  625.   
  626.             if (   whereInfo != null
  627.                 && (   (!whereInfo.rval && sqlParams.length == 0)
  628.                     || (sqlParams.length == 0) 
  629.                     || (   whereInfo.rval && sqlParams.length == 1 
  630.                         && whereInfo.rval == sqlParams[0].varName
  631.                        )
  632.                    )
  633.                )
  634.             {
  635.               // check for a single order by column
  636.               var orderByInfo = sbRecordset.getSimpleOrderByInfo(sqlObject);
  637.   
  638.               if (orderByInfo != null)
  639.               {
  640.                 // now check that the other clauses are empty
  641.                 if (!sqlObject.getHaving() && !sqlObject.getGroupBy())
  642.                 {
  643.                   retVal = true;
  644.                 }
  645.               }
  646.             }
  647.           }
  648.         }
  649.       }
  650.     }
  651.   }
  652.  
  653.   return retVal;
  654. }
  655.  
  656.