/// <summary>
        /// Gets the create method signature for the specified table.
        /// </summary>
        /// <param name="table">The table to get signature for.</param>
        internal static MethodSignature GetCreateSignature(TableInfo table)
        {
            string returnType = table.IdentityColumn != null ? table.IdentityColumn.ClrType : "void";
            var parameters = new Dictionary<string, string> { { "item", table.EntityName } };

            return new MethodSignature("Create", returnType, parameters);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="UpdateMethodBuilder" /> class.
 /// </summary>
 /// <param name="table">The table to build method for.</param>
 internal UpdateMethodBuilder(TableInfo table)
 {
     _table = table;
 }
        private static void UpdatePrimaryKeysInformation(TableInfo[] tables, TSqlObject[] keys)
        {
            // Get schema for all primary keys
            foreach (var key in keys)
            {
                string keyName = key.Name.Parts[1];
                string tableName = keyName.Substring(3);
                var columns = key.GetReferenced(PrimaryKeyConstraint.Columns).ToArray();
                var primaryKeyColumns = new ColumnInfo[columns.Length];

                for (int i = 0; i < primaryKeyColumns.Length; i++)
                {
                    primaryKeyColumns[i] = GetSchemaForColumn(columns[i]);
                }

                var targetTable = tables.FirstOrDefault(t => t.ShortName == tableName);
                if (null == targetTable)
                {
                    throw new InvalidOperationException(
                        "Could not find target table for primary key " + key.Name);
                }

                targetTable.PrimaryKey = primaryKeyColumns;
            }
        }
        private static TableInfo GetSchemaForTable(TSqlObject model)
        {
            TableInfo retVal = new TableInfo();
            retVal.EntityName = _pluralizationService.Singularize(model.Name.Parts[1]);
            retVal.ShortName = model.Name.Parts[1];
            retVal.FullName = model.Name.ToString();

            var columns = model.GetReferenced(Table.Columns).ToArray();
            retVal.Columns = new ColumnInfo[columns.Length];
            for (int i = 0; i < columns.Length; i++)
            {
                ColumnInfo column = GetSchemaForColumn(columns[i]);
                retVal.Columns[i] = column;
                if (columns[i].GetProperty<bool>(Column.IsIdentity))
                    retVal.IdentityColumn = column;
            }

            return retVal;
        }
        /// <summary>
        /// Gets the delete method signature for the specified table.
        /// </summary>
        /// <param name="table">The table to get signature for.</param>
        internal static MethodSignature GetDeleteSignature(TableInfo table)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            if (table.IdentityColumn != null)
            {
                string name = ColumnNameToVariableName(table.IdentityColumn.Name);
                parameters.Add(name, table.IdentityColumn.ClrType);
            }
            else if (null != table.PrimaryKey)
            {
                foreach (var column in table.PrimaryKey)
                {
                    string name = ColumnNameToVariableName(column.Name);
                    parameters.Add(name, column.ClrType);
                }
            }

            return new MethodSignature("Delete" + table.EntityName, "int", parameters);
        }
        /// <summary>
        /// Gets the read method signature for the specified table.
        /// </summary>
        /// <param name="table">The table to get signature for.</param>
        internal static MethodSignature GetReadSignature(TableInfo table)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add(ColumnNameToVariableName(table.IdentityColumn.Name),
                           table.IdentityColumn.ClrType);

            return new MethodSignature("Get" + table.EntityName, table.EntityName, parameters);
        }
        /// <summary>
        /// Gets the update method signature for the specified table.
        /// </summary>
        /// <param name="table">The table to get signature for.</param>
        internal static MethodSignature GetUpdateSignature(TableInfo table)
        {
            var parameters = new Dictionary<string, string> { { "item", table.EntityName } };

            return new MethodSignature("Update", "int", parameters);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DeleteMethodBuilder" /> class.
 /// </summary>
 /// <param name="table">The table to build method for.</param>
 internal DeleteMethodBuilder(TableInfo table)
 {
     _table = table;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ReadMethodBuilder" /> class.
 /// </summary>
 /// <param name="table">The table to build method for.</param>
 internal ReadMethodBuilder(TableInfo table)
 {
     _table = table;
 }
        /// <summary>
        /// Writes CRUD interface methods definition for the specified table.
        /// </summary>
        /// <param name="table">The table to write interface for.</param>
        public void WriteTableDataProviderInterface(TableInfo table)
        {
            MethodSignature[] signatures = new MethodSignature[4];
            signatures[0] = MethodSignature.GetCreateSignature(table);
            signatures[1] = MethodSignature.GetUpdateSignature(table);
            signatures[2] = MethodSignature.GetDeleteSignature(table);
            signatures[3] = MethodSignature.GetReadSignature(table);

            foreach (var signature in signatures)
                WriteLine(signature.ToString("i", null));
        }
        /// <summary>
        /// Writes data provider methods definition for the specified table.
        /// </summary>
        /// <param name="table">The table to write methods for.</param>
        public void WriteTableDataProviderImplementation(TableInfo table)
        {
            MethodSignature signature = MethodSignature.GetCreateSignature(table);
            IMethodBuilder builder = new CreateMethodBuilder(table);
            WrapMethodBody(signature, builder);

            signature = MethodSignature.GetUpdateSignature(table);
            builder = new UpdateMethodBuilder(table);
            WrapMethodBody(signature, builder);

            signature = MethodSignature.GetDeleteSignature(table);
            builder = new DeleteMethodBuilder(table);
            WrapMethodBody(signature, builder);

            signature = MethodSignature.GetReadSignature(table);
            builder = new ReadMethodBuilder(table);
            WrapMethodBody(signature, builder);
        }
        /// <summary>
        /// Writes the body of the POCO struct for the specified table.
        /// </summary>
        /// <param name="table">Target table.</param>
        /// <param name="excludedColumns">The excluded columns.</param>
        public void WritePocoDefinition(TableInfo table, HashSet<string> excludedColumns)
        {
            foreach (var column in table.Columns)
            {
                if (null != excludedColumns && excludedColumns.Contains(column.Name))
                    continue;

                WriteLine("public {0} {1} {{ get; set; }}", column.ClrType, column.Name);
            }
        }