Example #1
0
        private void CopyView(
            Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > leftSchema,
            Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > rightSchema,
            SQLiteCreateViewStatement left,
            SQLiteCreateViewStatement right, string leftdb, string rightdb, bool leftToRight)
        {
            using (SQLiteConnection leftConn = MakeDbConnection(leftdb))
            {
                leftConn.Open();
                using (SQLiteConnection rightConn = MakeDbConnection(rightdb))
                {
                    rightConn.Open();

                    SQLiteTransaction tx = null;
                    try
                    {
                        string name;
                        if (left != null)
                        {
                            name = left.ObjectName.ToString();
                        }
                        else
                        {
                            name = right.ObjectName.ToString();
                        }

                        if (leftToRight)
                        {
                            tx = rightConn.BeginTransaction();
                            ReplaceView(name, rightSchema, rightConn, left, tx, 15, 70);
                        }
                        else
                        {
                            tx = leftConn.BeginTransaction();
                            ReplaceView(name, leftSchema, leftConn, right, tx, 15, 70);
                        }

                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                        throw;
                    } // catch
                }     // using
            }         // using
        }
Example #2
0
        private void ReplaceView(string name,
                                 Dictionary <SchemaObject, Dictionary <string, SQLiteDdlStatement> > schema,
                                 SQLiteConnection conn, SQLiteCreateViewStatement stmt,
                                 SQLiteTransaction tx, int start, int end)
        {
            if (stmt != null && schema != null)
            {
                // Make sure that all tables that view refers to exist in the target database schema
                // before trying to copy the view there.

                List <string> tables = GetReferredTables(stmt.SelectStatement);
                foreach (string tableName in tables)
                {
                    if (!schema[SchemaObject.Table].ContainsKey(tableName))
                    {
                        throw new InvalidOperationException("The view " + stmt.ObjectName.ToString() +
                                                            " cannot be added because table " + tableName +
                                                            " does not exist in the target database.\r\nPlease make sure that this table " +
                                                            " is copied to the target database before trying to copy the view.");
                    }
                } // foreach
            }

            NotifyPrimaryProgress(false, start, "deleting view " + name);

            SQLiteCommand cmd = new SQLiteCommand(
                @"DROP VIEW IF EXISTS " + name, conn, tx);

            cmd.ExecuteNonQuery();

            if (_cancelled)
            {
                throw new UserCancellationException();
            }

            if (stmt != null)
            {
                NotifyPrimaryProgress(false, (end - start) / 2 + start, "re-creating view ...");

                // Re-create the view in the right database based on the
                // view schema in the left database.
                cmd = new SQLiteCommand(stmt.ToString(), conn, tx);
                cmd.ExecuteNonQuery();
            }
        }