Advertisement
Guest User

Untitled

a guest
May 12th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.31 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using MySql.Data.MySqlClient;
  5.  
  6. namespace ReleaseMule
  7. {
  8.     public class Mysql
  9.     {
  10.  
  11.  
  12.         #region Constants
  13.  
  14.         public static string host = "HOST";
  15.         public const string user = "USER";
  16.         public const string pass = "PASS";
  17.         public const string DupeDataBase = "JUPE";
  18.         public const string dupetable = "releases";
  19.         public const string nuketable = "nukes";
  20.         public static string connectstring = "Data Source="
  21.             + host + ";UserName="
  22.             + user + ";Password="
  23.             + pass + ";Persist Security Info=yes";
  24.         public static string Dupeconstring = "DataSource="
  25.                     + host + ";UserName="
  26.                     + user + ";Password="
  27.                     + pass + ";Database=" + DupeDataBase + ";Persist Security Info=yes";
  28.  
  29.         public const string showDupeQuery = "SELECT * FROM releases WHERE name LIKE '%";
  30.         public const string showReleasesQuery = "SELECT name from releases";
  31.         public const string showReleasesCountQuery = "SELECT COUNT(name) from releases";
  32.         public const string showReleasesQueryAll = "SELECT name,section,time,date from releases";
  33.         public const string showNukesCountQuery = "SELECT COUNT(name) from nukes";
  34.         public const int releasesfieldscount = 4;
  35.  
  36.         public const string releasesSchema = "(name varchar(512) NOT NULL PRIMARY KEY,date varchar(512),time varchar(255),section varchar(512))";
  37.         public const string nukesSchema = "(name varchar(512) NOT NULL PRIMARY KEY,date varchar(512),time varchar(255),section varchar(512),reason varchar(512))";
  38.    
  39.         public string ExecuteQueryReturnString(string Q, string connString)
  40.         {
  41.             string queryresults = "";
  42.             try
  43.             {
  44.                 MySqlConnection myConn = new MySqlConnection(connString);
  45.                 MySqlCommand myCommand = new MySqlCommand(Q, myConn);
  46.                 myCommand.Connection.Open();
  47.                 MySqlDataReader myReader =
  48.                     myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  49.                 while (myReader.Read())
  50.                 {
  51.                     for (int o = 0; o < myReader.FieldCount; o++)
  52.                         if (myReader.GetString(o) != null)
  53.                             queryresults = (myReader.GetString(o));
  54.                         else
  55.                             queryresults = "NULL";
  56.                 }
  57.                 myReader.Close();
  58.                 myConn.Close();
  59.  
  60.                 return queryresults;
  61.             }
  62.             catch (Exception)
  63.             {
  64.                 queryresults = "Not Found";
  65.  
  66.                 return queryresults;
  67.  
  68.             }
  69.         }
  70.  
  71.  
  72.  
  73.         public ArrayList ExecuteQuery(string Q, string connString,bool encrypt)
  74.         {
  75.             ArrayList queryresults = new ArrayList();
  76.             try
  77.             {
  78.                 MySqlConnection myConn = new MySqlConnection(connString);
  79.                 MySqlCommand myCommand = new MySqlCommand(Q, myConn);
  80.                 myCommand.Connection.Open();
  81.                 MySqlDataReader myReader =
  82.                     myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  83.                 while (myReader.Read())
  84.                 {
  85.                     for (int o = 0; o < myReader.FieldCount; o++)
  86.                         if (myReader.GetString(o) != null)
  87.                         {
  88.                             if (encrypt)
  89.                                 queryresults.Add(Encryption.decrypttxt(myReader.GetString(o)));
  90.                             else
  91.                                 queryresults.Add(myReader.GetString(o));
  92.                         }
  93.                         else
  94.                             queryresults.Add("NULL");
  95.                 }
  96.                 myReader.Close();
  97.                 myConn.Close();
  98.                 return queryresults;
  99.             }
  100.             catch (Exception ex)
  101.             {
  102.                 Console.WriteLine("eek , Mysql Error Bollox get some mofo to fix");
  103.                 queryresults.Add("EXCEPTION Nothing Found :" + ex);
  104.                 return queryresults;
  105.             }
  106.         }
  107.  
  108.  
  109.    
  110.         public ArrayList QueryDupeCheck(string todupe, string constring)
  111.         {
  112.             ArrayList queryresults = new ArrayList();
  113.             try
  114.             {
  115.                 string query = showDupeQuery + todupe + "%';";
  116.                 MySqlConnection myConn = new MySqlConnection(constring);
  117.                 MySqlCommand myCommand = new MySqlCommand(query, myConn);
  118.                 myCommand.Connection.Open();
  119.                 MySqlDataReader myReader =
  120.                     myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  121.                 while (myReader.Read())
  122.                 {
  123.                     for (int o = 0; o < myReader.FieldCount; o++)
  124.                         queryresults.Add(Encryption.decrypttxt(myReader.GetString(o)));
  125.                 }
  126.                 myReader.Close();
  127.                 myConn.Close();
  128.                 return queryresults;
  129.             }
  130.             catch (Exception ex)
  131.             {
  132.                 queryresults.Add("Nothing Found :" + ex);
  133.                 return queryresults;
  134.             }
  135.         }
  136.         public bool dupecheck(string release,bool show)
  137.         {
  138.  
  139.             try
  140.             {
  141.                 release = Encryption.encrypttxt(release);
  142.                 string query = "SELECT name FROM releases WHERE name LIKE '%" + release + "%';";
  143.                 MySqlConnection myConn = new MySqlConnection(Dupeconstring);
  144.                 MySqlCommand myCommand = new MySqlCommand(query, myConn);
  145.                 myCommand.Connection.Open();
  146.                 MySqlDataReader myReader =
  147.                     myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  148.                 while (myReader.Read())
  149.                 {
  150.                     for (int o = 0; o < myReader.FieldCount; o++)
  151.                     {
  152.                         if (myReader.GetString(o) == release)
  153.                         {
  154.                             if (show)
  155.                             {
  156.                                 Console.WriteLine(myReader.GetString(o));
  157.                             }
  158.                             return true;
  159.                         }
  160.                         else
  161.                         {
  162.                             if (show)
  163.                             {
  164.                                 if (myReader.GetString(o).Contains(release))
  165.                                 {
  166.                                     Console.WriteLine(release + " duped with " + myReader.GetString(o));
  167.                                 }
  168.                             }
  169.                             else
  170.                             {
  171.                                 Console.WriteLine("DUPPPPPPPE");
  172.                                 return false;
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.                 // fuckup
  178.                 Console.WriteLine("Ran Through");
  179.                 return false;
  180.                
  181.             }
  182.             catch (Exception)
  183.             {
  184.                 return false;
  185.             }
  186.  
  187.             //  return false;
  188.         }
  189.  
  190.         // public const string showDupeQuery = "SELECT * FROM releases WHERE name LIKE '%";
  191.         public string CreateTable(string tablename, string fields, string constring)
  192.         {
  193.  
  194.             MySqlConnection conDatabase = new MySqlConnection(constring);
  195.             MySqlCommand cmdDatabase = new MySqlCommand("CREATE TABLE " + tablename + " " + fields, conDatabase);
  196.             conDatabase.Open();
  197.             try
  198.             {
  199.                 cmdDatabase.ExecuteNonQuery();
  200.             }
  201.             catch (Exception f)
  202.             {
  203.                 string errormsg = "Exception Caused because :" + f;
  204.  
  205.                 conDatabase.Close();
  206.                 return (tablename + " Table Not Created: already exists");
  207.             }
  208.             conDatabase.Close();
  209.             return (tablename + " Table Created");
  210.         }
  211.      
  212.         public string DeleteTable(string tablename, string constring)
  213.         {
  214.             MySqlConnection conDatabase = new MySqlConnection(constring);
  215.             MySqlCommand cmdDatabase = new MySqlCommand("DROP TABLE " + tablename, conDatabase);
  216.             conDatabase.Open();
  217.             try
  218.             {
  219.                 cmdDatabase.ExecuteNonQuery();
  220.             }
  221.             catch (Exception f)
  222.             {
  223.                 string errormsg = "Exception Caused because :" + f;
  224.  
  225.                 conDatabase.Close();
  226.                 return ("Table " + tablename + " Not Deleted: it does not exist");
  227.             }
  228.             conDatabase.Close();
  229.             return ("Table " + tablename + "Deleted");
  230.         }
  231.      
  232.         public string CreateDb(string database)
  233.         {
  234.             MySqlConnection conDatabase = new MySqlConnection(connectstring);
  235.             MySqlCommand cmdDatabase = new MySqlCommand("CREATE DATABASE " + database, conDatabase);
  236.             conDatabase.Open();
  237.             try
  238.             {
  239.                 cmdDatabase.ExecuteNonQuery();
  240.             }
  241.             catch (Exception f)
  242.             {
  243.                 string errormsg = "Exception Caused because :" + f;
  244.  
  245.                 conDatabase.Close();
  246.                 return ("DataBase : " + database + " Not Created: Already Exists");
  247.             }
  248.             conDatabase.Close();
  249.             return ("DataBase :" + database + " Created");
  250.         }
  251.        public string DeleteDb(string database)
  252.         {
  253.             MySqlConnection conDatabase = new MySqlConnection(connectstring);
  254.             MySqlCommand cmdDatabase = new MySqlCommand("DROP DATABASE " + database, conDatabase);
  255.             conDatabase.Open();
  256.             try
  257.             {
  258.                 cmdDatabase.ExecuteNonQuery();
  259.             }
  260.             catch (Exception f)
  261.             {
  262.                 string errormsg = "Exception Caused because :" + f;
  263.  
  264.                 conDatabase.Close();
  265.                 return ("DataBase : " + database + " not deleted :" + errormsg);
  266.             }
  267.             conDatabase.Close();
  268.             return ("DataBase : " + database + " Deleted");
  269.         }
  270.  
  271.  
  272.         public string AddRelease(string release, string section, string time, string date)
  273.         {
  274.             string encrypted_release = Encryption.encrypttxt(release);
  275.             string encrypted_section = Encryption.encrypttxt(section);
  276.             string encrypted_time = Encryption.encrypttxt(time);
  277.             string encrypted_date = Encryption.encrypttxt(date);
  278.             release = encrypted_release;
  279.             date = encrypted_date;
  280.             section = encrypted_section;
  281.             time = encrypted_time;
  282.             MySqlConnection conDatabase = new MySqlConnection(Dupeconstring);
  283.  
  284.  
  285.             MySqlCommand cmdDatabase =
  286.                 new MySqlCommand(
  287.                     "INSERT INTO " + "releases " + "(name,date,time,section) values (\"" + release + "\",\"" + date +
  288.                     "\",\"" + time + "\",\" " + section + "\");", conDatabase);
  289.  
  290.             conDatabase.Open();
  291.             try
  292.             {
  293.                 cmdDatabase.ExecuteNonQuery();
  294.             }
  295.             catch (Exception f)
  296.             {
  297.                 string errormsg = "Exception Caused because :" + f;
  298.  
  299.                 conDatabase.Close();
  300.                 return ("Entry Not Created: " + errormsg);
  301.             }
  302.             conDatabase.Close();
  303.             return ("Entry Created");
  304.         }
  305.  
  306.         //"(name varchar(255) NOT NULL PRIMARY KEY,affilsites varchar(255),sections varchar(255))";
  307.    
  308.         public void InitDb()
  309.         {
  310.             // CreateDb
  311.            Console.WriteLine(CreateDb(DupeDataBase));
  312.             // CreateTables
  313.             Console.WriteLine(CreateTable("releases", releasesSchema, Dupeconstring));
  314.             Console.WriteLine(CreateTable("nukes", nukesSchema, Dupeconstring));
  315.          
  316.         }
  317.         public void DestoryDb()
  318.         {
  319.             Console.WriteLine(DeleteTable("releases", Dupeconstring));
  320.             Console.WriteLine(DeleteTable("nukes", Dupeconstring));
  321.             Console.WriteLine(DeleteDb(DupeDataBase));
  322.            
  323.  
  324.         }
  325.  
  326.  
  327.    
  328.  
  329.  
  330.  
  331.  
  332.  
  333.         #endregion
  334.  
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement