Fix in sqlalchemy
This commit is contained in:
parent
194179d252
commit
22b281f2f9
5 changed files with 188 additions and 10 deletions
|
|
@ -209,9 +209,9 @@ def start():
|
||||||
if db=="":
|
if db=="":
|
||||||
db='paramecio_db'
|
db='paramecio_db'
|
||||||
|
|
||||||
WebModel.connections={'default': {'name': 'default', 'host': host_db, 'user': user_db, 'password': pass_db, 'db': '', 'charset': 'utf8mb4', 'set_connection': False} }
|
WebModel.connections={'default': {'name': 'default', 'host': host_db, 'user': user_db, 'password': pass_db, 'db': '', 'charset': 'utf8mb4', 'set_connection': False, 'db_type': 'pymysql'} }
|
||||||
|
|
||||||
connection_code="WebModel.connections={'default': {'name': 'default', 'host': '"+host_db+"', 'user': '"+user_db+"', 'password': '"+pass_db+"', 'db': '"+db+"', 'charset': 'utf8mb4', 'set_connection': False} }"
|
connection_code="WebModel.connections={'default': {'name': 'default', 'host': '"+host_db+"', 'user': '"+user_db+"', 'password': '"+pass_db+"', 'db': '"+db+"', 'charset': 'utf8mb4', 'set_connection': False, 'db_type': 'pymysql'} }"
|
||||||
|
|
||||||
with open(path_settings+'/config.py', 'a') as f:
|
with open(path_settings+'/config.py', 'a') as f:
|
||||||
f.write("\n\n"+connection_code)
|
f.write("\n\n"+connection_code)
|
||||||
|
|
|
||||||
|
|
@ -98,9 +98,14 @@ class FloatField(PhangoField):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
value=str(value)
|
||||||
|
|
||||||
|
if value.find(',')!=-1:
|
||||||
|
value=value.replace(',', '.')
|
||||||
|
|
||||||
value=str(float(value))
|
value=str(float(value))
|
||||||
|
|
||||||
if value=="0" and self.required==True:
|
if value==0 and self.required==True:
|
||||||
self.txt_error=self.error_default
|
self.txt_error=self.error_default
|
||||||
self.error=True
|
self.error=True
|
||||||
except:
|
except:
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ class SqlClass:
|
||||||
def __init__(self, connection):
|
def __init__(self, connection):
|
||||||
|
|
||||||
self.max_overflow=-1
|
self.max_overflow=-1
|
||||||
self.pool_size=15
|
self.pool_size=0
|
||||||
self.error_connection=""
|
self.error_connection=""
|
||||||
# Data of connection
|
# Data of connection
|
||||||
self.connection=connection
|
self.connection=connection
|
||||||
|
|
@ -39,7 +39,7 @@ class SqlClass:
|
||||||
|
|
||||||
if mypool==None:
|
if mypool==None:
|
||||||
|
|
||||||
mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size, recycle=self.pool_recycle, use_threadlocal=False)
|
mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size, recycle=self.pool_recycle, use_threadlocal=True)
|
||||||
|
|
||||||
self.conn=mypool.connect()
|
self.conn=mypool.connect()
|
||||||
|
|
||||||
|
|
|
||||||
173
paramecio/cromosoma/databases/sqlalchemy.py
Normal file
173
paramecio/cromosoma/databases/sqlalchemy.py
Normal file
|
|
@ -0,0 +1,173 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
#import pymysql.cursors
|
||||||
|
#pymysql.install_as_MySQLdb
|
||||||
|
#import sqlalchemy.pool as pool
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
#mypool=None
|
||||||
|
#engine = create_engine('sqlite:///:memory:', echo=True)
|
||||||
|
engine=None
|
||||||
|
|
||||||
|
class SqlClass:
|
||||||
|
|
||||||
|
cursors_connect=None
|
||||||
|
|
||||||
|
def __init__(self, connection):
|
||||||
|
|
||||||
|
self.max_overflow=-1
|
||||||
|
self.pool_size=0
|
||||||
|
self.error_connection=""
|
||||||
|
# Data of connection
|
||||||
|
self.connection=connection
|
||||||
|
# Sql connection
|
||||||
|
self.conn=None
|
||||||
|
self.connected=False
|
||||||
|
self.pool_recycle=3600
|
||||||
|
self.connect()
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
|
||||||
|
global engine
|
||||||
|
|
||||||
|
if not engine:
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
if self.connection['db_type']=='pymysql':
|
||||||
|
|
||||||
|
import pymysql.cursors
|
||||||
|
|
||||||
|
SqlClass.cursors_connect=pymysql.cursors.DictCursor
|
||||||
|
else:
|
||||||
|
import MySQLdb.cursors
|
||||||
|
SqlClass.cursors_connect=MySQLdb.cursors.DictCursor
|
||||||
|
|
||||||
|
engine=create_engine("mysql+%s://%s:%s@%s/%s?charset=utf8mb4" % (self.connection['db_type'], self.connection['user'], self.connection['password'], self.connection['host'], self.connection['db']))
|
||||||
|
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
v = sys.exc_info()[1]
|
||||||
|
|
||||||
|
self.error_connection="Error in connection: %s %s" % (e, v)
|
||||||
|
|
||||||
|
#self.conn.close()
|
||||||
|
|
||||||
|
raise NameError(self.error_connection)
|
||||||
|
|
||||||
|
self.conn=engine.raw_connection()
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
"""
|
||||||
|
if self.conn==None:
|
||||||
|
try:
|
||||||
|
def getconn():
|
||||||
|
return pymysql.connect(self.connection['host'],
|
||||||
|
user=self.connection['user'],
|
||||||
|
passwd=self.connection['password'],
|
||||||
|
db=self.connection['db'],
|
||||||
|
charset='utf8mb4',
|
||||||
|
cursorclass=pymysql.cursors.DictCursor)
|
||||||
|
|
||||||
|
if mypool==None:
|
||||||
|
|
||||||
|
mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size, recycle=self.pool_recycle, use_threadlocal=True)
|
||||||
|
|
||||||
|
self.conn=mypool.connect()
|
||||||
|
|
||||||
|
self.conn.ping(True)
|
||||||
|
|
||||||
|
self.connected=True
|
||||||
|
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
v = sys.exc_info()[1]
|
||||||
|
|
||||||
|
self.error_connection="Error in connection: %s %s" % (e, v)
|
||||||
|
|
||||||
|
self.conn.close()
|
||||||
|
|
||||||
|
raise NameError(self.error_connection)
|
||||||
|
"""
|
||||||
|
|
||||||
|
#Make def query more simple if not debugging.
|
||||||
|
|
||||||
|
def query(self, sql_query, arguments=[], name_connection="default"):
|
||||||
|
|
||||||
|
|
||||||
|
cursor=self.conn.cursor(SqlClass.cursors_connect)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
cursor.execute(sql_query, arguments)
|
||||||
|
self.conn.commit()
|
||||||
|
return cursor
|
||||||
|
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
v = sys.exc_info()[1]
|
||||||
|
|
||||||
|
if hasattr(cursor, '_last_executed'):
|
||||||
|
sql_query=cursor._last_executed
|
||||||
|
|
||||||
|
self.error_connection="Error in query ||%s||Values: %s" % (sql_query, str(arguments))
|
||||||
|
|
||||||
|
#return False
|
||||||
|
raise NameError(self.error_connection)
|
||||||
|
|
||||||
|
|
||||||
|
#self.connect()
|
||||||
|
|
||||||
|
#if fetch_type=="ASSOC":
|
||||||
|
#fetch_type=MySQLdb.cursors.DictCursor
|
||||||
|
|
||||||
|
#with self.conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
|
||||||
|
"""
|
||||||
|
cursor=self.conn.cursor(pymysql.cursors.DictCursor)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
cursor.execute(sql_query, arguments)
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
return cursor
|
||||||
|
|
||||||
|
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
v = sys.exc_info()[1]
|
||||||
|
|
||||||
|
if hasattr(cursor, '_last_executed'):
|
||||||
|
sql_query=cursor._last_executed
|
||||||
|
#, traceback.format_exc()
|
||||||
|
self.error_connection="Error in query ||%s||Values: %s" % (sql_query, str(arguments))
|
||||||
|
|
||||||
|
#return False
|
||||||
|
raise NameError(self.error_connection)
|
||||||
|
"""
|
||||||
|
|
||||||
|
#Fetcho row return dictionary if is defined in query.
|
||||||
|
|
||||||
|
#def fetch(self, cursor):
|
||||||
|
|
||||||
|
#return cursor.fetchone()
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
|
||||||
|
if self.conn:
|
||||||
|
|
||||||
|
self.conn.close()
|
||||||
|
|
||||||
|
def close(self, name_connection="default"):
|
||||||
|
|
||||||
|
if self.conn:
|
||||||
|
|
||||||
|
self.conn.close()
|
||||||
|
self.conn=None
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5,7 +5,7 @@ import re
|
||||||
import uuid
|
import uuid
|
||||||
from importlib import import_module, reload
|
from importlib import import_module, reload
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from paramecio.cromosoma.databases.pymysql import SqlClass
|
from paramecio.cromosoma.databases.sqlalchemy import SqlClass
|
||||||
from paramecio.cromosoma.coreforms import BaseForm, HiddenForm
|
from paramecio.cromosoma.coreforms import BaseForm, HiddenForm
|
||||||
import copy
|
import copy
|
||||||
import traceback
|
import traceback
|
||||||
|
|
@ -230,7 +230,7 @@ class WebModel:
|
||||||
|
|
||||||
model=OrderedDict()
|
model=OrderedDict()
|
||||||
|
|
||||||
connections={'default': {'host': 'localhost', 'user': 'user', 'password': '', 'db': 'default', 'charset': 'utf8', 'set_connection': False} }
|
connections={'default': {'host': 'localhost', 'user': 'user', 'password': '', 'db': 'default', 'charset': 'utf8', 'set_connection': False, 'db_type': 'pymysql'} }
|
||||||
|
|
||||||
connection_id="default"
|
connection_id="default"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue