Example #1
0
        private bool ShouldUpdateTableDMLFile(Table table, string filePath, out DMLTableFileInfo dmlFileInfo)
        {
            dmlFileInfo = new DMLTableFileInfo()
            {
                SchemaName = table.SchemaName,
                TableName  = table.Name
            };

            try
            {
                dmlFileInfo.DataHash = GetTableDataHash(table);

                var dmlInfoFilePath = filePath.Replace(".sql", ".dmlinfo");
                if (!File.Exists(dmlInfoFilePath))
                {
                    return(true);
                }

                string line = null;
                using (StreamReader reader = new StreamReader(dmlInfoFilePath))
                {
                    line = reader.ReadLine();
                }

                if (line.IsNullOrWhiteSpace())
                {
                    return(true);
                }

                try
                {
                    var existingDmlFileInfo = JsonConvert.DeserializeObject <DMLTableFileInfo>(line);
                    if (existingDmlFileInfo == null || !dmlFileInfo.Generated)
                    {
                        return(true);
                    }

                    if (!existingDmlFileInfo.DataHash.Equals(dmlFileInfo.DataHash))
                    {
                        return(true);
                    }
                }
                catch { return(true); }
            }
            catch { return(true); }

            return(false);
        }
Example #2
0
        public void GenerateDML(Table table, string outputFilePath, Action <string> output = null)
        {
            DMLTableFileInfo dmlFileInfo = null;

            if (!ShouldUpdateTableDMLFile(table, outputFilePath, out dmlFileInfo))
            {
                return;
            }

            if (output != null)
            {
                output("Generating DML for " + table.FullName + "...");
            }

            if (File.Exists(outputFilePath))
            {
                File.Delete(outputFilePath);
            }

            var       index    = 0;
            const int setSize  = 100;
            var       commands = new List <Command>();

            do
            {
                commands = GetDMLCommands(table, index, setSize);
                if (commands != null && commands.Count > 0)
                {
                    outputFilePath.AppendAllLines(commands.Select(el => el.Text));
                }

                index += commands.Count;
            } while (commands.Count > 0 && commands.Count == setSize);

            var dmlInfoFilePath = outputFilePath.Replace(".sql", ".dmlinfo");

            dmlFileInfo.Generated = true;
            File.WriteAllText(dmlInfoFilePath, JsonConvert.SerializeObject(dmlFileInfo));
        }