понедельник, 3 ноября 2014 г.

Remove Tables Recursively

#region

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using MLM.Tests.Infrastructure;

#endregion

namespace MLM.IntegrationTests {
    internal class DataBaseHelper {

        private SqlConnection GetConnection() {
            var connectionHelper = new ConnectionHelperFake();
            return new SqlConnection(connectionHelper.CurrentConnection);
        }

        public void ClearTable( IEnumerable<string > tables) {
            var tablesToClear = tables == null ? null : tables.ToList();
            if (tablesToClear == null || !tablesToClear.Any()) {
                return;
            }
            List< string> dependentTables = GetDependentTables(tablesToClear).ToList();
            ClearTable(dependentTables);
            using ( SqlConnection connection = GetConnection()) {
                connection.Open();
                foreach ( string table in tablesToClear) {
                    if (TableExists(table))
                        using ( var command = new SqlCommand(string .Format("delete {0}", table), connection)) {
                            command.ExecuteNonQuery();
                        }
                }
            }
        }

        private IEnumerable<string > GetDependentTables(IEnumerable< string> tables) {
            string union = string.Join( ",", tables.Select(x => "'" + x + "'").ToArray());
            using ( SqlConnection connection = GetConnection()) {
                using ( var command = new SqlCommand(string .Format(@"
                        select object_name(s.fkeyid) 'DependentTable'
                        from sysforeignkeys s
                        inner join syscolumns c1
                        on ( s.fkeyid = c1.id and s.fkey = c1.colid )
                        inner join syscolumns c2
                        on ( s.rkeyid = c2.id and s.rkey = c2.colid )
                        where object_name(c2.id) in ( {0}) --and c2.name='ColumnName'",
                                                                  union),
                                                    connection)) {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read()) {
                        yield return reader.GetString(0);
                    }
                }
            }
        }

        private bool TableExists( string tableName) {
            using ( SqlConnection connection = GetConnection()) {
                using ( var command =
                    new SqlCommand(
                        string.Format( "select * from INFORMATION_SCHEMA.TABLES where table_name = '{0}'", tableName),
                        connection)) {
                    connection.Open();
                    object scalar = command.ExecuteScalar();
                    return scalar != null;
                }
            }
        }
    }
}


среда, 19 марта 2014 г.

The Zen of Python


    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

среда, 29 января 2014 г.

Top 66 Interview Questions to Answer BEFORE the Job Interview

Top 66 Interview Questions to Answer BEFORE the Job Interview (from pluralsight)

By Interviews
When I wrote the IT Job Interview Questions article back in 2008 I wrote it from the interviewee perspective. I went to several different job interviews before I joined TrainSignal and had a lot of these questions fresh in my mind.
Today, I want to include some tips from the other side: the interviewer’s perspective. I am currently looking for several people to fill positions on my team, and sometimes I am absolutely in shock at how poorly prepared the applicants are for their interviews.

понедельник, 20 января 2014 г.