MongoDB Tutorial 3: Drop database

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MongoDB Production Standalone
UniServer 6-Carbo.

Create a database and user

Introduction

Previous page covered creating a database this page covers a windows script to delete databases.

This component is a sub-set of the previous script.

Complete script

<?php

//-- Constants ----------------------------------------------------------------

define('IDC_USER_DATABASE2',    4030); // Text box Database name
define('IDC_USER_DROP',         4050); // Button Create new user

define('IDC_USER_LISTBOX2',     4070); // Database list box

// Common variables

$mongo_user_win2;        // Window handle
$user_new_db2;           // List of dbs

//=== Create function =========================================================
function drop_database_window($parent){
  global $mongo_user_win2;
  global $user_new_db2;

// Check server running
if(!mongodb_running()){
 $str  = "";
 $str .= "This item requires access to a running server.\n";
 $str .= "Please start MonGoDB server and run\n";
 $str .= "this item again.";
 wb_message_box(NULL, $str,"Server not running", WBC_INFO); 
 return; // Give up
}

//=== 1) Create main window ---------------------------------------------------

$mongo_user_win2 = wb_create_window($parent, ModalDialog, "MongoDB Drop Databse", WBC_CENTER, WBC_CENTER, 294, 230,
  WBC_INVISIBLE | WBC_NOTIFY | WBC_TOP, WBC_HEADERSEL);

//=== 2) Create controls for the main window ----------------------------------

wb_create_control($mongo_user_win2, Frame, 'Database to drop',  5,   5, 275, 55, 0, 0, 0, 0);
wb_create_control($mongo_user_win2, PushButton, 'Drop',        15,  25,  50, 25, IDC_USER_DROP, 0, 0, 0);
wb_create_control($mongo_user_win2, EditBox, '',               75,  25, 195, 20, IDC_USER_DATABASE2, 0, 0, 0);

wb_create_control($mongo_user_win2, Frame, 'Select a database', 5,  65, 275, 125, 0, 0, 0, 0);
$list2 = wb_create_control($mongo_user_win2, ListView, 'Head1',  20,  85, 250, 95, IDC_USER_LISTBOX2, 0x00100080, 0, 0);

//=== INIT ====================
//--- Set up listview ---

  // Set column titles and widths
  wb_set_text($list2, array(
    array("List of Databases", 200),
  ));

  $user_new_db2 = list_dbs_auth();        // Get list of dbs

  // Create rows and columns
  wb_create_items($list2,$user_new_db2);   // Fill list

//--- End Set up listview ---

//=== END INIT ================   

//=== 3) Assign handler function to the main window  --------------------------
wb_set_handler($mongo_user_win2, "process_drop_database");
  wb_set_image($mongo_user_win2, getcwd()."/images/utray.ico");  // Add logo   
  wb_set_visible($mongo_user_win2, true);               // Show window         

//=== 5) Enter application loop -----------------------------------------------
//wb_main_loop();                                  

}//==================================================== End Create function ===

//=== 4) Handler Function -----------------------------------------------------
function process_drop_database($window, $id, $ctrl=0, $lparam1=0, $lparam2=0){
  global $user_new_db2;

  switch($id) { 

  //=== DROP DATABASE =========================================================
  case  IDC_USER_DROP: 

  $db    = wb_get_text(wb_get_control($window,  IDC_USER_DATABASE2));// Get db

 // Validate - db name user may have typed in name 
 if (!(preg_match("/^[A-Za-z0-9_]+$/", $db))) {
  $str  = "";
  $str .= "If name is empty please enter a name\n";
  $str .= "Only alphanumeric characters allowed";
  wb_message_box($window, $str,"DB Name", WBC_INFO); 
  break;
 }

 // Validate - db name user may have typed in name 
 if ($db  == 'admin') {
  $str  = "";
  $str .= "The Admin database is not alloed\n";
  $str .= "to be deleted";
  wb_message_box($window, $str,"DB Admin", WBC_INFO); 

  // Refresh database list
  $user_new_db2 = list_dbs_auth();        // Get list of dbs
  $temp = $user_new_db2;                                               // Save array        
  wb_delete_items (wb_get_control($window, IDC_USER_LISTBOX2),NULL);   // Delete all list view entries
  wb_create_items (wb_get_control($window, IDC_USER_LISTBOX2),$temp);  // Add array

  wb_set_text(wb_get_control($window, IDC_USER_DATABASE2),""); // Reset Set DB

  break;
 }

 // Drop datbase
 drop_db_auth($db);  

 // Refresh database list
 $user_new_db2 = list_dbs_auth();        // Get list of dbs
 $temp = $user_new_db2;                                               // Save array        
 wb_delete_items (wb_get_control($window, IDC_USER_LISTBOX2),NULL);   // Delete all list view entries
 wb_create_items (wb_get_control($window, IDC_USER_LISTBOX2),$temp);  // Add array

 wb_set_text(wb_get_control($window, IDC_USER_DATABASE2),""); // Reset Set DB

   break;
  //=================================================== END DROP DATABASE ====


  //=== Item selected from list ==============================================
  case IDC_USER_LISTBOX2:                   // A row was selected from db list

  $sel = wb_get_selected($ctrl);               // Get selection
  $sel = $sel ? implode(", ", $sel) : "none";  // Set selected value 

  if($sel =="none"){
    wb_set_text(wb_get_control($window, IDC_USER_DATABASE2),""); // Reset Set DB
  }
  else{
    $selected_index = $sel;                 // Save Array index
    $contents = wb_get_text($ctrl);         // Get row selected Returns an array of arrays
    $first_array = $contents[0];            // Select first array (which is selected row)
    $db_selected_name = $first_array[0];    // Select first column (which is db)
    $db_selected_name = trim($db_selected_name); // Clean

    wb_set_text(wb_get_control($window, IDC_USER_DATABASE2),$db_selected_name); // Set DB
  }
  break;
  //============================================== Item selected from list ===

   case IDCLOSE:                          // Constant IDCLOSE (8) predefined 
    wb_destroy_window($window);           // Destroy the window
   break; 
  }
}
//==================================================== End Handler Function ===
?>


Top

Test

Edit file z_mongo\mongodb_1\control\mongo_db.php Add the following to the include section:

include_once "window_drop_database.inc.php"; // Window drop database

Change the following section as shown:

   //=== Display help info ====================================================
   case ID_HELP_BUTTON:          // Button 

      drop_database_window($window);
//      create_database_window($window);
//    create_port_change_window($window);
//    wb_exec('Notepad',INFO_TXT);

   break; 
   //================================================ END Display help info ===

Run

  • Run Run_mongo_db.bat
  • Click "Help and Information" button
  • Result shown on right

Top

Summary

That completes this pop-up interestingly it servers as a temple for deleting a user covered on next page.

Top