summaryrefslogtreecommitdiff
path: root/Aktywator/Sql.cs
diff options
context:
space:
mode:
authorMichał Zimniewicz <michzimny@users.noreply.github.com>2017-12-06 23:57:31 +0100
committerGitHub <noreply@github.com>2017-12-06 23:57:31 +0100
commit80cd31c1c8e7cef2cfbf6148df3d9ec2d2a23279 (patch)
tree4071e574a3e611cf4d8a19e231ff7ecc659949fa /Aktywator/Sql.cs
parent83e064ccb67f0b68b1f947026301cd3905d06a3c (diff)
parentf12d2b996b165d66f4c32eef4a3bdfb89309670d (diff)
Merge pull request #29 from michzimny/1.1-beta
Scalenie gałęzi beta do master
Diffstat (limited to 'Aktywator/Sql.cs')
-rw-r--r--Aktywator/Sql.cs43
1 files changed, 40 insertions, 3 deletions
diff --git a/Aktywator/Sql.cs b/Aktywator/Sql.cs
index c35b0dd..b3f2104 100644
--- a/Aktywator/Sql.cs
+++ b/Aktywator/Sql.cs
@@ -7,6 +7,10 @@ using data = System.Data.OleDb.OleDbDataReader;
namespace Aktywator
{
+ class OleDbRowMissingException : Exception
+ {
+ }
+
class Sql
{
private OleDbConnection connection;
@@ -34,12 +38,19 @@ namespace Aktywator
OleDbCommand cmd = new OleDbCommand(q, connection);
return cmd.ExecuteNonQuery();
}
- public string selectOne(string q)
+ public string selectOne(string q, bool checkForRow = false)
{
OleDbCommand cmd = new OleDbCommand(q, connection);
object o = cmd.ExecuteScalar();
- if (o == null) return "";
- else return o.ToString();
+ 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)
{
@@ -72,5 +83,31 @@ namespace Aktywator
}
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();
+ }
}
}