コード例 #1
0
ファイル: Database.cs プロジェクト: Etamiin/Inertia
        /// <summary>
        /// Selects the first element from the specified <see cref="Table"/> with the specified parameters and returns an instance of the <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="columnsToSelect"></param>
        /// <param name="condition"></param>
        /// <param name="distinct"></param>
        /// <returns></returns>
        public T Select <T>(SqlCondition condition, bool distinct, params string[] columnsToSelect) where T : Table
        {
            T table = null;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out table))
                {
                    if (condition == null)
                    {
                        condition = new SqlCondition();
                    }

                    condition.Limit(1);

                    var fields = Table.GetFields <T>();

                    cmd.SetQuery(QueryBuilder.GetSelectQuery(table, condition, columnsToSelect, distinct), condition);
                    cmd.OnReader((reader) => {
                        for (var i = 0; i < reader.FieldCount; i++)
                        {
                            var field = fields.FirstOrDefault((f) => f.Name == reader.GetName(i));
                            if (field != null)
                            {
                                object value = null;
                                if (field.FieldType == typeof(bool))
                                {
                                    value = reader.GetBoolean(i);
                                }
                                else if (!reader.IsDBNull(i))
                                {
                                    value = reader.GetValue(i);
                                }

                                field.SetValue(table, value);
                            }
                        }
                    });
                }
            });

            return(table);
        }
コード例 #2
0
ファイル: Database.cs プロジェクト: Etamiin/Inertia
        public T Min <T>(string columnName, SqlCondition condition) where T : Table
        {
            T table = null;

            ExecuteCommand((cmd) => {
                if (SqlManager.CreateTableInstance(out table))
                {
                    cmd.SetQuery(QueryBuilder.GetMinQuery(table, columnName, condition), condition);
                    cmd.OnReader((reader) => {
                        var field = Table.GetFields <T>().FirstOrDefault((f) => f.Name == columnName);
                        if (field != null)
                        {
                            field.SetValue(table, reader.GetValue(0));
                        }
                    });
                }
            });

            return(table);
        }