Exemplo n.º 1
0
        public bool TryGetNextReplacement(string template, InterpolationArgs args, out string placeHolder, out string replacement)
        {
            Match match = Regex.Match(template, @"(.*?)\{\{([^}]+)\}\}");

            if (!match.Success)
            {
                placeHolder = string.Empty;
                replacement = string.Empty;
                return false;
            }
            else
            {
                string placeHolderName = match.Groups[2].Value;

                args.IndentSpaces = match.Groups[1].Value.Length;

                placeHolder = string.Format("{{{{{0}}}}}", placeHolderName);

                replacement = PlaceHolderDictionary.ContainsKey(placeHolderName)
                            ? PlaceHolderDictionary[placeHolderName](args)
                            : string.Format("??{0}??", placeHolderName);

                return true;
            }
        }
Exemplo n.º 2
0
 public override string GetOutputFileName(InterpolationArgs args)
 {
     if (args.TemplateName == "ExecuteScripts")
     {
         return "ExecuteAll.bat";
     }
     else
     {
         return GetProcedure(args);
     }
 }
Exemplo n.º 3
0
 public override string GetOutputFileName(InterpolationArgs args)
 {
     if (args.TemplateName == "Entities")
     {
         return GetEntitiesDataAccessorClass(args);
     }
     else
     {
         return GetEntity(args);
     }
 }
Exemplo n.º 4
0
        private static string GetColumnListItem(InterpolationArgs args)
        {
            if (string.Compare(args.TemplateName, "SELECT", true) == 0)
            {
                return string.Format(
                    "{1}{2}{3}{0}",
                    args.ColumnIndex == args.Columns.Count - 1 ? string.Empty : Environment.NewLine,
                    args.ColumnIndex == 0 ? string.Empty : args.IndentString,
                    args.ColumnIndex == 0 ? " " : ",",
                    GetColumnWithTableAlias(args)
                );
            }
            else if (string.Compare(args.TemplateName, "INSERT", true) == 0)
            {
                string column = GetColumn(args);
                string item = string.Format(
                    "{1}{2}[{3}]{0}",
                    args.IsLastNonIdentity(column) ? string.Empty : Environment.NewLine,
                    args.IsFirstNonIdentity(column) ? string.Empty : args.IndentString,
                    args.IsFirstNonIdentity(column) ? " " : ",",
                    column
                );

                return args.IsIdentity(column) ? string.Empty : item;
            }
            else if (string.Compare(args.TemplateName, "UPDATE", true) == 0)
            {
                string column = GetColumn(args);
                string item = string.Format(
                    "{1}{2}[{3}] = {4}{0}",
                    args.IsLastNonIdentity(column) ? string.Empty : Environment.NewLine,
                    args.IsFirstNonIdentity(column) ? string.Empty : args.IndentString,
                    args.IsFirstNonIdentity(column) ? " " : ",",
                    column,
                    GetParameter(args)
                );

                return args.IsIdentity(column) ? string.Empty : item;
            }
            else
            {
                return "??ColumnListItem??";
            }
        }
Exemplo n.º 5
0
        public string GetExecuteScriptCommandListItem(InterpolationArgs args)
        {
            StringBuilder sb = new StringBuilder();

            string dir = Path.GetDirectoryName(args.OutputFilePath);
            string[] files = Directory.GetFiles(dir, "*.sql");

            for (int ii = 0; ii < files.Length; ii++)
            {
                sb.AppendFormat(
                    "{1}sqlcmd -U {2} -P {3} -S {4} -i \"{5}\"{0}",
                    /*  0 */ Environment.NewLine,
                    /*  1 */ args.IndentString,
                    /*  2 */ args.ConnectionStringBuilder.UserID,
                    /*  3 */ args.ConnectionStringBuilder.Password,
                    /*  4 */ args.ConnectionStringBuilder.DataSource,
                    /*  5 */ files[ii]
                );
            }

            return sb.ToString();
        }
Exemplo n.º 6
0
 private static string GetQualifiedTable(InterpolationArgs args)
 {
     return string.Format("[{0}].[{1}]", GetSchema(args), GetTable(args));
 }
Exemplo n.º 7
0
 private static string GetProcedure(InterpolationArgs args)
 {
     return GetProcedure(args.Column["TABLE_NAME"].ToString(), args.TemplateName);
 }
Exemplo n.º 8
0
        private void GenerateFile(InterpolationArgs args, string templatePath, Interpolator interpolator, bool generateDeferred)
        {
            string template = File.ReadAllText(templatePath);

            if (ShouldGenerateFile(args, ref template, templatePath, generateDeferred))
            {
                while (true)
                {
                    string placeHolder, replacement;
                    if (!interpolator.TryGetNextReplacement(template, args, out placeHolder, out replacement))
                        break;

                    template = template.Replace(placeHolder, replacement);
                }

                BackupExistingFile(args.OutputFilePath);
                File.WriteAllText(args.OutputFilePath, template);
            }
        }
Exemplo n.º 9
0
 private static string GetColumn(InterpolationArgs args)
 {
     return args.Column["COLUMN_NAME"].ToString();
 }
Exemplo n.º 10
0
 private string GetConnection(InterpolationArgs args)
 {
     return string.Format("_{0}ConnectionName", GetDatabase(args).ToCamelCase());
 }
Exemplo n.º 11
0
        private static string GetValueListItem(InterpolationArgs args)
        {
            string column = GetColumn(args);

            if (args.IsIdentity(column))
            {
                return string.Empty;
            }

            string conjunction = GetConjunction(args, dr => !args.IsIdentity(dr["COLUMN_NAME"].ToString()), string.Empty, " ", ",");
            string indent = args.IsFirstNonIdentity(column) ? string.Empty : args.IndentString;
            string endLine = args.IsLastNonIdentity(column) ? string.Empty : Environment.NewLine;

            return string.Format(
                "{1}{2}{3}{0}",
                /*  0 */ endLine,
                /*  1 */ indent,
                /*  2 */ conjunction,
                /*  3 */ GetParameter(args)
            );
        }
Exemplo n.º 12
0
 private static string GetTable(InterpolationArgs args)
 {
     return args.Column["TABLE_NAME"].ToString();
 }
Exemplo n.º 13
0
        private static string GetConjunction(InterpolationArgs args, Func<DataRow, bool> filter, string onlyOne, string firstOne, string other)
        {
            var columns = args.Columns.Where(filter);
            var column = args.Columns.Where(filter).FirstOrDefault();

            if (columns.Count() == 1)
            {
                return onlyOne;
            }
            else if (column != null && column["COLUMN_NAME"].ToString() == args.Column["COLUMN_NAME"].ToString())
            {
                return firstOne;
            }
            else
            {
                return other;
            }
        }
Exemplo n.º 14
0
 private static string GetConjunction(InterpolationArgs args, string onlyOne, string firstOne, string other)
 {
     return GetConjunction(args, dr => true, onlyOne, firstOne, other);
 }
Exemplo n.º 15
0
 private static string GetColumnWithTableAlias(InterpolationArgs args)
 {
     return string.Format("{0}.[{1}]", GetTableAlias(args), GetColumn(args));
 }
Exemplo n.º 16
0
        private static string GetColumnType(InterpolationArgs args)
        {
            var dataType = args.Column["DATA_TYPE"].ToString().ToUpper();
            var sb = new StringBuilder(dataType);

            switch (dataType)
            {
                case "CHAR":
                case "VARCHAR":
                case "NCHAR":
                case "NVARCHAR":
                case "BINARY":
                    int length = Convert.ToInt32(args.Column["CHARACTER_MAXIMUM_LENGTH"]);
                    sb.AppendFormat("({0})", length == -1 ? "MAX" : length.ToString());
                    break;

                case "DECIMAL":
                case "NUMERIC":
                    int precision = Convert.ToInt32(args.Column["NUMERIC_PRECISION"]);
                    int scale = Convert.ToInt32(args.Column["NUMERIC_PRECISION"]);
                    sb.AppendFormat("({0},{1})", precision, scale);
                    break;

                default:
                    break;
            }

            return sb.ToString();
        }
Exemplo n.º 17
0
        private bool ShouldGenerateFile(InterpolationArgs args, ref string template, string templatePath, bool generateDeferred)
        {
            string firstLine = template.Split(new string[] { Environment.NewLine }, 2, StringSplitOptions.None)[0];

            if (firstLine.StartsWith("#"))
            {
                if (firstLine.Contains("ONCE") && !GenerateOnce.Contains(templatePath))
                {
                    BackupExistingFile(args.OutputFilePath);
                    GenerateOnce.Add(templatePath);
                }

                if (firstLine.Contains("ONCE") && File.Exists(args.OutputFilePath))
                    return false;

                if (firstLine.Contains("DEFER") && !GenerateDeferred.Contains(templatePath))
                {
                    GenerateDeferred.Add(templatePath);
                }

                if (firstLine.Contains("DEFER") && !generateDeferred)
                    return false;

                template = template.Remove(0, firstLine.Length).TrimStart(Environment.NewLine.ToCharArray());
            }

            return true;
        }
Exemplo n.º 18
0
        private void GenerateFiles(
            string connectionString, 
            string tableName, 
            bool readOnly, 
            DataTable tables, 
            DataTable columns, 
            DataTable primaryKeys, 
            Dictionary<string, string> outputDirectories, 
            bool generateDeferred)
        {
            InterpolationArgs args = new InterpolationArgs
            {
                TableName = tableName,
                ConnectionString = connectionString,
                AllTables = tables.Rows.ToList<DataRow>(),
                Columns = FilterByTableName(columns, tableName),
                PrimaryKeys = FilterByTableName(primaryKeys, tableName),
            };

            string[] templatePaths = generateDeferred
                                   ? GenerateDeferred.ToArray()
                                   : Directory.GetFiles("Templates", "*.tmpl", SearchOption.AllDirectories);

            foreach (string templatePath in templatePaths)
            {
                Interpolator interpolator = Interpolator.GetInterpolatorForTemplate(templatePath);

                // For read-only tables (e.g. history tables), only generate a
                // SELECT stored procedure and the entity .cs file.
                if (readOnly &&
                    string.Compare(interpolator.TemplateName, "SELECT", true) != 0 &&
                    string.Compare(interpolator.TemplateName, "COUNT", true) != 0 &&
                    string.Compare(interpolator.TemplateName, "Entity", true) != 0)
                {
                    continue;
                }

                if (!outputDirectories.ContainsKey(interpolator.TemplateExtension) ||
                    string.IsNullOrWhiteSpace(outputDirectories[interpolator.TemplateExtension]))
                {
                    continue;
                }

                args.TemplateName = interpolator.TemplateName;
                args.OutputFilePath= GetOutputFilePath(args, outputDirectories, interpolator);

                GenerateFile(args, templatePath, interpolator, generateDeferred);
            }
        }
Exemplo n.º 19
0
 private static string GetQualifiedTableWithAlias(InterpolationArgs args)
 {
     return string.Format("{0} {1}", GetQualifiedTable(args), GetTableAlias(args));
 }
Exemplo n.º 20
0
 private static string GetParameter(InterpolationArgs args)
 {
     return string.Format("@{0}", GetColumn(args));
 }
Exemplo n.º 21
0
 private static string GetSchema(InterpolationArgs args)
 {
     return args.Column["TABLE_SCHEMA"].ToString();
 }
Exemplo n.º 22
0
        private static string GetParameterListItem(InterpolationArgs args)
        {
            string column = GetColumn(args);

            string conjunction, indent, endLine;
            GetParameterListItemPunc(args, column, out conjunction, out indent, out endLine);

            string item = string.Format(
                "{1}{2}{3} {4} = NULL{0}",
                /*  0 */ endLine,
                /*  1 */ indent,
                /*  2 */ conjunction,
                /*  2 */ GetParameter(args),
                /*  3 */ GetColumnType(args)
            );

            return GetParameterListItem(args, column, item);
        }
Exemplo n.º 23
0
 private static string GetTableAlias(InterpolationArgs args)
 {
     return GetTable(args)[0].ToString().ToLower();
 }
Exemplo n.º 24
0
        private string GetDataAccessorPropertiesList(InterpolationArgs args)
        {
            StringBuilder sb = new StringBuilder();
            string indent = string.Empty;

            foreach (DataRow row in args.AllTables)
            {
                string tableName = row["TABLE_NAME"].ToString();
                string tableNameCamelCase = tableName.ToCamelCase();

                sb.AppendFormat(
                    "{1}private static {5}<{3}> _{4} = null;   {0}" +
                    "{2}public static {5}<{3}> {3}             {0}" +
                    "{2}{{                                     {0}" +
                    "{2}    get                                {0}" +
                    "{2}    {{                                 {0}" +
                    "{2}        if (_{4} == null)              {0}" +
                    "{2}        {{                             {0}" +
                    "{2}            _{4} = new {5}<{3}>({6});  {0}" +
                    "{2}        }}                             {0}" +
                    "{2}        return _{4};                   {0}" +
                    "{2}    }}                                 {0}" +
                    "{2}}}                                     {0}" +
                    "{0}",
                    Environment.NewLine,
                    indent,
                    args.IndentString,
                    tableName,
                    tableNameCamelCase,
                    tableName.EndsWith("History") ? "ReadOnlyDataAccessor" : "DataAccessor",
                    tableName.EndsWith("History")
                        ? string.Format("new DataAccessor<{0}>({1})", tableName, GetConnection(args))
                        : GetConnection(args)
                );

                indent = args.IndentString;
            }

            return sb.ToString();
        }
Exemplo n.º 25
0
        private static string GetWhereClauseItem(InterpolationArgs args)
        {
            if (args.TemplateName == "SELECT" || args.TemplateName == "COUNT")
            {
                string conjunction = GetConjunction(args, string.Empty, new string(' ', 4), "AND ");

                return string.Format(
                    "{1}{2}({3} IS NULL OR {3} = {4}){0}",
                    args.ColumnIndex == args.Columns.Count - 1 ? string.Empty : Environment.NewLine,
                    args.ColumnIndex == 0 ? string.Empty : args.IndentString,
                    conjunction,
                    GetParameter(args),
                    GetColumnWithTableAlias(args)
                );
            }
            else if (args.TemplateName == "UPDATE")
            {
                string conjunction = GetConjunction(args, string.Empty, new string(' ', 4), "AND ");

                return string.Format(
                    "{1}{2}({3} = {4}){0}",
                    args.ColumnIndex == args.Columns.Count - 1 ? string.Empty : Environment.NewLine,
                    args.ColumnIndex == 0 ? string.Empty : args.IndentString,
                    conjunction,
                    GetParameter(args),
                    GetColumn(args)
                );
            }
            else if (args.TemplateName == "DELETE")
            {
                string column = GetColumn(args);

                string conjunction = GetConjunction(
                    args,
                    dr => args.IsPrimaryKey(dr["COLUMN_NAME"].ToString()),
                    string.Empty,
                    new string(' ', 4),
                    "AND "
                );

                string endLine = args.IsLastPrimaryKey(column) ? string.Empty : Environment.NewLine;

                string item = string.Format(
                    "{1}{2}({3} = {4}){0}",
                    endLine,
                    args.ColumnIndex == 0 ? string.Empty : args.IndentString,
                    conjunction,
                    GetParameter(args),
                    GetColumn(args)
                );

                return args.IsPrimaryKey(column) ? item : string.Empty;
            }
            else
            {
                return "??WhereClauseItem??";
            }
        }
Exemplo n.º 26
0
 private static void GetParameterListItemPunc(InterpolationArgs args, string column, out string conjunction, out string indent, out string endLine)
 {
     if (args.TemplateName == "DELETE")
     {
         conjunction = GetConjunction(args, dr => args.IsPrimaryKey(dr["COLUMN_NAME"].ToString()), string.Empty, " ", ",");
         indent = args.IsFirstPrimaryKey(column) ? string.Empty : args.IndentString;
         endLine = args.IsLastPrimaryKey(column) ? string.Empty : Environment.NewLine;
     }
     else if (args.TemplateName == "INSERT")
     {
         conjunction = GetConjunction(args, dr => !args.IsIdentity(dr["COLUMN_NAME"].ToString()), string.Empty, " ", ",");
         indent = args.IsFirstNonIdentity(column) ? string.Empty : args.IndentString;
         endLine = args.IsLastNonIdentity(column) ? string.Empty : Environment.NewLine;
     }
     else
     {
         conjunction = GetConjunction(args, string.Empty, " ", ",");
         indent = args.ColumnIndex == 0 ? string.Empty : args.IndentString;
         endLine = args.ColumnIndex == args.Columns.Count - 1 ? string.Empty : Environment.NewLine;
     }
 }
Exemplo n.º 27
0
 private static string GetCatalog(InterpolationArgs args)
 {
     return args.Columns[args.ColumnIndex]["TABLE_CATALOG"].ToString();
 }
Exemplo n.º 28
0
 private static string GetPrimaryKeyWhereClause(InterpolationArgs args)
 {
     var pkArgs = new InterpolationArgs
     {
         TemplateName = args.TemplateName,
         Columns = args.PrimaryKeys,
         IndentSpaces = args.IndentSpaces
     };
     return GetList(pkArgs, GetWhereClauseItem);
 }
Exemplo n.º 29
0
 private static string GetParameterListItem(InterpolationArgs args, string column, string item)
 {
     if (args.TemplateName == "INSERT")
     {
         return args.IsIdentity(column) ? string.Empty : item;
     }
     else if (args.TemplateName == "DELETE")
     {
         return args.IsPrimaryKey(column) ? item : string.Empty;
     }
     else
     {
         return item;
     }
 }
Exemplo n.º 30
0
        private static string GetOutputFilePath(InterpolationArgs args, Dictionary<string, string> outputDirectories, Interpolator interpolator)
        {
            string outputDirectory = outputDirectories[interpolator.TemplateExtension];
            string fileName = interpolator.GetOutputFileName(args);
            string outputFilePath = Path.Combine(outputDirectory, fileName);

            if (string.IsNullOrEmpty(Path.GetExtension(fileName)))
                outputFilePath = Path.ChangeExtension(outputFilePath, interpolator.TemplateExtension);

            return outputFilePath;
        }