コード例 #1
0
        private void CheckAddConstraints(Dictionary <string, object> data)
        {
            foreach (var item in this.Indexs.Where(o => o.IsUnique))
            {
                if (data.ContainsKey(item.FieldName))
                {
                    var value = data[item.FieldName];

                    if (value == null)
                    {
                        value = IndexHelper.DefaultValue(item.keyType);

                        // throw new Exception("Value not provided for Index: " + item.FieldName);
                    }

                    //    if (IndexHelper.IsDefaultValue(value, item.keyType))
                    //   {
                    //  throw new Exception("Value not provided for Index: " + item.FieldName);
                    // TODO: think of whether this is needed or not.
                    //  }

                    var block = item.Get(value);
                    if (block > 0)
                    {
                        throw new Exception("Uniqueness contraints validation failed. Index: " + item.FieldName);
                    }
                }
                else
                {
                    throw new Exception("Value not provided for index: " + item.FieldName);
                }
            }
        }
コード例 #2
0
        private void Init(Setting setting)
        {
            this.Indexs  = IndexHelper.CreatIndexs(setting, this.ObjectFolder);
            this.Setting = setting;
            var primary = this.Indexs.Find(o => o.IsPrimaryKey);

            this.PrimaryKey = primary.FieldName;
            ObjectConverter = new Dynamic.Converter.ObjectConverter(this.Setting.Columns.ToList(), this.PrimaryKey);
        }
コード例 #3
0
        public Dictionary <string, object> PrepareData(object dataobj, bool Update = false)
        {
            // prepare key...
            Dictionary <string, object> data = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            System.Collections.IDictionary idict = dataobj as System.Collections.IDictionary;

            IDictionary <string, object> dynamicobj = null;
            Type objecttype = null;

            if (idict == null)
            {
                dynamicobj = dataobj as IDictionary <string, object>;
            }

            if (idict == null && dynamicobj == null)
            {
                objecttype = dataobj.GetType();
            }

            foreach (var item in this.Setting.Columns)
            {
                if (item.IsSystem)
                {
                    // the only system field is the id fields.
                    object Value = null;

                    if (this.PrimaryKey != Dynamic.Constants.DefaultIdFieldName)
                    {
                        if (idict != null)
                        {
                            Value = Accessor.GetValueIDict(idict, this.PrimaryKey, item.ClrType);
                        }
                        else if (dynamicobj != null)
                        {
                            Value = Accessor.GetValue(dynamicobj, this.PrimaryKey, item.ClrType);
                        }
                        else
                        {
                            Value = Accessor.GetValue(dataobj, objecttype, this.PrimaryKey, item.ClrType);
                        }

                        if (Value == null)
                        {
                            var col = this.Setting.Columns.First(o => o.Name == this.PrimaryKey);
                            if (col != null)
                            {
                                Value = IndexHelper.DefaultValue(col.ClrType);
                            }
                        }
                    }
                    if (Value == null || _ParseKey(Value) == default(Guid))
                    {
                        if (idict != null)
                        {
                            Value = Accessor.GetValueIDict(idict, item.Name, item.ClrType);
                        }
                        else if (dynamicobj != null)
                        {
                            Value = Accessor.GetValue(dynamicobj, item.Name, item.ClrType);
                        }
                        else
                        {
                            Value = Accessor.GetValue(dataobj, objecttype, item.Name, item.ClrType);
                        }
                    }

                    if (Value == null || _ParseKey(Value) == default(Guid))
                    {
                        Value = Helper.IndexHelper.NewTimeGuid();
                    }
                    else
                    {
                        Value = _ParseKey(Value);
                    }
                    if (!data.ContainsKey(item.Name))
                    {
                        data.Add(item.Name, Value);
                    }
                }

                else
                {
                    object Value = null;

                    if (idict != null)
                    {
                        Value = Accessor.GetValueIDict(idict, item.Name, item.ClrType);
                    }
                    else if (dynamicobj != null)
                    {
                        Value = Accessor.GetValue(dynamicobj, item.Name, item.ClrType);
                    }
                    else
                    {
                        Value = Accessor.GetValue(dataobj, objecttype, item.Name, item.ClrType);
                    }

                    if (item.IsIncremental && !Update)
                    {
                        if (Value == null || Accessor.ChangeType <long>(Value) == 0)
                        {
                            var index = this.Indexs.Find(o => o.FieldName == item.Name);
                            Value = index.NextIncrement();
                        }

                        if (item.IsPrimaryKey && Value != null)
                        {
                            var keyvalue = _ParseKey(Value);
                            data[Dynamic.Constants.DefaultIdFieldName] = keyvalue;
                        }
                    }

                    if (Value == null)
                    {
                        if (item.IsIncremental)
                        {
                            var index = this.Indexs.Find(o => o.FieldName == item.Name);
                            Value = index.NextIncrement();
                        }
                        else if (item.IsIndex || item.IsPrimaryKey)
                        {
                            Value = IndexHelper.DefaultValue(item.ClrType);
                        }
                    }

                    if (Value == null && (item.IsIndex || item.IsPrimaryKey))
                    {
                        Value = IndexHelper.DefaultValue(item.ClrType);
                    }

                    // var rightvalue = Accessor.ChangeType(Value, item.ClrType);

                    data.Add(item.Name, Value);
                }
            }
            return(data);
        }
コード例 #4
0
        public void UpdateSetting(Setting newsetting)
        {
            ensureincremental(newsetting);

            var check = SettingHelper.UpdateSetting(newsetting.Columns.ToList(), this.Setting);

            lock (_Locker)
            {
                if (check.ShouldRebuild)
                {
                    RebuildTable(check.NewSetting);
                }
                else if (check.HasChange)
                {
                    // reinit index... and try to find remove or create index.

                    var newindexs = IndexHelper.CreatIndexs(check.NewSetting, this.ObjectFolder);

                    foreach (var item in newindexs)
                    {
                        item.Close();
                    }

                    List <string> toremove = new List <string>();

                    foreach (var item in this.Indexs)
                    {
                        var newer = newindexs.Find(o => o.FieldName == item.FieldName);
                        if (newer == null)
                        {
                            toremove.Add(item.FieldName);
                        }
                    }

                    Dictionary <string, bool> indexToRebuild = new Dictionary <string, bool>();

                    foreach (var item in newindexs)
                    {
                        var older = this.Indexs.Find(o => o.FieldName == item.FieldName);
                        if (older == null)
                        {
                            indexToRebuild.Add(item.FieldName, item.IsUnique);
                        }
                    }

                    foreach (var item in indexToRebuild)
                    {
                        this.CreateIndex(item.Key, item.Value, check.NewSetting);
                    }

                    foreach (var item in toremove)
                    {
                        this.RemoveIndex(item);
                    }

                    this.Setting = check.NewSetting;

                    SettingHelper.WriteSetting(this.SettingFile, this.Setting);

                    var primary = newindexs.Find(o => o.IsPrimaryKey);

                    this.ObjectConverter = new Dynamic.Converter.ObjectConverter(this.Setting.Columns.ToList(), primary.FieldName);

                    this.PrimaryKey = primary.FieldName;

                    foreach (var item in this.Indexs)
                    {
                        item.Close();
                    }

                    this.Indexs = newindexs;

                    foreach (var item in this.Indexs)
                    {
                        item.Close();
                    }
                }

                this.Close();
            }
        }
コード例 #5
0
        public void CreateIndex(string fieldName, bool unique = false, Setting setting = null)
        {
            if (setting == null)
            {
                setting = this.Setting;
            }
            lock (_Locker)
            {
                if (this.Indexs.Find(o => o.FieldName == fieldName) != null)
                {
                    throw new Exception("the index " + fieldName + " already exits");
                }

                if (fieldName == Constants.DefaultIdFieldName)
                {
                    throw new Exception(Constants.DefaultIdFieldName + " is reserved");
                }

                var col = setting.Columns.FirstOrDefault(o => o.Name == fieldName);

                if (col == null || col.Length == int.MaxValue)
                {
                    throw new Exception("Index fieldname must be in the column with fixed length");
                }


                string indexfile = IndexHelper.GetIndexFile(this.ObjectFolder, fieldName);

                var newindex = IndexHelper.CreateIndex(fieldName, col.ClrType, indexfile, unique, col.Length);

                if (col.IsIncremental)
                {
                    newindex.IsIncremental = true;
                    newindex.Seed          = col.Seed;
                    newindex.Increment     = col.Increment;
                }

                var primaryindex = this.Indexs.Find(o => o.IsSystem);

                foreach (var item in primaryindex.AllItems(true))
                {
                    object fieldvalue = null;
                    if (col.IsIncremental)
                    {
                        fieldvalue = newindex.NextIncrement();
                    }
                    else
                    {
                        var value = this._getvalue(item);
                        // need to check uniquness constraints...
                        fieldvalue = Accessor.GetValue(value, fieldName, col.ClrType);
                    }

                    if (unique)
                    {
                        if (fieldvalue == null)
                        {
                            newindex.Close();
                            newindex.DelSelf();
                            throw new Exception("Create index failed, Value not provided for Index: " + newindex.FieldName);
                        }
                        else
                        {
                            var oldblock = newindex.Get(fieldvalue);
                            if (oldblock > 0)
                            {
                                newindex.Close();
                                newindex.DelSelf();
                                throw new Exception("Violate uniqueness constraints for Index: " + newindex.FieldName);
                            }
                        }
                    }

                    if (fieldvalue == null)
                    {
                        fieldvalue = IndexHelper.DefaultValue(col.ClrType);
                    }
                    newindex.Add(fieldvalue, item);
                }


                col.IsIndex  = true;
                col.IsUnique = unique;
                this.Indexs.Add(newindex);
            }
        }