예제 #1
0
        public static int Add <T>(this IDBComplete <T> db, DatabaseLogger Logger = null)
            where T : new()
        {
            var control = db.GetBControl();

            if (control.PrimaryKey == null)
            {
                throw new NullReferenceException("No primary key found");
            }
            int ID = control.Insert(!control.PrimaryKey, Logger);  // assign id

            control.PrimaryKey.AssignRawValue(ID);
            return(ID);
        }
예제 #2
0
        bool Get(string Table, List <ColumnControl> stack, BuiltCondition condition, DatabaseLogger logger)
        {
            var   sel = Select(stack.Select(c => c.Key).ToArray()).From(Table);
            Ender get;

            if (condition == null)
            {
                get = sel;
            }
            else
            {
                get = sel.WhereCondition(condition);
            }
            try
            {
                using (var r = get.QuerySingle())
                {
                    if (r)
                    {
                        logger.Add(new DatabaseLog {
                            Level = LogLevel.Info, Query = get.FullQuery, Message = "Ran get query"
                        });
                        foreach (var c in stack)
                        {
                            c.SetValue(r);
                        }
                        return(true);
                    }
                    else
                    {
                        logger.Add(new DatabaseLog {
                            Level = LogLevel.Warning, Query = get.FullQuery, Message = "No results"
                        });
                    }
                    return(false);
                }
            }
            catch (Exception e)
            {
                logger.Add(new DatabaseLog {
                    Level = LogLevel.Error, Query = get.FullQuery, Message = e.Message
                });
                throw;
            }
        }
예제 #3
0
        public int Count(string Table, BuiltCondition Condition = null, DatabaseLogger Logger = null)
        {
            var   frm   = Select("COUNT(*)").From(Table);
            Ender ender = Condition == null ? (Ender)frm : frm.WhereCondition(Condition);

            try
            {
                using (var end = ender.QueryValue())
                {
                    Logger.Add(new DatabaseLog {
                        Level = LogLevel.Info, Message = "checked exists", Query = ender.FullQuery
                    });
                    return(end);
                }
            }
            catch (Exception e)
            {
                Logger.Add(new DatabaseLog {
                    Level = LogLevel.Error, Message = e.Message, Query = ender.FullQuery
                });
                throw;
            }
        }
예제 #4
0
        int Update(string Table, List <ColumnControl> stack, BuiltCondition condition, DatabaseLogger logger)
        {
            var upd = Update(Table);

            UpdateQueryer.Pairer pairer = null;
            foreach (var col in stack)
            {
                pairer = upd.Pair(col.Key, col.Value);
            }
            Ender query = pairer;

            if (condition != null)
            {
                query = pairer.WhereCondition(condition);
            }
            try
            {
                int updated = query.Execute();
                logger.Add(new DatabaseLog {
                    Level   = updated == 0? LogLevel.Warning : LogLevel.Info,
                    Query   = query.FullQuery,
                    Message = updated == 0? "No rows were updated" : $"Updated {updated} rows"
                });
                return(updated);
            }
            catch (Exception e)
            {
                logger.Add(new DatabaseLog {
                    Level = LogLevel.Error, Query = query.FullQuery, Message = e.Message
                });
                throw;
            }
        }
예제 #5
0
 public int Update <T>(string Table, ColumnController <T> Columns, BuiltCondition Condition = null, DatabaseLogger Logger = null)
 => Update(Table, Columns.Controls, Condition, Logger);
예제 #6
0
 public int Update(string Table, ColumnControl Columns, BuiltCondition Condition = null, DatabaseLogger Logger = null)
 => Update(Table, Columns.StackedControls, Condition, Logger);
예제 #7
0
        List <T> GetAll <T>(string Table, ColumnController <T> Control, List <ColumnControl> stack, BuiltCondition condition, DatabaseLogger logger)
            where T : new()
        {
            var   sel = Select(stack.Select(c => c.Key).ToArray()).From(Table);
            Ender get;

            if (condition == null)
            {
                get = sel;
            }
            else
            {
                get = sel.WhereCondition(condition);
            }

            logger.Add(new DatabaseLog {
                Level = LogLevel.Info, Query = get.FullQuery, Message = "Running get query"
            });
            try
            {
                using (var r = get.Query())
                {
                    var vals = new List <T>();
                    if (r)
                    {
                        var original = Control.Content;
                        try
                        {
                            while (r.Read())
                            {
                                var val = new T();
                                Control.Content = val;
                                foreach (var c in stack)
                                {
                                    c.SetValue(r);
                                }
                                vals.Add(val);
                            }
                        }
                        catch (Exception e)
                        {
                            Control.Content = original;
                            throw e;
                        }
                        Control.Content = original;
                    }
                    else
                    {
                        logger.Add(new DatabaseLog {
                            Level = LogLevel.Warning, Query = get.FullQuery, Message = "No results"
                        });
                    }
                    return(vals);
                }
            }
            catch (Exception e)
            {
                logger.Add(new DatabaseLog {
                    Level = LogLevel.Error, Query = get.FullQuery, Message = e.Message
                });
                throw e;
            }
        }
예제 #8
0
        public List <T> GetAll <T>(string Table, ColumnController <T> Controller, ColumnControl Control = null, BuiltCondition Condition = null, DatabaseLogger Logger = null)
            where T : new()
        {
            var stack = Control?.StackedControls ?? Controller.Controls;

            return(GetAll(Table, Controller, stack, Condition, Logger));
        }
예제 #9
0
 public int Insert <T>(string Table, ColumnController <T> Columns, DatabaseLogger Logger = null)
 => Insert(Table, Columns.Controls, Logger);
예제 #10
0
 public int Insert(string Table, ColumnControl Columns, DatabaseLogger Logger = null)
 => Insert(Table, Columns.StackedControls, Logger);
예제 #11
0
 public bool Get <T>(string Table, ColumnController <T> Control, BuiltCondition Condition = null, DatabaseLogger Logger = null)
 => Get(Table, Control.Controls, Condition, Logger);
예제 #12
0
 public bool Get(string Table, ColumnControl Control, BuiltCondition Condition = null, DatabaseLogger Logger = null)
 => Get(Table, Control.StackedControls, Condition, Logger);
예제 #13
0
 public bool Exists(string Table, BuiltCondition Condition = null, DatabaseLogger Logger = null)
 => Count(Table, Condition, Logger) > 0;