Você não pode selecionar mais de 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.

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