Esempio n. 1
0
        public ValueList[] Select(string TableName, ValueList Conditions)
        {
            // Check for bad values
            if (string.IsNullOrEmpty(TableName))
            {
                throw new ArgumentException("Table name cannot be empty");
            }
            if (Conditions == null)
            {
                throw new ArgumentNullException("Supplied condition list was null");
            }

            // Lock the write queue so we can read data right now
            lock (WriteQueue)
            {
                // Begin constructing an SQL command
                string Data = $"SELECT * FROM {TableName}";

                // Add conditions
                if (Conditions.Count > 0)
                {
                    Data += " WHERE ";
                    for (int i = 0; i < Conditions.Count; i++)
                    {
                        // Add name to command string
                        if (i > 0)
                        {
                            Data += " AND ";
                        }
                        Data += $"{Conditions[i].Name} = @{Conditions[i].Name}_1";
                    }
                }

                // Create SQL command
                SqliteCommand Command = new SqliteCommand(Data, Connection);

                // Add conditions from the conditions list
                foreach (var Condition in Conditions)
                {
                    Command.Parameters.AddWithValue($"{Condition.Name}_1", Condition.Value);
                }

                // Create an output list
                List <ValueList> Output = new List <ValueList>();

                // Execute command
                using (SqliteDataReader Reader = Command.ExecuteReader())
                {
                    while (Reader.Read())
                    {
                        ValueList Row = new ValueList();
                        for (int i = 0; i < Reader.FieldCount; i++)
                        {
                            Row[Reader.GetName(i)] = Reader.GetValue(i);
                        }
                        Output.Add(Row);
                    }
                }

                // Return output as an array
                return(Output.ToArray());
            }
        }
Esempio n. 2
0
        public void Update(string TableName, ValueList Values, ValueList Conditions)
        {
            // Check for bad values
            if (string.IsNullOrEmpty(TableName))
            {
                return;
            }
            if (Values == null || Values.Count == 0)
            {
                return;
            }
            if (Conditions == null || Conditions.Count == 0)
            {
                return;
            }

            // Begin constructing an SQL command
            string Data = $"UPDATE {TableName} SET ";

            // Loop through the given values
            for (int i = 0; i < Values.Count; i++)
            {
                // Add name to command string
                if (i > 0)
                {
                    Data += ", ";
                }
                Data += $"{Values[i].Name} = @{Values[i].Name}_1";
            }

            // Add conditions
            Data += " WHERE ";
            for (int i = 0; i < Conditions.Count; i++)
            {
                // Add name to command string
                if (i > 0)
                {
                    Data += " AND ";
                }
                Data += $"{Conditions[i].Name} = @{Conditions[i].Name}_2";
            }

            // Create SQL command
            var Command = new SqliteCommand(Data, Connection);

            // Add values from the value list
            foreach (var Value in Values)
            {
                Command.Parameters.AddWithValue($"{Value.Name}_1", Value.Value);
            }

            // Add conditions from the conditions list
            foreach (var Condition in Conditions)
            {
                Command.Parameters.AddWithValue($"{Condition.Name}_2", Condition.Value);
            }

            // Add data to our write queue
            WriteQueue.Enqueue(Command);
            ReadyEvent.Set();
        }