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.
 
 
 
 
 
 

1087 lines
32 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: MIT. See LICENSE
  3. # Database Module
  4. # --------------------
  5. import re
  6. import time
  7. from typing import Dict, List, Union
  8. import frappe
  9. import datetime
  10. import frappe.defaults
  11. import frappe.model.meta
  12. from frappe import _
  13. from time import time
  14. from frappe.utils import now, getdate, cast, get_datetime
  15. from frappe.model.utils.link_count import flush_local_link_count
  16. from frappe.query_builder.functions import Count
  17. from frappe.query_builder.functions import Min, Max, Avg, Sum
  18. from frappe.query_builder.utils import Column
  19. from .query import Query
  20. from pypika.terms import Criterion, PseudoColumn
  21. class Database(object):
  22. """
  23. Open a database connection with the given parmeters, if use_default is True, use the
  24. login details from `conf.py`. This is called by the request handler and is accessible using
  25. the `db` global variable. the `sql` method is also global to run queries
  26. """
  27. VARCHAR_LEN = 140
  28. MAX_COLUMN_LENGTH = 64
  29. OPTIONAL_COLUMNS = ["_user_tags", "_comments", "_assign", "_liked_by"]
  30. DEFAULT_SHORTCUTS = ['_Login', '__user', '_Full Name', 'Today', '__today', "now", "Now"]
  31. STANDARD_VARCHAR_COLUMNS = ('name', 'owner', 'modified_by', 'parent', 'parentfield', 'parenttype')
  32. DEFAULT_COLUMNS = ['name', 'creation', 'modified', 'modified_by', 'owner', 'docstatus', 'parent',
  33. 'parentfield', 'parenttype', 'idx']
  34. MAX_WRITES_PER_TRANSACTION = 200_000
  35. class InvalidColumnName(frappe.ValidationError): pass
  36. def __init__(self, host=None, user=None, password=None, ac_name=None, use_default=0, port=None):
  37. self.setup_type_map()
  38. self.host = host or frappe.conf.db_host or '127.0.0.1'
  39. self.port = port or frappe.conf.db_port or ''
  40. self.user = user or frappe.conf.db_name
  41. self.db_name = frappe.conf.db_name
  42. self._conn = None
  43. if ac_name:
  44. self.user = ac_name or frappe.conf.db_name
  45. if use_default:
  46. self.user = frappe.conf.db_name
  47. self.transaction_writes = 0
  48. self.auto_commit_on_many_writes = 0
  49. self.password = password or frappe.conf.db_password
  50. self.value_cache = {}
  51. self.query = Query()
  52. def setup_type_map(self):
  53. pass
  54. def connect(self):
  55. """Connects to a database as set in `site_config.json`."""
  56. self.cur_db_name = self.user
  57. self._conn = self.get_connection()
  58. self._cursor = self._conn.cursor()
  59. frappe.local.rollback_observers = []
  60. def use(self, db_name):
  61. """`USE` db_name."""
  62. self._conn.select_db(db_name)
  63. def get_connection(self):
  64. pass
  65. def get_database_size(self):
  66. pass
  67. def sql(self, query, values=(), as_dict = 0, as_list = 0, formatted = 0,
  68. debug=0, ignore_ddl=0, as_utf8=0, auto_commit=0, update=None,
  69. explain=False, run=True, pluck=False):
  70. """Execute a SQL query and fetch all rows.
  71. :param query: SQL query.
  72. :param values: List / dict of values to be escaped and substituted in the query.
  73. :param as_dict: Return as a dictionary.
  74. :param as_list: Always return as a list.
  75. :param formatted: Format values like date etc.
  76. :param debug: Print query and `EXPLAIN` in debug log.
  77. :param ignore_ddl: Catch exception if table, column missing.
  78. :param as_utf8: Encode values as UTF 8.
  79. :param auto_commit: Commit after executing the query.
  80. :param update: Update this dict to all rows (if returned `as_dict`).
  81. :param run: Returns query without executing it if False.
  82. Examples:
  83. # return customer names as dicts
  84. frappe.db.sql("select name from tabCustomer", as_dict=True)
  85. # return names beginning with a
  86. frappe.db.sql("select name from tabCustomer where name like %s", "a%")
  87. # values as dict
  88. frappe.db.sql("select name from tabCustomer where name like %(name)s and owner=%(owner)s",
  89. {"name": "a%", "owner":"test@example.com"})
  90. """
  91. query = str(query)
  92. if not run:
  93. return query
  94. if re.search(r'ifnull\(', query, flags=re.IGNORECASE):
  95. # replaces ifnull in query with coalesce
  96. query = re.sub(r'ifnull\(', 'coalesce(', query, flags=re.IGNORECASE)
  97. if not self._conn:
  98. self.connect()
  99. # in transaction validations
  100. self.check_transaction_status(query)
  101. self.clear_db_table_cache(query)
  102. # autocommit
  103. if auto_commit: self.commit()
  104. # execute
  105. try:
  106. if debug:
  107. time_start = time()
  108. self.log_query(query, values, debug, explain)
  109. if values!=():
  110. if isinstance(values, dict):
  111. values = dict(values)
  112. # MySQL-python==1.2.5 hack!
  113. if not isinstance(values, (dict, tuple, list)):
  114. values = (values,)
  115. self._cursor.execute(query, values)
  116. if frappe.flags.in_migrate:
  117. self.log_touched_tables(query, values)
  118. else:
  119. self._cursor.execute(query)
  120. if frappe.flags.in_migrate:
  121. self.log_touched_tables(query)
  122. if debug:
  123. time_end = time()
  124. frappe.errprint(("Execution time: {0} sec").format(round(time_end - time_start, 2)))
  125. except Exception as e:
  126. if frappe.conf.db_type == 'postgres':
  127. self.rollback()
  128. elif self.is_syntax_error(e):
  129. # only for mariadb
  130. frappe.errprint('Syntax error in query:')
  131. frappe.errprint(query)
  132. elif self.is_deadlocked(e):
  133. raise frappe.QueryDeadlockError(e)
  134. elif self.is_timedout(e):
  135. raise frappe.QueryTimeoutError(e)
  136. if ignore_ddl and (self.is_missing_column(e) or self.is_missing_table(e) or self.cant_drop_field_or_key(e)):
  137. pass
  138. else:
  139. raise
  140. if auto_commit: self.commit()
  141. if not self._cursor.description:
  142. return ()
  143. if pluck:
  144. return [r[0] for r in self._cursor.fetchall()]
  145. # scrub output if required
  146. if as_dict:
  147. ret = self.fetch_as_dict(formatted, as_utf8)
  148. if update:
  149. for r in ret:
  150. r.update(update)
  151. return ret
  152. elif as_list:
  153. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  154. elif as_utf8:
  155. return self.convert_to_lists(self._cursor.fetchall(), formatted, as_utf8)
  156. else:
  157. return self._cursor.fetchall()
  158. def log_query(self, query, values, debug, explain):
  159. # for debugging in tests
  160. if frappe.conf.get('allow_tests') and frappe.cache().get_value('flag_print_sql'):
  161. print(self.mogrify(query, values))
  162. # debug
  163. if debug:
  164. if explain and query.strip().lower().startswith('select'):
  165. self.explain_query(query, values)
  166. frappe.errprint(self.mogrify(query, values))
  167. # info
  168. if (frappe.conf.get("logging") or False)==2:
  169. frappe.log("<<<< query")
  170. frappe.log(self.mogrify(query, values))
  171. frappe.log(">>>>")
  172. def mogrify(self, query, values):
  173. '''build the query string with values'''
  174. if not values:
  175. return query
  176. else:
  177. try:
  178. return self._cursor.mogrify(query, values)
  179. except: # noqa: E722
  180. return (query, values)
  181. def explain_query(self, query, values=None):
  182. """Print `EXPLAIN` in error log."""
  183. try:
  184. frappe.errprint("--- query explain ---")
  185. if values is None:
  186. self._cursor.execute("explain " + query)
  187. else:
  188. self._cursor.execute("explain " + query, values)
  189. import json
  190. frappe.errprint(json.dumps(self.fetch_as_dict(), indent=1))
  191. frappe.errprint("--- query explain end ---")
  192. except Exception:
  193. frappe.errprint("error in query explain")
  194. def sql_list(self, query, values=(), debug=False, **kwargs):
  195. """Return data as list of single elements (first column).
  196. Example:
  197. # doctypes = ["DocType", "DocField", "User", ...]
  198. doctypes = frappe.db.sql_list("select name from DocType")
  199. """
  200. return [r[0] for r in self.sql(query, values, **kwargs, debug=debug)]
  201. def sql_ddl(self, query, values=(), debug=False):
  202. """Commit and execute a query. DDL (Data Definition Language) queries that alter schema
  203. autocommit in MariaDB."""
  204. self.commit()
  205. self.sql(query, debug=debug)
  206. def check_transaction_status(self, query):
  207. """Raises exception if more than 20,000 `INSERT`, `UPDATE` queries are
  208. executed in one transaction. This is to ensure that writes are always flushed otherwise this
  209. could cause the system to hang."""
  210. if self.transaction_writes and \
  211. query and query.strip().split()[0].lower() in ['start', 'alter', 'drop', 'create', "begin", "truncate"]:
  212. raise Exception('This statement can cause implicit commit')
  213. if query and query.strip().lower() in ('commit', 'rollback'):
  214. self.transaction_writes = 0
  215. if query[:6].lower() in ('update', 'insert', 'delete'):
  216. self.transaction_writes += 1
  217. if self.transaction_writes > self.MAX_WRITES_PER_TRANSACTION:
  218. if self.auto_commit_on_many_writes:
  219. self.commit()
  220. else:
  221. frappe.throw(_("Too many writes in one request. Please send smaller requests"), frappe.ValidationError)
  222. def fetch_as_dict(self, formatted=0, as_utf8=0):
  223. """Internal. Converts results to dict."""
  224. result = self._cursor.fetchall()
  225. ret = []
  226. if result:
  227. keys = [column[0] for column in self._cursor.description]
  228. for r in result:
  229. values = []
  230. for value in r:
  231. if as_utf8 and isinstance(value, str):
  232. value = value.encode('utf-8')
  233. values.append(value)
  234. ret.append(frappe._dict(zip(keys, values)))
  235. return ret
  236. @staticmethod
  237. def clear_db_table_cache(query):
  238. if query and query.strip().split()[0].lower() in {'drop', 'create'}:
  239. frappe.cache().delete_key('db_tables')
  240. @staticmethod
  241. def needs_formatting(result, formatted):
  242. """Returns true if the first row in the result has a Date, Datetime, Long Int."""
  243. if result and result[0]:
  244. for v in result[0]:
  245. if isinstance(v, (datetime.date, datetime.timedelta, datetime.datetime, int)):
  246. return True
  247. if formatted and isinstance(v, (int, float)):
  248. return True
  249. return False
  250. def get_description(self):
  251. """Returns result metadata."""
  252. return self._cursor.description
  253. @staticmethod
  254. def convert_to_lists(res, formatted=0, as_utf8=0):
  255. """Convert tuple output to lists (internal)."""
  256. nres = []
  257. for r in res:
  258. nr = []
  259. for val in r:
  260. if as_utf8 and isinstance(val, str):
  261. val = val.encode('utf-8')
  262. nr.append(val)
  263. nres.append(nr)
  264. return nres
  265. def get(self, doctype, filters=None, as_dict=True, cache=False):
  266. """Returns `get_value` with fieldname='*'"""
  267. return self.get_value(doctype, filters, "*", as_dict=as_dict, cache=cache)
  268. def get_value(
  269. self,
  270. doctype,
  271. filters=None,
  272. fieldname="name",
  273. ignore=None,
  274. as_dict=False,
  275. debug=False,
  276. order_by="KEEP_DEFAULT_ORDERING",
  277. cache=False,
  278. for_update=False,
  279. run=True,
  280. pluck=False,
  281. distinct=False,
  282. ):
  283. """Returns a document property or list of properties.
  284. :param doctype: DocType name.
  285. :param filters: Filters like `{"x":"y"}` or name of the document. `None` if Single DocType.
  286. :param fieldname: Column name.
  287. :param ignore: Don't raise exception if table, column is missing.
  288. :param as_dict: Return values as dict.
  289. :param debug: Print query in error log.
  290. :param order_by: Column to order by
  291. Example:
  292. # return first customer starting with a
  293. frappe.db.get_value("Customer", {"name": ("like a%")})
  294. # return last login of **User** `test@example.com`
  295. frappe.db.get_value("User", "test@example.com", "last_login")
  296. last_login, last_ip = frappe.db.get_value("User", "test@example.com",
  297. ["last_login", "last_ip"])
  298. # returns default date_format
  299. frappe.db.get_value("System Settings", None, "date_format")
  300. """
  301. ret = self.get_values(doctype, filters, fieldname, ignore, as_dict, debug,
  302. order_by, cache=cache, for_update=for_update, run=run, pluck=pluck, distinct=distinct)
  303. if not run:
  304. return ret
  305. return ((len(ret[0]) > 1 or as_dict) and ret[0] or ret[0][0]) if ret else None
  306. def get_values(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False,
  307. debug=False, order_by="KEEP_DEFAULT_ORDERING", update=None, cache=False, for_update=False,
  308. run=True, pluck=False, distinct=False):
  309. """Returns multiple document properties.
  310. :param doctype: DocType name.
  311. :param filters: Filters like `{"x":"y"}` or name of the document.
  312. :param fieldname: Column name.
  313. :param ignore: Don't raise exception if table, column is missing.
  314. :param as_dict: Return values as dict.
  315. :param debug: Print query in error log.
  316. :param order_by: Column to order by,
  317. :param distinct: Get Distinct results.
  318. Example:
  319. # return first customer starting with a
  320. customers = frappe.db.get_values("Customer", {"name": ("like a%")})
  321. # return last login of **User** `test@example.com`
  322. user = frappe.db.get_values("User", "test@example.com", "*")[0]
  323. """
  324. out = None
  325. if cache and isinstance(filters, str) and \
  326. (doctype, filters, fieldname) in self.value_cache:
  327. return self.value_cache[(doctype, filters, fieldname)]
  328. if distinct:
  329. order_by = None
  330. if isinstance(filters, list):
  331. out = self._get_value_for_many_names(
  332. doctype,
  333. filters,
  334. fieldname,
  335. order_by,
  336. debug=debug,
  337. run=run,
  338. pluck=pluck,
  339. distinct=distinct,
  340. )
  341. else:
  342. fields = fieldname
  343. if fieldname!="*":
  344. if isinstance(fieldname, str):
  345. fields = [fieldname]
  346. else:
  347. fields = fieldname
  348. if (filters is not None) and (filters!=doctype or doctype=="DocType"):
  349. try:
  350. if order_by:
  351. order_by = "modified" if order_by == "KEEP_DEFAULT_ORDERING" else order_by
  352. out = self._get_values_from_table(
  353. fields,
  354. filters,
  355. doctype,
  356. as_dict,
  357. debug,
  358. order_by,
  359. update,
  360. for_update=for_update,
  361. run=run,
  362. pluck=pluck,
  363. distinct=distinct
  364. )
  365. except Exception as e:
  366. if ignore and (frappe.db.is_missing_column(e) or frappe.db.is_table_missing(e)):
  367. # table or column not found, return None
  368. out = None
  369. elif (not ignore) and frappe.db.is_table_missing(e):
  370. # table not found, look in singles
  371. out = self.get_values_from_single(fields, filters, doctype, as_dict, debug, update, run=run, distinct=distinct)
  372. else:
  373. raise
  374. else:
  375. out = self.get_values_from_single(fields, filters, doctype, as_dict, debug, update, run=run, pluck=pluck, distinct=distinct)
  376. if cache and isinstance(filters, str):
  377. self.value_cache[(doctype, filters, fieldname)] = out
  378. return out
  379. def get_values_from_single(
  380. self,
  381. fields,
  382. filters,
  383. doctype,
  384. as_dict=False,
  385. debug=False,
  386. update=None,
  387. run=True,
  388. pluck=False,
  389. distinct=False,
  390. ):
  391. """Get values from `tabSingles` (Single DocTypes) (internal).
  392. :param fields: List of fields,
  393. :param filters: Filters (dict).
  394. :param doctype: DocType name.
  395. """
  396. # TODO
  397. # if not frappe.model.meta.is_single(doctype):
  398. # raise frappe.DoesNotExistError("DocType", doctype)
  399. if fields=="*" or isinstance(filters, dict):
  400. # check if single doc matches with filters
  401. values = self.get_singles_dict(doctype)
  402. if isinstance(filters, dict):
  403. for key, value in filters.items():
  404. if values.get(key) != value:
  405. return []
  406. if as_dict:
  407. return values and [values] or []
  408. if isinstance(fields, list):
  409. return [map(values.get, fields)]
  410. else:
  411. r = self.query.get_sql(
  412. "Singles",
  413. filters={"field": ("in", tuple(fields)), "doctype": doctype},
  414. fields=["field", "value"],
  415. distinct=distinct,
  416. ).run(pluck=pluck, debug=debug, as_dict=False)
  417. if not run:
  418. return r
  419. if as_dict:
  420. if r:
  421. r = frappe._dict(r)
  422. if update:
  423. r.update(update)
  424. return [r]
  425. else:
  426. return []
  427. else:
  428. return r and [[i[1] for i in r]] or []
  429. def get_singles_dict(self, doctype, debug = False):
  430. """Get Single DocType as dict.
  431. :param doctype: DocType of the single object whose value is requested
  432. Example:
  433. # Get coulmn and value of the single doctype Accounts Settings
  434. account_settings = frappe.db.get_singles_dict("Accounts Settings")
  435. """
  436. result = self.query.get_sql(
  437. "Singles", filters={"doctype": doctype}, fields=["field", "value"]
  438. ).run()
  439. dict_ = frappe._dict(result)
  440. return dict_
  441. @staticmethod
  442. def get_all(*args, **kwargs):
  443. return frappe.get_all(*args, **kwargs)
  444. @staticmethod
  445. def get_list(*args, **kwargs):
  446. return frappe.get_list(*args, **kwargs)
  447. def get_single_value(self, doctype, fieldname, cache=False):
  448. """Get property of Single DocType. Cache locally by default
  449. :param doctype: DocType of the single object whose value is requested
  450. :param fieldname: `fieldname` of the property whose value is requested
  451. Example:
  452. # Get the default value of the company from the Global Defaults doctype.
  453. company = frappe.db.get_single_value('Global Defaults', 'default_company')
  454. """
  455. if not doctype in self.value_cache:
  456. self.value_cache[doctype] = {}
  457. if fieldname in self.value_cache[doctype]:
  458. return self.value_cache[doctype][fieldname]
  459. val = self.query.get_sql(
  460. table="Singles",
  461. filters={"doctype": doctype, "field": fieldname},
  462. fields="value",
  463. ).run()
  464. val = val[0][0] if val else None
  465. df = frappe.get_meta(doctype).get_field(fieldname)
  466. if not df:
  467. frappe.throw(_('Invalid field name: {0}').format(frappe.bold(fieldname)), self.InvalidColumnName)
  468. val = cast(df.fieldtype, val)
  469. self.value_cache[doctype][fieldname] = val
  470. return val
  471. def get_singles_value(self, *args, **kwargs):
  472. """Alias for get_single_value"""
  473. return self.get_single_value(*args, **kwargs)
  474. def _get_values_from_table(
  475. self,
  476. fields,
  477. filters,
  478. doctype,
  479. as_dict,
  480. debug,
  481. order_by=None,
  482. update=None,
  483. for_update=False,
  484. run=True,
  485. pluck=False,
  486. distinct=False,
  487. ):
  488. field_objects = []
  489. if not isinstance(fields, Criterion):
  490. for field in fields:
  491. if "(" in str(field) or " as " in str(field):
  492. field_objects.append(PseudoColumn(field))
  493. else:
  494. field_objects.append(field)
  495. query = self.query.get_sql(
  496. table=doctype,
  497. filters=filters,
  498. orderby=order_by,
  499. for_update=for_update,
  500. field_objects=field_objects,
  501. fields=fields,
  502. distinct=distinct,
  503. )
  504. if (
  505. fields == "*"
  506. and not isinstance(fields, (list, tuple))
  507. and not isinstance(fields, Criterion)
  508. ):
  509. as_dict = True
  510. r = self.sql(
  511. query, as_dict=as_dict, debug=debug, update=update, run=run, pluck=pluck
  512. )
  513. return r
  514. def _get_value_for_many_names(self, doctype, names, field, order_by, debug=False, run=True, pluck=False, distinct=False):
  515. names = list(filter(None, names))
  516. if names:
  517. return self.get_all(
  518. doctype,
  519. fields=field,
  520. filters=names,
  521. order_by=order_by,
  522. pluck=pluck,
  523. debug=debug,
  524. as_list=1,
  525. run=run,
  526. distinct=distinct,
  527. )
  528. else:
  529. return {}
  530. def update(self, *args, **kwargs):
  531. """Update multiple values. Alias for `set_value`."""
  532. return self.set_value(*args, **kwargs)
  533. def set_value(self, dt, dn, field, val=None, modified=None, modified_by=None,
  534. update_modified=True, debug=False, for_update=True):
  535. """Set a single value in the database, do not call the ORM triggers
  536. but update the modified timestamp (unless specified not to).
  537. **Warning:** this function will not call Document events and should be avoided in normal cases.
  538. :param dt: DocType name.
  539. :param dn: Document name.
  540. :param field: Property / field name or dictionary of values to be updated
  541. :param value: Value to be updated.
  542. :param modified: Use this as the `modified` timestamp.
  543. :param modified_by: Set this user as `modified_by`.
  544. :param update_modified: default True. Set as false, if you don't want to update the timestamp.
  545. :param debug: Print the query in the developer / js console.
  546. :param for_update: Will add a row-level lock to the value that is being set so that it can be released on commit.
  547. """
  548. if not modified:
  549. modified = now()
  550. if not modified_by:
  551. modified_by = frappe.session.user
  552. to_update = {}
  553. if update_modified:
  554. to_update = {"modified": modified, "modified_by": modified_by}
  555. if isinstance(field, dict):
  556. to_update.update(field)
  557. else:
  558. to_update.update({field: val})
  559. if dn and dt!=dn:
  560. # with table
  561. set_values = []
  562. for key in to_update:
  563. set_values.append('`{0}`=%({0})s'.format(key))
  564. for name in self.get_values(dt, dn, 'name', for_update=for_update, debug=debug):
  565. values = dict(name=name[0])
  566. values.update(to_update)
  567. self.sql("""update `tab{0}`
  568. set {1} where name=%(name)s""".format(dt, ', '.join(set_values)),
  569. values, debug=debug)
  570. else:
  571. # for singles
  572. keys = list(to_update)
  573. self.sql('''
  574. delete from `tabSingles`
  575. where field in ({0}) and
  576. doctype=%s'''.format(', '.join(['%s']*len(keys))),
  577. list(keys) + [dt], debug=debug)
  578. for key, value in to_update.items():
  579. self.sql('''insert into `tabSingles` (doctype, field, value) values (%s, %s, %s)''',
  580. (dt, key, value), debug=debug)
  581. if dt in self.value_cache:
  582. del self.value_cache[dt]
  583. frappe.clear_document_cache(dt, dn)
  584. @staticmethod
  585. def set(doc, field, val):
  586. """Set value in document. **Avoid**"""
  587. doc.db_set(field, val)
  588. def touch(self, doctype, docname):
  589. """Update the modified timestamp of this document."""
  590. modified = now()
  591. self.sql("""update `tab{doctype}` set `modified`=%s
  592. where name=%s""".format(doctype=doctype), (modified, docname))
  593. return modified
  594. @staticmethod
  595. def set_temp(value):
  596. """Set a temperory value and return a key."""
  597. key = frappe.generate_hash()
  598. frappe.cache().hset("temp", key, value)
  599. return key
  600. @staticmethod
  601. def get_temp(key):
  602. """Return the temperory value and delete it."""
  603. return frappe.cache().hget("temp", key)
  604. def set_global(self, key, val, user='__global'):
  605. """Save a global key value. Global values will be automatically set if they match fieldname."""
  606. self.set_default(key, val, user)
  607. def get_global(self, key, user='__global'):
  608. """Returns a global key value."""
  609. return self.get_default(key, user)
  610. def get_default(self, key, parent="__default"):
  611. """Returns default value as a list if multiple or single"""
  612. d = self.get_defaults(key, parent)
  613. return isinstance(d, list) and d[0] or d
  614. @staticmethod
  615. def set_default(key, val, parent="__default", parenttype=None):
  616. """Sets a global / user default value."""
  617. frappe.defaults.set_default(key, val, parent, parenttype)
  618. @staticmethod
  619. def add_default(key, val, parent="__default", parenttype=None):
  620. """Append a default value for a key, there can be multiple default values for a particular key."""
  621. frappe.defaults.add_default(key, val, parent, parenttype)
  622. @staticmethod
  623. def get_defaults(key=None, parent="__default"):
  624. """Get all defaults"""
  625. if key:
  626. defaults = frappe.defaults.get_defaults(parent)
  627. d = defaults.get(key, None)
  628. if(not d and key != frappe.scrub(key)):
  629. d = defaults.get(frappe.scrub(key), None)
  630. return d
  631. else:
  632. return frappe.defaults.get_defaults(parent)
  633. def begin(self):
  634. self.sql("START TRANSACTION")
  635. def commit(self):
  636. """Commit current transaction. Calls SQL `COMMIT`."""
  637. for method in frappe.local.before_commit:
  638. frappe.call(method[0], *(method[1] or []), **(method[2] or {}))
  639. self.sql("commit")
  640. frappe.local.rollback_observers = []
  641. self.flush_realtime_log()
  642. enqueue_jobs_after_commit()
  643. flush_local_link_count()
  644. def add_before_commit(self, method, args=None, kwargs=None):
  645. frappe.local.before_commit.append([method, args, kwargs])
  646. @staticmethod
  647. def flush_realtime_log():
  648. for args in frappe.local.realtime_log:
  649. frappe.realtime.emit_via_redis(*args)
  650. frappe.local.realtime_log = []
  651. def rollback(self):
  652. """`ROLLBACK` current transaction."""
  653. self.sql("rollback")
  654. self.begin()
  655. for obj in frappe.local.rollback_observers:
  656. if hasattr(obj, "on_rollback"):
  657. obj.on_rollback()
  658. frappe.local.rollback_observers = []
  659. def field_exists(self, dt, fn):
  660. """Return true of field exists."""
  661. return self.exists('DocField', {
  662. 'fieldname': fn,
  663. 'parent': dt
  664. })
  665. def table_exists(self, doctype):
  666. """Returns True if table for given doctype exists."""
  667. return ("tab" + doctype) in self.get_tables()
  668. def has_table(self, doctype):
  669. return self.table_exists(doctype)
  670. def get_tables(self):
  671. tables = frappe.cache().get_value('db_tables')
  672. if not tables:
  673. table_rows = self.sql("""
  674. SELECT table_name
  675. FROM information_schema.tables
  676. WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
  677. """)
  678. tables = {d[0] for d in table_rows}
  679. frappe.cache().set_value('db_tables', tables)
  680. return tables
  681. def a_row_exists(self, doctype):
  682. """Returns True if atleast one row exists."""
  683. return self.sql("select name from `tab{doctype}` limit 1".format(doctype=doctype))
  684. def exists(self, dt, dn=None, cache=False):
  685. """Returns true if document exists.
  686. :param dt: DocType name.
  687. :param dn: Document name or filter dict."""
  688. if isinstance(dt, str):
  689. if dt!="DocType" and dt==dn:
  690. return True # single always exists (!)
  691. try:
  692. return self.get_value(dt, dn, "name", cache=cache)
  693. except Exception:
  694. return None
  695. elif isinstance(dt, dict) and dt.get('doctype'):
  696. try:
  697. conditions = []
  698. for d in dt:
  699. if d == 'doctype': continue
  700. conditions.append([d, '=', dt[d]])
  701. return self.get_all(dt['doctype'], filters=conditions, as_list=1)
  702. except Exception:
  703. return None
  704. def count(self, dt, filters=None, debug=False, cache=False):
  705. """Returns `COUNT(*)` for given DocType and filters."""
  706. if cache and not filters:
  707. cache_count = frappe.cache().get_value('doctype:count:{}'.format(dt))
  708. if cache_count is not None:
  709. return cache_count
  710. query = self.query.get_sql(table=dt, filters=filters, fields=Count("*"))
  711. if filters:
  712. count = self.sql(query, debug=debug)[0][0]
  713. return count
  714. else:
  715. count = self.sql(query, debug=debug)[0][0]
  716. if cache:
  717. frappe.cache().set_value('doctype:count:{}'.format(dt), count, expires_in_sec = 86400)
  718. return count
  719. @staticmethod
  720. def format_date(date):
  721. return getdate(date).strftime("%Y-%m-%d")
  722. @staticmethod
  723. def format_datetime(datetime):
  724. if not datetime:
  725. return '0001-01-01 00:00:00.000000'
  726. if isinstance(datetime, str):
  727. if ':' not in datetime:
  728. datetime = datetime + ' 00:00:00.000000'
  729. else:
  730. datetime = datetime.strftime("%Y-%m-%d %H:%M:%S.%f")
  731. return datetime
  732. def get_creation_count(self, doctype, minutes):
  733. """Get count of records created in the last x minutes"""
  734. from frappe.utils import now_datetime
  735. from dateutil.relativedelta import relativedelta
  736. return self.sql("""select count(name) from `tab{doctype}`
  737. where creation >= %s""".format(doctype=doctype),
  738. now_datetime() - relativedelta(minutes=minutes))[0][0]
  739. def get_db_table_columns(self, table):
  740. """Returns list of column names from given table."""
  741. columns = frappe.cache().hget('table_columns', table)
  742. if columns is None:
  743. columns = [r[0] for r in self.sql('''
  744. select column_name
  745. from information_schema.columns
  746. where table_name = %s ''', table)]
  747. if columns:
  748. frappe.cache().hset('table_columns', table, columns)
  749. return columns
  750. def get_table_columns(self, doctype):
  751. """Returns list of column names from given doctype."""
  752. columns = self.get_db_table_columns('tab' + doctype)
  753. if not columns:
  754. raise self.TableMissingError('DocType', doctype)
  755. return columns
  756. def has_column(self, doctype, column):
  757. """Returns True if column exists in database."""
  758. return column in self.get_table_columns(doctype)
  759. def get_column_type(self, doctype, column):
  760. return self.sql('''SELECT column_type FROM INFORMATION_SCHEMA.COLUMNS
  761. WHERE table_name = 'tab{0}' AND column_name = '{1}' '''.format(doctype, column))[0][0]
  762. def has_index(self, table_name, index_name):
  763. raise NotImplementedError
  764. def add_index(self, doctype, fields, index_name=None):
  765. raise NotImplementedError
  766. def add_unique(self, doctype, fields, constraint_name=None):
  767. raise NotImplementedError
  768. @staticmethod
  769. def get_index_name(fields):
  770. index_name = "_".join(fields) + "_index"
  771. # remove index length if present e.g. (10) from index name
  772. index_name = re.sub(r"\s*\([^)]+\)\s*", r"", index_name)
  773. return index_name
  774. def get_system_setting(self, key):
  775. def _load_system_settings():
  776. return self.get_singles_dict("System Settings")
  777. return frappe.cache().get_value("system_settings", _load_system_settings).get(key)
  778. def close(self):
  779. """Close database connection."""
  780. if self._conn:
  781. # self._cursor.close()
  782. self._conn.close()
  783. self._cursor = None
  784. self._conn = None
  785. @staticmethod
  786. def escape(s, percent=True):
  787. """Excape quotes and percent in given string."""
  788. # implemented in specific class
  789. raise NotImplementedError
  790. @staticmethod
  791. def is_column_missing(e):
  792. return frappe.db.is_missing_column(e)
  793. def get_descendants(self, doctype, name):
  794. '''Return descendants of the current record'''
  795. node_location_indexes = self.get_value(doctype, name, ('lft', 'rgt'))
  796. if node_location_indexes:
  797. lft, rgt = node_location_indexes
  798. return self.sql_list('''select name from `tab{doctype}`
  799. where lft > {lft} and rgt < {rgt}'''.format(doctype=doctype, lft=lft, rgt=rgt))
  800. else:
  801. # when document does not exist
  802. return []
  803. def is_missing_table_or_column(self, e):
  804. return self.is_missing_column(e) or self.is_missing_table(e)
  805. def multisql(self, sql_dict, values=(), **kwargs):
  806. current_dialect = frappe.db.db_type or 'mariadb'
  807. query = sql_dict.get(current_dialect)
  808. return self.sql(query, values, **kwargs)
  809. def delete(self, doctype: str, filters: Union[Dict, List] = None, debug=False, **kwargs):
  810. """Delete rows from a table in site which match the passed filters. This
  811. does trigger DocType hooks. Simply runs a DELETE query in the database.
  812. Doctype name can be passed directly, it will be pre-pended with `tab`.
  813. """
  814. values = ()
  815. filters = filters or kwargs.get("conditions")
  816. query = self.query.build_conditions(table=doctype, filters=filters).delete()
  817. if "debug" not in kwargs:
  818. kwargs["debug"] = debug
  819. return self.sql(query, values, **kwargs)
  820. def truncate(self, doctype: str):
  821. """Truncate a table in the database. This runs a DDL command `TRUNCATE TABLE`.
  822. This cannot be rolled back.
  823. Doctype name can be passed directly, it will be pre-pended with `tab`.
  824. """
  825. table = doctype if doctype.startswith("__") else f"tab{doctype}"
  826. return self.sql_ddl(f"truncate `{table}`")
  827. def clear_table(self, doctype):
  828. return self.truncate(doctype)
  829. def get_last_created(self, doctype):
  830. last_record = self.get_all(doctype, ('creation'), limit=1, order_by='creation desc')
  831. if last_record:
  832. return get_datetime(last_record[0].creation)
  833. else:
  834. return None
  835. def log_touched_tables(self, query, values=None):
  836. if values:
  837. query = frappe.safe_decode(self._cursor.mogrify(query, values))
  838. if query.strip().lower().split()[0] in ('insert', 'delete', 'update', 'alter', 'drop', 'rename'):
  839. # single_word_regex is designed to match following patterns
  840. # `tabXxx`, tabXxx and "tabXxx"
  841. # multi_word_regex is designed to match following patterns
  842. # `tabXxx Xxx` and "tabXxx Xxx"
  843. # ([`"]?) Captures " or ` at the begining of the table name (if provided)
  844. # \1 matches the first captured group (quote character) at the end of the table name
  845. # multi word table name must have surrounding quotes.
  846. # (tab([A-Z]\w+)( [A-Z]\w+)*) Captures table names that start with "tab"
  847. # and are continued with multiple words that start with a captital letter
  848. # e.g. 'tabXxx' or 'tabXxx Xxx' or 'tabXxx Xxx Xxx' and so on
  849. single_word_regex = r'([`"]?)(tab([A-Z]\w+))\1'
  850. multi_word_regex = r'([`"])(tab([A-Z]\w+)( [A-Z]\w+)+)\1'
  851. tables = []
  852. for regex in (single_word_regex, multi_word_regex):
  853. tables += [groups[1] for groups in re.findall(regex, query)]
  854. if frappe.flags.touched_tables is None:
  855. frappe.flags.touched_tables = set()
  856. frappe.flags.touched_tables.update(tables)
  857. def bulk_insert(self, doctype, fields, values, ignore_duplicates=False):
  858. """
  859. Insert multiple records at a time
  860. :param doctype: Doctype name
  861. :param fields: list of fields
  862. :params values: list of list of values
  863. """
  864. insert_list = []
  865. fields = ", ".join("`"+field+"`" for field in fields)
  866. for idx, value in enumerate(values):
  867. insert_list.append(tuple(value))
  868. if idx and (idx%10000 == 0 or idx < len(values)-1):
  869. self.sql("""INSERT {ignore_duplicates} INTO `tab{doctype}` ({fields}) VALUES {values}""".format(
  870. ignore_duplicates="IGNORE" if ignore_duplicates else "",
  871. doctype=doctype,
  872. fields=fields,
  873. values=", ".join(['%s'] * len(insert_list))
  874. ), tuple(insert_list))
  875. insert_list = []
  876. def enqueue_jobs_after_commit():
  877. from frappe.utils.background_jobs import execute_job, get_queue
  878. if frappe.flags.enqueue_after_commit and len(frappe.flags.enqueue_after_commit) > 0:
  879. for job in frappe.flags.enqueue_after_commit:
  880. q = get_queue(job.get("queue"), is_async=job.get("is_async"))
  881. q.enqueue_call(execute_job, timeout=job.get("timeout"),
  882. kwargs=job.get("queue_args"))
  883. frappe.flags.enqueue_after_commit = []