summaryrefslogtreecommitdiff
path: root/Aktywator/Sql.cs
blob: b3f21049f171fe56b626bea10bda8a0f47d32792 (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
using System;
using System.Collections.Generic;
using System.Text;

using System.Data.OleDb;
using data = System.Data.OleDb.OleDbDataReader;

namespace Aktywator
{
    class OleDbRowMissingException : Exception
    {
    }

    class Sql
    {
        private OleDbConnection connection;

        public Sql(string filename)
        {
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + filename + ";";
            connection = new OleDbConnection(connStr);
            connection.Open();
        }

        public void close()
        {
            connection.Close();
        }

        public bool isOpen()
        {
            if (connection == null) return false;
            else return connection.State == System.Data.ConnectionState.Open;
        }

        public int query(string q)
        {
            OleDbCommand cmd = new OleDbCommand(q, connection);
            return cmd.ExecuteNonQuery();
        }
        public string selectOne(string q, bool checkForRow = false)
        {
            OleDbCommand cmd = new OleDbCommand(q, connection);
            object o = cmd.ExecuteScalar();
            if (o == null) // it's null if the row does not exist, it'd be DBNull.Value if NULL was the value in existing row
            {
                if (!checkForRow)
                {
                    return "";
                }
                throw new OleDbRowMissingException();
            }
            return o.ToString();
        }
        public data select(string q)
        {
            OleDbCommand cmd = new OleDbCommand(q, connection);
            return cmd.ExecuteReader();
        }

        public bool checkTableExists(string tableName)
        {
            try
            {
                selectOne("select count(*) from " + tableName);
            }
            catch (OleDbException)
            {
                return false;
            }
            return true;
        }

        public bool checkFieldExists(string tableName, string fieldName)
        {
            try
            {
                selectOne("select " + fieldName + " from " + tableName);
            }
            catch (OleDbException)
            {
                return false;
            }
            return true;
        }

        internal void insert(string table, Dictionary<string, object> columns)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO ");
            query.Append(table);
            query.Append(" (");
            List<string> keys = new List<string>();
            List<string> parameters = new List<string>();
            foreach (string key in columns.Keys)
            {
                keys.Add("`" + key + "`");
                parameters.Add("@" + key);
            }
            string[] fields = keys.ToArray();
            query.Append(String.Join(", ", fields));
            query.Append(") VALUES(");
            query.Append(String.Join(", ", parameters.ToArray()));
            query.Append(")");
            OleDbCommand command = new OleDbCommand(query.ToString(), connection);
            foreach (KeyValuePair<string, object> column in columns)
            {
                command.Parameters.AddWithValue("@" + column.Key, column.Value);
            }
            command.ExecuteScalar();
        }
    }
}