Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MySql.Data.MySqlClient;
  6. using System.Windows.Forms;
  7. using System.Data;
  8.  
  9. namespace SebBot_3
  10. {
  11.     static class SQL
  12.     {
  13.         // *************************************************************************************
  14.         // MAIN REF
  15.         public static Frm_MAIN MAIN;
  16.  
  17.         // *************************************************************************************
  18.         // public
  19.  
  20.         static public string ServerIp
  21.         {
  22.             get
  23.             {
  24.                 return m_ServerIp;
  25.             }
  26.             set
  27.             {
  28.                 m_ServerIp = value;
  29.             }
  30.         }
  31.  
  32.         static public bool DB_Available
  33.         {
  34.             get
  35.             {
  36.                 return m_DB_Available;
  37.             }
  38.             set
  39.             {
  40.                 m_DB_Available = value;
  41.             }
  42.         }
  43.  
  44.         // *************************************************************************************
  45.         // private
  46.         static private MySqlConnection _myConnection;
  47.         static private string m_ServerIp;
  48.         static private bool m_DB_Available;
  49.  
  50.         // *************************************************************************************
  51.         // enum
  52.         public enum eEventType
  53.         {
  54.             JOIN = 0,
  55.             PART = 1,
  56.             QUIT = 2,
  57.             KICK = 3,
  58.             BAN = 4,
  59.             PRIVMSG = 10,
  60.             ACTION = 11,
  61.             NOTICE = 12,
  62.             CTCP = 13,
  63.             NICK = 20,
  64.             MODE = 30
  65.         };
  66.  
  67.  
  68.  
  69.         // *************************************************************************************
  70.         // constructor
  71.         static SQL()
  72.         {
  73.  
  74.         }
  75.  
  76.         // *************************************************************************************
  77.         // Connect
  78.         static public void Connect()
  79.         {
  80.             try
  81.             {
  82.                 _myConnection = new MySqlConnection("datasource=" + "192.168.1.250" + ";username=" + "root" + ";password=" + "sl1107" + ";database=" + "db_BOT");
  83.                 _myConnection.Open();
  84.  
  85.                 if (_myConnection.State == System.Data.ConnectionState.Open)
  86.                 {
  87.                     DB_Available = true;
  88.                 }
  89.             }
  90.             catch (Exception ex)
  91.             {
  92.                 DB_Available = false;
  93.                 MessageBox.Show(ex.Message);
  94.             }
  95.         }
  96.  
  97.         static public void Disconnect()
  98.         {
  99.             try
  100.             {
  101.                 _myConnection.Close();
  102.             }
  103.             catch (Exception ex)
  104.             {
  105.                 MessageBox.Show(ex.Message);
  106.             }
  107.         }
  108.  
  109.         // *************************************************************************************
  110.         // Add Event
  111.         static public void AddEvent(eEventType evt,string nick, string ident, string host, string message, string info)
  112.         {
  113.             string listField = string.Empty;
  114.             string listValue = string.Empty;
  115.             try
  116.             {
  117.                 if (_myConnection.State != System.Data.ConnectionState.Open && DB_Available) return;
  118.  
  119.                 if (!string.IsNullOrEmpty(message) && message.Contains("'"))
  120.                 {
  121.                     message = message.Replace("'", " ");
  122.                 }
  123.  
  124.                 listField = "dt_event";
  125.                 listField += ",s_event_type";
  126.                 if (!string.IsNullOrEmpty(nick)) listField += ",s_nick";
  127.                 if (!string.IsNullOrEmpty(ident)) listField += ",s_ident";
  128.                 if (!string.IsNullOrEmpty(host)) listField += ",s_host";
  129.                 if (!string.IsNullOrEmpty(message)) listField += ",s_message";
  130.                 if (!string.IsNullOrEmpty(info)) listField += ",s_infos";
  131.  
  132.                 listValue = "'" + GetDateTimeNow() + "'";
  133.                 listValue += ",'" + evt.ToString() + "'";
  134.                 if (!string.IsNullOrEmpty(nick)) listValue += ",'" + nick + "'";
  135.                 if (!string.IsNullOrEmpty(ident)) listValue += ",'" + ident + "'";
  136.                 if (!string.IsNullOrEmpty(host)) listValue += ",'" + host + "'";
  137.                 if (!string.IsNullOrEmpty(message))  listValue += ",'" + message + "'";
  138.                 if (!string.IsNullOrEmpty(info)) listValue += ",'" + info + "'";
  139.  
  140.                 string Query_Sql = "INSERT INTO " + "tb_events" + " (" + listField + ") VALUES (" + listValue + ")";
  141.                 MySqlCommand myCmd = new MySqlCommand(Query_Sql);
  142.  
  143.                 myCmd.Connection = _myConnection;
  144.  
  145.                 myCmd.ExecuteNonQuery();
  146.  
  147.             }
  148.             catch (Exception ex)
  149.             {
  150.                 //MessageBox.Show(myex.Message);
  151.                 //DB_Available = false;
  152.             }
  153.         }
  154.  
  155.  
  156.         // *************************************************************************************
  157.         // DB Info
  158.         static public string GetDB_Size()
  159.         {
  160.             string s = string.Empty;
  161.             int nb = 0;
  162.             ulong tbl = 0;
  163.             ulong tblmx = 0;
  164.             float tbsz = 0.0f;
  165.             float tbprc = 0.0f;
  166.  
  167.             try
  168.             {
  169.                 if (_myConnection.State != System.Data.ConnectionState.Open) return "Base de donnée non-connectée.";
  170.  
  171.                 MySqlCommand myCmd = new MySqlCommand("SELECT COUNT(*) FROM tb_events", _myConnection);
  172.                 MySqlDataReader myReader = myCmd.ExecuteReader();
  173.                 while (myReader.Read() != false)
  174.                 {
  175.                     nb = myReader.GetInt32(0);
  176.                 }
  177.                 myReader.Close();
  178.  
  179.  
  180.                 myCmd = new MySqlCommand("SELECT data_length, Max_data_length FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tb_events'", _myConnection);
  181.                 myReader = myCmd.ExecuteReader();
  182.                 while (myReader.Read() != false)
  183.                 {
  184.                     //tbl = myReader.GetInt32(0);
  185.                     if (myReader.FieldCount == 2)
  186.                     {
  187.                         tbl = myReader.GetUInt64("data_length");
  188.                         tblmx = myReader.GetUInt64("Max_data_length");
  189.                     }
  190.                 }
  191.  
  192.                 myReader.Close();
  193.  
  194.                 tbsz =((float)tbl / 1000000.0f);
  195.  
  196.                 tbprc = (tbsz / 50000.0f) * 100.0f;
  197.  
  198.                 s = IRCCodes.K(8,1) + " " + nb.ToString() + IRCCodes.K(0,1) + " enregistrements dans la base de donnée, sa taille est de"+IRCCodes.K(8,1) + " " + tbsz.ToString("0.00") + " Mb"+ IRCCodes.K(0,1)+  " ("+ IRCCodes.K(8,1) + " " +tbprc.ToString("0.000") + " %" + IRCCodes.K(0,1) + " )" ;
  199.  
  200.                 return s;
  201.             }
  202.             catch (Exception ex)
  203.             {
  204.                 MessageBox.Show(ex.Message);
  205.                 DB_Available = false;
  206.                 return "data n/a";
  207.             }
  208.         }
  209.  
  210.         static public void GetDB_Seen(string for_nick, string arg)
  211.         {
  212.             try
  213.             {
  214.                 if (_myConnection.State != System.Data.ConnectionState.Open)
  215.                 {
  216.                     IRC_Connections.MASTER_Notice_To_Nick(for_nick, "Base de donnée non-connectée.");
  217.                     return;
  218.                 }
  219.  
  220.                 string sarg;
  221.                 string query;
  222.                 if (arg.Contains("."))
  223.                 {
  224.                     query = "Select * from tb_events WHERE s_host = '" + arg + "' ORDER BY dt_event DESC";
  225.                 }
  226.                 else
  227.                 {
  228.                     query = "Select * from tb_events WHERE s_nick = '" + arg + "'";
  229.                 }
  230.  
  231.                 string ret_seen = "je ne sais pas.";
  232.  
  233.                 MySqlDataAdapter adapter = new MySqlDataAdapter(query, _myConnection);
  234.                 DataSet ds = new DataSet("RES");
  235.                 adapter.Fill(ds);
  236.  
  237.  
  238.                 string s_host;
  239.                 string s_nick;
  240.                 string s_dt_evt;
  241.                 string s_type_evt;
  242.  
  243.                 int nb = ds.Tables[0].Rows.Count;
  244.                 if (nb == 0)
  245.                 {
  246.                     IRC_Connections.MASTER_Notice_To_Nick(for_nick, "0 Infos sur " + arg);
  247.                     return;
  248.                 }
  249.                 if (nb > 3)
  250.                 {
  251.                     nb = 3;
  252.                 }
  253.                 for (int i = 0; i < nb; i++)
  254.                 {
  255.                     s_host = ds.Tables[0].Rows[i]["s_host"].ToString();
  256.                     s_nick = ds.Tables[0].Rows[i]["s_nick"].ToString();
  257.                     s_dt_evt = ds.Tables[0].Rows[i]["dt_event"].ToString();
  258.                     s_type_evt = ds.Tables[0].Rows[i]["s_event_type"].ToString();
  259.  
  260.                     ret_seen = IRCCodes.K(0, 1) + "(" + IRCCodes.K(8, 1) + s_nick;
  261.                     ret_seen += IRCCodes.K(0, 1) + ") a été vue le " + IRCCodes.K(8, 1) + " " + s_dt_evt;
  262.                     ret_seen += IRCCodes.K(0, 1) + " avec le host " + IRCCodes.K(8, 1) + " " + s_host;
  263.                     ret_seen += IRCCodes.K(0, 1) + " et cetait un (" + IRCCodes.K(11, 1) + " " + s_type_evt + IRCCodes.K(0, 1) + " )";
  264.                     IRC_Connections.MASTER_Notice_To_Nick(for_nick, ret_seen);
  265.                 }
  266.  
  267.             }
  268.             catch (Exception ex)
  269.             {
  270.                 MessageBox.Show(ex.Message);
  271.                 IRC_Connections.MASTER_Notice_To_Nick(for_nick, "Infos non-disponible");
  272.             }
  273.         }
  274.  
  275.         // *************************************************************************************
  276.         // Get Date Time ( timestamp )
  277.         static public string GetDateTimeNow()
  278.         {
  279.             return DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") + " " + DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00");
  280.         }
  281.        
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement