Welcome, guest | Sign In | My Account | Store | Cart

I use Entry to control the data. The SQLAction only do easy works.

Python, 213 lines
  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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: lintao

import MySQLdb
from gensql import gensql
from Entry import Entry

class Error(Exception):
    pass


class SQLAction(object):

    def __init__(self, logininfo):
        self.__login(logininfo)
        self.__entryies = {}
        self.cursor = {}

    def __login(self, logininfo):
        try:
            self.conn = MySQLdb.connect(
                    **logininfo)
        except:
            raise Error,"Login failed"

    def execute(self, sql, tablename):
        self.cursor[tablename] = self.conn.cursor()
        self.cursor[tablename].execute(sql)

    def commit(self):
        self.conn.commit()

    def __describe(self, tablename):
        sql = "describe %s;"%tablename
        self.execute(sql, tablename)
        return list(i[0] for i in self.cursor[tablename].fetchall())
        pass

    # Begin with statement, select
    def select(self, tablename, conddicts=None):
        return self.innerSelect(self, tablename, conddicts)

    class innerSelect(object):
        def __init__(self, outer, tablename, cond=None):
            self.outer = outer
            self.tablename = tablename
            self.cond = cond
            self.entry = self.outer.getentry(self.tablename)
            self.select()

        def select(self):
            sql = gensql("select", \
                    self.tablename, \
                    self.entry.getfield(), \
                    self.cond)
            #print sql
            self.outer.execute(sql, self.tablename)
            pass

        def next(self):
            tmpone = self.outer.cursor[self.tablename].fetchone()
            if not tmpone:
                return False
            with self.entry.edit():
                self.entry.update(dict(zip(self.entry.getfield(), tmpone)))
            self.entry.enablequery()
            return True

        def __enter__(self):
            self.entry.enablequery()
            return self
            pass
        def __getitem__(self, key):
            return self.entry[key]
        def __setitem__(self, key, value):
            raise Error, "Can't set key and value"
        def __exit__(self, exc_type, exc_value, traceback):
            self.entry.disablequery()
            pass
    # End with statement, select

    # Begin with statement, update
    def update(self, tablename, conddicts):
        return self.innerUpdate(self, tablename, conddicts)

    class innerUpdate(object):
        def __init__(self, outer, tablename, cond):
            self.outer = outer
            self.tablename = tablename
            self.cond = cond
            self.entry = self.outer.getentry(self.tablename)
            self.select()
        def __getitem__(self, key):
            return self.entry[key]
        def __setitem__(self, key, value):
            self.entry[key] = value

        def select(self):
            sql = gensql("select", \
                    self.tablename, \
                    self.entry.getfield(), \
                    self.cond)
            #print sql
            self.outer.execute(sql, self.tablename)
            pass

        def next(self):
            tmpone = self.outer.cursor[self.tablename].fetchone()
            if not tmpone:
                return False
            with self.entry.edit():
                self.entry.update(dict(zip(self.entry.getfield(), tmpone)))
            self.entry.enableedit()
            self.oldentry = self.entry.copydict()
            return True

        def update(self):
            sql = gensql("update", \
                    self.tablename, \
                    self.entry.copydict(), \
                    self.oldentry)
            self.outer.execute(sql, self.tablename)
            self.outer.commit()
            pass
        def ok(self):
            self.update()

        def __enter__(self):
            self.entry.enableedit()
            return self
            pass
        def __exit__(self, exc_type, exc_value, traceback):
            self.entry.disableedit()
            pass
    # End with statement, update


    # Begin with statement, insert
    def insert(self, tablename):
        return self.innerInsert(self, tablename)

    class innerInsert(object):
        def __init__(self, outer, tablename):
            self.outer = outer
            self.tablename = tablename
            self.entry = self.outer.getentry(self.tablename)

        def __enter__(self):
            self.entry.enableedit()
            return self
            pass

        def __getitem__(self, key):
            return self.entry[key]
        def __setitem__(self, key, value):
            self.entry[key] = value

        def ok(self):
            self.insert()
        def insert(self):
            sql = gensql("insert", \
                    self.tablename, \
                    self.entry.copydict())
            self.outer.execute(sql, self.tablename)
            self.outer.commit()


        def __exit__(self, exc_type, exc_value, traceback):
            self.entry.disableedit()
            pass
    # End with statement, insert

    def newentry(self, tablename):
        self.__entryies[tablename] = Entry(self.__describe(tablename))

    def getentry(self, tablename):
        return self.__entryies.get(tablename, None)

if __name__ == "__main__":
    logininfo = {"user":"lint", "host":"localhost", "passwd":"123456", "db":"TestBesBkk"}
    sqlaction = SQLAction(logininfo)
    sqlaction.newentry("person")
    oneentry = sqlaction.getentry("person")
    #with oneentry.edit():
    #    oneentry["id_p"] = "5"
    #    oneentry["name_p"] = "lint07"
    #    oneentry["age"] = 22

    #with oneentry.query():
    #    print oneentry["id_p"]
    #    print oneentry["name_p"]
    #    print oneentry["age"]
    #print sqlaction.cursor
    #with sqlaction.insert("person") as s:
    #    s["id_p"] = 1000
    #    s["name_p"] = "lintao07"
    #    s["age"] = 22222
    #    s.ok()
    with sqlaction.update("person", {"id_p":1000}) as s:
        while s.next():
            s["name_p"] = s["name_p"] + str(s["age"])
            s["age"] = 55555
            s.ok()


    #with sqlaction.select("person", None) as s:
    #    print s.entry.getfield()
    #    while s.next():
    #        print s["id_p"]
    #        print s["name_p"]
    #        print s["age"]
    #        #print s["noage"]
Created by MirGuest on Tue, 9 Aug 2011 (GPL3)
Python recipes (4591)
MirGuest's recipes (4)

Required Modules

  • (none specified)

Other Information and Tasks