예제 #1
0
        public void SetUp()
        {
            var tables = new[] {
                new NpgTableInfo {
                    Name    = "Table",
                    Columns = new[] {
                        new NpgColumnInfo {
                            Name     = "serial",
                            DataType = NpgDataType.Serial
                        },
                        new NpgColumnInfo {
                            Name     = "id",
                            DataType = NpgDataType.Float8
                        }
                    }
                },
                new NpgTableInfo {
                    Name    = "Table2",
                    Columns = new[] {
                        new NpgColumnInfo {
                            Name     = "id",
                            DataType = NpgDataType.Int4
                        },
                        new NpgColumnInfo {
                            Name     = "bigserial",
                            DataType = NpgDataType.Bigserial
                        }
                    }
                }
            };

            _helper = SerialHelper.Create(tables);
        }
예제 #2
0
        protected override void OkCommandExecuted(object obj)
        {
            InProgress = true;
            ProgressIsIndeterminate = true;
            var script = _service.ScriptHelper.ExtractScriptLines(PHmiModelContext.GetPHmiScriptStream());

            Progress    = 0;
            ProgressMax = script.Length;
            _service.ActionHelper.Async(() =>
            {
                FileStream stream = null;
                try
                {
                    _service.NpgHelper.CreateDatabase(ConnectionParameters);
                    _service.ActionHelper.Dispatch(() => { ProgressIsIndeterminate = false; });
                    _service.NpgHelper.ExecuteScript(
                        ConnectionParameters.ConnectionString,
                        script.Select(r => new NpgQuery(r)).ToArray(),
                        true,
                        row => _service.ActionHelper.Dispatch(() => { Progress = row; }));

                    _service.ActionHelper.Dispatch(() =>
                    {
                        ProgressIsIndeterminate = true;
                    });
                    stream = File.OpenRead(FileName);
                    var binaryFormatter = _service.ExImHelper.CreateFormatter();
                    var prevTable       = string.Empty;
                    var createScript    = _service.ScriptHelper.ExtractScriptLines(
                        PHmiModelContext.GetPHmiScriptStream());
                    var tables       = _service.ExImHelper.GetTables(createScript);
                    var serialHelper = SerialHelper.Create(tables);
                    using (var connection = new NpgsqlConnection(ConnectionParameters.ConnectionString))
                    {
                        while (stream.Position != stream.Length)
                        {
                            var tableData  = (TableData)binaryFormatter.Deserialize(stream);
                            var scriptItem = _service.ExImHelper.GetInsertScriptItem(tableData);
                            if (scriptItem == null)
                            {
                                continue;
                            }
                            var count = _service.NpgHelper.ExecuteNonQuery(connection, scriptItem);
                            var table = prevTable;
                            _service.ActionHelper.Dispatch(() =>
                            {
                                if (table != tableData.TableName && !string.IsNullOrEmpty(table))
                                {
                                    TablesStored++;
                                }
                                RowsStored += count;
                            });
                            prevTable = tableData.TableName;
                            serialHelper.Update(tableData);
                        }
                        foreach (var alterScript in serialHelper.CreateAlterScriptItem())
                        {
                            _service.NpgHelper.ExecuteNonQuery(connection, alterScript);
                        }
                    }
                    _service.DialogHelper.Message(
                        string.Format("{0} {1}{2}{3} {4}",
                                      Res.TablesStored,
                                      TablesStored.ToString("N0"),
                                      Environment.NewLine,
                                      Res.RowsStored,
                                      RowsStored.ToString("N0")),
                        Res.ImportCompleted,
                        owner: View);
                    _service.ActionHelper.Dispatch(() => base.OkCommandExecuted(obj));
                }
                catch (Exception exception)
                {
                    ExceptionDialog.Show(exception, View);
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                    _service.ActionHelper.Dispatch(() =>
                    {
                        InProgress = false;
                        ProgressIsIndeterminate = false;
                    });
                }
            });
        }