Page MenuHomec4science

sqlobject.py
No OneTemporary

File Metadata

Created
Thu, Oct 17, 05:00

sqlobject.py

from __future__ import print_function
import copy
class SQLObject(object):
"""
"""
def __getitem__(self, index) :
return self.entries[index]
def __setitem__(self, index, value) :
self.entries[index] = value
def __init__(self,my_id=0):
self._is_input = True
self.foreign_keys = {}
self.notNull = {}
self.types = {}
self.entries = {}
self.id = my_id
def __copy__(self):
_cp.types = self.types.copy()
_cp.entries = self.entries.copy()
_cp.id = self.id
_cp.foreign_keys = self.foreign_keys.copy()
_cp.notNull = self.foreign_keys.copy()
return _cp
def __deepcopy__(self, memo):
_cp = type(self)()
_cp.types = copy.deepcopy(self.types.copy(),memo)
_cp.entries = copy.deepcopy(self.entries.copy(),memo)
_cp.id = self.id
_cp.foreign_keys = copy.deepcopy(self.foreign_keys,memo)
_cp.notNull = copy.deepcopy(self.foreign_keys,memo)
return _cp
def prepare(self, conn):
self._conn = conn
conn.setObjectItemTypes(self)
@property
def is_input(self):
return _is_input
@is_input.setter
def is_input(self, value):
self._is_input = value
@property
def id(self):
if not hasattr(self, "_id"):
raise Exception("You either forgot to save the id at insert for "
"class '{0}' or id this object doesn't know its id "
"yet because it has never been inserted into the"
"data base".format(self.__class__.__name__))
return self._id
@id.setter
def id(self, value):
self._id = value
def __conform__(self, foo):
if foo is ISQLQuote:
return self
@classmethod
def from_select(cls, fetched_row):
raise Exception("Your forgot to implement the select method for class "
"'{0}'!".format(self.__class__.__name__))
def _sql_members(self):
raise Exception("Till, you forgot to implement the property "
"__sql_members for the class '{0}'".format(
self.__class__.__name__))
def insert(self, schema):
params = list()
# print (self.types)
for key,value in self.entries.items():
# print (key)
# print (self.types[key])
# print (value)
params.append(self.types[key](value))
request = "INSERT INTO {0}.{1} ({2}) VALUES ({3}) RETURNING id".format(schema,self.table_name,','.join(self.entries.keys()),','.join(["%s" for item in params])), params
return request
def update(self, schema):
params = list()
for key,value in self.entries.items():
# print (key)
# print (self.types[key])
# print (value)
params.append(self.types[key](value))
request = "UPDATE {0}.{1} SET ({2}) = ({3}) WHERE id = {4}".format(schema,self.table_name,','.join(self.entries.keys()),','.join(["%s" for item in params]),self.id), params
return request
def getquoted(self):
objs = [sql_adapt(member) for member in self._sql_members()]
for obj in objs:
if hasattr(obj, 'prepare'):
obj.prepare(self._conn)
quoted_objs = [obj.getquoted() for obj in objs]
return '(' + ', '.join(quoted_objs) + ')'
def createTableRequest(self, schema):
query_string = "CREATE TABLE {0}.{1} ( id SERIAL PRIMARY KEY,".format(schema,self.table_name)
for key,value in self.types.items():
if (value == float):
type_string = "DOUBLE PRECISION"
elif (value == int):
type_string = "INTEGER"
elif (value == str):
type_string = "TEXT"
elif (value == bool):
type_string = "BOOLEAN"
elif (value == "TIMESTAMP"):
type_string = "TIMESTAMP"
else:
print (value)
raise BaseError("type '{0}' not handled".format(value))
query_string += "{0} {1} ".format(key, type_string)
if (self.notNull.has_key(key)):
query_string += " NOT NULL"
query_string += ","
for key, value in self.foreign_keys.items():
query_string += "FOREIGN KEY ({0}) REFERENCES {1}.{2},".format(key,schema,value)
return query_string[:-1] +");"

Event Timeline