Exemple #1
0
        /// <summary>
        /// Used when a new device is being added to the system
        /// </summary>
        /// <param name="di">The device being added</param>
        /// <param name="conn">The DB connection to use</param>
        private static void AddDevice(DeviceInfo di, DateTimeOffset timestamp, SQLiteConnection conn)
        {
            Inserter inserter = new Inserter("Devices", conn);

            if (di.id >= 0)
            {
                inserter.Set("DeviceID", di.id);
            }
            inserter.Set("Name", di.name, true);
            inserter.Set("Type", (int)di.type);
            inserter.Set("IPAddress", di.ipAddress, true);
            inserter.Set("Username", di.username, true);
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();

            inserter.Set("Password", sed.Encrypt(di.password), true);
            inserter.Set("DateActivated", timestamp);
            if (di.groupID < 0)
            {
                inserter.SetNull("GroupID");
            }
            else
            {
                inserter.Set("GroupID", di.groupID);
            }

            inserter.Execute();

            long device_id = conn.LastInsertRowId;

            Database.AddCollectors(di, device_id, timestamp, conn);
        }
Exemple #2
0
        public void NotBeNullable()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery("CREATE TABLE Temp(A INTEGER NOT NULL PRIMARY KEY, B TEXT NOT NULL)");
                    Assert.True(conn.DoesTableExist("Temp"));

                    Inserter i = new Inserter("Temp", conn);
                    i.Set("A", 1);
                    i.SetNull("B");

                    Assert.Throws <SQLiteException>(() => i.Execute());
                }
            }
        }
Exemple #3
0
        public void InsertNullOK()
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database db = new Database(new Context(fd.Fi));
                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    conn.ExecuteNonQuery(CreateTable);
                    Assert.True(conn.DoesTableExist("Temp"));

                    int    count = 100;
                    Random r     = new Random();
                    Dictionary <int, string> dict = new Dictionary <int, string>();
                    for (int i = 0; i < count; ++i)
                    {
                        Inserter inserter = new Inserter("Temp", conn);
                        int      a        = r.Next();
                        inserter.Set("A", a);

                        // Make every other one null
                        if (i % 2 == 0)
                        {
                            string b = Guid.NewGuid().ToString();
                            inserter.Set("B", b, false);
                            dict[a] = b;
                        }
                        else
                        {
                            inserter.SetNull("B");
                            dict[a] = null;
                        }

                        inserter.Execute();
                    }

                    foreach (int a in dict.Keys)
                    {
                        string sql = $"SELECT B FROM Temp WHERE A = {a}";
                        using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                            using (SQLiteDataReader reader = command.ExecuteReader())
                            {
                                bool read = reader.Read();
                                Assert.True(read);
                                if (read)
                                {
                                    if (dict[a] != null)
                                    {
                                        Assert.False(reader.IsDBNull(0));
                                        string b = reader.GetString(0);
                                        Assert.Equal(dict[a], b);
                                    }
                                    else
                                    {
                                        Assert.True(reader.IsDBNull(0));
                                        Assert.Throws <InvalidCastException>(() => reader.GetString(0));
                                    }
                                }
                            }
                    }
                }
            }
        }