您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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