Ejemplo n.º 1
0
        /// <summary>
        /// Exports content to SQL file.
        /// </summary>
        void ToSQL(object sender, EventArgs e)
        {
            var sfd = new SaveFileDialog();

            sfd.Filter = "SQL File|*.sql";
            if (sfd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            File.Delete(sfd.FileName);
            using (var fileStream = new StreamWriter(sfd.OpenFile()))
            {
                var recordCount = 0;
                fileStream.WriteLine(String.Join(Environment.NewLine, new string[] {
                    @"DROP TABLE IF EXISTS `" + Path.GetFileNameWithoutExtension(FileSelectionBox.Text) + @"Records`;",
                    @"CREATE TABLE `" + Path.GetFileNameWithoutExtension(FileSelectionBox.Text) + @"Records` (",
                    GenerateSqlHeader(),
                    @");",
                    ""
                }));
                var fields = SelectedFileType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                             .ToArray();
                var recordLine = new List <string>();
                var objects    = (sender as ToolStripMenuItem).Tag.ToString() == "1" ? ContentView.FilteredObjects : ContentView.Objects;
                foreach (var record in objects)
                {
                    if (recordCount % 500 == 0)
                    {
                        fileStream.WriteLine("INSERT INTO `" + Path.GetFileNameWithoutExtension(FileSelectionBox.Text) + @"Records` VALUES");
                    }

                    recordLine.Clear();
                    foreach (var field in fields)
                    {
                        var arraySize = 1;
                        var fieldType = field.FieldType;
                        if (field.FieldType.IsArray)
                        {
                            arraySize = (Attribute.GetCustomAttribute(field, typeof(StoragePresenceAttribute)) as StoragePresenceAttribute).ArraySize;
                            fieldType = field.FieldType.GetElementType();
                        }

                        dynamic fValue = field.GetValue(record); // Just so we do not use another variable name (my lazy ass won)
                        if (arraySize > 1)
                        {
                            fValue = ((Array)field.GetValue(record)).Cast <object>().ToArray();
                        }

                        for (var i = 0; i < arraySize; ++i)
                        {
                            var fieldFormat = (fieldType == typeof(string)) ? @"""{0}""" : @"{0}";
                            var asString    = String.Format(fieldFormat, arraySize > 1 ? fValue[i] : fValue);
                            if (fieldType == typeof(float))
                            {
                                asString = asString.Replace(@",", @".");
                            }
                            recordLine.Add(asString);
                        }
                    }

                    fileStream.Write(String.Format("({0})", String.Join(@", ", recordLine.ToArray())));
                    fileStream.WriteLine(((recordCount + 1) % 500 == 0 || (recordCount + 1) == ContentView.GetItemCount()) ? ";" : ",");
                    if ((recordCount + 1) % 500 == 0)
                    {
                        fileStream.WriteLine();
                    }

                    ++recordCount;
                }
            }
            StatusLabel.Text = String.Format("SQL saved to {0}.", sfd.FileName);
        }