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.
 
 
 
 
 
 

432 line
13 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.is_testing = 0
  42. self.in_transaction = 0
  43. self.transaction_writes = 0
  44. self.testing_tables = []
  45. self.auto_commit_on_many_writes = 0
  46. self.password = password or webnotes.get_db_password(self.user)
  47. self.connect()
  48. if self.user != 'root':
  49. self.use(self.user)
  50. def get_db_login(self, ac_name):
  51. return ac_name
  52. def connect(self):
  53. """
  54. Connect to a database
  55. """
  56. self._conn = MySQLdb.connect(user=self.user, host=self.host, passwd=self.password, use_unicode=True)
  57. self._conn.converter[246]=float
  58. self._conn.set_character_set('utf8')
  59. self._cursor = self._conn.cursor()
  60. def use(self, db_name):
  61. """
  62. `USE` db_name
  63. """
  64. self._conn.select_db(db_name)
  65. self.cur_db_name = db_name
  66. def check_transaction_status(self, query):
  67. """
  68. Update *in_transaction* and check if "START TRANSACTION" is not called twice
  69. """
  70. if self.in_transaction and query and query.strip().split()[0].lower() in ['start', 'alter', 'drop', 'create']:
  71. raise Exception, 'This statement can cause implicit commit'
  72. if query and query.strip().lower()=='start transaction':
  73. self.in_transaction = 1
  74. self.transaction_writes = 0
  75. if query and query.strip().split()[0].lower() in ['commit', 'rollback']:
  76. self.in_transaction = 0
  77. if self.in_transaction and query[:6].lower() in ['update', 'insert']:
  78. self.transaction_writes += 1
  79. if self.transaction_writes > 10000:
  80. if self.auto_commit_on_many_writes:
  81. webnotes.conn.commit()
  82. webnotes.conn.begin()
  83. else:
  84. webnotes.msgprint('A very long query was encountered. If you are trying to import data, please do so using smaller files')
  85. raise Exception, 'Bad Query!!! Too many writes'
  86. def fetch_as_dict(self, formatted=0, as_utf8=0):
  87. """
  88. Internal - get results as dictionary
  89. """
  90. result = self._cursor.fetchall()
  91. ret = []
  92. for r in result:
  93. row_dict = webnotes.DictObj({})
  94. for i in range(len(r)):
  95. val = self.convert_to_simple_type(r[i], formatted)
  96. if as_utf8 and type(val) is unicode:
  97. val = val.encode('utf-8')
  98. row_dict[self._cursor.description[i][0]] = val
  99. ret.append(row_dict)
  100. return ret
  101. def validate_query(self, q):
  102. cmd = q.strip().lower().split()[0]
  103. if cmd in ['alter', 'drop', 'truncate'] and webnotes.user.name != 'Administrator':
  104. webnotes.msgprint('Not allowed to execute query')
  105. raise Execption
  106. # ======================================================================================
  107. def sql(self, query, values=(), as_dict = 0, as_list = 0, formatted = 0,
  108. debug=0, ignore_ddl=0, as_utf8=0, auto_commit=0):
  109. """
  110. * Execute a `query`, with given `values`
  111. * returns as a dictionary if as_dict = 1
  112. * returns as a list of lists (with cleaned up dates) if as_list = 1
  113. """
  114. # in transaction validations
  115. self.check_transaction_status(query)
  116. # autocommit
  117. if auto_commit and self.in_transaction: self.commit()
  118. if auto_commit: self.begin()
  119. # execute
  120. try:
  121. if values!=():
  122. if debug: webnotes.errprint(query % values)
  123. self._cursor.execute(query, values)
  124. else:
  125. if debug: webnotes.errprint(query)
  126. self._cursor.execute(query)
  127. except Exception, e:
  128. # ignore data definition errors
  129. if ignore_ddl and e.args[0] in (1146,1054,1091):
  130. pass
  131. else:
  132. raise e
  133. if auto_commit: self.commit()
  134. # scrub output if required
  135. if as_dict:
  136. return self.fetch_as_dict(formatted, as_utf8)
  137. elif as_list:
  138. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  139. elif as_utf8:
  140. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  141. else:
  142. return self._cursor.fetchall()
  143. def get_description(self):
  144. """
  145. Get metadata of the last query
  146. """
  147. return self._cursor.description
  148. # ======================================================================================
  149. def convert_to_simple_type(self, v, formatted=0):
  150. import datetime
  151. from webnotes.utils import formatdate, fmt_money
  152. # date
  153. if type(v)==datetime.date:
  154. v = str(v)
  155. if formatted:
  156. v = formatdate(v)
  157. # time
  158. elif type(v)==datetime.timedelta:
  159. h = int(v.seconds/60/60)
  160. v = str(h) + ':' + str(v.seconds/60 - h*60)
  161. if v[1]==':':
  162. v='0'+v
  163. # datetime
  164. elif type(v)==datetime.datetime:
  165. v = str(v)
  166. # long
  167. elif type(v)==long:
  168. v=int(v)
  169. # convert to strings... (if formatted)
  170. if formatted:
  171. if type(v)==float:
  172. v=fmt_money(v)
  173. if type(v)==int:
  174. v=str(v)
  175. return v
  176. # ======================================================================================
  177. def convert_to_lists(self, res, formatted=0, as_utf8=0):
  178. """
  179. Convert the given result set to a list of lists (with cleaned up dates and decimals)
  180. """
  181. nres = []
  182. for r in res:
  183. nr = []
  184. for c in r:
  185. val = self.convert_to_simple_type(c, formatted)
  186. if as_utf8 and type(val) is unicode:
  187. val = val.encode('utf-8')
  188. nr.append(val)
  189. nres.append(nr)
  190. return nres
  191. # ======================================================================================
  192. def convert_to_utf8(self, res, formatted=0):
  193. """
  194. Convert the given result set to a list of lists and as utf8 (with cleaned up dates and decimals)
  195. """
  196. nres = []
  197. for r in res:
  198. nr = []
  199. for c in r:
  200. if type(c) is unicode:
  201. c = c.encode('utf-8')
  202. nr.append(self.convert_to_simple_type(c, formatted))
  203. nres.append(nr)
  204. return nres
  205. # ======================================================================================
  206. def replace_tab_by_test(self, query):
  207. """
  208. Relace all ``tab`` + doctype to ``test`` + doctype
  209. """
  210. if self.is_testing:
  211. tl = self.get_testing_tables()
  212. for t in tl:
  213. query = query.replace(t, 'test' + t[3:])
  214. return query
  215. def get_testing_tables(self):
  216. """
  217. Get list of all tables for which `tab` is to be replaced by `test` before a query is executed
  218. """
  219. if not self.testing_tables:
  220. testing_tables = ['tab'+r[0] for r in self.sql('SELECT name from tabDocType where docstatus<2 and (issingle=0 or issingle is null)', allow_testing = 0)]
  221. testing_tables+=['tabSeries','tabSingles'] # tabSessions is not included here
  222. return self.testing_tables
  223. # ======================================================================================
  224. # get a single value from a record
  225. def build_conditions(self, filters):
  226. def _build_condition(key):
  227. """
  228. filter's key is passed by map function
  229. build conditions like:
  230. * ifnull(`fieldname`, default_value) = %(fieldname)s
  231. * `fieldname` = %(fieldname)s
  232. """
  233. if "[" in key:
  234. split_key = key.split("[")
  235. return "ifnull(`" + split_key[0] + "`, " + split_key[1][:-1] + ") = %(" + key + ")s"
  236. else:
  237. return "`" + key + "` = %(" + key + ")s"
  238. if isinstance(filters, basestring):
  239. filters = { "name": filters }
  240. conditions = map(_build_condition, filters)
  241. return " and ".join(conditions), filters
  242. def get_value(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False):
  243. """Get a single / multiple value from a record.
  244. For Single DocType, let filters be = None"""
  245. if filters is not None and (filters!=doctype or filters=='DocType'):
  246. fl = isinstance(fieldname, basestring) and fieldname or "`, `".join(fieldname)
  247. conditions, filters = self.build_conditions(filters)
  248. try:
  249. r = self.sql("select `%s` from `tab%s` where %s" % (fl, doctype, conditions),
  250. filters, as_dict)
  251. except Exception, e:
  252. if e.args[0]==1054 and ignore:
  253. return None
  254. else:
  255. raise e
  256. return r and (len(r[0]) > 1 and r[0] or r[0][0]) or None
  257. else:
  258. fieldname = isinstance(fieldname, basestring) and [fieldname] or fieldname
  259. r = self.sql("select field, value from tabSingles where field in (%s) and \
  260. doctype=%s" % (', '.join(['%s']*len(fieldname)), '%s'), tuple(fieldname) + (doctype,), as_dict=False)
  261. if as_dict:
  262. return r and webnotes.DictObj(r) or None
  263. else:
  264. return r and (len(r) > 1 and [i[0] for i in r] or r[0][1]) or None
  265. def set_value(self, dt, dn, field, val, modified = None):
  266. from webnotes.utils import now
  267. if dn and dt!=dn:
  268. self.sql("update `tab"+dt+"` set `"+field+"`=%s, modified=%s where name=%s", (val, modified or now(), dn))
  269. else:
  270. if self.sql("select value from tabSingles where field=%s and doctype=%s", (field, dt)):
  271. self.sql("update tabSingles set value=%s where field=%s and doctype=%s", (val, field, dt))
  272. else:
  273. self.sql("insert into tabSingles(doctype, field, value) values (%s, %s, %s)", (dt, field, val))
  274. def set(self, doc, field, val):
  275. from webnotes.utils import now
  276. doc.modified = now()
  277. self.set_value(doc.doctype, doc.name, field, val, doc.modified)
  278. doc.fields[field] = val
  279. # ======================================================================================
  280. def set_global(self, key, val, user='__global'):
  281. res = self.sql('select defkey from `tabDefaultValue` where defkey=%s and parent=%s', (key, user))
  282. if res:
  283. self.sql('update `tabDefaultValue` set defvalue=%s where parent=%s and defkey=%s', (str(val), user, key))
  284. else:
  285. self.sql('insert into `tabDefaultValue` (name, defkey, defvalue, parent) values (%s,%s,%s,%s)', (user+'_'+key, key, str(val), user))
  286. def get_global(self, key, user='__global'):
  287. g = self.sql("select defvalue from tabDefaultValue where defkey=%s and parent=%s", (key, user))
  288. return g and g[0][0] or None
  289. # ======================================================================================
  290. def set_default(self, key, val):
  291. """set control panel default (tabDefaultVal)"""
  292. if self.sql("""select defkey from `tabDefaultValue` where
  293. defkey=%s and parent = "Control Panel" """, key):
  294. # update
  295. self.sql("""update `tabDefaultValue` set defvalue=%s
  296. where parent = "Control Panel" and defkey=%s""", (val, key))
  297. else:
  298. from webnotes.model.doc import Document
  299. d = Document('DefaultValue')
  300. d.parent = 'Control Panel'
  301. d.parenttype = 'Control Panel'
  302. d.parentfield = 'system_defaults'
  303. d.defkey = key
  304. d.defvalue = val
  305. d.save(1)
  306. def get_default(self, key):
  307. """get default value"""
  308. ret = self.sql("""select defvalue from tabDefaultValue where defkey=%s""", key)
  309. return ret and ret[0][0] or None
  310. def get_defaults(self, key=None):
  311. """get all defaults"""
  312. if key:
  313. return self.get_default(key)
  314. else:
  315. res = self.sql("""select defkey, defvalue from `tabDefaultValue`
  316. where parent = "Control Panel" """)
  317. d = {}
  318. for rec in res:
  319. d[rec[0]] = rec[1] or ''
  320. return d
  321. def begin(self):
  322. if not self.in_transaction:
  323. self.sql("start transaction")
  324. def commit(self):
  325. self.sql("commit")
  326. def rollback(self):
  327. self.sql("ROLLBACK")
  328. # ======================================================================================
  329. def field_exists(self, dt, fn):
  330. """
  331. Returns True if `fn` exists in `DocType` `dt`
  332. """
  333. return self.sql("select name from tabDocField where fieldname=%s and parent=%s", (dt, fn))
  334. def exists(self, dt, dn=None):
  335. """
  336. Returns true if the record exists
  337. """
  338. if isinstance(dt, basestring):
  339. try:
  340. return self.sql('select name from `tab%s` where name=%s' % (dt, '%s'), dn)
  341. except:
  342. return None
  343. elif isinstance(dt, dict) and dt.get('doctype'):
  344. try:
  345. conditions = []
  346. for d in dt:
  347. if d == 'doctype': continue
  348. conditions.append('`%s` = "%s"' % (d, dt[d].replace('"', '\"')))
  349. return self.sql('select name from `tab%s` where %s' % \
  350. (dt['doctype'], " and ".join(conditions)))
  351. except:
  352. return None
  353. # ======================================================================================
  354. def close(self):
  355. """
  356. Close my connection
  357. """
  358. if self._conn:
  359. self._cursor.close()
  360. self._conn.close()
  361. self._cursor = None
  362. self._conn = None