aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MySQL/MySQLRaw.cs
blob: e8f80aac6856e5d53302f82e2a3617a5a45e536e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// https://dev.mysql.com/doc/connector-net/en/


using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using log4net;
using MySql.Data.MySqlClient;

namespace OpenSim.Data.MySQL
{
    public class MySQLRaw
    {
	private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 
	private string m_connectString;

	public MySQLRaw(string connect)
	{
	    m_connectString = connect;
	}

	public int Count(string table)
	{
	    return Count(table, "");
	}
	public int Count(string table, string wher)
	{
	    string query = "SELECT Count(*) FROM " + table;
	    if ("" != wher)
		query = query + " WHERE " + wher;
	    int result = -1;

	    object r = doScalarQuery(query);
        	if (r != null)
        	    result = Convert.ToInt32(r);

	    return result;
	}

	public List< Hashtable > Select(string table, string select, string wher, string order)
	{
	    if ("" == select)
		select = "*";
	    string query = "SELECT " + select + " FROM " + table;
	    if ("" != wher)
		query = query + " WHERE " + wher;
	    if ("" != order)
		query = query + " ORDER BY " + order;

            using (MySqlConnection dbcon = new MySqlConnection(m_connectString))
            {
                dbcon.Open();
		MySqlCommand cmd = new MySqlCommand(query, dbcon);
		MySqlDataReader rdr = cmd.ExecuteReader();
		List<string> names = new List<string>();
		DataTable schema = rdr.GetSchemaTable();
		List< Hashtable > list = new List< Hashtable >();

        	foreach (DataRow row in schema.Rows)
        	{
        	    string tbl = "";
        	    string nm = "";
        	    string tp = "";
        	    foreach (DataColumn col in schema.Columns)
        	    {
        		if ("BaseTableName"	== col.ColumnName)	tbl = row[col].ToString();
        		if ("ColumnName"	== col.ColumnName)	nm  = row[col].ToString();
        		if ("DataType"		== col.ColumnName)	tp  = row[col].ToString();
        	    }
        	    names.Add(nm);
        	}

		while (rdr.Read())
		{
		    Hashtable r = new Hashtable();
		    foreach (string name in names)
		    {
			r[name] = rdr[name];
		    }
		    list.Add(r);
		}

		rdr.Close();
                dbcon.Close();
		return list;
	    }
	}

	private object doScalarQuery(string query)
	{
	    try
	    {
        	using (MySqlConnection dbcon = new MySqlConnection(m_connectString))
        	{
        	    dbcon.Open();
		    MySqlCommand cmd = new MySqlCommand(query, dbcon);
        	    Object ret = cmd.ExecuteScalar();
        	    dbcon.Close();
        	    return ret;
        	}
	    }
	    catch (MySqlException e)
	    {
		m_log.ErrorFormat("[MYSQL RAW]:  Problem connecting to the database {0}", e.Message);
		return null;
	    }
	}

	private void doNonQuery(string query)
	{
            using (MySqlConnection dbcon = new MySqlConnection(m_connectString))
            {
                dbcon.Open();
		MySqlCommand cmd = new MySqlCommand(query, dbcon);
		cmd.ExecuteNonQuery();
                dbcon.Close();
	    }
	}

	public void Insert(string table)
	{
	    string query = "INSERT INTO " + table + " (name, age) VALUES('John Smith', '33')";
	    doNonQuery(query);
	}

	public void Update(string table, string wher)
	{
	    string query = "UPDATE " + table + " SET name='Joe', age='22'";
	    if ("" != wher)
		query = query + " WHERE " + wher;
	    doNonQuery(query);
	}

	public void Delete(string table, string wher)
	{
	    string query = "DELETE FROM " + table;
	    if ("" != wher)
		query = query + " WHERE " + wher;
	    doNonQuery(query);
	}

    }
}