db.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. """
  2. Database related functions to be used in Python scripts.
  3. Usage:
  4. ::
  5. from grass.script import db as grass
  6. grass.db_describe(table)
  7. ...
  8. (C) 2008-2015 by the GRASS Development Team
  9. This program is free software under the GNU General Public
  10. License (>=v2). Read the file COPYING that comes with GRASS
  11. for details.
  12. .. sectionauthor:: Glynn Clements
  13. .. sectionauthor:: Martin Landa <landa.martin gmail.com>
  14. """
  15. from core import *
  16. from utils import try_remove
  17. from grass.exceptions import CalledModuleError
  18. def db_describe(table, **args):
  19. """Return the list of columns for a database table
  20. (interface to `db.describe -c`). Example:
  21. >>> run_command('g.copy', vector='firestations,myfirestations')
  22. 0
  23. >>> db_describe('myfirestations') # doctest: +ELLIPSIS
  24. {'nrows': 71, 'cols': [['cat', 'INTEGER', '20'], ... 'ncols': 22}
  25. >>> run_command('g.remove', flags='f', type='vector', name='myfirestations')
  26. 0
  27. :param str table: table name
  28. :param list args:
  29. :return: parsed module output
  30. """
  31. s = read_command('db.describe', flags='c', table=table, **args)
  32. if not s:
  33. fatal(_("Unable to describe table <%s>") % table)
  34. cols = []
  35. result = {}
  36. for l in s.splitlines():
  37. f = l.split(':')
  38. key = f[0]
  39. f[1] = f[1].lstrip(' ')
  40. if key.startswith('Column '):
  41. n = int(key.split(' ')[1])
  42. cols.insert(n, f[1:])
  43. elif key in ['ncols', 'nrows']:
  44. result[key] = int(f[1])
  45. else:
  46. result[key] = f[1:]
  47. result['cols'] = cols
  48. return result
  49. def db_table_exist(table, **args):
  50. """Check if table exists.
  51. If no driver or database are given, then default settings is used
  52. (check db_connection()).
  53. >>> run_command('g.copy', vector='firestations,myfirestations')
  54. 0
  55. >>> db_table_exist('myfirestations')
  56. True
  57. >>> run_command('g.remove', flags='f', type='vector', name='myfirestations')
  58. 0
  59. :param str table: table name
  60. :param args:
  61. :return: True for success, False otherwise
  62. """
  63. nuldev = file(os.devnull, 'w+')
  64. ok = True
  65. try:
  66. run_command('db.describe', flags='c', table=table,
  67. stdout=nuldev, stderr=nuldev, **args)
  68. except CalledModuleError:
  69. ok = False
  70. finally:
  71. nuldev.close()
  72. return ok
  73. def db_connection(force=False):
  74. """Return the current database connection parameters
  75. (interface to `db.connect -g`). Example:
  76. >>> db_connection()
  77. {'group': '', 'schema': '', 'driver': 'sqlite', 'database': '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db'}
  78. :param force True to set up default DB connection if not defined
  79. :return: parsed output of db.connect
  80. """
  81. try:
  82. nuldev = file(os.devnull, 'w')
  83. conn = parse_command('db.connect', flags='g', stderr=nuldev)
  84. nuldev.close()
  85. except CalledModuleError:
  86. conn = None
  87. if not conn and force:
  88. run_command('db.connect', flags='c')
  89. conn = parse_command('db.connect', flags='g')
  90. return conn
  91. def db_select(sql=None, filename=None, table=None, **args):
  92. """Perform SQL select statement
  93. Note: one of <em>sql</em>, <em>filename</em>, or <em>table</em>
  94. arguments must be provided.
  95. Examples:
  96. >>> run_command('g.copy', vector='firestations,myfirestations')
  97. 0
  98. >>> db_select(sql = 'SELECT cat,CITY FROM myfirestations WHERE cat < 4')
  99. (('1', 'Morrisville'), ('2', 'Morrisville'), ('3', 'Apex'))
  100. Simplyfied usage (it performs <tt>SELECT * FROM myfirestations</tt>.)
  101. >>> db_select(table = 'myfirestations') # doctest: +ELLIPSIS
  102. (('1', '24', 'Morrisville #3', ... 'HS2A', '1.37'))
  103. >>> run_command('g.remove', flags='f', type='vector', name='myfirestations')
  104. 0
  105. :param str sql: SQL statement to perform (or None)
  106. :param str filename: name of file with SQL statements (or None)
  107. :param str table: name of table to query (or None)
  108. :param str args: see \gmod{db.select} arguments
  109. """
  110. fname = tempfile(create=False)
  111. if sql:
  112. args['sql'] = sql
  113. elif filename:
  114. args['input'] = filename
  115. elif table:
  116. args['table'] = table
  117. else:
  118. fatal(_("Programmer error: '%(sql)s', '%(filename)s', or '%(table)s' must be provided") %
  119. {'sql': 'sql', 'filename': 'filename', 'table': 'table'} )
  120. if 'sep' not in args:
  121. args['sep'] = '|'
  122. try:
  123. run_command('db.select', quiet=True, flags='c',
  124. output=fname, **args)
  125. except CalledModuleError:
  126. fatal(_("Fetching data failed"))
  127. ofile = open(fname)
  128. result = map(lambda x: tuple(x.rstrip(os.linesep).split(args['sep'])),
  129. ofile.readlines())
  130. ofile.close()
  131. try_remove(fname)
  132. return tuple(result)
  133. def db_table_in_vector(table):
  134. """Return the name of vector connected to the table.
  135. It returns None if no vectors are connected to the table.
  136. >>> run_command('g.copy', vector='firestations,myfirestations')
  137. 0
  138. >>> db_table_in_vector('myfirestations')
  139. ['myfirestations@user1']
  140. >>> db_table_in_vector('mfirestations')
  141. >>> run_command('g.remove', flags='f', type='vector', name='myfirestations')
  142. 0
  143. :param str table: name of table to query
  144. """
  145. from vector import vector_db
  146. nuldev = file(os.devnull, 'w')
  147. used = []
  148. vects = list_strings('vect')
  149. for vect in vects:
  150. for f in vector_db(vect, stderr=nuldev).itervalues():
  151. if not f:
  152. continue
  153. if f['table'] == table:
  154. used.append(vect)
  155. break
  156. if len(used) > 0:
  157. return used
  158. else:
  159. return None
  160. def db_begin_transaction(driver):
  161. """Begin transaction.
  162. :return: SQL command as string
  163. """
  164. if driver in ('sqlite', 'pg'):
  165. return 'BEGIN'
  166. if driver == 'mysql':
  167. return 'START TRANSACTION'
  168. return ''
  169. def db_commit_transaction(driver):
  170. """Commit transaction.
  171. :return: SQL command as string
  172. """
  173. if driver in ('sqlite', 'pg', 'mysql'):
  174. return 'COMMIT'
  175. return ''