table.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Aug 8 15:29:21 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import numpy as np
  8. try:
  9. from collections import OrderedDict
  10. except:
  11. from grass.pygrass.orderdict import OrderedDict
  12. import grass.lib.vector as libvect
  13. from grass.pygrass.gis import Mapset
  14. from grass.pygrass.errors import DBError
  15. from grass.script.db import db_table_in_vector
  16. from grass.script.core import warning
  17. import sys
  18. import sql
  19. import os
  20. DRIVERS = ('sqlite', 'pg')
  21. def get_path(path):
  22. """Return the full path to the database; replacing environment variable
  23. with real values ::
  24. >>> path = '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db'
  25. >>> new_path = get_path(path)
  26. >>> from grass.script.core import gisenv
  27. >>> import os
  28. >>> new_path2 = os.path.join(gisenv()['GISDBASE'], gisenv()['LOCATION_NAME'],
  29. ... gisenv()['MAPSET'], 'sqlite', 'sqlite.db')
  30. >>> new_path == new_path2
  31. True
  32. """
  33. if "$" not in path:
  34. return path
  35. else:
  36. mapset = Mapset()
  37. path = path.replace('$GISDBASE', mapset.gisdbase)
  38. path = path.replace('$LOCATION_NAME', mapset.location)
  39. path = path.replace('$MAPSET', mapset.name)
  40. return path
  41. class Filters(object):
  42. """Help user to build a simple sql query. ::
  43. >>> filter = Filters('table')
  44. >>> filter.get_sql()
  45. 'SELECT * FROM table;'
  46. >>> filter.where("area<10000").get_sql()
  47. 'SELECT * FROM table WHERE area<10000;'
  48. >>> filter.select("cat", "area").get_sql()
  49. 'SELECT cat, area FROM table WHERE area<10000;'
  50. >>> filter.order_by("area").limit(10).get_sql()
  51. 'SELECT cat, area FROM table WHERE area<10000 ORDER BY area LIMIT 10;'
  52. ..
  53. """
  54. def __init__(self, tname):
  55. self.tname = tname
  56. self._select = None
  57. self._where = None
  58. self._orderby = None
  59. self._limit = None
  60. def __repr__(self):
  61. return "Filters(%r)" % self.get_sql()
  62. def select(self, *args):
  63. """Create the select query"""
  64. cols = ', '.join(args) if args else '*'
  65. select = sql.SELECT[:-1]
  66. self._select = select.format(cols=cols, tname=self.tname)
  67. return self
  68. def where(self, condition):
  69. """Create the where condition"""
  70. self._where = 'WHERE {condition}'.format(condition=condition)
  71. return self
  72. def order_by(self, orderby):
  73. """Create the order by condition"""
  74. if not isinstance(orderby, str):
  75. orderby = ', '.join(orderby)
  76. self._orderby = 'ORDER BY {orderby}'.format(orderby=orderby)
  77. return self
  78. def limit(self, number):
  79. """Create the limit condition"""
  80. if not isinstance(number, int):
  81. raise ValueError("Must be an integer.")
  82. else:
  83. self._limit = 'LIMIT {number}'.format(number=number)
  84. return self
  85. def get_sql(self):
  86. """Return the SQL query"""
  87. sql_list = list()
  88. if self._select is None:
  89. self.select()
  90. sql_list.append(self._select)
  91. if self._where is not None:
  92. sql_list.append(self._where)
  93. if self._orderby is not None:
  94. sql_list.append(self._orderby)
  95. if self._limit is not None:
  96. sql_list.append(self._limit)
  97. return "%s;" % ' '.join(sql_list)
  98. def reset(self):
  99. """Clean internal variables"""
  100. self._select = None
  101. self._where = None
  102. self._orderby = None
  103. self._limit = None
  104. class Columns(object):
  105. """Object to work with columns table.
  106. It is possible to instantiate a Columns object given the table name and
  107. the database connection.
  108. For a sqlite table: ::
  109. >>> import sqlite3
  110. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  111. >>> cols_sqlite = Columns('census',
  112. ... sqlite3.connect(get_path(path)))
  113. >>> cols_sqlite.tname
  114. 'census'
  115. For a postgreSQL table: ::
  116. >>> import psycopg2 as pg #doctest: +SKIP
  117. >>> cols_pg = Columns('boundary_municp_pg',
  118. ... pg.connect('host=localhost dbname=grassdb')) #doctest: +SKIP
  119. >>> cols_pg.tname #doctest: +SKIP
  120. 'boundary_municp_pg' #doctest: +SKIP
  121. ..
  122. """
  123. def __init__(self, tname, connection, key='cat'):
  124. self.tname = tname
  125. self.conn = connection
  126. self.key = key
  127. self.odict = None
  128. self.update_odict()
  129. def __contains__(self, item):
  130. return item in self.names()
  131. def __repr__(self):
  132. return "Columns(%r)" % self.items()
  133. def __getitem__(self, key):
  134. return self.odict[key]
  135. def __setitem__(self, name, new_type):
  136. self.cast(name, new_type)
  137. self.update_odict(self)
  138. def __iter__(self):
  139. return self.odict.__iter__()
  140. def __len__(self):
  141. return self.odict.__len__()
  142. def __eq__(self, obj):
  143. return obj.tname == self.tname and obj.odict == self.odict
  144. def is_pg(self):
  145. """Return True if is a psycopg connection. ::
  146. >>> import sqlite3
  147. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  148. >>> cols_sqlite = Columns('census',
  149. ... sqlite3.connect(get_path(path)))
  150. >>> cols_sqlite.is_pg()
  151. False
  152. >>> import psycopg2 as pg #doctest: +SKIP
  153. >>> cols_pg = Columns('boundary_municp_pg',
  154. ... pg.connect('host=localhost dbname=grassdb')) #doctest: +SKIP
  155. >>> cols_pg.is_pg() #doctest: +SKIP
  156. True
  157. ..
  158. """
  159. return hasattr(self.conn, 'xid')
  160. def update_odict(self):
  161. """Read columns name and types from table and update the odict
  162. attribute.
  163. """
  164. if self.is_pg():
  165. # is a postgres connection
  166. cur = self.conn.cursor()
  167. cur.execute("SELECT oid,typname FROM pg_type")
  168. diz = dict(cur.fetchall())
  169. odict = OrderedDict()
  170. import psycopg2 as pg
  171. try:
  172. cur.execute(sql.SELECT.format(cols='*', tname=self.tname))
  173. descr = cur.description
  174. for column in descr:
  175. name, ctype = column[:2]
  176. odict[name] = diz[ctype]
  177. except pg.ProgrammingError:
  178. pass
  179. self.odict = odict
  180. else:
  181. # is a sqlite connection
  182. cur = self.conn.cursor()
  183. cur.execute(sql.PRAGMA.format(tname=self.tname))
  184. descr = cur.fetchall()
  185. odict = OrderedDict()
  186. for column in descr:
  187. name, ctype = column[1:3]
  188. odict[name] = ctype
  189. self.odict = odict
  190. values = ','.join(['?', ] * self.__len__())
  191. kv = ','.join(['%s=?' % k for k in self.odict.keys()])
  192. where = "%s=?" % self.key
  193. self.insert_str = sql.INSERT.format(tname=self.tname, values=values)
  194. self.update_str = sql.UPDATE_WHERE.format(tname=self.tname,
  195. values=kv, condition=where)
  196. def sql_descr(self, remove=None):
  197. """Return a string with description of columns.
  198. Remove it is used to remove a columns.::
  199. >>> import sqlite3
  200. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  201. >>> cols_sqlite = Columns('census',
  202. ... sqlite3.connect(get_path(path)))
  203. >>> cols_sqlite.sql_descr() # doctest: +ELLIPSIS
  204. u'cat INTEGER, OBJECTID INTEGER, AREA DOUBLE PRECISION, ...'
  205. >>> import psycopg2 as pg # doctest: +SKIP
  206. >>> cols_pg = Columns('boundary_municp_pg',
  207. ... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
  208. >>> cols_pg.sql_descr() # doctest: +ELLIPSIS +SKIP
  209. 'cat int4, objectid int4, area float8, perimeter float8, ...'
  210. """
  211. if remove:
  212. return ', '.join(['%s %s' % (key, val) for key, val in self.items()
  213. if key != remove])
  214. else:
  215. return ', '.join(['%s %s' % (key, val)
  216. for key, val in self.items()])
  217. def types(self):
  218. """Return a list with the column types. ::
  219. >>> import sqlite3
  220. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  221. >>> cols_sqlite = Columns('census',
  222. ... sqlite3.connect(get_path(path)))
  223. >>> cols_sqlite.types() # doctest: +ELLIPSIS
  224. [u'INTEGER', u'INTEGER', ...]
  225. >>> import psycopg2 as pg # doctest: +SKIP
  226. >>> cols_pg = Columns('boundary_municp_pg',
  227. ... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
  228. >>> cols_pg.types() # doctest: +ELLIPSIS +SKIP
  229. ['int4', 'int4', 'float8', 'float8', 'float8', ...]
  230. ..
  231. """
  232. return self.odict.values()
  233. def names(self, remove=None, unicod=True):
  234. """Return a list with the column names.
  235. Remove it is used to remove a columns.::
  236. >>> import sqlite3
  237. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  238. >>> cols_sqlite = Columns('census',
  239. ... sqlite3.connect(get_path(path)))
  240. >>> cols_sqlite.names() # doctest: +ELLIPSIS
  241. [u'cat', u'OBJECTID', u'AREA', u'PERIMETER', ...]
  242. >>> import psycopg2 as pg # doctest: +SKIP
  243. >>> cols_pg = Columns('boundary_municp_pg', # doctest: +SKIP
  244. ... pg.connect('host=localhost dbname=grassdb'))
  245. >>> cols_pg.names() # doctest: +ELLIPSIS +SKIP
  246. ['cat', 'objectid', 'area', 'perimeter', ...]
  247. ..
  248. """
  249. if remove:
  250. nams = self.odict.keys()
  251. nams.remove(remove)
  252. else:
  253. nams = self.odict.keys()
  254. if unicod:
  255. return nams
  256. else:
  257. return [str(name) for name in nams]
  258. def items(self):
  259. """Return a list of tuple with column name and column type. ::
  260. >>> import sqlite3
  261. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  262. >>> cols_sqlite = Columns('census',
  263. ... sqlite3.connect(get_path(path)))
  264. >>> cols_sqlite.items() # doctest: +ELLIPSIS
  265. [(u'cat', u'INTEGER'), ...]
  266. >>> import psycopg2 as pg # doctest: +SKIP
  267. >>> cols_pg = Columns('boundary_municp_pg',
  268. ... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
  269. >>> cols_pg.items() # doctest: +ELLIPSIS +SKIP
  270. [('cat', 'int4'), ('objectid', 'int4'), ('area', 'float8'), ...]
  271. ..
  272. """
  273. return self.odict.items()
  274. def add(self, col_name, col_type):
  275. """Add a new column to the table. ::
  276. >>> import sqlite3
  277. >>> path = '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db'
  278. >>> from grass.pygrass.functions import copy, remove
  279. >>> copy('census','mycensus','vect')
  280. >>> cols_sqlite = Columns('mycensus',
  281. ... sqlite3.connect(get_path(path)))
  282. >>> cols_sqlite.add('n_pizza', 'INT')
  283. >>> 'n_pizza' in cols_sqlite
  284. True
  285. >>> import psycopg2 as pg # doctest: +SKIP
  286. >>> cols_pg = Columns('boundary_municp_pg',
  287. ... pg.connect('host=localhost dbname=grassdb')) #doctest: +SKIP
  288. >>> cols_pg.add('n_pizza', 'INT') # doctest: +SKIP
  289. >>> 'n_pizza' in cols_pg # doctest: +SKIP
  290. True
  291. >>> remove('mycensus', 'vect')
  292. ..
  293. """
  294. valid_type = ('DOUBLE PRECISION', 'INT', 'DATE')
  295. if 'VARCHAR' in col_type or col_type.upper() not in valid_type:
  296. str_err = "Type is not supported, supported types are: %s"
  297. raise TypeError(str_err % ", ".join(valid_type))
  298. cur = self.conn.cursor()
  299. cur.execute(sql.ADD_COL.format(tname=self.tname,
  300. cname=col_name,
  301. ctype=col_type))
  302. self.conn.commit()
  303. cur.close()
  304. self.update_odict()
  305. def rename(self, old_name, new_name):
  306. """Rename a column of the table. ::
  307. >>> import sqlite3
  308. >>> path = '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db'
  309. >>> from grass.pygrass.functions import copy, remove
  310. >>> copy('census','mycensus','vect')
  311. >>> cols_sqlite = Columns('mycensus',
  312. ... sqlite3.connect(get_path(path)))
  313. >>> cols_sqlite.add('n_pizza', 'INT')
  314. >>> 'n_pizza' in cols_sqlite
  315. True
  316. >>> cols_sqlite.rename('n_pizza', 'n_pizzas') # doctest: +ELLIPSIS
  317. >>> 'n_pizza' in cols_sqlite
  318. False
  319. >>> 'n_pizzas' in cols_sqlite
  320. True
  321. >>> import psycopg2 as pg # doctest: +SKIP
  322. >>> cols_pg = Columns('boundary_municp_pg',
  323. ... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
  324. >>> cols_pg.rename('n_pizza', 'n_pizzas') # doctest: +SKIP
  325. >>> 'n_pizza' in cols_pg # doctest: +SKIP
  326. False
  327. >>> 'n_pizzas' in cols_pg # doctest: +SKIP
  328. True
  329. >>> remove('mycensus', 'vect')
  330. ..
  331. """
  332. cur = self.conn.cursor()
  333. if self.is_pg():
  334. cur.execute(sql.RENAME_COL.format(tname=self.tname,
  335. old_name=old_name,
  336. new_name=new_name))
  337. self.conn.commit()
  338. cur.close()
  339. self.update_odict()
  340. else:
  341. cur.execute(sql.ADD_COL.format(tname=self.tname,
  342. cname=new_name,
  343. ctype=str(self.odict[old_name])))
  344. cur.execute(sql.UPDATE.format(tname=self.tname,
  345. new_col=new_name,
  346. old_col=old_name))
  347. self.conn.commit()
  348. cur.close()
  349. self.update_odict()
  350. self.drop(old_name)
  351. def cast(self, col_name, new_type):
  352. """Change the column type. ::
  353. >>> import sqlite3
  354. >>> path = '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db'
  355. >>> from grass.pygrass.functions import copy, remove
  356. >>> copy('census','mycensus','vect')
  357. >>> cols_sqlite = Columns('mycensus',
  358. ... sqlite3.connect(get_path(path)))
  359. >>> cols_sqlite.add('n_pizzas', 'INT')
  360. >>> cols_sqlite.cast('n_pizzas', 'float8') # doctest: +ELLIPSIS
  361. Traceback (most recent call last):
  362. ...
  363. DBError: 'SQLite does not support to cast columns.'
  364. >>> import psycopg2 as pg # doctest: +SKIP
  365. >>> cols_pg = Columns('boundary_municp_pg',
  366. ... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
  367. >>> cols_pg.cast('n_pizzas', 'float8') # doctest: +SKIP
  368. >>> cols_pg['n_pizzas'] # doctest: +SKIP
  369. 'float8'
  370. >>> remove('mycensus', 'vect')
  371. .. warning ::
  372. It is not possible to cast a column with sqlite
  373. ..
  374. """
  375. if self.is_pg():
  376. cur = self.conn.cursor()
  377. cur.execute(sql.CAST_COL.format(tname=self.tname, col=col_name,
  378. ctype=new_type))
  379. self.conn.commit()
  380. cur.close()
  381. self.update_odict()
  382. else:
  383. # sqlite does not support rename columns:
  384. raise DBError('SQLite does not support to cast columns.')
  385. def drop(self, col_name):
  386. """Drop a column from the table. ::
  387. >>> import sqlite3
  388. >>> path = '$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db'
  389. >>> from grass.pygrass.functions import copy, remove
  390. >>> copy('census','mycensus','vect')
  391. >>> cols_sqlite = Columns('mycensus',
  392. ... sqlite3.connect(get_path(path)))
  393. >>> cols_sqlite.drop('CHILD') # doctest: +ELLIPSIS
  394. >>> 'CHILD' in cols_sqlite
  395. False
  396. >>> import psycopg2 as pg # doctest: +SKIP
  397. >>> cols_pg = Columns('boundary_municp_pg',
  398. ... pg.connect('host=localhost dbname=grassdb')) # doctest: +SKIP
  399. >>> cols_pg.drop('CHILD') # doctest: +SKIP
  400. >>> 'CHILD' in cols_pg # doctest: +SKIP
  401. False
  402. >>> remove('mycensus','vect')
  403. ..
  404. """
  405. cur = self.conn.cursor()
  406. if self.is_pg():
  407. cur.execute(sql.DROP_COL.format(tname=self.tname,
  408. cname=col_name))
  409. else:
  410. desc = str(self.sql_descr(remove=col_name))
  411. names = ', '.join(self.names(remove=col_name, unicod=False))
  412. queries = sql.DROP_COL_SQLITE.format(tname=self.tname,
  413. keycol=self.key,
  414. coldef=desc,
  415. colnames=names).split('\n')
  416. for query in queries:
  417. cur.execute(query)
  418. self.conn.commit()
  419. cur.close()
  420. self.update_odict()
  421. class Link(object):
  422. """Define a Link between vector map and the attributes table.
  423. It is possible to define a Link object or given all the information
  424. (layer, name, table name, key, database, driver): ::
  425. >>> link = Link(1, 'link0', 'census', 'cat',
  426. ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db', 'sqlite')
  427. >>> link.number # doctest: +SKIP
  428. 1
  429. >>> link.name
  430. 'link0'
  431. >>> link.table_name
  432. 'census'
  433. >>> link.key
  434. 'cat'
  435. >>> link.database
  436. '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  437. >>> link.driver
  438. 'sqlite'
  439. >>> link
  440. Link(1, link0, sqlite)
  441. It is possible to change parameters with: ::
  442. >>> link.driver = 'pg' # doctest: +SKIP
  443. >>> link.driver # doctest: +SKIP
  444. 'pg'
  445. >>> link.driver = 'postgres' # doctest: +ELLIPSIS +SKIP
  446. Traceback (most recent call last):
  447. ...
  448. TypeError: Driver not supported, use: sqlite, pg.
  449. >>> link.driver # doctest: +SKIP
  450. 'pg'
  451. >>> link.number = 0 # doctest: +ELLIPSIS +SKIP
  452. Traceback (most recent call last):
  453. ...
  454. TypeError: Number must be positive and greater than 0.
  455. Or given a c_fieldinfo object that is a ctypes pointer to the field_info C
  456. struct. ::
  457. >>> link = Link(c_fieldinfo = ctypes.pointer(libvect.field_info()))
  458. ..
  459. """
  460. def _get_layer(self):
  461. return self.c_fieldinfo.contents.number
  462. def _set_layer(self, number):
  463. if number <= 0:
  464. raise TypeError("Number must be positive and greater than 0.")
  465. self.c_fieldinfo.contents.number = number
  466. layer = property(fget=_get_layer, fset=_set_layer)
  467. def _get_name(self):
  468. return self.c_fieldinfo.contents.name
  469. def _set_name(self, name):
  470. self.c_fieldinfo.contents.name = name
  471. name = property(fget=_get_name, fset=_set_name)
  472. def _get_table(self):
  473. return self.c_fieldinfo.contents.table
  474. def _set_table(self, new_name):
  475. self.c_fieldinfo.contents.table = new_name
  476. table_name = property(fget=_get_table, fset=_set_table)
  477. def _get_key(self):
  478. return self.c_fieldinfo.contents.key
  479. def _set_key(self, key):
  480. self.c_fieldinfo.contents.key = key
  481. key = property(fget=_get_key, fset=_set_key)
  482. def _get_database(self):
  483. return self.c_fieldinfo.contents.database
  484. def _set_database(self, database):
  485. self.c_fieldinfo.contents.database = database
  486. database = property(fget=_get_database, fset=_set_database)
  487. def _get_driver(self):
  488. return self.c_fieldinfo.contents.driver
  489. def _set_driver(self, driver):
  490. if driver not in ('sqlite', 'pg'):
  491. str_err = "Driver not supported, use: %s." % ", ".join(DRIVERS)
  492. raise TypeError(str_err)
  493. self.c_fieldinfo.contents.driver = driver
  494. driver = property(fget=_get_driver, fset=_set_driver)
  495. def __init__(self, layer=1, name=None, table=None, key=None,
  496. database=None, driver=None, c_fieldinfo=None):
  497. if c_fieldinfo is not None:
  498. self.c_fieldinfo = c_fieldinfo
  499. else:
  500. self.c_fieldinfo = ctypes.pointer(libvect.field_info())
  501. self.layer = layer
  502. self.name = name
  503. self.table_name = table
  504. self.key = key
  505. self.database = database
  506. self.driver = driver
  507. def __repr__(self):
  508. return "Link(%d, %s, %s)" % (self.layer, self.name, self.driver)
  509. def connection(self):
  510. """Return a connection object. ::
  511. >>> link = Link(1, 'link0', 'census', 'cat',
  512. ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db',
  513. ... 'sqlite')
  514. >>> conn = link.connection()
  515. >>> cur = conn.cursor()
  516. >>> cur.execute("SELECT cat,TOTAL_POP,PERIMETER FROM %s" %
  517. ... link.table_name) # doctest: +ELLIPSIS
  518. <sqlite3.Cursor object at ...>
  519. >>> cur.fetchone()
  520. (1, 44, 757.669)
  521. >>> cur.close()
  522. >>> conn.close()
  523. ...
  524. """
  525. if self.driver == 'sqlite':
  526. import sqlite3
  527. # Numpy is using some custom integer data types to efficiently
  528. # pack data into memory. Since these types aren't familiar to
  529. # sqlite, you'll have to tell it about how to handle them.
  530. for t in (np.int8, np.int16, np.int32, np.int64, np.uint8,
  531. np.uint16, np.uint32, np.uint64):
  532. sqlite3.register_adapter(t, long)
  533. dbpath = get_path(self.database)
  534. dbdirpath = os.path.split(dbpath)[0]
  535. if not os.path.exists(dbdirpath):
  536. os.mkdir(dbdirpath)
  537. return sqlite3.connect(dbpath)
  538. elif self.driver == 'pg':
  539. try:
  540. import psycopg2
  541. db = ' '.join(self.database.split(','))
  542. return psycopg2.connect(db)
  543. except ImportError:
  544. er = "You need to install psycopg2 to connect with this table."
  545. raise ImportError(er)
  546. else:
  547. str_err = "Driver is not supported yet, pleas use: sqlite or pg"
  548. raise TypeError(str_err)
  549. def table(self):
  550. """Return a Table object. ::
  551. >>> link = Link(1, 'link0', 'census', 'cat',
  552. ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db',
  553. ... 'sqlite')
  554. >>> table = link.table()
  555. >>> table.filters.select('cat', 'TOTAL_POP', 'PERIMETER')
  556. Filters('SELECT cat, TOTAL_POP, PERIMETER FROM census;')
  557. >>> cur = table.execute()
  558. >>> cur.fetchone()
  559. (1, 44, 757.669)
  560. >>> cur.close()
  561. ..
  562. """
  563. return Table(self.table_name, self.connection(), self.key)
  564. def info(self):
  565. """Print information of the link. ::
  566. >>> link = Link(1, 'link0', 'census', 'cat',
  567. ... '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db',
  568. ... 'sqlite')
  569. >>> link.info()
  570. layer: 1
  571. name: link0
  572. table: census
  573. key: cat
  574. database: $GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db
  575. driver: sqlite
  576. ..
  577. """
  578. print "layer: ", self.layer
  579. print "name: ", self.name
  580. print "table: ", self.table_name
  581. print "key: ", self.key
  582. print "database: ", self.database
  583. print "driver: ", self.driver
  584. class DBlinks(object):
  585. """Interface containing link to the table DB. ::
  586. >>> from grass.pygrass.vector import VectorTopo
  587. >>> cens = VectorTopo('census')
  588. >>> cens.open()
  589. >>> dblinks = DBlinks(cens.c_mapinfo)
  590. >>> dblinks
  591. DBlinks([Link(1, census, sqlite)])
  592. >>> dblinks[0]
  593. Link(1, census, sqlite)
  594. >>> dblinks['census']
  595. Link(1, census, sqlite)
  596. ..
  597. """
  598. def __init__(self, c_mapinfo):
  599. self.c_mapinfo = c_mapinfo
  600. def __len__(self):
  601. return self.num_dblinks()
  602. def __iter__(self):
  603. return (self.by_index(i)
  604. for i in xrange(self.num_dblinks()))
  605. def __getitem__(self, item):
  606. """
  607. """
  608. if isinstance(item, int):
  609. return self.by_index(item)
  610. else:
  611. return self.by_name(item)
  612. def __repr__(self):
  613. return "DBlinks(%r)" % [link for link in self.__iter__()]
  614. def by_index(self, indx):
  615. c_fieldinfo = libvect.Vect_get_dblink(self.c_mapinfo, indx)
  616. return Link(c_fieldinfo=c_fieldinfo)
  617. def by_layer(self, layer):
  618. c_fieldinfo = libvect.Vect_get_field(self.c_mapinfo, layer)
  619. return Link(c_fieldinfo=c_fieldinfo)
  620. def by_name(self, name):
  621. c_fieldinfo = libvect.Vect_get_field_by_name(self.c_mapinfo, name)
  622. return Link(c_fieldinfo=c_fieldinfo)
  623. def num_dblinks(self):
  624. return libvect.Vect_get_num_dblinks(self.c_mapinfo)
  625. def add(self, link):
  626. """Add a new link.
  627. Need to open vector map in write mode::
  628. >>> from grass.pygrass.vector import VectorTopo
  629. >>> municip = VectorTopo('census')
  630. >>> municip.open()
  631. >>> dblinks = DBlinks(municip.c_mapinfo)
  632. >>> dblinks
  633. DBlinks([Link(1, census, sqlite)])
  634. >>> link = Link(2, 'pg_link', 'boundary_municp_pg', 'cat',
  635. ... 'host=localhost dbname=grassdb', 'pg') # doctest: +SKIP
  636. >>> dblinks.add(link) # doctest: +SKIP
  637. >>> dblinks # doctest: +SKIP
  638. DBlinks([Link(1, boundary_municp, sqlite)])
  639. ..
  640. """
  641. #TODO: check if open in write mode or not.
  642. libvect.Vect_map_add_dblink(self.c_mapinfo,
  643. link.layer, link.name, link.table_name,
  644. link.key, link.database, link.driver)
  645. def remove(self, key, force=False):
  646. """Remove a link. If force set to true remove also the table ::
  647. >>> from grass.pygrass.vector import VectorTopo
  648. >>> municip = VectorTopo('census')
  649. >>> municip.open()
  650. >>> dblinks = DBlinks(municip.c_mapinfo)
  651. >>> dblinks
  652. DBlinks([Link(1, census, sqlite)])
  653. >>> dblinks.remove('pg_link') # doctest: +SKIP
  654. >>> dblinks # need to open vector map in write mode
  655. DBlinks([Link(1, census, sqlite)])
  656. ..
  657. """
  658. if force:
  659. link = self.by_name(key)
  660. table = link.table()
  661. table.drop()
  662. if isinstance(key, str):
  663. key = self.from_name_to_num(key)
  664. libvect.Vect_map_del_dblink(self.c_mapinfo, key)
  665. def from_name_to_num(self, name):
  666. """
  667. Vect_get_field_number
  668. """
  669. return libvect.Vect_get_field_number(self.c_mapinfo, name)
  670. class Table(object):
  671. """::
  672. >>> import sqlite3
  673. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  674. >>> tab_sqlite = Table(name='census',
  675. ... connection=sqlite3.connect(get_path(path)))
  676. >>> tab_sqlite.name
  677. 'census'
  678. >>> import psycopg2 # doctest: +SKIP
  679. >>> tab_pg = Table('boundary_municp_pg',
  680. ... psycopg2.connect('host=localhost dbname=grassdb',
  681. ... 'pg')) # doctest: +SKIP
  682. >>> tab_pg.columns # doctest: +ELLIPSIS +SKIP
  683. Columns([('cat', 'int4'), ...])
  684. ..
  685. """
  686. def _get_name(self):
  687. """Private method to return the name of table"""
  688. return self._name
  689. def _set_name(self, new_name):
  690. """Private method to set the name of table"""
  691. old_name = self._name
  692. cur = self.conn.cursor()
  693. cur.execute(sql.RENAME_TAB.format(old_name=old_name,
  694. new_name=new_name))
  695. cur.commit()
  696. cur.close()
  697. name = property(fget=_get_name, fset=_set_name)
  698. def __init__(self, name, connection, key='cat'):
  699. self._name = name
  700. self.conn = connection
  701. self.key = key
  702. self.columns = Columns(self.name,
  703. self.conn,
  704. self.key)
  705. self.filters = Filters(self.name)
  706. def __repr__(self):
  707. """::
  708. >>> import sqlite3
  709. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  710. >>> tab_sqlite = Table(name='census',
  711. ... connection=sqlite3.connect(get_path(path)))
  712. >>> tab_sqlite
  713. Table('census')
  714. ..
  715. """
  716. return "Table(%r)" % (self.name)
  717. def __iter__(self):
  718. cur = self.execute()
  719. return (cur.fetchone() for _ in xrange(self.__len__()))
  720. def __len__(self):
  721. """Return the number of rows"""
  722. return self.n_rows()
  723. def drop(self, name=None, force=False):
  724. """Private method to drop table from database"""
  725. if name:
  726. name = name
  727. else:
  728. name = self.name
  729. used = db_table_in_vector(name)
  730. if len(used) > 0:
  731. warning(_("Deleting table <%s> which is attached to following map(s):") % table)
  732. for vect in used:
  733. warning("%s" % vect)
  734. if not force:
  735. warning(_("You must use the force flag to actually remove it. Exiting."))
  736. sys.exit(0)
  737. else:
  738. cur = self.conn.cursor()
  739. cur.execute(sql.DROP_TAB.format(tname=name))
  740. else:
  741. cur = self.conn.cursor()
  742. cur.execute(sql.DROP_TAB.format(tname=name))
  743. def n_rows(self):
  744. """Return the number of rows
  745. >>> import sqlite3
  746. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  747. >>> tab_sqlite = Table(name='census',
  748. ... connection=sqlite3.connect(get_path(path)))
  749. >>> tab_sqlite.n_rows()
  750. 2537
  751. """
  752. cur = self.conn.cursor()
  753. cur.execute(sql.SELECT.format(cols='Count(*)', tname=self.name))
  754. number = cur.fetchone()[0]
  755. cur.close()
  756. return number
  757. def execute(self, sql_code=None):
  758. """Execute SQL code from a given string or build with filters and
  759. return a cursor object. ::
  760. >>> import sqlite3
  761. >>> path = '$GISDBASE/$LOCATION_NAME/PERMANENT/sqlite/sqlite.db'
  762. >>> tab_sqlite = Table(name='census',
  763. ... connection=sqlite3.connect(get_path(path)))
  764. >>> tab_sqlite.filters.select('cat', 'TOTAL_POP').order_by('AREA')
  765. Filters('SELECT cat, TOTAL_POP FROM census ORDER BY AREA;')
  766. >>> cur = tab_sqlite.execute()
  767. >>> cur.fetchone()
  768. (1856, 0)
  769. ..
  770. """
  771. try:
  772. sqlc = sql_code if sql_code else self.filters.get_sql()
  773. cur = self.conn.cursor()
  774. #if hasattr(self.cur, 'executescript'):
  775. # return cur.executescript(sqlc)
  776. return cur.execute(sqlc)
  777. except:
  778. raise ValueError("The SQL is not correct:\n%r" % sqlc)
  779. def insert(self, values, many=False):
  780. """Insert a new row"""
  781. cur = self.conn.cursor()
  782. if many:
  783. return cur.executemany(self.columns.insert_str, values)
  784. return cur.execute(self.columns.insert_str, values)
  785. def update(self, key, values):
  786. """Update a column for each row"""
  787. cur = self.conn.cursor()
  788. vals = list(values)
  789. vals.append(key)
  790. return cur.execute(self.columns.update_str, vals)
  791. def create(self, cols, name=None):
  792. """Create a new table"""
  793. cur = self.conn.cursor()
  794. coldef = ',\n'.join(['%s %s' % col for col in cols])
  795. if name:
  796. newname = name
  797. else:
  798. newname = self.name
  799. cur.execute(sql.CREATE_TAB.format(tname=newname, coldef=coldef))
  800. self.conn.commit()
  801. cur.close()
  802. self.columns.update_odict()