summaryrefslogtreecommitdiff
path: root/Aktywator/Setting.cs
blob: 45d6b58c2d495b0b8c5ea14e27f53f3cf91ef456 (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
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 Setting(string name, CheckBox field, Bws bws)
        {
            this.name = name;
            this.field = field;
            this.bws = bws;
        }

        public void load()
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT ");
            str.Append(this.name);
            str.Append(" FROM Settings");
            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)
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT ");
            str.Append(name);
            str.Append(" FROM Settings");
            try
            {
                return bws.sql.selectOne(str.ToString());
            }
            catch (OleDbException)
            {
                if (errors.Length > 0) errors.Append(", ");
                errors.Append(name);
                return "";
            }
        }

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

        public static void save(string name, string value, Bws bws, StringBuilder errors)
        {
            StringBuilder str = new StringBuilder();
            str.Append("UPDATE Settings SET ");
            str.Append(name);
            str.Append("=");
            str.Append(value);
            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();
        }
    }
}