File: sybase.py

package info (click to toggle)
albatross 1.36-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,472 kB
  • ctags: 1,822
  • sloc: python: 7,437; makefile: 146; sh: 132
file content (141 lines) | stat: -rwxr-xr-x 4,153 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import Sybase
from albatross import SimpleSessionApp
from albatross.apacheapp import Request


def get_table_list(ctx):
    db = ctx.app.db
    db.execute('use %s' % ctx.locals.database)
    c = db.cursor()
    c.execute('select name from sysobjects'
              ' where type in ("U", "S")'
              ' order by name')
    tables = map(lambda r: r[0], c.fetchall())
    ctx.locals.tables = tables


def get_database_list(ctx):
    c = ctx.app.db.cursor()
    c.execute('select name from master..sysdatabases')
    databases = map(lambda r: r[0], c.fetchall())
    ctx.locals.databases = databases


def get_table_desc(ctx):
    db = ctx.app.db
    db.execute('use %s' % ctx.locals.database)
    c = db.cursor()
    c.execute('select id from sysobjects'
              ' where type in ("U", "S") and name = "%s"' % ctx.locals.table)
    table_id, = c.fetchone()
    c.execute('select c.name, t.name, c.length, c.status'
              ' from syscolumns c, systypes t'
              ' where c.id = %d and c.usertype = t.usertype'
              ' order by c.colid' % table_id)
    ctx.locals.columns = c.fetchall()


def get_table_view(ctx):
    db = ctx.app.db
    db.execute('use %s' % ctx.locals.database)
    c = db.cursor()
    c.execute('select u.name from sysusers u, sysobjects o'
              ' where o.type in ("U","S") and o.name = "%s"'
              ' and o.uid = u.uid' % ctx.locals.table)
    owner, = c.fetchone()
    table = '%s.%s' % (owner, ctx.locals.table)
    c = db.cursor()
    c.execute('select * from %s' % table)
    ctx.locals.desc = c.description
    ctx.locals.rows = c.fetchall()


def request_field(ctx, name):
    if ctx.request.has_field(name):
        return ctx.request.field_value(name)


class TableList:

    name = 'table_list'

    def page_display(self, ctx):
        get_database_list(ctx)
        get_table_list(ctx)
        ctx.run_template('table-list.html')

    def page_process(self, ctx):
        if ctx.req_equals('changedb'):
            ctx.locals.database = request_field(ctx, 'database')
        elif ctx.req_equals('desc'):
            ctx.locals.table = request_field(ctx, 'desc')
            ctx.set_page('table_desc')
        elif ctx.req_equals('view'):
            ctx.locals.table = request_field(ctx, 'view')
            ctx.set_page('table_view')


class TableDesc:

    name = 'table_desc'

    def page_display(self, ctx):
        get_table_desc(ctx)
        ctx.run_template('table-desc.html')

    def page_process(self, ctx):
        if ctx.req_equals('changetable'):
            ctx.locals.table = request_field(ctx, 'table')
        elif ctx.req_equals('back'):
            ctx.set_page('table_list')


class TableView:

    name = 'table_view'

    def page_display(self, ctx):
        get_table_view(ctx)
        ctx.run_template('table-view.html')

    def page_process(self, ctx):
        if ctx.req_equals('changetable'):
            try:
                del ctx.locals.r
            except AttributeError:
                pass
            ctx.locals.table = request_field(ctx, 'table')
        elif ctx.req_equals('back'):
            ctx.set_page('table_list')


class App(SimpleSessionApp):

    def __init__(self):
        SimpleSessionApp.__init__(self,
                                  base_url='sybase.py',
                                  template_path='-=-install_dir-=-',
                                  start_page='table_list',
                                  secret='-=-secret-=-',
                                  session_appid='sybase')
        for page_class in (TableList, TableDesc, TableView):
            self.register_page(page_class.name, page_class())
        self.db = Sybase.connect('rat', 'sa', '')

    def load_session(self, ctx):
        SimpleSessionApp.load_session(self, ctx)
        ctx.default_session_var('database', 'master')
        ctx.default_session_var('tables', [])
        ctx.default_session_var('table', None)

    def create_context(self):
        ctx = SimpleSessionApp.create_context(self)
        ctx.run_template_once('macros.html')
        return ctx


app = App()


def handler(req):
    return app.run(Request(req))