public void Drop(DatabaseObjectName name)
 {
     using (var connection = new SqlConnection(connectionString))
     {
         connection.Execute(dropTableCommand, name.Qualified);
     }
 }
 public void Drop(DatabaseObjectName name)
 {
     using (var connection = new SqlConnection(connectionString))
     {
         connection.Execute(dropProcedureCommand, name);
     }
 }
        public TableBackedViewDefinition(DatabaseObjectName name, DatabaseObjectName backingTable)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));
            if (backingTable == null) throw new ArgumentNullException(nameof(backingTable));

            Name = name;
            BackingTable = backingTable;
        }
        public string Sql(DatabaseObjectName name, int columnCount)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));
            if (columnCount < 1) throw new ArgumentException("The column count must be greater than zero", "columnCount");

            return string.Format(
                InsertTableFormat,
                name,
                null,
                BindingMarkers(columnCount));
        }
        public string Sql(DatabaseObjectName name, IEnumerable<string> columnNames)
        {
            if (name == null) throw new ArgumentNullException(nameof(name));
            if (columnNames == null || columnNames == Enumerable.Empty<string>()) throw new ArgumentNullException("columnNames");

            return string.Format(
                InsertTableFormat,
                name,
                GetColumnNames(columnNames),
                BindingMarkers(columnNames.Count()));
        }
        public void CreateView(DatabaseObjectName tableName, DatabaseObjectName viewName)
        {
            if (tableName == null) throw new ArgumentNullException(nameof(tableName));
            if (viewName == null) throw new ArgumentNullException(nameof(viewName));

            var definition = new TableBackedViewDefinition(viewName, tableName);

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Execute(new TableBackedViewCreateSqlGenerator().Sql(definition));
            }
        }
        public TableDefinition GetTableDefinition(DatabaseObjectName tableName)
        {
            var mapper = new DataRecordToColumnMapper();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                return new TableDefinition(tableName,
                    connection.Execute(
                        (reader) => mapper.ToColumnDefinition(reader),
                        TableDefinitionQuery,
                        tableName.Qualified));
            }
        }
 public ProcedureDefinition GetDefinition(DatabaseObjectName name)
 {
     using (var connection = new SqlConnection(connectionString))
     {
         using (var command = connection.CreateCommand())
         {
             command.CommandText = name.Qualified;
             command.CommandType = CommandType.StoredProcedure;
             connection.Open();
             SqlCommandBuilder.DeriveParameters(command);
             var mapper = new SqlParameterToProcedureParameterMapper();
             var parameters = command.Parameters.Cast<SqlParameter>().Select(x => mapper.FromSqlParameter(x));
             return new ProcedureDefinition(name, parameters);
         }
     }
 }
        public void Insert(DatabaseObjectName tableName, TableData tableData)
        {
            if (tableName == null) throw new ArgumentNullException(nameof(tableName));
            if (tableData == null) throw new ArgumentNullException(nameof(tableData));

            bool hasColumnNames = tableData.ColumnNames != null && !tableData.ColumnNames.Equals(Enumerable.Empty<string>());
            var generator = new TableInsertSqlGenerator();
            using (var connection = new SqlConnection(connectionString))
            {
                foreach (var row in tableData.Rows)
                {
                    string command = hasColumnNames ? generator.Sql(tableName, tableData.ColumnNames) : generator.Sql(tableName, row.Count());

                    connection.ExecuteWithParameters(command, row);
                }
            }
        }
        public TableDefinition GetTableDefinition(DatabaseObjectName viewName)
        {
            var mapper = new SchemaRowToColumnMapper();
            var viewDefinition = new TableDefinition(viewName);

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = string.Format(TableDefinitionQuery, viewName.Qualified);
                    connection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        foreach (DataRow row in reader.GetSchemaTable().Rows)
                        {
                            viewDefinition.Columns.Add(mapper.ToColumnDefinition(row));
                        }
                    }
                }
            }

            return viewDefinition;
        }
 public static string CSharpTableDefinition(DatabaseObjectName tableName, string connectionString)
 {
     var tableChecker = new TableCheck(connectionString);
     var table = tableChecker.GetDefinition(tableName);
     return table.ToCSharp();
 }
 public static void CreateView(this TableDefinition definition, DatabaseActions database, DatabaseObjectName viewName)
 {
     if (database == null) throw new ArgumentNullException("database");
     new TableActions(database.ConnectionString).CreateView(definition.Name, viewName);
 }