Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

376 рядки
11 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # Database Module
  23. # --------------------
  24. from __future__ import unicode_literals
  25. import MySQLdb
  26. import webnotes
  27. import conf
  28. class Database:
  29. """
  30. Open a database connection with the given parmeters, if use_default is True, use the
  31. login details from `conf.py`. This is called by the request handler and is accessible using
  32. the `conn` global variable. the `sql` method is also global to run queries
  33. """
  34. def __init__(self, host=None, user=None, password=None, ac_name=None, use_default = 0):
  35. self.host = host or 'localhost'
  36. self.user = user or conf.db_name
  37. if ac_name:
  38. self.user = self.get_db_login(ac_name) or conf.db_name
  39. if use_default:
  40. self.user = conf.db_name
  41. self.transaction_writes = 0
  42. self.auto_commit_on_many_writes = 0
  43. self.password = password or webnotes.get_db_password(self.user)
  44. self.connect()
  45. if self.user != 'root':
  46. self.use(self.user)
  47. def get_db_login(self, ac_name):
  48. return ac_name
  49. def connect(self):
  50. """
  51. Connect to a database
  52. """
  53. self._conn = MySQLdb.connect(user=self.user, host=self.host, passwd=self.password,
  54. use_unicode=True, charset='utf8')
  55. self._conn.converter[246]=float
  56. self._cursor = self._conn.cursor()
  57. def use(self, db_name):
  58. """
  59. `USE` db_name
  60. """
  61. self._conn.select_db(db_name)
  62. self.cur_db_name = db_name
  63. def validate_query(self, q):
  64. cmd = q.strip().lower().split()[0]
  65. if cmd in ['alter', 'drop', 'truncate'] and webnotes.user.name != 'Administrator':
  66. webnotes.msgprint('Not allowed to execute query')
  67. raise Exception
  68. def sql(self, query, values=(), as_dict = 0, as_list = 0, formatted = 0,
  69. debug=0, ignore_ddl=0, as_utf8=0, auto_commit=0, update=None):
  70. """
  71. * Execute a `query`, with given `values`
  72. * returns as a dictionary if as_dict = 1
  73. * returns as a list of lists (with cleaned up dates) if as_list = 1
  74. """
  75. # in transaction validations
  76. self.check_transaction_status(query)
  77. # autocommit
  78. if auto_commit: self.commit()
  79. # execute
  80. try:
  81. if values!=():
  82. if isinstance(values, dict):
  83. values = dict(values)
  84. if debug:
  85. try:
  86. webnotes.errprint(query % values)
  87. except TypeError:
  88. webnotes.errprint([query, values])
  89. self._cursor.execute(query, values)
  90. else:
  91. if debug: webnotes.errprint(query)
  92. self._cursor.execute(query)
  93. except Exception, e:
  94. # ignore data definition errors
  95. if ignore_ddl and e.args[0] in (1146,1054,1091):
  96. pass
  97. else:
  98. raise e
  99. if auto_commit: self.commit()
  100. # scrub output if required
  101. if as_dict:
  102. ret = self.fetch_as_dict(formatted, as_utf8)
  103. if update:
  104. for r in ret:
  105. r.update(update)
  106. return ret
  107. elif as_list:
  108. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  109. elif as_utf8:
  110. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  111. else:
  112. return self._cursor.fetchall()
  113. def sql_list(self, query, values=(), debug=False):
  114. return [r[0] for r in self.sql(query, values, debug=debug)]
  115. def sql_ddl(self, query, values=()):
  116. self.commit()
  117. self.sql(query)
  118. def check_transaction_status(self, query):
  119. if self.transaction_writes and query and query.strip().split()[0].lower() in ['start', 'alter', 'drop', 'create', "begin"]:
  120. raise Exception, 'This statement can cause implicit commit'
  121. if query and query.strip().lower()=='commit':
  122. self.transaction_writes = 0
  123. if query[:6].lower() in ['update', 'insert']:
  124. self.transaction_writes += 1
  125. if self.transaction_writes > 10000:
  126. if self.auto_commit_on_many_writes:
  127. webnotes.conn.commit()
  128. webnotes.conn.begin()
  129. else:
  130. webnotes.msgprint('A very long query was encountered. If you are trying to import data, please do so using smaller files')
  131. raise Exception, 'Bad Query!!! Too many writes'
  132. def fetch_as_dict(self, formatted=0, as_utf8=0):
  133. result = self._cursor.fetchall()
  134. ret = []
  135. for r in result:
  136. row_dict = webnotes._dict({})
  137. for i in range(len(r)):
  138. val = self.convert_to_simple_type(r[i], formatted)
  139. if as_utf8 and type(val) is unicode:
  140. val = val.encode('utf-8')
  141. row_dict[self._cursor.description[i][0]] = val
  142. ret.append(row_dict)
  143. return ret
  144. def get_description(self):
  145. return self._cursor.description
  146. def convert_to_simple_type(self, v, formatted=0):
  147. import datetime
  148. from webnotes.utils import formatdate, fmt_money
  149. # date
  150. if type(v)==datetime.date:
  151. v = unicode(v)
  152. if formatted:
  153. v = formatdate(v)
  154. # time
  155. elif type(v)==datetime.timedelta:
  156. v = unicode(v)
  157. # datetime
  158. elif type(v)==datetime.datetime:
  159. v = unicode(v)
  160. # long
  161. elif type(v)==long:
  162. v=int(v)
  163. # convert to strings... (if formatted)
  164. if formatted:
  165. if type(v)==float:
  166. v=fmt_money(v)
  167. if type(v)==int:
  168. v=str(v)
  169. return v
  170. def convert_to_lists(self, res, formatted=0, as_utf8=0):
  171. nres = []
  172. for r in res:
  173. nr = []
  174. for c in r:
  175. val = self.convert_to_simple_type(c, formatted)
  176. if as_utf8 and type(val) is unicode:
  177. val = val.encode('utf-8')
  178. nr.append(val)
  179. nres.append(nr)
  180. return nres
  181. def convert_to_utf8(self, res, formatted=0):
  182. nres = []
  183. for r in res:
  184. nr = []
  185. for c in r:
  186. if type(c) is unicode:
  187. c = c.encode('utf-8')
  188. nr.append(self.convert_to_simple_type(c, formatted))
  189. nres.append(nr)
  190. return nres
  191. def build_conditions(self, filters):
  192. def _build_condition(key):
  193. """
  194. filter's key is passed by map function
  195. build conditions like:
  196. * ifnull(`fieldname`, default_value) = %(fieldname)s
  197. * `fieldname` = %(fieldname)s
  198. """
  199. if "[" in key:
  200. split_key = key.split("[")
  201. return "ifnull(`" + split_key[0] + "`, " + split_key[1][:-1] + ") = %(" + key + ")s"
  202. else:
  203. return "`" + key + "` = %(" + key + ")s"
  204. if isinstance(filters, basestring):
  205. filters = { "name": filters }
  206. conditions = map(_build_condition, filters)
  207. return " and ".join(conditions), filters
  208. def get(self, doctype, filters=None, as_dict=True):
  209. return self.get_value(doctype, filters, "*", as_dict=as_dict)
  210. def get_value(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False):
  211. """Get a single / multiple value from a record.
  212. For Single DocType, let filters be = None"""
  213. if filters is not None and (filters!=doctype or filters=='DocType'):
  214. if fieldname!="*" and isinstance(fieldname, basestring):
  215. fieldname = "`" + fieldname + "`"
  216. fl = isinstance(fieldname, basestring) and fieldname or \
  217. ("`" + "`, `".join(fieldname) + "`")
  218. conditions, filters = self.build_conditions(filters)
  219. try:
  220. r = self.sql("select %s from `tab%s` where %s" % (fl, doctype,
  221. conditions), filters, as_dict)
  222. except Exception, e:
  223. if e.args[0]==1054 and ignore:
  224. return None
  225. else:
  226. raise e
  227. return r and (len(r[0]) > 1 and r[0] or r[0][0]) or None
  228. else:
  229. fieldname = isinstance(fieldname, basestring) and [fieldname] or fieldname
  230. r = self.sql("select field, value from tabSingles where field in (%s) and \
  231. doctype=%s" % (', '.join(['%s']*len(fieldname)), '%s'), tuple(fieldname) + (doctype,), as_dict=False)
  232. if as_dict:
  233. return r and webnotes._dict(r) or None
  234. else:
  235. return r and (len(r) > 1 and [i[0] for i in r] or r[0][1]) or None
  236. def set_value(self, dt, dn, field, val, modified=None, modified_by=None):
  237. from webnotes.utils import now
  238. if dn and dt!=dn:
  239. self.sql("""update `tab%s` set `%s`=%s, modified=%s, modified_by=%s
  240. where name=%s""" % (dt, field, "%s", "%s", "%s", "%s"),
  241. (val, modified or now(), modified_by or webnotes.session["user"], dn))
  242. else:
  243. if self.sql("select value from tabSingles where field=%s and doctype=%s", (field, dt)):
  244. self.sql("update tabSingles set value=%s where field=%s and doctype=%s", (val, field, dt))
  245. else:
  246. self.sql("insert into tabSingles(doctype, field, value) values (%s, %s, %s)", (dt, field, val))
  247. def set(self, doc, field, val):
  248. from webnotes.utils import now
  249. doc.modified = now()
  250. doc.modified_by = webnotes.session["user"]
  251. self.set_value(doc.doctype, doc.name, field, val, doc.modified, doc.modified_by)
  252. doc.fields[field] = val
  253. def set_global(self, key, val, user='__global'):
  254. self.set_default(key, val, user)
  255. def get_global(self, key, user='__global'):
  256. return self.get_default(key, user)
  257. def set_default(self, key, val, parent="Control Panel"):
  258. """set control panel default (tabDefaultVal)"""
  259. import webnotes.defaults
  260. webnotes.defaults.set_default(key, val, parent)
  261. def add_default(self, key, val, parent="Control Panel"):
  262. import webnotes.defaults
  263. webnotes.defaults.add_default(key, val, parent)
  264. def get_default(self, key, parent="Control Panel"):
  265. """get default value"""
  266. import webnotes.defaults
  267. d = webnotes.defaults.get_defaults(parent).get(key)
  268. return isinstance(d, list) and d[0] or d
  269. def get_defaults_as_list(self, key, parent="Control Panel"):
  270. import webnotes.defaults
  271. d = webnotes.defaults.get_default(key, parent)
  272. return isinstance(d, basestring) and [d] or d
  273. def get_defaults(self, key=None, parent="Control Panel"):
  274. """get all defaults"""
  275. import webnotes.defaults
  276. if key:
  277. return webnotes.defaults.get_defaults(parent).get(key)
  278. else:
  279. return webnotes.defaults.get_defaults(parent)
  280. def begin(self):
  281. return # not required
  282. def commit(self):
  283. self.sql("commit")
  284. def rollback(self):
  285. self.sql("ROLLBACK")
  286. def field_exists(self, dt, fn):
  287. return self.sql("select name from tabDocField where fieldname=%s and parent=%s", (dt, fn))
  288. def table_exists(self, tablename):
  289. return tablename in [d[0] for d in self.sql("show tables")]
  290. def exists(self, dt, dn=None):
  291. if isinstance(dt, basestring):
  292. try:
  293. return self.sql('select name from `tab%s` where name=%s' % (dt, '%s'), dn)
  294. except:
  295. return None
  296. elif isinstance(dt, dict) and dt.get('doctype'):
  297. try:
  298. conditions = []
  299. for d in dt:
  300. if d == 'doctype': continue
  301. conditions.append('`%s` = "%s"' % (d, dt[d].replace('"', '\"')))
  302. return self.sql('select name from `tab%s` where %s' % \
  303. (dt['doctype'], " and ".join(conditions)))
  304. except:
  305. return None
  306. def get_table_columns(self, doctype):
  307. return [r[0] for r in self.sql("DESC `tab%s`" % doctype)]
  308. def close(self):
  309. if self._conn:
  310. self._cursor.close()
  311. self._conn.close()
  312. self._cursor = None
  313. self._conn = None