Example #1
0
        public void CreateAlterScriptItemTest()
        {
            _helper.Items["Table"]["serial"].MaxValue     = 21;
            _helper.Items["Table2"]["bigserial"].MaxValue = 22;
            var items = _helper.CreateAlterScriptItem();

            Assert.AreEqual(2, items.Length);
            Assert.AreEqual("ALTER SEQUENCE Table_serial_seq RESTART WITH 22", items[0].Text);
            Assert.IsEmpty(items[0].Parameters);
            Assert.AreEqual("ALTER SEQUENCE Table2_bigserial_seq RESTART WITH 23", items[1].Text);
            Assert.IsEmpty(items[1].Parameters);
        }
Example #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);
                    BinaryFormatter binaryFormatter = _service.ExImHelper.CreateFormatter();
                    string prevTable = string.Empty;
                    var createScript = _service.ScriptHelper.ExtractScriptLines(
                        PHmiModelContext.GetPHmiScriptStream());
                    var tables = _service.ExImHelper.GetTables(createScript);
                    SerialHelper serialHelper = SerialHelper.Create(tables);
                    using (var connection = new NpgsqlConnection(ConnectionParameters.ConnectionString)) {
                        while (stream.Position != stream.Length)
                        {
                            var tableData       = (TableData)binaryFormatter.Deserialize(stream);
                            NpgQuery scriptItem = _service.ExImHelper.GetInsertScriptItem(tableData);
                            if (scriptItem == null)
                            {
                                continue;
                            }
                            int count    = _service.NpgHelper.ExecuteNonQuery(connection, scriptItem);
                            string table = prevTable;
                            _service.ActionHelper.Dispatch(() => {
                                if (table != tableData.TableName && !string.IsNullOrEmpty(table))
                                {
                                    TablesStored++;
                                }
                                RowsStored += count;
                            });
                            prevTable = tableData.TableName;
                            serialHelper.Update(tableData);
                        }

                        foreach (NpgQuery 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;
                    });
                }
            });
        }