Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.OleDb;
- using System.IO;
- using System.Linq;
- using System.Net.Mime;
- using System.Security.Policy;
- using System.Text;
- using System.Threading.Tasks;
- using WCFServiceLibrary.UserHandling;
- namespace WCFServiceLibrary
- {
- internal static class DatabaseHandler
- {
- private static readonly OleDbConnection _connection;
- static DatabaseHandler()
- {
- Logger.Log("Setting up connection string...", LogSeverity.Information);
- CreateTablesIfNotExists();
- var finfo = new FileInfo("users.txt");
- _connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
- + finfo.DirectoryName + ";Extended Properties='text;HDR=Yes;FMT=Delimited';");
- }
- private static void CreateTablesIfNotExists()
- {
- if (!File.Exists("users.txt"))
- {
- Logger.Log("Users table doesn't exist! I'll just create it now.", LogSeverity.Warning);
- using (var sw = File.CreateText("users.txt"))
- {
- sw.WriteLine("username;email;passwordhash");
- sw.Flush();
- }
- }
- if (!File.Exists("proxies.txt"))
- {
- Logger.Log("Proxies table doesn't exist! I'll just create it now.", LogSeverity.Information);
- using (var sw = File.CreateText("proxies.txt"))
- {
- sw.WriteLine("proxy_ip");
- sw.Flush();
- }
- }
- }
- internal static List<User> GetUsers()
- {
- Logger.Log("Getting all the registered users.", LogSeverity.Information);
- return GetUsersFromReader(ExecuteQuery("SELECT * FROM users.txt"));
- }
- private static List<User> GetUsersFromReader(IDataReader dataReader)
- {
- Logger.Log("Reading data from users.", LogSeverity.Information);
- var users = new List<User>();
- while (dataReader.Read())
- {
- users.Add(new User
- {
- Username = dataReader.GetString(0),
- EmailAddress = dataReader.GetString(1),
- PasswordHash = dataReader.GetString(2)
- });
- }
- dataReader.Close();
- return users;
- }
- private static IDataReader ExecuteQuery(string query)
- {
- Logger.Log("Executing SQL query: " + query, LogSeverity.Information);
- var cmd = new OleDbCommand(query, _connection);
- OpenConnection();
- var result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- return result;
- }
- private static void OpenConnection()
- {
- if (_connection.State == ConnectionState.Closed) _connection.Open();
- }
- private static int ExecuteNonQuery(OleDbCommand cmd)
- {
- Logger.Log("Executing SQL command: " + cmd.CommandText, LogSeverity.Information);
- cmd.Connection = _connection;
- OpenConnection();
- var result = cmd.ExecuteNonQuery();
- _connection.Close();
- return result;
- }
- internal static RegisterResult AddUser(User user)
- {
- Logger.Log("Adding new user to database by the name of " + user.Username, LogSeverity.Information);
- var cmd = new OleDbCommand("INSERT INTO [users.txt] VALUES (@username,@email,@password)", _connection);
- cmd.Parameters.Add("@username", OleDbType.BSTR).Value = user.Username;
- cmd.Parameters.Add("@email", OleDbType.BSTR).Value = user.EmailAddress;
- cmd.Parameters.Add("@password", OleDbType.BSTR).Value = user.PasswordHash;
- var affectedRows = ExecuteNonQuery(cmd);
- return affectedRows > 0
- ? new RegisterResult { Success = true }
- : new RegisterResult { Error = "Failed to register user!" };
- }
- internal static ProxyResult AddProxy(Proxy proxy)
- {
- Logger.Log("Adding new proxy to the database by the IP of " + proxy.ProxyIp, LogSeverity.Information);
- var cmd = new OleDbCommand("INSERT INTO [proxies.txt] VALUES (@proxy_ip, @port, @responsetime)");
- cmd.Parameters.Add("@proxy_ip", OleDbType.BSTR).Value = proxy.ProxyIp;
- cmd.Parameters.Add("@port", OleDbType.Integer).Value = proxy.Port;
- cmd.Parameters.Add("@responsetime", OleDbType.Integer).Value = proxy.ResponseTime;
- var affectedRows = ExecuteNonQuery(cmd);
- return affectedRows > 0
- ? new ProxyResult { Success = true }
- : new ProxyResult { Error = "Failed to insert proxy!" };
- }
- internal static List<Proxy> GetProxies()
- {
- Logger.Log("Getting all the proxies.", LogSeverity.Information);
- return GetProxiesFromReader(ExecuteQuery("SELECT * FROM proxies.txt ORDER BY responsetime"));
- }
- // na ez az ami nem működik, mert az oledb révén a fájl használatban van.
- // viszont az oledb mezei egyszerűséggel nem támogatja a delete utasítást ...
- internal static void RemoveProxy(Proxy proxy)
- {
- var proxies = File.ReadAllLines("proxies.txt").ToList();
- for (int i = 0; i < proxies.Count; i++)
- {
- if (proxies[i].Contains(proxy.ProxyIp))
- {
- proxies.RemoveAt(i);
- break;
- }
- }
- File.WriteAllLines("proxies.txt", proxies);
- }
- private static List<Proxy> GetProxiesFromReader(IDataReader dataReader)
- {
- Logger.Log("Reading data from proxies.", LogSeverity.Information);
- var proxies = new List<Proxy>();
- while (dataReader.Read())
- {
- proxies.Add(new Proxy(dataReader.GetString(0), int.Parse(dataReader.GetString(1)), int.Parse(dataReader.GetString(2))));
- }
- return proxies;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment