Esempio n. 1
0
        public Result GetScript(bool bDrop, bool bInsert, bool bCached, Channel channel)
        {
            channel.CheckAdmin();

            Result r = new Result(1);

            r.Type[0]  = ColumnType.VarChar;
            r.Table[0] = "SYSTEM_SCRIPT";
            r.Label[0] = "COMMAND";
            r.Name[0]  = "COMMAND";

            StringBuilder a = new StringBuilder();

            for (int i = 0; i < tTable.Count; i++)
            {
                Table t = (Table)tTable[i];

                if (bDrop)
                {
                    AddRow(r, "DROP TABLE \"" + t.Name + "\"");
                }

                a.Remove(0, a.Length);
                a.Append("CREATE ");

                if (t.IsCached)
                {
                    a.Append("CACHED ");
                }

                a.Append("TABLE ");
                a.Append('"');
                a.Append(t.Name);
                a.Append('"');
                a.Append("(");

                int   columns = t.ColumnCount;
                Index pki     = t.GetIndex("SYSTEM_PK");
                int   pk      = (pki == null) ? -1 : pki.Columns[0];

                for (int j = 0; j < columns; j++)
                {
                    a.Append('"');
                    a.Append(t.GetColumnName(j));
                    a.Append('"');
                    a.Append(" ");
                    a.Append(Column.GetColumnTypeString(t.GetType(j)));

                    if (!t.GetColumnIsNullable(j))
                    {
                        a.Append(" NOT NULL");
                    }

                    if (j == t.IdentityColumn)
                    {
                        a.Append(" IDENTITY");
                    }

                    if (j == pk)
                    {
                        a.Append(" PRIMARY KEY");
                    }

                    if (j < columns - 1)
                    {
                        a.Append(",");
                    }
                }

                ArrayList v = t.Constraints;

                for (int j = 0; j < v.Count; j++)
                {
                    Constraint c = (Constraint)v[j];

                    if (c.ConstraintType == ConstraintType.ForeignKey)
                    {
                        a.Append(",FOREIGN KEY");

                        int[] col = c.RefTableColumns;

                        a.Append(GetColumnList(c.RefTable, col, col.Length));
                        a.Append("REFERENCES ");
                        a.Append(c.MainTable.Name);

                        col = c.MainTableColumns;

                        a.Append(GetColumnList(c.MainTable, col, col.Length));
                    }
                    else if (c.ConstraintType == ConstraintType.Unique)
                    {
                        a.Append(",UNIQUE");

                        int[] col = c.MainTableColumns;

                        a.Append(GetColumnList(c.MainTable, col, col.Length));
                    }
                }

                a.Append(")");
                AddRow(r, a.ToString());

                Index index = null;

                while (true)
                {
                    index = t.GetNextIndex(index);

                    if (index == null)
                    {
                        break;
                    }

                    string indexname = index.Name;

                    if (indexname.Equals("SYSTEM_PK"))
                    {
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_FOREIGN_KEY"))
                    {
                        // foreign keys where created in the 'create table'
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_CONSTRAINT"))
                    {
                        // constraints where created in the 'create table'
                        continue;
                    }

                    a.Remove(0, a.Length);
                    a.Append("CREATE ");

                    if (index.IsUnique)
                    {
                        a.Append("UNIQUE ");
                    }

                    a.Append("INDEX ");
                    a.Append(indexname);
                    a.Append(" ON ");
                    a.Append(t.Name);

                    int[] col = index.Columns;
                    int   len = col.Length;

                    if (!index.IsUnique)
                    {
                        len--;
                    }

                    a.Append(GetColumnList(t, col, len));
                    AddRow(r, a.ToString());
                }

                if (bInsert)
                {
                    Index primary   = t.PrimaryIndex;
                    Node  x         = primary.First();
                    bool  integrity = true;

                    if (x != null)
                    {
                        integrity = false;

                        AddRow(r, "SET REFERENTIAL_INTEGRITY FALSE");
                    }

                    while (x != null)
                    {
                        AddRow(r, t.GetInsertStatement(x.GetData()));

                        x = primary.Next(x);
                    }

                    if (!integrity)
                    {
                        AddRow(r, "SET REFERENTIAL_INTEGRITY TRUE");
                    }
                }

                if (bCached && t.IsCached)
                {
                    a.Remove(0, a.Length);
                    a.Append("SET TABLE ");

                    a.Append('"');
                    a.Append(t.Name);
                    a.Append('"');
                    a.Append(" INDEX '");
                    a.Append(t.IndexRoots);
                    a.Append("'");
                    AddRow(r, a.ToString());
                }
            }

            ArrayList uList = aAccess.GetUsers();

            for (int i = 0; i < uList.Count; i++)
            {
                User u = (User)uList[i];

                // todo: this is not a nice implementation
                if (u == null)
                {
                    continue;
                }

                string name = u.Name;

                if (!name.Equals("PUBLIC"))
                {
                    a.Remove(0, a.Length);
                    a.Append("CREATE USER ");

                    a.Append(name);
                    a.Append(" PASSWORD ");
                    a.Append("\"" + u.Password + "\"");

                    if (u.IsAdmin)
                    {
                        a.Append(" ADMIN");
                    }

                    AddRow(r, a.ToString());
                }

                Hashtable rights = u.Rights;

                if (rights == null)
                {
                    continue;
                }

                foreach (string dbObject in rights.Keys)
                {
                    AccessType right = (AccessType)rights[dbObject];

                    if (right == AccessType.None)
                    {
                        continue;
                    }

                    a.Remove(0, a.Length);
                    a.Append("GRANT ");

                    a.Append(Access.GetRight(right));
                    a.Append(" ON ");
                    a.Append(dbObject);
                    a.Append(" TO ");
                    a.Append(u.Name);
                    AddRow(r, a.ToString());
                }
            }

            if (dDatabase.IsIgnoreCase)
            {
                AddRow(r, "SET IGNORECASE TRUE");
            }

            Hashtable h = dDatabase.Alias;

            foreach (string alias in h.Keys)
            {
                string className = (string)h[alias];
                AddRow(r, "CREATE ALIAS " + alias + " FOR \"" + className + "\"");
            }

            return(r);
        }
Esempio n. 2
0
        /// <summary>
        /// Script the database objects to a file.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="file"></param>
        /// <param name="full"></param>
        /// <param name="channel"></param>
        public static void ScriptToFile(Database db, string file, bool full, Channel channel)
        {
            if ((new FileInfo(file)).Exists)
            {
                // there must be no such file; overwriting not allowed for security
                throw TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file);
            }

            try
            {
                DateTime time = DateTime.Now;

                // only ddl commands; needs not so much memory
                Result r;

                if (full)
                {
                    // no drop, no insert, and no positions for cached tables
                    r = db.GetScript(false, false, false, channel);
                }
                else
                {
                    // no drop, no insert, but positions for cached tables
                    r = db.GetScript(false, false, true, channel);
                }

                Record       n = r.Root;
                StreamWriter w = new StreamWriter(file);

                while (n != null)
                {
                    writeLine(w, (string)n.Data[0]);

                    n = n.Next;
                }

                // inserts are done separetely to save memory
                ArrayList tables = db.Tables;

                for (int i = 0; i < tables.Count; i++)
                {
                    Table t = (Table)tables[i];

                    // cached tables have the index roots set in the ddl script
                    if (full || !t.IsCached)
                    {
                        Index primary = t.PrimaryIndex;
                        Node  x       = primary.First();

                        while (x != null)
                        {
                            writeLine(w, t.GetInsertStatement(x.GetData()));

                            x = primary.Next(x);
                        }
                    }
                }

                w.Close();

                TimeSpan execution = DateTime.Now.Subtract(time);

                if (TracingHelper.TraceEnabled)
                {
                    TracingHelper.Write((Int64)execution.TotalMilliseconds);
                }
            }
            catch (IOException e)
            {
                TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file + " " + e);
            }
        }