summaryrefslogtreecommitdiff
path: root/Aktywator/Setting.cs
blob: 613065fe7a2e3cf26bb38ee068ed81290635b6fb (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Aktywator
{
    class Setting
    {
        public string name;
        public string type;
        public string defaultStr;
        public string table;
        public CheckBox field;
        private Bws bws;
        public Version bcsV;
        public Version fwV;

        public Setting(string name, CheckBox field, Bws bws, Version bcsVersion, Version firmwareVersion)
        {
            this.name = name;
            this.field = field;
            this.bws = bws;
            this.bcsV = bcsVersion;
            this.fwV = firmwareVersion;
        }

        public void load(string section = null)
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT ");
            str.Append(this.name);
            str.Append(" FROM Settings");
            if (section != null)
            {
                str.Append(" WHERE `Section` = ");
                str.Append(section);
            }
            field.Checked = false;
            string a = bws.sql.selectOne(str.ToString());
            field.Checked = a.ToUpper() == "TRUE" ? true : false;
        }

        public static string load(string name, Bws bws, StringBuilder errors, string section = null)
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT ");
            str.Append(name);
            str.Append(" FROM Settings");
            if (section != null)
            {
                str.Append(" WHERE `Section` = ");
                str.Append(section);
            }
            try
            {
                return bws.sql.selectOne(str.ToString());
            }
            catch (OleDbException)
            {
                if (errors.Length > 0) errors.Append(", ");
                errors.Append(name);
                return "";
            }
        }

        public void save(string section = null)
        {
            StringBuilder str = new StringBuilder();
            str.Append("UPDATE Settings SET ");
            str.Append(this.name);
            if (field.Checked) str.Append("=true");
            else str.Append("=false");
            if (section != null)
            {
                str.Append(" WHERE `Section` = ");
                str.Append(section);
            }
            bws.sql.query(str.ToString());
        }

        public static void save(string name, string value, Bws bws, StringBuilder errors, string section = null)
        {
            StringBuilder str = new StringBuilder();
            str.Append("UPDATE Settings SET ");
            str.Append(name);
            str.Append("=");
            str.Append(value);
            if (section != null)
            {
                str.Append(" WHERE `Section` = ");
                str.Append(section);
            }
            try
            {
                bws.sql.query(str.ToString());
            }
            catch (OleDbException)
            {
                if (errors.Length > 0) errors.Append(", ");
                errors.Append(name);
            }
        }

        public Setting(string name, string type)
        {
            this.name = name;
            this.type = type;
            this.defaultStr = "";
            this.table = "Settings";
        }

        public Setting(string name, string type, string defaultStr)
        {
            this.name = name;
            this.type = type;
            this.defaultStr = defaultStr;
            this.table = "Settings";
        }

        public Setting(string name, string type, string defaultStr, string table)
        {
            this.name = name;
            this.type = type;
            this.defaultStr = defaultStr;
            this.table = table;
        }

        public string getAddColumnSql()
        {
            StringBuilder str = new StringBuilder();
            str.Append("ALTER TABLE ");
            str.Append(this.table);
            str.Append(" ADD COLUMN ");
            str.Append(this.name);
            str.Append(" ");
            str.Append(this.type);
            str.Append(";");
            return str.ToString();
        }

        public string getSetDefaultSql()
        {
            StringBuilder str = new StringBuilder();
            if (defaultStr.Length > 0)
            {
                str.Append("UPDATE ");
                str.Append(this.table);
                str.Append(" SET ");
                str.Append(this.name);
                str.Append("=");
                str.Append(this.defaultStr);
                str.Append(";");
            }
            return str.ToString();
        }

        public void createField(Sql sql, bool setDefault = true)
        {
            try
            {
                sql.query(this.getAddColumnSql());
                if (setDefault)
                {
                    sql.query(this.getSetDefaultSql());
                }
            }
            catch (OleDbException)
            {
            }
        }
    }
}