public static string GetValueByKey(string tableName, string columnName, object key, bool extension = false)
        {
            string value    = string.Empty;
            var    relation = TableKeys.GetColumnRelation(tableName, columnName, key);

            if (string.IsNullOrEmpty(relation.columnName) == false)
            {
                var cacheDict = GetValuesCache();
                var cacheKey  = CreateCacheKey(tableName, columnName, key, true, extension);
                if (cacheDict.ContainsKey(cacheKey))
                {
                    value = cacheDict[cacheKey];
                }
                else
                {
                    var    table        = TableManager.GetTable(relation.tableName);
                    string valueColName = GetValueColumnName(table, relation.columnName);
                    try {
                        if (extension)
                        {
                            value = SelectValueByKey(table, relation.columnName, key, valueColName);
                        }
                        else
                        {
                            value = TableManager.SelectScalarFromTable(table, relation.columnName, key, valueColName).ToString();
                        }
                        cacheDict.Add(cacheKey, value.ToString());
                    } catch {
                        //TODO вывод в лог
                    }
                }
            }
            return(value);
        }
        /// <param name="tableName">Текущая таблица</param>
        /// <param name="colName">Название текущей колонки</param>
        public static object GetKeyByValue(string tableName, string colName, object value)
        {
            object result   = value;
            var    relation = TableKeys.GetColumnRelation(tableName, colName);

            if (string.IsNullOrWhiteSpace(relation.columnName) == false)
            {
                int key;
                if (value is int)//если пришел уже ключ
                {
                    result = value;
                }
                else if (int.TryParse(value.ToString(), out key))//пробуем парсить ключ
                {
                    result = key;
                }
                else
                {
                    var cacheDict = GetKeysCache();
                    var cacheKey  = CreateCacheKey(tableName, colName, value, false, false);
                    if (cacheDict.ContainsKey(cacheKey))//если закешировано
                    {
                        result = cacheDict[cacheKey];
                    }
                    else
                    {
                        var    relationTable   = TableManager.GetTable(relation.tableName);
                        var    valueColumnName = GetValueColumnName(relationTable, relation.columnName);
                        object selectObj       = SelectKeyByValue(relationTable, relation.columnName, value, valueColumnName);
                        if (selectObj != DBNull.Value)
                        {
                            result = selectObj;
                            cacheDict.Add(cacheKey, result);
                        }
                    }
                }
            }
            return(result);
        }
        public static SortedDictionary <string, int> GetRelativeItemsSource(string tableName, string columnName, bool addNullItem)
        {
            var relation = TableKeys.GetColumnRelation(tableName, columnName);
            var result   = new SortedDictionary <string, int>();

            if (string.IsNullOrEmpty(relation.tableName) == false)
            {
                var       table        = TableManager.GetTable(relation.tableName);
                string    valueColName = GetValueColumnName(table, relation.columnName);
                DataRow[] rows         = table.Select();
                foreach (DataRow row in rows)
                {
                    var key   = (int)row[relation.columnName];
                    var value = GetRowStringView(row, relation.columnName, valueColName);
                    result.Add(value, key);
                }
            }
            if (addNullItem)
            {
                result.Add("", -1);
            }
            return(result);
        }
        public static SortedDictionary <string, int> GetTableKeyItems(string tableName, string columnName, bool addNullItem = true)
        {
            DataTable tab    = TableManager.GetTable(tableName);
            bool      isFk   = TableKeys.CheckColumnKeyType(tableName, columnName, TableKeys.KeyType.ForeignKey);
            var       result = new SortedDictionary <string, int>();

            if (isFk)
            {
                var keysList = GetUniqueSet(tab, columnName);
                foreach (var key in keysList)
                {
                    var value = GetValueByKey(tableName, columnName, key, true);
                    if (result.ContainsKey(value) == false)
                    {
                        result.Add(value, (int)key);
                    }
                }
                if (result.ContainsKey("") == false && addNullItem)
                {
                    result.Add("", -1);
                }
            }
            return(result);
        }
        public static SortedSet <string> GetTableItems(string tableName, string columnName)
        {
            DataTable tab        = TableManager.GetTable(tableName);
            bool      isFk       = TableKeys.CheckColumnKeyType(tableName, columnName, TableKeys.KeyType.ForeignKey);
            var       keysList   = GetUniqueSet(tab, columnName);
            var       valuesList = new SortedSet <string>();

            foreach (var value in keysList)
            {
                if (isFk)
                {
                    valuesList.Add(GetValueByKey(tableName, columnName, value, true));
                }
                else
                {
                    valuesList.Add(value.ToString());
                }
            }
            if (valuesList.Contains("") == false)
            {
                valuesList.Add("");
            }
            return(valuesList);
        }
        public static DataRow DecodeRow(DataRow sourceRow, string tableName)
        {
            var destTable = TableManager.GetTable(tableName);

            return(DecodeRow(sourceRow, destTable));
        }