Use the with statement to insert, select, and update a table. Every entry may be just a dictionary. You can see the Test. Here, You need gensql module(http://code.activestate.com/recipes/577605-auto-generate-simple-sql-statements/) to auto generate sql.
| 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | #!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: lintao
# TODO
# This may have some bugs.
# So, I need to develop it.
import MySQLdb
from gensql import gensql
import copy
class LoginInfo(object):
    def __init__(self):
        self.user = "lint"
        self.passwd = "123456"
        self.host = "localhost"
        self.db = ""
class Table(object):
    def __init__(self, tablename, logininfo):
        self.tablename = tablename
        self.__login(logininfo)
        self.__describe_table()
        # Variable
        self.__condition__enable = False
        self.__set_condition__enable = False
        pass
    def __login(self, logininfo):
        try:
            self.conn = MySQLdb.connect(
                    host = logininfo.host,
                    user = logininfo.user,
                    passwd = logininfo.passwd,
                    db = logininfo.db)
        except:
            raise Exception, "Login failed"
    def __execute(self, sql):
        self.cursor = self.conn.cursor()
        self.cursor.execute(sql)
    def __commit(self):
        self.conn.commit()
    def __describe_table(self):
        sql = "describe %s;" %self.tablename
        self.__execute(sql)
        self.tablekeys = [ i[0] for i in self.cursor.fetchall()]
        pass
    def __check_inputkey(self, inputkey):
        if not inputkey:
            return True
        if isinstance(inputkey, str):
            return (inputkey in self.tablekeys)
        return all((i in self.tablekeys) for i in inputkey)
    def __getitem__(self, key):
        if not self.__condition__enable:
            raise Exception, "Please enable conditions, or in the with statement"
            return
        if not (self.__check_inputkey(key) or key=="*"):
            raise Exception, "Not found the key:(%s)"%key
            return 
        if self.__one_entry:
            if key == "*":
                return self.__one_entry
            return self.__one_entry[key]
    def selectBeginCond(self, conddicts=None):
        """ 
        You need to begin with cond,
        so I can begin to fetch data
        """
        if self.__condition__enable:
            return
        self.__condition__enable = True
        if not self.__check_inputkey(conddicts):
            raise Exception, "The condition has some errors"
        sql = gensql("select", \
                self.tablename, \
                ",".join(self.tablekeys), \
                conddicts)
        self.__execute(sql)
        self.__one_entry = None
        pass
    def selectNext(self):
        if self.__condition__enable:
            tmpone = self.cursor.fetchone()
            if tmpone:
                self.__one_entry = dict(zip(self.tablekeys, \
                        tmpone))
                if self.__one_entry:
                    return True
        return False
        pass
    def selectEndCond(self):
        if not self.__condition__enable:
            return
        self.__condition__enable = False
        self.__one_entry = None
        pass
    # 
    def selectcond(self, cond=None):
        return self.innerselect(self, cond)
    
    # Use the with Statement
    class innerselect(object):
        def __init__(self, outer, cond):
            self.outer = outer
            self.cond = cond
        def __enter__(self):
            self.outer.selectBeginCond(self.cond)
            return self
        def next(self):
            return self.outer.selectNext()
        def __getitem__(self, key):
            return self.outer[key]
        def __getitem__raise(self, *args, **kwds):
            raise Exception, "Can't call out the with statement"
        def __exit__(self, exc_type, exc_value, traceback):
            self.outer.selectEndCond()
    #
    def updatecond(self, cond=None):
        return self.innerupdate(self, cond)
    class innerupdate(object):
        def __init__(self, outer, cond):
            self.outer = outer
            self.cond = cond
        def __enter__(self):
            self.outer.updateBeginCond(self.cond)
            return self
        def next(self):
            return self.outer.updateNext()
        def ok(self):
            self.outer.updateOK()
        def __getitem__(self, key):
            return self.outer[key]
        def __setitem__(self, key, value):
            self.outer[key] = value
        def __exit__(self, exc_type, exc_value, traceback):
            self.outer.updateEndCond()
    # TODO
    def updateBeginCond(self, conddicts):
        if self.__set_condition__enable:
            return
        self.__set_condition__enable = True
        self.selectBeginCond(conddicts)
        # It is to save the conddicts
        self.__tmp__dict__update = copy.deepcopy(conddicts)
        pass
    def updateEndCond(self):
        # if it didn't start, just return
        if not self.__set_condition__enable:
            return
        self.__tmp__dict__update = None
        self.selectEndCond()
        self.__set_condition__enable = False
        pass
    def updateNext(self):
        if not self.__set_condition__enable:
            return False
        return self.selectNext()
    def updateOK(self):
        #print self.__one_entry
        # TODO
        # Really to update
        if not isinstance(self.__one_entry, dict):
            return False
        sql = gensql("update", \
                self.tablename, \
                self.__one_entry, \
                self.__tmp__dict__update)
        self.__execute(sql)
        self.__commit()
        return True
        pass
    def __setitem__(self, key, value):
        if not (self.__condition__enable and self.__set_condition__enable):
            pass
        if not (self.__check_inputkey(key)):
            raise Exception, "Not found the key:(%s)"%key
            return 
        self.__one_entry[key] = value
        pass
    # TODO
    def insertBegin(self):
        if self.__condition__enable or self.__set_condition__enable:
            return 
        self.__condition__enable = True
        self.__set_condition__enable = True
        self.__one_entry = {} #dict((key, None) for key in self.tablekeys)
        pass
    def insertEnd(self):
        if not (self.__condition__enable and self.__set_condition__enable):
            return
        self.__condition__enable = False
        self.__set_condition__enable = False
        if not any(self.__one_entry.values()):
            print "nothing to insert"
            return
        sql = gensql("insert", \
                self.tablename, \
                self.__one_entry)
        self.__execute(sql)
        self.__commit()
        self.__one_entry = None
        pass
    def insertcond(self):
        return self.innerinsert(self)
        pass
    class innerinsert(object):
        def __init__(self, outer):
            self.outer = outer
        def __enter__(self):
            self.outer.insertBegin()
            return self
        def __getitem__(self, key):
            return self.outer[key]
        def __setitem__(self, key, value):
            self.outer[key] = value
        def __exit__(self, exc_type, exc_value, traceback):
            self.outer.insertEnd()
    pass
if __name__ == "__main__":
    logininfo = LoginInfo()
    logininfo.db = "TestBesBkk"
    t = Table("person", logininfo)
    # Test 0
    #print t.tablekeys
    #print t.s__check_inputkey("id_p")
    #print t.s__check_inputkey(["id_p","p"])
    # Test 1
    #t.selectBeginCond({"id_p":2})
    #while t.selectNext():
    #    print t["id_p"], 
    #    print t["name_p"]
    #    print t["*"]
    #    #print t["notexist"]
    #t.selectEndCond()
    #pass
    # Test 2
    #t.updateBeginCond({"id_p":3})
    #while t.updateNext():
    #    #t["id_p"] = "3"
    #    t["id_p"] = "3"
    #    t.updateOK()
    #t.updateEndCond()
    # Post Test 3
    #t.selectBeginCond()
    #while t.selectNext():
    #    print t["id_p"], 
    #    print t["name_p"]
    #    print t["*"]
    #t.selectEndCond()
    # Test 4
    #t.insertBegin()
    #t["id_p"] = 10
    #t.insertEnd()
    # Test 5
    #with t.selectcond() as l:
    #    while l.next():
    #        print l["*"]
    ##print l["*"]
    # Test 6
    #with t.updatecond({"id_p":3}) as l:
    #    while l.next():
    #        l["name_p"] = "lint@ihep"
    #        l["age"] = "222"
    #        l.ok()
    # Test 7
    with t.insertcond() as l:
        l["name_p"] = "lintao51@gmail.com"
        l["id_p"] = 100
        l["age"] = 22
    print l["name_p"]
 | 

 Download
Download Copy to clipboard
Copy to clipboard
