hytos / AppDatabase.py @ d35a1a7e
이력 | 보기 | 이력해설 | 다운로드 (2.22 KB)
1 |
# coding: utf-8
|
---|---|
2 |
""" This is AppDatabase module """
|
3 |
|
4 |
import sqlite3 |
5 |
import pymssql |
6 |
|
7 |
class AppDatabase: |
8 |
""" This is AppDatabase class """
|
9 |
def __init__(self, type, host, user, password, db_path): |
10 |
self._DBType = type |
11 |
self._host = host
|
12 |
self._user = user
|
13 |
self._password = password
|
14 |
self._db_path = db_path
|
15 |
|
16 |
@property
|
17 |
def db_type(self): |
18 |
""" return database type """
|
19 |
return self._DBType |
20 |
|
21 |
@db_type.setter
|
22 |
def db_type(self, value): |
23 |
self._DBType = value
|
24 |
|
25 |
@property
|
26 |
def host(self): |
27 |
""" return host for mssql """
|
28 |
return self._host |
29 |
|
30 |
@host.setter
|
31 |
def host(self, value): |
32 |
self._host = value
|
33 |
|
34 |
@property
|
35 |
def user(self): |
36 |
""" return user for mssql """
|
37 |
return self._user |
38 |
|
39 |
@user.setter
|
40 |
def user(self, value): |
41 |
self._user = value
|
42 |
|
43 |
@property
|
44 |
def password(self): |
45 |
""" return password for mssql """
|
46 |
return self._password |
47 |
|
48 |
@password.setter
|
49 |
def password(self, value): |
50 |
self._password = value
|
51 |
|
52 |
@property
|
53 |
def db_name(self): |
54 |
""" return database name """
|
55 |
return self._db_path |
56 |
|
57 |
@property
|
58 |
def file_path(self): |
59 |
""" return sqlite database file path """
|
60 |
return self._db_path |
61 |
|
62 |
@file_path.setter
|
63 |
def file_path(self, value): |
64 |
self._db_path = value
|
65 |
|
66 |
@property
|
67 |
def place_holder(self): |
68 |
""" return database placeholder """
|
69 |
return '?' if self.db_type == 'SQLite' else '%s' if self.db_type == 'MSSQL' else None |
70 |
|
71 |
def connect(self): |
72 |
""" return database connection depends on database type """
|
73 |
conn = None
|
74 |
if self._DBType == 'SQLite': |
75 |
conn = sqlite3.connect(self.file_path, isolation_level=None) |
76 |
conn.row_factory = sqlite3.Row |
77 |
#cursor = conn.cursor()
|
78 |
elif self._DBType == 'MSSQL': |
79 |
conn = pymssql.connect(host=self._host, user=self._user, password=self._password, database=self.db_name, charset='utf8', autocommit=False) |
80 |
#cursor = conn.cursor(as_dic=True)
|
81 |
|
82 |
return conn
|
83 |
|
84 |
def to_sql(self, sql): |
85 |
""" convert given sql string for database """
|
86 |
|
87 |
return sql.replace('?', '%s') if self.db_type == "MSSQL" else sql |