Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
RadoRado
committed
Feb 8, 2016
1 parent
8970d4e
commit c795424
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import inspect | ||
|
||
TYPES = { | ||
int: 'INTEGER', | ||
str: 'VARCHAR' | ||
} | ||
|
||
|
||
def map_python_to_sql(py_type): | ||
if py_type not in TYPES: | ||
return TYPES[str] | ||
|
||
return TYPES[py_type] | ||
|
||
|
||
def varchar(): | ||
return str | ||
|
||
|
||
def integer(): | ||
return int | ||
|
||
|
||
def pluralize(string): | ||
plural_forms = ['s', 'es'] | ||
|
||
for form in plural_forms: | ||
if string.endswith(form): | ||
return string | ||
|
||
# FIXME: Not a proper english | ||
return string + 's' | ||
|
||
|
||
def extract_dict_from_class(cls): | ||
members = [m for m in inspect.getmembers(cls) | ||
if '__' not in m[0]] | ||
attributes = {m[0]: m[1] for m in members} | ||
return (cls.__name__, attributes) | ||
|
||
|
||
def class_to_table(name, attributes): | ||
create_sql = """ | ||
CREATE TABLE {name}( | ||
id INTEGER PRIMARY KEY AUTO INCRAMENT, | ||
{columns} | ||
); | ||
""" | ||
data = { | ||
"name": pluralize(name), | ||
"columns": "" | ||
} | ||
|
||
columns = [] | ||
for attr in attributes: | ||
value_type = attributes[attr] | ||
columns.append("{} {}".format(attr, | ||
map_python_to_sql(value_type))) | ||
|
||
data['columns'] = ",\n".join(columns) | ||
return create_sql.format(**data) | ||
|
||
|
||
class Person: | ||
email = varchar() | ||
password = varchar() | ||
age = integer() | ||
|
||
print(class_to_table(*extract_dict_from_class(Person))) |