10
10
11
11
DEFAULT_VARCHAR_SIZE = 25
12
12
MAX_VARCHAR_LENGTH = 1000
13
+ SKIP_ID_FIELD = False
14
+ SKIP_CLASS_FIELD = False
13
15
14
16
def camel_to_snake (name ):
15
17
name = name .replace (' ' , '_' )
@@ -26,10 +28,9 @@ def enquote(identifier):
26
28
return f"`{ identifier } `"
27
29
28
30
def type_to_mysql (column_name , py_type , max_length ):
29
- DEFAULT_VARCHAR_SIZE = 25
30
- MAX_VARCHAR_LENGTH = 1000
31
+ varchar_type = f'VARCHAR({ min (max (DEFAULT_VARCHAR_SIZE , (max_length // DEFAULT_VARCHAR_SIZE + 1 ) * DEFAULT_VARCHAR_SIZE ), MAX_VARCHAR_LENGTH )} )' if max_length and max_length <= MAX_VARCHAR_LENGTH else 'TEXT'
31
32
type_mapping = {
32
- 'str' : f'VARCHAR( { min ( max ( DEFAULT_VARCHAR_SIZE , ( max_length // DEFAULT_VARCHAR_SIZE + 1 ) * DEFAULT_VARCHAR_SIZE ), MAX_VARCHAR_LENGTH ) } )' if max_length <= MAX_VARCHAR_LENGTH else 'TEXT' ,
33
+ 'str' : varchar_type ,
33
34
'int' : 'INT' ,
34
35
'float' : 'FLOAT' ,
35
36
'bool' : 'BOOLEAN' ,
@@ -107,7 +108,7 @@ def create_mysql_table(mysql_cursor, collection_name, document):
107
108
max_lengths = {camel_to_snake (key ): len (str (value )) for key , value in document .items () if key not in ['_id' , '_class' ]}
108
109
structure = {}
109
110
for key , value in document .items ():
110
- if key not in ['_id' , '_class' ]:
111
+ if key not in ['_id' , '_class' ] or ( key == '_id' and not SKIP_ID_FIELD ) or ( key == '_class' and not SKIP_CLASS_FIELD ) :
111
112
if isinstance (value , dict ):
112
113
structure .update (process_nested_document (value , camel_to_snake (key )))
113
114
else :
@@ -123,7 +124,7 @@ def create_mysql_table(mysql_cursor, collection_name, document):
123
124
124
125
def insert_into_mysql (mysql_cursor , collection_name , document ):
125
126
collection_name = camel_to_snake (collection_name )
126
- document = {camel_to_snake (key ): value for key , value in document .items () if key not in ['_id' , '_class' ]}
127
+ document = {camel_to_snake (key ): value for key , value in document .items () if key not in ['_id' , '_class' ] or ( key == '_id' and not SKIP_ID_FIELD ) or ( key == '_class' and not SKIP_CLASS_FIELD ) }
127
128
document = convert_document (document )
128
129
keys = ', ' .join (enquote (key ) for key in document .keys ())
129
130
values = ', ' .join (['%s' for _ in document .values ()])
0 commit comments