table.py 30 KB

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