home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 July / 07_02.iso / software / mdmx / files / DreamweaverMXInstaller.exe / Disk1 / data1.cab / Configuration_En / Commands / ServerBeh-CF-SimpRS.js < prev    next >
Encoding:
JavaScript  |  2002-05-01  |  18.0 KB  |  655 lines

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