mirror of
https://github.com/yweber/lodel2.git
synced 2026-06-10 04:50:49 +02:00
[#63] New DataSource Module
* New submodule MySQL * Unsuseful methods deleted * Methods to create table and field names in the unified DataSource class
This commit is contained in:
parent
10379c6a95
commit
e39a00b361
3 changed files with 87 additions and 0 deletions
87
DataSource/MySQL/MySQL.py
Normal file
87
DataSource/MySQL/MySQL.py
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
# -*- coding: utf8 -*-
|
||||||
|
|
||||||
|
|
||||||
|
import EditorialModel
|
||||||
|
|
||||||
|
## @brief Manages the accesses to a MySQL datasource
|
||||||
|
class MySQL(object):
|
||||||
|
|
||||||
|
_relations_table_name = 'relation'
|
||||||
|
_relations_field_nature = 'nature'
|
||||||
|
_field_lodel_id = 'lodel_id'
|
||||||
|
_class_table_prefix = 'class_'
|
||||||
|
_objects_table_name = 'object'
|
||||||
|
_connections = {
|
||||||
|
'default':{
|
||||||
|
'module': pymysql,
|
||||||
|
'host': '127.0.0.1',
|
||||||
|
'user': 'lodel',
|
||||||
|
'passwd': 'bruno',
|
||||||
|
'db': 'lodel2'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
## @brief gets the table name from class name
|
||||||
|
# @param class_name str
|
||||||
|
# @return str
|
||||||
|
def get_table_name_from_class(cls, class_name):
|
||||||
|
return "%s%s" % (cls._class_table_prefix, class_name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
## @brief gets the table name given a class, a type and a field names
|
||||||
|
# @param class_name str
|
||||||
|
# @param type_name str
|
||||||
|
# @param field_name str
|
||||||
|
# @return str
|
||||||
|
def get_r2t2table_name(cls, class_name, type_name, field_name):
|
||||||
|
return "%s_%s_%s" % (class_name, type_name, field_name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
## @brief gets the fk name between two tables
|
||||||
|
# @param src_table_name str
|
||||||
|
# @param dst_table_name str
|
||||||
|
# @return str
|
||||||
|
def _fk_name(cls, src_table_name, dst_table_name):
|
||||||
|
return "fk_%s_%s" % (src_table_name, dst_table_name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
## @brief Identifier escaping
|
||||||
|
# @param idname str : An SQL identifier
|
||||||
|
def escape_idname(cls, idname):
|
||||||
|
if '`' in idname:
|
||||||
|
raise ValueError("Invalid name : '%s'" % idname)
|
||||||
|
return '`%s`' % idname
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
## @brief Given a fieldtype, returns a MySQL type specifier
|
||||||
|
# @param emfieldType EmFieldType : A fieldtype
|
||||||
|
# @return str
|
||||||
|
def get_type_spec_from_fieldtype(cls, emfieldtype):
|
||||||
|
|
||||||
|
ftype = emfieldtype.ftype
|
||||||
|
|
||||||
|
if ftype == 'char' or ftype == 'str':
|
||||||
|
res = "VARCHAR(%d)" % emfieldtype.max_length
|
||||||
|
elif ftype == 'text':
|
||||||
|
res = 'TEXT'
|
||||||
|
elif ftype == 'datetime':
|
||||||
|
res = "DATETIME"
|
||||||
|
# client side workaround for only one column with CURRENT_TIMESTAMP : giving NULL to timestamp that don't allows NULL
|
||||||
|
# cf. https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html#idm139961275230400
|
||||||
|
# The solution for the migration handler is to create triggers :
|
||||||
|
# CREATE TRIGGER trigger_name BEFORE INSERT ON `my_super_table`
|
||||||
|
# FOR EACH ROW SET NEW.my_date_column = NOW();
|
||||||
|
# and
|
||||||
|
# CREATE TRIGGER trigger_name BEFORE UPDATE ON
|
||||||
|
elif ftype == 'bool':
|
||||||
|
res = "BOOL"
|
||||||
|
elif ftype == 'int':
|
||||||
|
res = "INT"
|
||||||
|
elif ftype == 'rel2type':
|
||||||
|
res = "INT"
|
||||||
|
else:
|
||||||
|
raise ValueError("Unsupported fieldtype ftype : %s" % ftype)
|
||||||
|
|
||||||
|
return res
|
||||||
0
DataSource/MySQL/__init__.py
Normal file
0
DataSource/MySQL/__init__.py
Normal file
0
DataSource/__init__.py
Normal file
0
DataSource/__init__.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue