using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using Tamir.SharpSsh.jsch;
using MyFunctions;
namespace Tripmania {
public class MyUserInfo : UserInfo {
private String password;
public String getPassword() { return password; }
public bool promptYesNo(String str) { return true; }
public String getPassphrase() { return null; }
public bool promptPassphrase(String message) { return true; }
public bool promptPassword(String message) { return true; }
public void showMessage(String message) { }
}
public class DataAccess {
private struct infoSSH {
public string server;
public string user;
public string password;
public System.Int32 port;
}
private MySqlConnection connection;
private MySqlDataAdapter da;
private MySqlCommandBuilder cb;
public DataSet data;
private infoSSH sessionInfo;
private Session sshSession;
private bool sshConnection = false;
public DataAccess(string user, string password, string database) { if (!(user == "" || password == "" || database == "")) this.connection = new MySqlConnection(String.Format("Server=localhost;Database={0};Uid={1};Pwd={2};", database, user, password)); }
public DataAccess(string user, string password, string database, string server) { if (!(user == "" || password == "" || database == "" || server == "")) this.connection = new MySqlConnection(String.Format("Server={0};Database={1};Uid={2};Pwd={3};", server, database, user, password)); }
public void setSSHInfo(string server, string user, string password) {
this.sessionInfo.server = server;
this.sessionInfo.user = user;
this.sessionInfo.password = password;
this.sessionInfo.port = 22;
this.sshConnection = true;
}
public void setSSHInfo(string server, string user, string password, System.Int32 port) {
this.sessionInfo.server = server;
this.sessionInfo.user = user;
this.sessionInfo.password = password;
this.sessionInfo.port = port;
this.sshConnection = true;
}
public void clearSSHInfo() {
this.sessionInfo.server = "";
this.sessionInfo.user = "";
this.sessionInfo.password = "";
this.sessionInfo.port = 0;
this.sshConnection = false;
}
private bool makeSSHTunnel() {
if (this.sessionInfo.server == "") return false;
JSch sshConnection = new JSch();
this.sshSession = sshConnection.getSession(sessionInfo.user, sessionInfo.server, 22);
this.sshSession.setHost(sessionInfo.server);
this.sshSession.setPassword(sessionInfo.password);
UserInfo ui = new MyUserInfo();
this.sshSession.setUserInfo(ui);
this.sshSession.connect();
this.sshSession.setPortForwardingL(3306, "localhost", 3306);
return true;
}
private bool destroySSHTunnel() {
if (!this.sshSession.isConnected()) return false;
this.sshSession.delPortForwardingL(3306);
this.sshSession.disconnect();
return true;
}
public bool loadData(string[] fields, string[] tables) {
if (this.sshConnection) if(!this.makeSSHTunnel()) return false;
if (fields.Count() == 0 || tables.Count() == 0) {
this.destroySSHTunnel();
return false;
}
string sql = "SELECT " + String.Join(",", fields) + " FROM " + String.Join(",", tables);
this.connection.Open();
this.da = new MySqlDataAdapter(sql, this.connection);
this.cb = new MySqlCommandBuilder(da);
this.data = new DataSet();
this.da.FillSchema(data, SchemaType.Mapped);
this.connection.Close();
this.destroySSHTunnel();
return true;
}
public bool loadData(string[] fields, string[] tables, string[] conditions) {
if (this.sshConnection) if (!this.makeSSHTunnel()) return false;
if (fields.Count() == 0 || tables.Count() == 0 || conditions.Count() == 0) {
this.destroySSHTunnel();
return false;
}
string sql = "SELECT " + String.Join(",", fields) + " FROM " + String.Join(",", tables) + " WHERE " + String.Join(",", conditions);
this.connection.Open();
this.da = new MySqlDataAdapter(sql, this.connection);
this.cb = new MySqlCommandBuilder(da);
this.data = new DataSet();
this.da.FillSchema(data, SchemaType.Mapped);
this.connection.Close();
this.destroySSHTunnel();
return true;
}
public bool loadData(string[] fields, string[] tables, string[] conditions, string[] orderFields, string selectionOrder = "") {
if (this.sshConnection) if (!this.makeSSHTunnel()) return false;
if (fields.Count() == 0 || tables.Count() == 0 || conditions.Count() == 0) {
this.destroySSHTunnel();
return false;
}
string sql = "SELECT " + String.Join(",", fields) + " FROM " + String.Join(",", tables) + " WHERE " + String.Join(",", conditions) + " ORDER BY " + String.Join(",", orderFields);
selectionOrder = selectionOrder.Trim();
if (selectionOrder != "ASC" && selectionOrder != "DESC") selectionOrder = "";
if (selectionOrder != "") sql += " " + selectionOrder;
this.connection.Open();
this.da = new MySqlDataAdapter(sql, this.connection);
this.cb = new MySqlCommandBuilder(da);
this.data = new DataSet();
this.da.FillSchema(data, SchemaType.Mapped);
this.connection.Close();
this.destroySSHTunnel();
return true;
}
public bool updateData() {
if (this.sshConnection) if (!this.makeSSHTunnel()) return false;
if (data.Tables.Count == 0) {
this.destroySSHTunnel();
return false;
}
this.connection.Open();
da.Update(data);
this.connection.Close();
this.destroySSHTunnel();
return true;
}
public bool unloadData() {
if (data.Tables.Count == 0) return false;
data.Tables.Clear();
return true;
}
public int executeSQL(string sql) {
if (this.sshConnection) if (!this.makeSSHTunnel()) return -1;
if (sql == "") {
this.destroySSHTunnel();
return -1;
}
MySqlCommand cm = new MySqlCommand();
this.connection.Open();
cm.Connection = this.connection;
cm.CommandType = CommandType.Text;
cm.CommandText = sql;
int res = cm.ExecuteNonQuery();
this.connection.Close();
this.destroySSHTunnel();
return res;
}
public bool checkRegistry(string table, string[] condition) {
if (table == "" || condition.Count() == 0) return false;
if (this.sshConnection) if (!this.makeSSHTunnel()) return false;
MySqlCommand cm = new MySqlCommand();
this.connection.Open();
cm.Connection = this.connection;
cm.CommandType = CommandType.Text;
cm.CommandText = "SELECT COUNT(*) FROM " + table;
cm.CommandText += " WHERE " + condition[0];
for (int i = 1; i < condition.Count(); i++) cm.CommandText += " AND " + condition[i];
int res = Convert.ToInt32(cm.ExecuteScalar());
this.connection.Close();
this.destroySSHTunnel();
if (res > 0) return true;
else return false;
}
}
}