public TableDefinition[] EnsureTables(string tableName, TableDefinition[] defaultTables)
 {
     TableDefinition[] tables;
     if (defaultTables.Length == 0)
     {
         tables = new TableDefinition[1];
         tables[0] = EnsureTable(tableName);
     }
     else
     {
         tables = new TableDefinition[defaultTables.Length];
     }
     for (var i = 0; i < defaultTables.Length; i++)
     {
         tables[i] = EnsureTable(tableName, defaultTables[i]);
     }
     return tables;
 }
        public void LoadTableDefinitions(string file)
        {
            var cfg = BasicTableDefinitions.Load(file);

            m_DbVersionLocation = cfg.DBVersion;

            foreach (var tableDef in cfg.Tables)
            {
                if (tableDef.Name == null) throw new ArgumentNullException("tableDef.Name", "Did you mis-type the Name attribute of the table with MainDataHolder = " + tableDef.MainDataHolder + " ?");
                if (TableDefinitionMap.ContainsKey(tableDef.Name))
                {
                    throw new DataHolderException("Duplicate Table definition \"{0}\" in File {1}", tableDef.Name, file);
                }

                var arrayConstraints = new Dictionary<string, ArrayConstraint>();
                if (tableDef.ArrayConstraints != null)
                {
                    foreach (var arrayConstraint in tableDef.ArrayConstraints)
                    {
                        arrayConstraints.Add(arrayConstraint.Column, arrayConstraint);
                    }
                }

                var primaryColumns = tableDef.PrimaryColumns;
                if (primaryColumns == null || primaryColumns.Length == 0)
                {
                    throw new DataHolderException("TableDefinition did not define any PrimaryColumns: " + tableDef);
                }

                var table = new TableDefinition(tableDef.Name, primaryColumns, arrayConstraints,
                    tableDef.Variables ?? VariableDefinition.EmptyArray)
                    {
                        MainDataHolderName = tableDef.MainDataHolder
                    };

                TableDefinitionMap.Add(tableDef.Name, table);
            }
        }
Exemplo n.º 3
0
		static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
			Dictionary<string, List<SimpleDataColumn>> mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
		{
			var column = fieldDef.Column;

			var tables = defs.EnsureTables(fieldDef.Table, defaultTables);
			object defaultValue;
			if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
			{
				defaultValue = StringParser.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
			}
			else
			{
				if (String.IsNullOrEmpty(column))
				{
					return;
				}
				defaultValue = null;
			}

			foreach (var table in tables)
			{
				var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
				if (!dataHolders.Contains(accessor.DataHolderDefinition))
				{
					dataHolders.Add(accessor.DataHolderDefinition);
				}

				var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

				SimpleDataColumn dataColumn;
                var dbAttrs = member.GetCustomAttributes<DBAttribute>();
                var attr = dbAttrs.Where(attribute => attribute is PersistentAttribute).FirstOrDefault() as PersistentAttribute;

				if (String.IsNullOrEmpty(column))
				{
					// use default value
					mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
				}
				else
				{
					dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
					if (dataColumn == null)
					{
                        var type = member.GetActualType();
                        if (attr != null)
                            type = attr.ReadType ?? type;
                        var reader = Converters.GetReader(type);
                        mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, reader));
					}
				}

				dataColumn.FieldList.Add(accessor);
			}
		}
 public TableDefinition EnsureTable(string tableName, TableDefinition defaultTable)
 {
     TableDefinition table;
     if (tableName != null)
     {
         table = EnsureTable(tableName);
     }
     else
     {
         table = defaultTable;
     }
     return table;
 }
Exemplo n.º 5
0
 public UpdateKeyValueList(TableDefinition table)
     : base(table)
 {
     Where = new List<KeyValuePair<string, object>>();
 }
Exemplo n.º 6
0
 public UpdateKeyValueList(TableDefinition table, List<KeyValuePair<string, object>> where)
     : this(table)
 {
     Where = where;
 }
Exemplo n.º 7
0
 public KeyValueListBase(TableDefinition table)
 {
     Table = table;
     Pairs = new List<KeyValuePair<string, object>>();
 }
Exemplo n.º 8
0
 public UpdateKeyValueList(TableDefinition table)
     : base(table)
 {
     this.Where = new List <KeyValuePair <string, object> >();
 }
Exemplo n.º 9
0
 public IDataReader CreateReader(TableDefinition def, int tableIndex)
 {
     return m_session.Batcher.ExecuteReader(m_selectCommands[tableIndex]);
 }
Exemplo n.º 10
0
		static void AddMapping(LightDBDefinitionSet defs, TableDefinition[] defaultTables, SimpleFlatFieldDefinition fieldDef,
			Dictionary<string, List<SimpleDataColumn>> mappedFields, IFlatDataFieldAccessor accessor, MemberInfo member)
		{
			var column = fieldDef.Column;

			var tables = defs.EnsureTables(fieldDef.Table, defaultTables);
			object defaultValue;
			if (!String.IsNullOrEmpty(fieldDef.DefaultStringValue))
			{
				defaultValue = Utility.Parse(fieldDef.DefaultStringValue, member.GetVariableType());
			}
			else
			{
				if (String.IsNullOrEmpty(column))
				{
					return;
				}
				defaultValue = null;
			}

			if (accessor.DataHolderDefinition.Type.Name.EndsWith("TrainerEntry"))
			{
				defs.ToString();
			}

			foreach (var table in tables)
			{
				var dataHolders = defs.m_tableDataHolderMap.GetOrCreate(table);
				if (!dataHolders.Contains(accessor.DataHolderDefinition))
				{
					dataHolders.Add(accessor.DataHolderDefinition);
				}

				var mappedFieldMap = mappedFields.GetOrCreate(table.Name);

				SimpleDataColumn dataColumn;

				if (String.IsNullOrEmpty(column))
				{
					// use default value
					mappedFieldMap.Add(dataColumn = new SimpleDataColumn(fieldDef.Name, defaultValue));
				}
				else
				{
					dataColumn = mappedFieldMap.Find((cmpField) => cmpField.ColumnName == column);
					if (dataColumn == null)
					{
						mappedFieldMap.Add(dataColumn = new SimpleDataColumn(column, Converters.GetReader(member.GetActualType())));
					}
				}

				dataColumn.FieldList.Add(accessor);
			}
		}
Exemplo n.º 11
0
        /// <summary>
        /// Fetches all sets of objects, defined through the given mapping.
        /// </summary>
        /// <returns></returns>
        public void Fetch()
        {
            List <Action> actionList = new List <Action>(10000);

            TableDefinition[]    tableDefinitions  = m_mapping.TableDefinitions;
            DataHolderDefinition holderDefinition1 = null;

            for (int tableIndex = 0; tableIndex < tableDefinitions.Length; ++tableIndex)
            {
                TableDefinition def = tableDefinitions[tableIndex];
                try
                {
                    using (IDataReader reader = m_db.CreateReader(def, tableIndex))
                    {
                        SimpleDataColumn[] columnDefinitions = def.ColumnDefinitions;
                        while (reader.Read())
                        {
                            object key = def.GetId(reader);
                            DataHolderDefinition holderDefinition2 = null;
                            IDataHolder          dataHolder1       = null;
                            object           firstValue            = null;
                            SimpleDataColumn simpleDataColumn      = null;
                            try
                            {
                                for (int index1 = 0; index1 < columnDefinitions.Length; ++index1)
                                {
                                    simpleDataColumn = columnDefinitions[index1];
                                    firstValue       = null;
                                    firstValue       = simpleDataColumn.ReadValue(reader);
                                    for (int index2 = 0; index2 < simpleDataColumn.FieldList.Count; ++index2)
                                    {
                                        IFlatDataFieldAccessor field = simpleDataColumn.FieldList[index2];
                                        holderDefinition1 = field.DataHolderDefinition;
                                        if (holderDefinition1 == null)
                                        {
                                            throw new DataHolderException(
                                                      "Invalid DataField did not have a DataHolderDefinition: " +
                                                      field);
                                        }
                                        IDataHolder dataHolder2;
                                        if (holderDefinition2 != field.DataHolderDefinition || dataHolder1 == null)
                                        {
                                            Dictionary <object, IDataHolder> dictionary =
                                                dataHolderMap.GetOrCreate(
                                                    holderDefinition1.Type);
                                            if (!dictionary.TryGetValue(key, out dataHolder2))
                                            {
                                                if (!def.IsDefaultTable)
                                                {
                                                    LightDBMgr.OnInvalidData(
                                                        "Invalid DataHolder was not defined in its Default-Tables - DataHolder: {0}; Id(s): {1}; Table: {2}",
                                                        (object)holderDefinition1, key, (object)def);
                                                    index1 = columnDefinitions.Length;
                                                    break;
                                                }

                                                dictionary.Add(key,
                                                               dataHolder2 =
                                                                   (IDataHolder)holderDefinition1.CreateHolder(firstValue));
                                                actionList.Add(dataHolder2.FinalizeDataHolder);
                                            }

                                            dataHolder1       = dataHolder2;
                                            holderDefinition2 = holderDefinition1;
                                        }
                                        else
                                        {
                                            dataHolder2 = dataHolder1;
                                        }

                                        field.Set(dataHolder2, firstValue);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                object[] objArray1;
                                if (!(key is Array))
                                {
                                    objArray1 = new object[1] {
                                        key
                                    }
                                }
                                ;
                                else
                                {
                                    objArray1 = (object[])key;
                                }
                                object[] objArray2 = objArray1;
                                throw new DataHolderException(ex,
                                                              "Unable to parse data-cell (Column: {0}, Id(s): {1}{2})", (object)simpleDataColumn,
                                                              (object)objArray2.ToString(", ",
                                                                                         idObj =>
                                                                                         idObj.GetType().IsEnum
                        ? Convert.ChangeType(idObj, Enum.GetUnderlyingType(idObj.GetType()))
                        : idObj), firstValue != null ? (object)(", Value: " + firstValue) : (object)"");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new DataHolderException(ex, "Failed to read from Table \"{0}\" {1}", (object)def,
                                                  holderDefinition1 != null ? (object)("DataHolder: " + (object)holderDefinition1) : (object)"");
                }
            }

            for (int index = 0; index < actionList.Count; ++index)
            {
                actionList[index]();
            }
            dataHolderMap.Clear();
            m_fetched = true;
        }
Exemplo n.º 12
0
 public KeyValueListBase(TableDefinition table)
 {
     this.Table = table;
     this.Pairs = new List <KeyValuePair <string, object> >();
 }
Exemplo n.º 13
0
 public KeyValueListBase(TableDefinition table, List <KeyValuePair <string, object> > pairs)
     : this(table)
 {
     this.Pairs = pairs;
 }
Exemplo n.º 14
0
        private static void InitVars(TableDefinition table, SimpleDataColumn[] fields)
        {
            var i = 0;
            foreach (var variable in table.Variables)
            {
                IDataField field;
                if (!table.MainDataHolder.Fields.TryGetValue(variable.Name, out field) || !(field is IFlatDataFieldAccessor))
                {
                    throw new DataHolderException("Table \"{0}\" defined invalid Variable {1}. Name does not refer to an actual property within DataHolder {2}.",
                        table, variable, table.MainDataHolder);
                }

                try
                {
                    var defaultValue = variable.Eval(field.MappedMember.GetVariableType());
                    var fld = new SimpleDataColumn(variable.Name, defaultValue);
                    fld.FieldList.Add((IFlatDataFieldAccessor)field);
                    fields[i++] = fld;
                }
                catch (Exception e)
                {
                    throw new DataHolderException(e, "Unable to parse default-value \"{0}\" to Type \"{1}\" from Variable \"{2}\"",
                                                  variable.StringValue, field.MappedMember.Name, variable.Name);
                }
            }
        }
Exemplo n.º 15
0
 public KeyValueListBase(TableDefinition table, List<KeyValuePair<string, object>> pairs)
     : this(table)
 {
     Pairs = pairs;
 }
Exemplo n.º 16
0
        private void RegisterDefintion(LightRecordXmlConfig cfg, Dictionary<string, List<SimpleDataColumn>> fieldMap)
        {
            var dataHolders = DataHolderDefinitions;
            XmlDataHolderDefinition lastDef = null;
            foreach (XmlDataHolderDefinition dataRawDef in cfg)
            {
                try
                {
                    if (dataRawDef.Name == null)
                    {
                        throw new DataHolderException("Invalid DataHolder-definition has no name ({0}).",
                            lastDef == null ? "First in file" : "After: " + lastDef);
                    }

                    dataRawDef.EnsureFieldsNotNull();

                    var dataDef = dataHolders.Where(def =>
                        def.Name.Equals(dataRawDef.Name, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

                    if (dataDef == null)
                    {
                        LightDBMgr.OnInvalidData("Invalid DataHolder-definition refers to non-existing DataHolder: " + dataRawDef.Name);
                        continue;
                    }

                    TableDefinition[] oldTables, defaultTables;
                    // Set DefaultTable
                    //if (dataRawDef.DefaultTables == null)
                    //{
                    //    throw new DataHolderException("DataHolder-definition did not specify any DefaultTable(s): " + dataRawDef);
                    //}

                    if (DefaultTables.TryGetValue(dataDef.Name, out defaultTables))
                    {
                        oldTables = defaultTables;
                    }
                    else
                    {
                        oldTables = null;
                        if (dataRawDef.DefaultTables != null)
                        {
                            defaultTables = new TableDefinition[dataRawDef.DefaultTables.Length];
                        }
                        else
                        {
                            defaultTables = new TableDefinition[0];
                        }
                    }

                    for (var i = 0; i < defaultTables.Length; i++)
                    {
                        var tableName = dataRawDef.DefaultTables[i].Trim();

                        TableDefinition defaultTable;
                        if (!TableDefinitionMap.TryGetValue(tableName, out defaultTable))
                        {
                            throw new DataHolderException("DefaultTable \"{0}\" of DataHolder \"{1}\" is not defined - " +
                                                          "Make sure to define the table in the Table collection.", tableName, dataRawDef);
                        }
                        //if (DefaultTables.ContainsKey(dataDef.Name))
                        //{
                        //    throw new DataHolderException("Found duplicate DataHolder-definition: " + dataDef.Name + " ({0})", cfg.FileName);
                        //}

                        defaultTables[i] = defaultTable;
                        defaultTable.SetMainDataHolder(dataDef, true);
                    }

                    DefaultTables[dataDef.Name] = defaultTables;

                    if (dataRawDef.DataHolderName.Contains("Trainer"))
                    {
                        ToString();
                    }
                    AddFieldMappings(dataRawDef.Fields, dataDef.Fields, fieldMap);
                    if (oldTables != null)
                    {
                        var offset = oldTables.Length;
                        Array.Resize(ref oldTables, oldTables.Length + defaultTables.Length);
                        Array.Copy(defaultTables, 0, oldTables, offset, defaultTables.Length);
                        DefaultTables[dataDef.Name] = oldTables;
                    }
                    lastDef = dataRawDef;
                }
                catch (Exception e)
                {
                    throw new DataHolderException(e, "Failed to parse DataHolder-definition \"" + dataRawDef + "\" from {0}", cfg.FileName);
                }
            }
        }
Exemplo n.º 17
0
 public UpdateKeyValueList(TableDefinition table, List <KeyValuePair <string, object> > where)
     : this(table)
 {
     this.Where = where;
 }