Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

501 linhas
15 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. import datetime
  29. _toc = ["webnotes.db.Database"]
  30. class Database:
  31. """
  32. Open a database connection with the given parmeters, if use_default is True, use the
  33. login details from `conf.py`. This is called by the request handler and is accessible using
  34. the `conn` global variable. the `sql` method is also global to run queries
  35. """
  36. def __init__(self, host=None, user=None, password=None, ac_name=None, use_default = 0):
  37. self.host = host or 'localhost'
  38. self.user = user or conf.db_name
  39. if ac_name:
  40. self.user = self.get_db_login(ac_name) or conf.db_name
  41. if use_default:
  42. self.user = conf.db_name
  43. self.transaction_writes = 0
  44. self.auto_commit_on_many_writes = 0
  45. self.password = password or webnotes.get_db_password(self.user)
  46. self.connect()
  47. if self.user != 'root':
  48. self.use(self.user)
  49. def get_db_login(self, ac_name):
  50. return ac_name
  51. def connect(self):
  52. """
  53. Connect to a database
  54. """
  55. self._conn = MySQLdb.connect(user=self.user, host=self.host, passwd=self.password,
  56. use_unicode=True, charset='utf8')
  57. self._conn.converter[246]=float
  58. self._cursor = self._conn.cursor()
  59. def use(self, db_name):
  60. """
  61. `USE` db_name
  62. """
  63. self._conn.select_db(db_name)
  64. self.cur_db_name = db_name
  65. def validate_query(self, q):
  66. cmd = q.strip().lower().split()[0]
  67. if cmd in ['alter', 'drop', 'truncate'] and webnotes.user.name != 'Administrator':
  68. webnotes.msgprint('Not allowed to execute query')
  69. raise Exception
  70. def sql(self, query, values=(), as_dict = 0, as_list = 0, formatted = 0,
  71. debug=0, ignore_ddl=0, as_utf8=0, auto_commit=0, update=None):
  72. """
  73. * Execute a `query`, with given `values`
  74. * returns as a dictionary if as_dict = 1
  75. * returns as a list of lists (with cleaned up dates) if as_list = 1
  76. """
  77. # in transaction validations
  78. self.check_transaction_status(query)
  79. # autocommit
  80. if auto_commit: self.commit()
  81. # execute
  82. try:
  83. if values!=():
  84. if isinstance(values, dict):
  85. values = dict(values)
  86. if debug:
  87. try:
  88. self.explain_query(query, values)
  89. webnotes.errprint(query % values)
  90. except TypeError:
  91. webnotes.errprint([query, values])
  92. if getattr(conf, "logging", False)==2:
  93. webnotes.log("<<<< query")
  94. webnotes.log(query)
  95. webnotes.log("with values:")
  96. webnotes.log(values)
  97. webnotes.log(">>>>")
  98. self._cursor.execute(query, values)
  99. else:
  100. if debug:
  101. self.explain_query(query)
  102. webnotes.errprint(query)
  103. if getattr(conf, "logging", False)==2:
  104. webnotes.log("<<<< query")
  105. webnotes.log(query)
  106. webnotes.log(">>>>")
  107. self._cursor.execute(query)
  108. except Exception, e:
  109. # ignore data definition errors
  110. if ignore_ddl and e.args[0] in (1146,1054,1091):
  111. pass
  112. else:
  113. raise e
  114. if auto_commit: self.commit()
  115. # scrub output if required
  116. if as_dict:
  117. ret = self.fetch_as_dict(formatted, as_utf8)
  118. if update:
  119. for r in ret:
  120. r.update(update)
  121. return ret
  122. elif as_list:
  123. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  124. elif as_utf8:
  125. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  126. else:
  127. return self._cursor.fetchall()
  128. def explain_query(self, query, values=None):
  129. try:
  130. webnotes.errprint("--- query explain ---")
  131. if values is None:
  132. self._cursor.execute("explain " + query)
  133. else:
  134. self._cursor.execute("explain " + query, values)
  135. import json
  136. webnotes.errprint(json.dumps(self.fetch_as_dict(), indent=1))
  137. webnotes.errprint("--- query explain end ---")
  138. except:
  139. webnotes.errprint("error in query explain")
  140. def sql_list(self, query, values=(), debug=False):
  141. return [r[0] for r in self.sql(query, values, debug=debug)]
  142. def sql_ddl(self, query, values=()):
  143. self.commit()
  144. self.sql(query)
  145. def check_transaction_status(self, query):
  146. if self.transaction_writes and query and query.strip().split()[0].lower() in ['start', 'alter', 'drop', 'create', "begin"]:
  147. raise Exception, 'This statement can cause implicit commit'
  148. if query and query.strip().lower()=='commit':
  149. self.transaction_writes = 0
  150. if query[:6].lower() in ['update', 'insert']:
  151. self.transaction_writes += 1
  152. if self.transaction_writes > 10000:
  153. if self.auto_commit_on_many_writes:
  154. webnotes.conn.commit()
  155. webnotes.conn.begin()
  156. else:
  157. webnotes.msgprint('A very long query was encountered. If you are trying to import data, please do so using smaller files')
  158. raise Exception, 'Bad Query!!! Too many writes'
  159. def fetch_as_dict(self, formatted=0, as_utf8=0):
  160. result = self._cursor.fetchall()
  161. ret = []
  162. needs_formatting = self.needs_formatting(result, formatted)
  163. for r in result:
  164. row_dict = webnotes._dict({})
  165. for i in range(len(r)):
  166. if needs_formatting:
  167. val = self.convert_to_simple_type(r[i], formatted)
  168. else:
  169. val = r[i]
  170. if as_utf8 and type(val) is unicode:
  171. val = val.encode('utf-8')
  172. row_dict[self._cursor.description[i][0]] = val
  173. ret.append(row_dict)
  174. return ret
  175. def needs_formatting(self, result, formatted):
  176. if result and result[0]:
  177. for v in result[0]:
  178. if isinstance(v, (datetime.date, datetime.timedelta, datetime.datetime, long)):
  179. return True
  180. if formatted and isinstance(v, (int, float)):
  181. return True
  182. return False
  183. def get_description(self):
  184. return self._cursor.description
  185. def convert_to_simple_type(self, v, formatted=0):
  186. from webnotes.utils import formatdate, fmt_money
  187. if isinstance(v, (datetime.date, datetime.timedelta, datetime.datetime, long)):
  188. if isinstance(v, datetime.date):
  189. v = unicode(v)
  190. if formatted:
  191. v = formatdate(v)
  192. # time
  193. elif isinstance(v, (datetime.timedelta, datetime.datetime)):
  194. v = unicode(v)
  195. # long
  196. elif isinstance(v, long):
  197. v=int(v)
  198. # convert to strings... (if formatted)
  199. if formatted:
  200. if isinstance(v, float):
  201. v=fmt_money(v)
  202. elif isinstance(v, int):
  203. v = unicode(v)
  204. return v
  205. def convert_to_lists(self, res, formatted=0, as_utf8=0):
  206. nres = []
  207. needs_formatting = self.needs_formatting(res, formatted)
  208. for r in res:
  209. nr = []
  210. for c in r:
  211. if needs_formatting:
  212. val = self.convert_to_simple_type(c, formatted)
  213. else:
  214. val = c
  215. if as_utf8 and type(val) is unicode:
  216. val = val.encode('utf-8')
  217. nr.append(val)
  218. nres.append(nr)
  219. return nres
  220. def convert_to_utf8(self, res, formatted=0):
  221. nres = []
  222. for r in res:
  223. nr = []
  224. for c in r:
  225. if type(c) is unicode:
  226. c = c.encode('utf-8')
  227. nr.append(self.convert_to_simple_type(c, formatted))
  228. nres.append(nr)
  229. return nres
  230. def build_conditions(self, filters):
  231. def _build_condition(key):
  232. """
  233. filter's key is passed by map function
  234. build conditions like:
  235. * ifnull(`fieldname`, default_value) = %(fieldname)s
  236. * `fieldname` [=, !=, >, >=, <, <=] %(fieldname)s
  237. """
  238. _operator = "="
  239. value = filters.get(key)
  240. if isinstance(value, (list, tuple)):
  241. _operator = value[0]
  242. filters[key] = value[1]
  243. if _operator not in ["=", "!=", ">", ">=", "<", "<=", "like"]:
  244. _operator = "="
  245. if "[" in key:
  246. split_key = key.split("[")
  247. return "ifnull(`" + split_key[0] + "`, " + split_key[1][:-1] + ") " \
  248. + _operator + " %(" + key + ")s"
  249. else:
  250. return "`" + key + "` " + _operator + " %(" + key + ")s"
  251. if isinstance(filters, basestring):
  252. filters = { "name": filters }
  253. conditions = map(_build_condition, filters)
  254. return " and ".join(conditions), filters
  255. def get(self, doctype, filters=None, as_dict=True):
  256. return self.get_value(doctype, filters, "*", as_dict=as_dict)
  257. def get_value(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False, debug=False):
  258. """Get a single / multiple value from a record.
  259. For Single DocType, let filters be = None"""
  260. ret = self.get_values(doctype, filters, fieldname, ignore, as_dict, debug)
  261. return ret and ((len(ret[0]) > 1 or as_dict) and ret[0] or ret[0][0]) or None
  262. def get_single_value(self, doctype, fieldname):
  263. values = self.get_values_from_single(doctype, None, fieldname)
  264. return values[0] if values else None
  265. def get_values(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False, debug=False):
  266. if isinstance(filters, list):
  267. return self.get_value_for_many_names(doctype, filters, fieldname, debug=debug)
  268. fields = fieldname
  269. if fieldname!="*":
  270. if isinstance(fieldname, basestring):
  271. fields = [fieldname]
  272. else:
  273. fields = fieldname
  274. if (filters is not None) and (filters!=doctype or doctype=="DocType"):
  275. try:
  276. return self.get_values_from_table(fields, filters, doctype, as_dict, debug)
  277. except Exception, e:
  278. if ignore and e.args[0] in (1146, 1054):
  279. # table or column not found, return None
  280. return None
  281. elif (not ignore) and e.args[0]==1146:
  282. # table not found, look in singles
  283. pass
  284. else:
  285. raise e
  286. return self.get_values_from_single(fields, filters, doctype, as_dict, debug)
  287. def get_values_from_single(self, fields, filters, doctype, as_dict=False, debug=False):
  288. if fields=="*" or isinstance(filters, dict):
  289. r = self.sql("""select field, value from tabSingles where doctype=%s""", doctype)
  290. # check if single doc matches with filters
  291. values = webnotes._dict(r)
  292. if isinstance(filters, dict):
  293. for key, value in filters.items():
  294. if values.get(key) != value:
  295. return []
  296. if as_dict:
  297. return values and [values] or []
  298. if isinstance(fields, list):
  299. return [map(lambda d: values.get(d), fields)]
  300. else:
  301. r = self.sql("""select field, value
  302. from tabSingles where field in (%s) and doctype=%s""" \
  303. % (', '.join(['%s'] * len(fields)), '%s'),
  304. tuple(fields) + (doctype,), as_dict=False, debug=debug)
  305. if as_dict:
  306. return r and [webnotes._dict(r)] or []
  307. else:
  308. return r and [[i[1] for i in r]] or []
  309. def get_values_from_table(self, fields, filters, doctype, as_dict, debug):
  310. fl = []
  311. if isinstance(fields, (list, tuple)):
  312. for f in fields:
  313. if "(" in f: # function
  314. fl.append(f)
  315. else:
  316. fl.append("`" + f + "`")
  317. fl = ", ".join(fields)
  318. else:
  319. fl = fields
  320. conditions, filters = self.build_conditions(filters)
  321. r = self.sql("select %s from `tab%s` where %s" % (fl, doctype,
  322. conditions), filters, as_dict=as_dict, debug=debug)
  323. return r
  324. def get_value_for_many_names(self, doctype, names, field, debug=False):
  325. names = filter(None, names)
  326. if names:
  327. return dict(self.sql("select name, `%s` from `tab%s` where name in (%s)" \
  328. % (field, doctype, ", ".join(["%s"]*len(names))), names, debug=debug))
  329. else:
  330. return {}
  331. def set_value(self, dt, dn, field, val, modified=None, modified_by=None):
  332. from webnotes.utils import now
  333. if dn and dt!=dn:
  334. self.sql("""update `tab%s` set `%s`=%s, modified=%s, modified_by=%s
  335. where name=%s""" % (dt, field, "%s", "%s", "%s", "%s"),
  336. (val, modified or now(), modified_by or webnotes.session["user"], dn))
  337. else:
  338. if self.sql("select value from tabSingles where field=%s and doctype=%s", (field, dt)):
  339. self.sql("""update tabSingles set value=%s where field=%s and doctype=%s""",
  340. (val, field, dt))
  341. else:
  342. self.sql("""insert into tabSingles(doctype, field, value)
  343. values (%s, %s, %s)""", (dt, field, val, ))
  344. if field!="modified":
  345. self.set_value(dt, dn, "modified", modified or now())
  346. def set(self, doc, field, val):
  347. from webnotes.utils import now
  348. doc.modified = now()
  349. doc.modified_by = webnotes.session["user"]
  350. self.set_value(doc.doctype, doc.name, field, val, doc.modified, doc.modified_by)
  351. doc.fields[field] = val
  352. def set_global(self, key, val, user='__global'):
  353. self.set_default(key, val, user)
  354. def get_global(self, key, user='__global'):
  355. return self.get_default(key, user)
  356. def set_default(self, key, val, parent="Control Panel"):
  357. """set control panel default (tabDefaultVal)"""
  358. import webnotes.defaults
  359. webnotes.defaults.set_default(key, val, parent)
  360. def add_default(self, key, val, parent="Control Panel"):
  361. import webnotes.defaults
  362. webnotes.defaults.add_default(key, val, parent)
  363. def get_default(self, key, parent="Control Panel"):
  364. """get default value"""
  365. import webnotes.defaults
  366. d = webnotes.defaults.get_defaults(parent).get(key)
  367. return isinstance(d, list) and d[0] or d
  368. def get_defaults_as_list(self, key, parent="Control Panel"):
  369. import webnotes.defaults
  370. d = webnotes.defaults.get_default(key, parent)
  371. return isinstance(d, basestring) and [d] or d
  372. def get_defaults(self, key=None, parent="Control Panel"):
  373. """get all defaults"""
  374. import webnotes.defaults
  375. if key:
  376. return webnotes.defaults.get_defaults(parent).get(key)
  377. else:
  378. return webnotes.defaults.get_defaults(parent)
  379. def begin(self):
  380. return # not required
  381. def commit(self):
  382. self.sql("commit")
  383. def rollback(self):
  384. self.sql("ROLLBACK")
  385. self.transaction_writes = 0
  386. def field_exists(self, dt, fn):
  387. return self.sql("select name from tabDocField where fieldname=%s and parent=%s", (dt, fn))
  388. def table_exists(self, tablename):
  389. return tablename in [d[0] for d in self.sql("show tables")]
  390. def exists(self, dt, dn=None):
  391. if isinstance(dt, basestring):
  392. if dt==dn:
  393. return True # single always exists (!)
  394. try:
  395. return self.sql('select name from `tab%s` where name=%s' % (dt, '%s'), dn)
  396. except:
  397. return None
  398. elif isinstance(dt, dict) and dt.get('doctype'):
  399. try:
  400. conditions = []
  401. for d in dt:
  402. if d == 'doctype': continue
  403. conditions.append('`%s` = "%s"' % (d, dt[d].replace('"', '\"')))
  404. return self.sql('select name from `tab%s` where %s' % \
  405. (dt['doctype'], " and ".join(conditions)))
  406. except:
  407. return None
  408. def get_table_columns(self, doctype):
  409. return [r[0] for r in self.sql("DESC `tab%s`" % doctype)]
  410. def close(self):
  411. if self._conn:
  412. self._cursor.close()
  413. self._conn.close()
  414. self._cursor = None
  415. self._conn = None