namespace Holo { using System; using System.Collections; using System.Data.Odbc; public static class DB { private static OdbcConnection dbConnection; public static bool checkExists(string Query) { try { return new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteReader().HasRows; } catch (Exception exception) { Out.WriteError("Errore '" + exception.Message + "' in '" + Query + "'"); return false; } } public static void closeConnection() { Out.WriteLine("Chiusura connessione col database..."); try { dbConnection.Close(); Out.WriteLine("Connessione conclusa."); } catch { Out.WriteLine("Nessun database connesso."); } } public static bool openConnection(string dbHost, int dbPort, string dbName, string dbUsername, string dbPassword) { try { Out.WriteLine(string.Concat(new object[] { "Connetto con ", dbName, " in ", dbHost, ":", dbPort, " per '", dbUsername, "'" })); dbConnection = new OdbcConnection(string.Concat(new object[] { "Driver={MySQL ODBC 5.1 Driver};Server=", dbHost, ";Port=", dbPort, ";Database=", dbName, ";User=", dbUsername, ";Password=", dbPassword, ";Option=3;" })); dbConnection.Open(); Out.WriteLine("Connessione effettuatra col database."); return true; } catch (Exception exception) { Out.WriteError("Connessione fallita! Errore: " + exception.Message); return false; } } public static void runQuery(string Query) { try { new OdbcCommand(Query, dbConnection).ExecuteScalar(); } catch (Exception exception) { Out.WriteError("Errore '" + exception.Message + "' in '" + Query + "'"); } } public static string runRead(string Query) { try { return new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteScalar().ToString(); } catch (Exception exception) { Out.WriteError("Errore '" + exception.Message + "' in '" + Query + "'"); return ""; } } public static int runRead(string Query, object Tick) { try { return Convert.ToInt32(new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteScalar()); } catch (Exception exception) { Out.WriteError("Error '" + exception.Message + "' at '" + Query + "'"); return 0; } } public static string[] runReadColumn(string Query, int maxResults) { if (maxResults > 0) { Query = Query + " LIMIT " + maxResults; } try { ArrayList list = new ArrayList(); OdbcDataReader reader = new OdbcCommand(Query, dbConnection).ExecuteReader(); while (reader.Read()) { try { list.Add(reader[0].ToString()); } catch { list.Add(""); } } reader.Close(); return (string[]) list.ToArray(typeof(string)); } catch (Exception exception) { Out.WriteError("Errore '" + exception.Message + "' in '" + Query + "'"); return new string[0]; } } public static int[] runReadColumn(string Query, int maxResults, object Tick) { if (maxResults > 0) { Query = Query + " LIMIT " + maxResults; } try { ArrayList list = new ArrayList(); OdbcDataReader reader = new OdbcCommand(Query, dbConnection).ExecuteReader(); while (reader.Read()) { try { list.Add(reader.GetInt32(0)); } catch { list.Add(0); } } reader.Close(); return (int[]) list.ToArray(typeof(int)); } catch (Exception exception) { Out.WriteError("Errore '" + exception.Message + "' in '" + Query + "'"); return new int[0]; } } public static string[] runReadRow(string Query) { try { ArrayList list = new ArrayList(); OdbcDataReader reader = new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { try { list.Add(reader[i].ToString()); } catch { list.Add(""); } } } reader.Close(); return (string[]) list.ToArray(typeof(string)); } catch (Exception exception) { Out.WriteError("Errore '" + exception.Message + "' in '" + Query + "'"); return new string[0]; } } public static int[] runReadRow(string Query, object Tick) { try { ArrayList list = new ArrayList(); OdbcDataReader reader = new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { try { list.Add(reader.GetInt32(i)); } catch { list.Add(0); } } } reader.Close(); return (int[]) list.ToArray(typeof(int)); } catch (Exception exception) { Out.WriteError("Error '" + exception.Message + "' at '" + Query + "'"); return new int[0]; } } public static string runReadUnsafe(string Query) { try { return new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteScalar().ToString(); } catch { return ""; } } public static int runReadUnsafe(string Query, object Tick) { try { return Convert.ToInt32(new OdbcCommand(Query + " LIMIT 1", dbConnection).ExecuteScalar()); } catch { return 0; } } internal static void sendData(string p) { throw new NotImplementedException(); } public static string Stripslash(string Query) { try { return Query.Replace(@"\", @"\").Replace("'", @"\'"); } catch { return ""; } } } }