root/livinglogic.python.orasql/src/ll/orasql/scripts/oradrop.py @ 182:0d4d435866b6

Revision 182:0d4d435866b6, 3.5 KB (checked in by Walter Doerwald <walter@…>, 13 years ago)

Add class Privilege that models an object grant.

Add Connection.iterprivileges() for getting all privileges.

Add a script oragrant.py for copying privileges.

Update copyright year.

Line 
1#!/usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4## Copyright 2005-2007 by LivingLogic AG, Bayreuth/Germany.
5## Copyright 2005-2007 by Walter Dörwald
6##
7## All Rights Reserved
8##
9## See orasql/__init__.py for the license
10
11
12import sys, os, optparse
13
14from ll import astyle, orasql
15
16
17s4warning = astyle.Style.fromenv("LL_ORASQL_REPRANSI_WARNING", "red:black")
18s4error = astyle.Style.fromenv("LL_ORASQL_REPRANSI_ERROR", "red:black")
19s4connectstring = astyle.Style.fromenv("LL_ORASQL_REPRANSI_CONNECTSTRING", "yellow:black")
20s4object = astyle.Style.fromenv("LL_ORASQL_REPRANSI_OBJECT", "green:black")
21
22
23def main(args=None):
24    colors = ("yes", "no", "auto")
25    fks = ("keep", "disable", "drop")
26    p = optparse.OptionParser(usage="usage: %prog [options] connectstring >output.sql")
27    p.add_option("-v", "--verbose", dest="verbose", help="Give a progress report?", default=False, action="store_true")
28    p.add_option("-c", "--color", dest="color", help="Color output (%s)" % ", ".join(colors), default="auto", choices=colors)
29    p.add_option("-f", "--fks", dest="fks", help="How should foreign keys from other schemas be treated (%s)?" % ", ".join(fks), default="disable", choices=fks)
30    p.add_option("-x", "--execute", dest="execute", action="store_true", help="immediately execute the commands instead of printing them?")
31    p.add_option("-k", "--keepjunk", dest="keepjunk", help="Output objects with '$' in their name?", default=False, action="store_true")
32    p.add_option("-i", "--ignore", dest="ignore", help="Ignore errors?", default=False, action="store_true")
33
34    (options, args) = p.parse_args(args)
35    if len(args) != 1:
36        p.error("incorrect number of arguments")
37        return 1
38
39    if options.color == "yes":
40        color = True
41    elif options.color == "no":
42        color = False
43    else:
44        color = None
45
46    stdout = astyle.Stream(sys.stdout, color)
47    stderr = astyle.Stream(sys.stderr, color)
48
49    connection = orasql.connect(args[0])
50
51    term = not options.execute
52   
53    cs = s4connectstring(connection.connectstring())
54
55    def keep(obj):
56        if obj.owner is not None and not isinstance(obj, orasql.ForeignKey):
57            return False
58        if options.keepjunk:
59            return True
60        if "$" in obj.name:
61            return False
62        return True
63
64    ddls = []
65    for (i, obj) in enumerate(connection.iterobjects(mode="drop", schema="user")):
66        keepdef = keep(obj)
67        # Get DDL
68        ddl = ""
69        action = "skipped"
70        if obj.owner is not None:
71            if isinstance(obj, orasql.ForeignKey):
72                if options.fks == "disable":
73                    ddl = obj.disableddl(cursor, term)
74                    action = "disabled"
75                elif options.fks == "drop":
76                    ddl = obj.dropddl(cursor, term)
77                    action = None
78        elif keepdef:
79            ddl = obj.dropddl(connection, term)
80            action = None
81
82        # Progress report
83        if options.verbose:
84            msg = astyle.style_default("oradrop.py: ", cs, ": fetching #%d " % (i+1), s4object(str(obj)))
85            if action is not None:
86                msg = astyle.style_default(msg, " ", s4warning("(%s)" % action))
87            stderr.writeln(msg)
88
89        if ddl:
90            # Print or execute DDL
91            if options.execute:
92                ddls.append((obj, ddl))
93            else:
94                stdout.write(ddl)
95
96    # Execute DDL
97    if options.execute:
98        cursor = connection.cursor()
99        for (i, (obj, ddl)) in enumerate(ddls):
100            if options.verbose:
101                stderr.writeln("oradrop.py: ", cs, ": dropping #%d/%d " % (i+1, len(ddls)), s4object(str(obj)))
102            try:
103                cursor.execute(ddl)
104            except orasql.DatabaseError, exc:
105                if not options.ignore or "ORA-01013" in str(exc):
106                    raise
107                stderr.writeln("oradrop.py: ", s4error("%s: %s" % (exc.__class__, str(exc).strip())))
108
109
110if __name__ == "__main__":
111    sys.exit(main())
Note: See TracBrowser for help on using the browser.