Пример #1
0
        public ICollection <Type> Select <Type> (DbQuery query, Func <DbRow, Type> construct_from_container)
        {
            var sql_query = new DbQueryToSql(this, query);

            Collection <Type> results = new Collection <Type>();

            using (SqlDataReader reader = SqlQuery.Select(connection_string, sql_query.Query, CommandType.Text, CommandBehavior.CloseConnection, sql_query.Parameters))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        DbRow c = new DbRow(this);

                        c.LoadValuesWithoutRead(reader);

                        Type t = construct_from_container(c);

                        results.Add(t);
                    }
                }
            }

            return(results);
        }
Пример #2
0
        public void Select(DbQuery query)
        {
            var sql_query = new DbQueryToSql(this, query);

            using (SqlDataReader reader = SqlQuery.Select(connection_string, sql_query.Query, CommandType.Text, CommandBehavior.SingleResult | CommandBehavior.CloseConnection, sql_query.Parameters))
            {
                LoadValues(reader);
            }
        }
Пример #3
0
        public int SelectCount(DbQuery query)
        {
            string select_columns = query.Columns;
            string from           = query.From;

            string where = query.Where;
            string join = null;

            if (string.IsNullOrEmpty(select_columns))
            {
                select_columns = GetSelectColumns();
            }
            if (string.IsNullOrEmpty(from))
            {
                from = table_name;
            }
            if (!string.IsNullOrEmpty(where))
            {
                where = string.Format("WHERE {0}", where);
            }

            List <SqlQueryParameter> sql_parameters = null;

            if (query.Parameters != null)
            {
                sql_parameters = new List <SqlQueryParameter>(query.Parameters);
            }

            if (query.Join != null)
            {
                var join_clause = query.Join;
                var join_query  = new DbQueryToSql(this, join_clause.Query);

                join = string.Format("{0} JOIN {1} ON {2}", join_clause.Type, join_query.Query, join_clause.Condition);

                if (join_query.Parameters != null)
                {
                    if (sql_parameters == null)
                    {
                        sql_parameters = new List <SqlQueryParameter>();
                    }

                    sql_parameters.AddRange(join_query.Parameters);
                }
            }

            string select_statement;

            select_statement = string.Format("SELECT COUNT ({0}) FROM {1} {2} {3}",
                                             select_columns,
                                             from,
                                             join,
                                             where);

            return(Convert.ToInt32(SqlQuery.execute_sql_scalar(connection_string, select_statement, CommandType.Text, sql_parameters != null ? sql_parameters.ToArray() : null)));
        }
        private static string BuildSqlJoin(DbRow container, DbQuery.DbJoin join, List <SqlQueryParameter> sql_parameters)
        {
            if (join != null)
            {
                StringBuilder sb_join = new StringBuilder();

                if (!string.IsNullOrEmpty(join.Container))
                {
                    sb_join.AppendFormat("{0} JOIN {1} ON {2}", join.Type, join.Container, join.Condition);
                }
                else
                {
                    var parsed_join = new DbQueryToSql(container, join.Query);

                    sb_join.AppendFormat("{0} JOIN {1} ON {2}", join.Type, parsed_join.Query, join.Condition);

                    if (parsed_join.Parameters != null)
                    {
                        if (sql_parameters == null)
                        {
                            sql_parameters = new List <SqlQueryParameter>();
                        }

                        sql_parameters.AddRange(parsed_join.Parameters);
                    }
                }
                if (join.ChainedJoin != null)
                {
                    sb_join.Append(' ').Append(BuildSqlJoin(container, join.ChainedJoin, sql_parameters));
                }

                return(sb_join.ToString());
            }
            else
            {
                return(null);
            }
        }
Пример #5
0
        public Type SelectSingle <Type>(DbQuery query, Func <DbRow, Type> construct_from_container) where Type : class
        {
            var sql_query = new DbQueryToSql(this, query);

            Type t = null;

            using (SqlDataReader reader = SqlQuery.Select(connection_string, sql_query.Query, CommandType.Text, CommandBehavior.CloseConnection, sql_query.Parameters))
            {
                if (reader.HasRows)
                {
                    if (reader.Read())
                    {
                        DbRow c = new DbRow(this);

                        c.LoadValuesWithoutRead(reader);

                        t = construct_from_container(c);
                    }
                }
            }

            return(t);
        }