using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client; // For Oracle
namespace Sql180_Developer
{
public partial class frmComputeStatistics : Form
{
// Variable definition
private String strCommand = null;
private OracleConnection con = null;
private OracleCommand cmd = null;
public frmComputeStatistics()
{
InitializeComponent();
}
private void frmComputeStatistics_Load(object sender, EventArgs e)
{
// getting connection
con = OneTimeVars.Instance().OracleConn;
rbSchema.Checked = true;
cbxOwner.Text = "";
cbxObjName.Text = "";
cbxPartName.Text = "";
}
private void rbDB_CheckedChanged(object sender, EventArgs e)
{
if (rbDB.Checked)
{
groupBox2.Visible = false;
}
else
{
groupBox2.Visible = true;
}
}
private void rbSchema_CheckedChanged(object sender, EventArgs e)
{
if (rbSchema.Checked)
{
cbxObjName.Visible = false;
cbxPartName.Visible = false;
label1.Visible = false;
label2.Visible = false;
}
else
{
cbxObjName.Visible = true;
cbxPartName.Visible = true;
label1.Visible = true;
label2.Visible = true;
}
}
private void cbxOwner_DropDown(object sender, EventArgs e)
{
//binding combo box of users from the DB
if (con.State != ConnectionState.Open)
{
MessageBox.Show("You are not connected", "No Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
OracleCommand cmdUser = new OracleCommand("select username from all_users", con);
OracleDataAdapter daUser = new OracleDataAdapter(cmdUser);
DataSet dsUser = new DataSet();
daUser.Fill(dsUser, "all_users");
cbxOwner.DataSource = dsUser;
cbxOwner.DisplayMember = "all_users.username";
}
}
private void cbxObjName_DropDown(object sender, EventArgs e)
{
String strQuery;
String type;
// if user chose to analyze a table
if (rbTable.Checked)
{
type = "TABLE";
}
else
{
type = "INDEX";
}
//binding combo box of users from the DB
if (con.State != ConnectionState.Open)
{
MessageBox.Show("You are not connected", "No Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (cbxOwner.Text.Equals(""))
{
strQuery = "select object_name from all_objects where object_type = '" + type + "'";
}
else
{
strQuery = "select object_name from all_objects where owner = '" + cbxOwner.Text + "' and object_type = '" + type + "'";
}
OracleCommand cmdObject = new OracleCommand(strQuery, con);
OracleDataAdapter daObject = new OracleDataAdapter(cmdObject);
DataSet dsObject = new DataSet();
daObject.Fill(dsObject, "all_objects");
cbxObjName.DataSource = dsObject;
cbxObjName.DisplayMember = "all_objects.object_name";
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnCompute_Click(object sender, EventArgs e)
{
String IsSample = "FALSE";
String IsCascade = "FALSE";
if (ckSample.Checked)
{
IsSample = "TRUE";
}
if (ckCascade.Checked)
{
IsCascade = "TRUE";
}
strCommand = "begin ";
if (rbDB.Checked) //Compute statistics for all DB
{
// ensuring that the user want to compute statistics on all DB
DialogResult reply = MessageBox.Show("You are about to compute statistics on ALL DB.\nIt can take a long time.\n\nAre you sure ?",
"Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (reply == DialogResult.No)
{
return;
}
strCommand += " DBMS_STATS.GATHER_DATABASE_STATS(" +
"estimate_percent=>" + txtEstimatePcnt.Text + "," +
"block_sample=>" + IsSample + "," +
"method_opt=>'" + cbxOptions.Text + "'," +
"degree=>" + cbxDegree.Text + "," +
"granularity=>'" + cbxGranularity.Text + "'," +
"cascade=>" + IsCascade + "); end;";
}
else if (rbSchema.Checked) //Compute statistics for specified schema
{
// gathering statistics for current schema if not given any...
if (cbxOwner.Text.Equals(""))
{
cbxOwner.Text = OneTimeVars.Instance().getUsername().ToUpper();
}
// ensuring that the user want to compute statistics on SYS or SYSTEM
if (cbxOwner.Text.Equals("SYS") || cbxOwner.Text.Equals("SYSTEM"))
{
DialogResult reply = MessageBox.Show("You are about to compute statistics on " + cbxOwner.Text + " objects.\nIt can take a long time...\n\nAre you sure ?",
"Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (reply == DialogResult.No)
{
return;
}
}
strCommand += " DBMS_STATS.GATHER_SCHEMA_STATS(" +
"ownname=>'" + cbxOwner.Text + "'," +
"estimate_percent=>" + txtEstimatePcnt.Text + "," +
"block_sample=>" + IsSample + "," +
"method_opt=>'" + cbxOptions.Text + "'," +
"degree=>" + cbxDegree.Text + "," +
"granularity=>'" + cbxGranularity.Text + "'," +
"cascade=>" + IsCascade + "); end;";
}
else if (rbTable.Checked) //Compute statistics for specified table
{
strCommand += " DBMS_STATS.GATHER_TABLE_STATS(" +
"ownname=>'" + cbxOwner.Text + "'," +
"tabname=>'" + cbxObjName.Text + "'," +
"estimate_percent=>" + txtEstimatePcnt.Text + "," +
"block_sample=>" + IsSample + "," +
"method_opt=>'" + cbxOptions.Text + "'," +
"degree=>" + cbxDegree.Text + "," +
"granularity=>'" + cbxGranularity.Text + "'," +
"cascade=>" + IsCascade + "); end;";
}
else if (rbIndex.Checked) //Compute statistics for specified index
{
strCommand += " DBMS_STATS.GATHER_INDEX_STATS(" +
"ownname=>'" + cbxOwner.Text + "'," +
"indname=>'" + cbxObjName.Text + "'," +
"estimate_percent=>" + txtEstimatePcnt.Text + "," +
"block_sample=>" + IsSample + "," +
"method_opt=>'" + cbxOptions.Text + "'," +
"degree=>" + cbxDegree.Text + "," +
"granularity=>'" + cbxGranularity.Text + "'," +
"cascade=>" + IsCascade + "); end;";
}
try
{
cmd = new OracleCommand(strCommand, con);
cmd.CommandType = CommandType.Text;
this.Cursor = Cursors.WaitCursor;
cmd.ExecuteNonQuery();
this.Cursor = Cursors.Default;
DialogResult reply = MessageBox.Show("Statistics Computed Sucessfuly. Do you want to compute other statistics ?",
"Statistics Computed", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (reply == DialogResult.No)
{
this.Close();
}
}
catch (Exception ex)
{
this.Cursor = Cursors.Default;
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnInfo_Click(object sender, EventArgs e)
{
String strInfo = "The screen helps you analyze your database and compute statistics\n\n" +
"You can compute statistics in 4 levels :\n" +
"1. Database - compute statistics on ALL DB! (not recommended to do \n" +
" in live production...)\n" +
"2. Schema - compute on all specific schema's objects.\n" +
"3. Table - obvious ...\n" +
"4. Index - obvious ...\n\n" +
"You can set also your computing of statistics to be on sample of the data,\n" +
"cascade statistics, choose a specific degree and choose different methods.";
frmInfo frm = new frmInfo();
frm.Owner = this;
frm.SetText(strInfo);
frm.SetTitle(this.Text);
frm.Show();
}
}
}