Exemple #1
0
        public static RowList <T> Get <T>(RowList <T> rowList = null, string database = null)
            where T : IDataModel, new()
        {
            // TODO: Add new config option "Verify data against DB"
            if (!SQLConnector.Enabled)
            {
                return(null);
            }

            if (!SQLUtil.IsTableVisible <T>())
            {
                return(null);
            }

            var result = new RowList <T>();

            using (var command = SQLConnector.CreateCommand(new SQLSelect <T>(rowList, database).Build()))
            {
                if (command == null)
                {
                    return(null);
                }

                var fields      = SQLUtil.GetFields <T>();
                var fieldsCount = fields.Select(f => f.Item3.First().Count).Sum();
                using (MySqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var instance = (T)Activator.CreateInstance(typeof(T));
                        var values   = GetValues(reader, fieldsCount);

                        var i = 0;
                        foreach (var field in fields)
                        {
                            SQLUtil.SetFieldValueByDB(instance, field, values, i);
                            i += field.Item3.First().Count;
                        }

                        result.Add(instance);
                    }
                }
            }

            return(result);
        }