示例#1
0
        public int Create(AbstractDataItem item)
        {
            if (this._conString == null)
            {
                throw new MissingFieldException("SQLiteConnectionString");
            }

            if (item == null)
            {
                throw new ArgumentNullException("DataItem");
            }

            item.State   = (int)DataItemState.Default;
            item.Created = DateTime.UtcNow;
            item.Updated = null;

            using (var db = new SQLiteConnection(this._conString)) {
                if (db.Insert(item) == 1)
                {
                    return(item.Id);
                }
            }

            return(0);
        }
示例#2
0
 public void AddItem(AbstractDataItem item)
 {
     if (!Items.Contains(item))
     {
         Items.Add(item);
     }
 }
示例#3
0
        public AbstractFormsController(AbstractFormContext context)
        {
            _context = context;

            if (_context.AbstractForms.Count() == 0)
            {
                AbstractForm form = new AbstractForm();
                form.Name = "Display Type 1";
                AbstractSection section = new AbstractSection();
                section.Label = "Demographics";

                AbstractDataItem item = new AbstractDataItem();
                item.Label = "First Name";
                section.DataItems.Add(item);

                item       = new AbstractDataItem();
                item.Label = "Last Name";
                section.DataItems.Add(item);

                item       = new AbstractDataItem();
                item.Label = "Birth Date";
                section.DataItems.Add(item);

                item       = new AbstractDataItem();
                item.Label = "Street Address";
                section.DataItems.Add(item);

                item       = new AbstractDataItem();
                item.Label = "City";
                section.DataItems.Add(item);

                item       = new AbstractDataItem();
                item.Label = "State";
                section.DataItems.Add(item);

                form.Sections.Add(section);

                _context.AbstractForms.Add(form);

                _context.SaveChanges();
            }
        }
示例#4
0
        public int Update(AbstractDataItem item)
        {
            if (String.IsNullOrEmpty(this.ConString))
            {
                throw new MissingFieldException("ConnectionString");
            }

            if (item == null)
            {
                throw new ArgumentNullException("IdItem");
            }

            if (item.Id < 0)
            {
                throw new ArithmeticException("InvalidId");
            }

            item.Updated = DateTime.UtcNow;

            Type dstType  = item.GetType();
            var  propList = dstType.GetProperties();

            if (GetTableInfo(dstType, out string tblName, out List <Tuple <string, Type> > colNameList))
            {
                List <Tuple <string, object> > valueList = null;
                foreach (var prop in propList.Where(
                             obj => !obj.Name.Equals("Id") &&
                             !obj.Name.Equals("Created")))
                {
                    var attrList = prop.GetCustomAttributes(true);
                    if (attrList != null)
                    {
                        foreach (var attr in attrList)
                        {
                            if (attr is AppColumnAttribute)
                            {
                                var appAttr = attr as AppColumnAttribute;
                                if (appAttr.CanUpdate)
                                {
                                    if (valueList == null)
                                    {
                                        valueList = new List <Tuple <string, object> >();
                                    }

                                    valueList.Add(new Tuple <string, object>(
                                                      appAttr.Name, prop.GetValue(item)));

                                    break;
                                }
                            }
                        }
                    }
                }

                if (!DictionaryUtils.IsNullOrEmpty(valueList))
                {
                    using (var con = new SqliteConnection(this.ConString)) {
                        using (var cmd = con.CreateCommand()) {
                            var tmpList = new List <string>();
                            foreach (var kvItem in valueList)
                            {
                                tmpList.Add($"{kvItem.Item1}=@{kvItem.Item1}");
                                cmd.Parameters.Add(new SqliteParameter($"@{kvItem.Item1}", kvItem.Item2));
                            }

                            cmd.CommandText = $"UPDATE {tblName} SET {String.Join(',', tmpList)} " +
                                              $"WHERE id={item.Id}";

                            con.Open();

                            this._logger.LogDebug(cmd.CommandText);
                            return(cmd.ExecuteNonQuery());
                        }
                    }
                }
            }
            return(0);
        }
示例#5
0
        public Int64 Create(AbstractDataItem item)
        {
            if (String.IsNullOrEmpty(this.ConString))
            {
                throw new MissingFieldException("ConnectionString");
            }

            if (item == null)
            {
                throw new ArgumentNullException("IdItem");
            }

            item.State   = (int)DataItemState.Default;
            item.Created = DateTime.UtcNow;
            item.Updated = null;

            Type dstType  = item.GetType();
            var  propList = dstType.GetProperties();

            if (GetTableInfo(dstType, out string tblName, out List <Tuple <string, Type> > colNameList))
            {
                List <Tuple <string, object> > valueList = null;
                foreach (var prop in propList)
                {
                    var attrList = prop.GetCustomAttributes(true);
                    if (attrList != null)
                    {
                        foreach (var attr in attrList)
                        {
                            if (attr is AppColumnAttribute)
                            {
                                var appAttr = attr as AppColumnAttribute;
                                if (appAttr.CanInsert)
                                {
                                    if (valueList == null)
                                    {
                                        valueList = new List <Tuple <string, object> >();
                                    }

                                    valueList.Add(new Tuple <string, object>(
                                                      appAttr.Name, prop.GetValue(item)));
                                }
                                break;
                            }
                        }
                    }
                }

                if (!DictionaryUtils.IsNullOrEmpty(valueList))
                {
                    using (var con = new SqliteConnection(this.ConString)) {
                        using (var cmd = con.CreateCommand()) {
                            cmd.CommandText = $"INSERT INTO {tblName} ({String.Join(',', valueList.Select(obj => obj.Item1))}) " +
                                              $"VALUES ({String.Join(',', valueList.Select(obj => "@" + obj.Item1))})";

                            foreach (var kvItem in valueList)
                            {
                                cmd.Parameters.Add(new SqliteParameter($"@{kvItem.Item1}", kvItem.Item2));
                            }

                            con.Open();

                            this._logger.LogDebug(cmd.CommandText);
                            int result = cmd.ExecuteNonQuery();
                            if (result > 0)
                            {
                                cmd.CommandText = "select last_insert_rowid()";

                                this._logger.LogDebug(cmd.CommandText);
                                return((Int64)cmd.ExecuteScalar());
                            }
                        }
                    }
                }
            }
            return(0);
        }
示例#6
0
 internal void SetActualData(AbstractDataItem dataItem)
 {
     _dataItem = dataItem;
 }