]>
git.datanom.net - pwp.git/blob - app/DB/db.py
3 sys
.path
.append('../..')
4 from models
import User
, Portfolio
, Album
, Photo
, AccessRight
5 from enum
import IntEnum
, unique
7 from baseorm
import Observer
10 class DBDriver(IntEnum
):
17 def __init__(self
, password
, driver
= DBDriver
.PG
, user
= 'pwp', database
= 'pwp', host
= 'localhost', port
= None):
18 self
.password
= password
20 self
.database
= database
25 if driver
== DBDriver
.PG
:
26 self
.__filename
__ = 'db_postgres.sql'
27 elif driver
== DBDriver
.MySQL
:
28 self
.__filename
__ = 'db_mysql.sql'
31 if self
.driver
is DBDriver
.PG
:
32 from postgres
import Postgres
34 self
.db
= Postgres(self
.database
, self
.user
, self
.password
, self
.host
, self
.port
)
35 except Exception as e
:
37 elif self
.driver
is DBDriver
.MySQL
:
38 from mysqld
import Mysql
40 self
.db
= Mysql(self
.database
, self
.user
, self
.password
, self
.host
, self
.port
)
41 except Exception as e
:
44 raise Exception("%s: Unknown DB driver" % self
.driver
)
46 def update(self
, subject
):
47 print("update: {0}".format(subject
))
53 file = open(self
.__filename
__, "r")
60 def store(self
, object):
61 if inspect
.isclass(object):
62 raise Exception("{0}: Class not instance".format(object))
64 if hasattr(object, '__tablename__'):
65 table
= object.__tablename
__
67 table
= object.__class
__.__name
__
69 print("Table<{0}>".format(table
))
70 v
= [i
for i
in dir(object) if isinstance(getattr(type(object), i
, None), property)]
72 print("{0}: {1}".format(p
, getattr(object, p
)))
80 value
= getattr(object, p
)
90 if hasattr(value
, 'id'):
91 values
.append(value
.id)
96 if action
== 'insert':
97 if table
.lower() == 'user' and self
.driver
is DBDriver
.PG
:
98 sql
= 'insert into "' + table
+ '" ('
100 sql
= 'insert into ' + table
+ ' ('
101 for i
in range(0, len(column
)):
103 if field
.lower() == 'user' and self
.driver
is DBDriver
.PG
:
104 field
= '"{0}"'.format(field
)
110 for i
in range(0, len(values
)):
116 object.id = self
.db
.insert(sql
, values
)
118 if table
.lower() == 'user' and self
.driver
is DBDriver
.PG
:
119 sql
= 'update "' + table
+ '" set '
121 sql
= 'update ' + table
+ ' set '
122 for i
in range(0, len(column
)):
124 sql
+= '{0} = %s'.format(column
[i
])
126 sql
+= ', {0} = %s'.format(column
[i
])
127 sql
+= ' where id = {0}'.format(id)
128 self
.db
.update(sql
, values
)
134 default
= '<%s.%s object at %s>' % (self
.__class
__.__module
__, self
.__class
__.__name
__, hex(id(self
)))
135 return "%s <Driver: %s, User: %s, database: %s>" % (default
, self
.driver
.name
, self
.user
, self
.database
)
138 #print("%s: deleted" % self.__class__.__name__)
141 except AttributeError:
145 def __init__(self
, x
):
161 db
= DB('test', DBDriver
.MySQL
)
165 user
= User('test', 'test@test.dk', 'test', 'test')
169 portfolio
= Portfolio('test', user
)
171 album
= Album('test', portfolio
)
173 accessright
= AccessRight(user
)
175 photo
= Photo('test', album
)
This page took 0.135035 seconds and 6 git commands to generate.