<commit_before># MySQLdb Wrapper
A simple wrapper for MySQLdb, supports comman database operations with friendly interfaces.

## Usage
Build up connection to MySQL server using this module: 
```
>> from mysqldb_ex import MySQLdb_Ex

>> db = MySQLdb_Ex(host='localhost',  # host name of MySQL server
                port=3306,         # port number of MySQL server, default is 3306
                user='root',       # user account for connecting to MySQL server, default is None
                password='<PASSWORD>', # password for connecting to MySQL server, default is None
                db_name='test')    # database name
>> db
>> <Server: localhost:3306, Version: 5.6.31-log, Status: connected>
>> db  # if disconnect
>> <Server: localhost:3306, Status: disconnected>
```

Table example(table's name is 'users'):

| ID |    Name    | FirstName | LastName |    Job   |  Pay  |
|:--:|:-----------|:----------|:---------|:---------|:------|
| 1  | <NAME> | John      | Smith    | Manager  | 12000 |
| 2  | <NAME> | Peter     | Lutz     | Teacher  | 6000  |
| 3  | <NAME>  | Tom       | Jones    | Engineer | 8000  |

* Get fields' name of any table:
```
>> db.get_fields(table='users')
>> [u'ID', u'Name', u'FirstName', u'LastName', u'Job', u'Pay']
```

* Get records from any table:
```
>> db.get(table='users')
>> [{'Name': u'<NAME>', 'FirstName': u'John', 'Pay': 1200L, 
     'Job': u'Manager', 'LastName': u'Smith', 'ID': 1L}, 
    {'Name': u'<NAME>', 'FirstName': u'Peter', 'Pay': 6000L, 
     'Job': u'Teacher', 'LastName': u'Lutz', 'ID': 2L}, 
    {'Name': u'<NAME>', 'FirstName': u'Tom', 'Pay': 8000L, 
     'Job': u'Engineer', 'LastName': u'Jones', 'ID': 3L}]

>> db.get_one(table='users', fields=['Name', 'Pay'], conditions=['Job="Teacher"'])
>> {'Pay': 6000L, 'Name': u'<NAME>'}

>> db.get(table='users', fields=['Name', 'Pay'], conditions=['Job="Teacher"', 'OR', 'Job="Engineer"'])
>> [{'Pay': 6000L, 'Name': u'<NAME>'}, 
    {'Pay': 8000L, 'Name': u'<NAME>'}]
```

* Get a great number of records from database, `ssget` is recommanded:
```
>> db.ssget('users')
>> <MySQLdb.cursors.SSDictCursor object at 0x7f2db3f12590>

>> list(db.ssget('users'))
>> [{'Name': u'<NAME>', 'FirstName': u'John', 'Pay': 1200L, 
     'Job': u'Manager', 'LastName': u'Smith', 'ID': 1L}, 
    {'Name': u'<NAME>', 'FirstName': u'Peter', 'Pay': 6000L, 
     'Job': u'Teacher', 'LastName': u'Lutz', 'ID': 2L}, 
    {'Name': u'<NAME>', 'FirstName': u'Tom', 'Pay': 8000L, 
     'Job': u'Engineer', 'LastName': u'Jones', 'ID': 3L}]
```

* Delete an record in any table
```
>> db.delete(table='users', conditions=['FirstName="John"', 'AND', 'LastName="Smith"'])
>> 1  # the number of records have been deleted
```

* Insert a new record into any table
```
>> db.insert(table='users', data={'Name': '<NAME>', 'FirstName': 'John', 'Pay': 1200, 
        'Job': 'Manager', 'LastName': 'Smith'})
>> 4  # the row id for this new record
```

* Update records in any table
```
>> db.update(table='users', data={'Pay': 7000})
>> 3  # the number of records have been changed
>> db.get(table='users', fields=['Name', 'Pay'])
>> ({'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 7000L, 'Name': u'<NAME>'})

>> db.update(table='users', data={'Pay': 9000}, conditions=['Name="<NAME>"', ])
>> 1
>> db.get(table='users', fields=['Name', 'Pay'])
>> ({'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 9000L, 'Name': u'<NAME>'})
```

* Clear up any table
```
>> db.clear_table(table='users')
>> db.get(table='users')
>> ()
```

* Sometimes, the `ID` is wanted after a new record is inserted. One of the reasonable way is to assign the record with an `ID`, then insert with this `ID`. So when `UUID` is adopted, it should be generated before inserting. With this module, `UUID` can be generated like:
```
>> db.uuid()
>> 
```

## Cursor Class
Default, mysqldb returns query result as rows, but in practice, results as a dict may be more usable. So the cursor class is set to be MySQLdb.cursors.DictCursor in this module. For quering for a great number of records, MySQLdb.cursors.ssDictCursor is adopted, with which query results will be saved in server and return a iterator which will save space and maybe enhance efficiency.

## Timeout Issue
MySQL server will automatically break the connection to any client which keeps unactive for a certain time, default is 8 hours, and MySQLdb will raise an error when excuting any SQL query in this case. This module deals with this issue by reconnecting to the server when catch the error.

## License
The module is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
<commit_msg>add new function<commit_after># MySQLdb Wrapper
A simple wrapper for MySQLdb, supports comman database operations with friendly interfaces.

## Usage
Build up connection to MySQL server using this module: 
```
>> from mysqldb_ex import MySQLdb_Ex

>> db = MySQLdb_Ex(host='localhost',  # host name of MySQL server
                port=3306,         # port number of MySQL server, default is 3306
                user='root',       # user account for connecting to MySQL server, default is None
                password='<PASSWORD>', # password for connecting to MySQL server, default is None
                db_name='test')    # database name
>> db
>> <Server: localhost:3306, Version: 5.6.31-log, Status: connected>
>> db  # if disconnect
>> <Server: localhost:3306, Status: disconnected>
```

Table example(table's name is 'users'):

| ID |    Name    | FirstName | LastName |    Job   |  Pay  |
|:--:|:-----------|:----------|:---------|:---------|:------|
| 1  | <NAME> | John      | Smith    | Manager  | 12000 |
| 2  | <NAME> | Peter     | Lutz     | Teacher  | 6000  |
| 3  | <NAME>  | Tom       | Jones    | Engineer | 8000  |

* Get fields' name of any table:
```
>> db.get_fields(table='users')
>> [u'ID', u'Name', u'FirstName', u'LastName', u'Job', u'Pay']
```

* Get records from any table:
```
>> db.get(table='users')
>> [{'Name': u'<NAME>', 'FirstName': u'John', 'Pay': 1200L, 
     'Job': u'Manager', 'LastName': u'Smith', 'ID': 1L}, 
    {'Name': u'<NAME>', 'FirstName': u'Peter', 'Pay': 6000L, 
     'Job': u'Teacher', 'LastName': u'Lutz', 'ID': 2L}, 
    {'Name': u'<NAME>', 'FirstName': u'Tom', 'Pay': 8000L, 
     'Job': u'Engineer', 'LastName': u'Jones', 'ID': 3L}]

>> db.get_one(table='users', fields=['Name', 'Pay'], conditions=['Job="Teacher"'])
>> {'Pay': 6000L, 'Name': u'<NAME>'}

>> db.get(table='users', fields=['Name', 'Pay'], conditions=['Job="Teacher"', 'OR', 'Job="Engineer"'])
>> [{'Pay': 6000L, 'Name': u'<NAME>'}, 
    {'Pay': 8000L, 'Name': u'<NAME>'}]
```

* Get a great number of records from database, `ssget` is recommanded:
```
>> db.ssget('users')
>> <MySQLdb.cursors.SSDictCursor object at 0x7f2db3f12590>

>> list(db.ssget('users'))
>> [{'Name': u'<NAME>', 'FirstName': u'John', 'Pay': 1200L, 
     'Job': u'Manager', 'LastName': u'Smith', 'ID': 1L}, 
    {'Name': u'<NAME>', 'FirstName': u'Peter', 'Pay': 6000L, 
     'Job': u'Teacher', 'LastName': u'Lutz', 'ID': 2L}, 
    {'Name': u'<NAME>', 'FirstName': u'Tom', 'Pay': 8000L, 
     'Job': u'Engineer', 'LastName': u'Jones', 'ID': 3L}]
```

* Delete an record in any table
```
>> db.delete(table='users', conditions=['FirstName="John"', 'AND', 'LastName="Smith"'])
>> 1  # the number of records have been deleted
```

* Insert a new record into any table
```
>> db.insert(table='users', data={'Name': '<NAME>', 'FirstName': 'John', 'Pay': 1200, 
        'Job': 'Manager', 'LastName': 'Smith'})
>> 4  # the row id for this new record
```

* Update records in any table
```
>> db.update(table='users', data={'Pay': 7000})
>> 3  # the number of records have been changed
>> db.get(table='users', fields=['Name', 'Pay'])
>> ({'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 7000L, 'Name': u'<NAME>'})

>> db.update(table='users', data={'Pay': 9000}, conditions=['Name="<NAME>"', ])
>> 1
>> db.get(table='users', fields=['Name', 'Pay'])
>> ({'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 7000L, 'Name': u'<NAME>'}, 
    {'Pay': 9000L, 'Name': u'<NAME>'})
```

* Clear up any table
```
>> db.clear_table(table='users')
>> db.get(table='users')
>> ()
```

* Sometimes, the `ID` is wanted after a new record is inserted. One of the reasonable way is to assign the record with an `ID`, then insert with this `ID`. So when `UUID` is adopted, it should be generated before inserting. With this module, `UUID` can be generated like:
```
>> db.uuid()
>> 1392ddce-1b74-11e7-966d-f46d04db2563
```

## Cursor Class
Default, mysqldb returns query result as rows, but in practice, results as a dict may be more usable. So the cursor class is set to be MySQLdb.cursors.DictCursor in this module. For quering for a great number of records, MySQLdb.cursors.ssDictCursor is adopted, with which query results will be saved in server and return a iterator which will save space and maybe enhance efficiency.

## Timeout Issue
MySQL server will automatically break the connection to any client which keeps unactive for a certain time, default is 8 hours, and MySQLdb will raise an error when excuting any SQL query in this case. This module deals with this issue by reconnecting to the server when catch the error.

## License
The module is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

{'fraction_non_alphanumeric': 0.20078116752876596, 'fraction_numerical': 0.029241000738942258, 'mean_word_length': 3.926677067082683, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 191, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6129677', 'n_tokens_mistral': 3466, 'n_tokens_neox': 3121, 'n_words': 1049}