public string GetRepositoryMethods(Table tableInfo)
        {
            var sb = new StringBuilder();
            string primaryKeyColumnName = "Id";   // sane default, just in case
            string primaryKeyColumnType = "int?"; // sane defailt, just in case
            string className = tableInfo.Name.UppercaseFirst();
            string objectName = tableInfo.Name.ToLowerInvariant();

            var pkResult = tableInfo.Columns.Find(m => m.IsPrimaryKey == true);
            if (pkResult != null)
            {
                primaryKeyColumnName = pkResult.Name;
                primaryKeyColumnType = pkResult.Type;
            }

            var columnNames =  tableInfo.Columns.Select(m => { return m.Name; });

            sb.AppendLine("\n\t\t/***************************************/");
            sb.AppendLine(GetGetAllMethod(className, columnNames, tableInfo.AdoAdapterConnectionClassName, tableInfo.Schema));
            sb.AppendLine(GetGetMethod(className, primaryKeyColumnName, primaryKeyColumnType, columnNames, tableInfo.AdoAdapterConnectionClassName, tableInfo.Schema));
            sb.AppendLine(GetCreateMethod(className, objectName, primaryKeyColumnName, columnNames, tableInfo.AdoAdapterConnectionClassName, tableInfo.Schema));
            sb.AppendLine(GetSaveMethod(className, objectName, primaryKeyColumnName, columnNames, tableInfo.AdoAdapterConnectionClassName, tableInfo.Schema));
            sb.AppendLine(GetDeleteMethod(className, primaryKeyColumnName, primaryKeyColumnType, tableInfo.AdoAdapterConnectionClassName, tableInfo.Schema));

            return sb.ToString();
        }
        public void Generate(Table table)
        {
            var sb = new StringBuilder();
            string className = table.Name.UppercaseFirst();

            AppendUsings(sb, table);

            sb.AppendFormat("\nnamespace {0}\n", _config.ModelNamespace);
            sb.AppendLine("{"); // Begin NameSpace
            sb.AppendFormat("\tpublic class {0}", className);
            sb.AppendLine("\n\t{"); // Begin Class

            table.Columns.ForEach(m => {
                AppendProperty(m, sb);
            });

            if (_config.IncludeRepositoryMethods && _repositoryGenerator != null)
            {
                sb.AppendLine(_repositoryGenerator.GetRepositoryMethods(table));
            }

            sb.AppendLine("\t}"); // End Class
            sb.AppendLine("}\n"); // End NameSpace

            FileWriter.Write(_config.ClassOutputPath, className, sb.ToString());
        }
        private void AppendUsings(StringBuilder sb, Table tableInfo)
        {
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Linq;");

            if (_config.IncludeRepositoryMethods && _repositoryGenerator != null)
            {
                sb.AppendLine(_repositoryGenerator.GetUsings(tableInfo));
            }

            if (_config.IncludeDataAnnotations)
            {
                sb.AppendLine("using System.ComponentModel.DataAnnotations;");
            }
        }
        /// <summary>
        /// Get full table info for a list of tables.
        /// </summary>
        /// <param name="tableList">Enumerable containing the names of the tables to be retrieved.</param>
        /// <returns>List of Table with full column information.</returns>
        public List<Table> GetFullTableInfo(IEnumerable<string> tableList)
        {
            string query = @"select
                                cols.column_name as Name,
                                cols.data_type as Type,
                                CAST(cols.is_nullable as boolean) as IsNullable,
                                CAST(case when pk.COLUMN_NAME is NULL then 0 else 1 end as boolean) as IsPrimaryKey
                            from
                                information_schema.columns cols left join
                                (select kcu.TABLE_SCHEMA, kcu.TABLE_NAME, kcu.CONSTRAINT_NAME, tc.CONSTRAINT_TYPE, kcu.COLUMN_NAME, kcu.ORDINAL_POSITION
                                    from INFORMATION_SCHEMA.TABLE_CONSTRAINTS as tc
                                    join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as kcu
                                        on kcu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
                                        and kcu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
                                        and kcu.TABLE_SCHEMA = tc.TABLE_SCHEMA
                                        and kcu.TABLE_NAME = tc.TABLE_NAME
                                    where tc.CONSTRAINT_TYPE = 'PRIMARY KEY')
                                    as pk	on cols.TABLE_SCHEMA = pk.TABLE_SCHEMA
                                        and cols.TABLE_NAME = pk.TABLE_NAME
                                        and cols.COLUMN_NAME = pk.COLUMN_NAME
                            where
                                cols.TABLE_SCHEMA = @schema and
                                cols.TABLE_NAME = @name
                            order by
                                cols.ordinal_position";

            var tables = new List<Table>(tableList.Count());

            using (var conn = new NpgsqlConnection(ConnectionString))
            {
                conn.Open();

                foreach (var value in tableList)
                {
                    var split = value.Split('.');
                    var table = new Table();
                    table.AdoAdapterConnectionClassName = "NpgsqlConnection";
                    table.AdoAdapterNamespace = "Npgsql";
                    table.Name = split[1];
                    table.Schema = split[0];
                    table.Columns = conn.Query<Column>(query, new { schema = table.Schema, name = table.Name }).ToList<Column>();
                    table.Columns.ForEach(m => m.Type = GetNetDataType(m.Type, m.IsNullable));
                    tables.Add(table);
                }
            }

            return tables;
        }
 public string GetUsings(Table tableInfo)
 {
     return "using Simple.Data;";
 }
 public string GetUsings(Table tableInfo)
 {
     return string.Format("using Dapper;\nusing {0};", tableInfo.AdoAdapterNamespace);
 }