You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db.py 14 KiB

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