1
2 __version__ = '$Revision: 8175 $'.split()[1]
3 __date__ = '$Date: 2008-01-09 18:14:06 -0500 (Wed, 09 Jan 2008) $'.split()[1]
4 __author__ = 'Kurt Schwehr'
5
6 __doc__='''
7 AIS database utilities.
8
9 @author: '''+__author__+'''
10 @version: ''' + __version__ +'''
11 @copyright: 2008
12 @var __date__: Date of last svn commit
13 @status: under development
14 @since: 2008 Jan 09
15 @license: GPL v2
16 @undocumented: __version__ __author__ __doc__ parser
17
18 @todo: probably lots that can be added here
19 @todo: createtables for the binary messages
20 '''
21
22 import os
23 import sys
24 import ais
25
26 dbTypes=(
27 'postgres'
28 ,'sqlite'
29 )
30 '''
31 The choices of databases that are supported.
32 '''
33
35 '''
36 Standard command line options
37 @param parser: OptionParser parser that will get the additional options
38 @param dbType: 'postgres' or 'sqlite'
39 '''
40 if dbType != 'all' and dbType not in dbTypes:
41 print dbType != 'all'
42 print dbTypes not in dbTypes, dbTypes
43
44 sys.exit('unknown database type: '+dbType)
45
46 if dbType in ('all','postgres'):
47 if verbose: sys.stderr.write('Adding postgres options\n')
48 parser.add_option('-d','--database-name',dest='databaseName',default='ais'
49 ,help='Name of database within the postgres server [default: %default]')
50 parser.add_option('-D','--database-host',dest='databaseHost',default='localhost'
51 ,help='Host name of the computer serving the dbx [default: %default]')
52
53 defaultUser = os.getlogin()
54 parser.add_option('-u','--database-user',dest='databaseUser',default=defaultUser
55 ,help='Host name of the to access the database with [default: %default]')
56
57 parser.add_option('-p','--database-passwd',dest='databasePasswd',default=None
58 ,help='Password to access the database with [default: None]')
59
60 if dbType in ('all','sqlite'):
61 if verbose: sys.stderr.write('Adding sqlite options\n')
62 parser.add_option('-f','--database-file',dest='databaseFilename',default='ais.db3'
63 ,help='Name of the sqlite3 database file to write [default: %default]')
64
65
66 -def createTables(cx,dbType='sqlite',includeList=None, excludeList=None,verbose=False):
67 '''
68 @param cx: database connection
69 @type cx: db API 2.0 object
70 @param dbType: postgres or sqlite
71 @type dbType: str
72 @param includeList: If a list of message numbers is passed, only these are created
73 @type includeList: list of integers
74 @param excludeList: If a list of message numbers is passed, all but these are created
75 @type excludeList: list of integers
76 '''
77 cu = cx.cursor()
78
79 tables=[]
80 for msgNum in ais.msgModByNumber:
81 if excludeList is not None and msgNum in excludeList: continue
82 if includeList is not None and msgNum not in includeList: continue
83
84 aisMod = ais.msgModByNumber[msgNum]
85
86 if aisMod.dbTableName in tables:
87 if verbose: print msgNum,' ... skipping - already in the db -',aisMod.dbTableName
88 else:
89 if verbose: print msgNum,' ... adding '+aisMod.dbTableName+' table to db'
90 cu.execute(str(aisMod.sqlCreate(dbType=dbType)))
91 tables.append(aisMod.dbTableName)
92
93 cx.commit()
94
95 -def dropTables(cx,dbType='sqlite',includeList=None, excludeList=None,verbose=False):
96 '''
97 Kiss your data goodbye
98
99 @param cx: database connection
100 @type cx: db API 2.0 object
101 @param dbType: postgres or sqlite
102 @type dbType: str
103 @param includeList: If a list of message numbers is passed, only these are created
104 @type includeList: list of integers
105 @param excludeList: If a list of message numbers is passed, all but these are created
106 @type excludeList: list of integers
107 '''
108 cu = cx.cursor()
109
110 tables=[]
111 for msgNum in ais.msgModByNumber:
112 if excludeList is not None and msgNum in excludeList: continue
113 if includeList is not None and msgNum not in includeList: continue
114
115 aisMod = ais.msgModByNumber[msgNum]
116
117 if aisMod.dbTableName in tables:
118 if verbose: print msgNum,' ... skipping - already dropped from the db -',aisMod.dbTableName
119 else:
120 if verbose: print msgNum,' ... dropping '+aisMod.dbTableName+' table to db'
121 cu.execute('DROP TABLE '+aisMod.dbTableName+';')
122 tables.append(aisMod.dbTableName)
123
124 cx.commit()
125
126
127
128
130 '''
131 options must include the above standard options
132 '''
133 verbose = options.verbose
134 if dbType is None:
135 dbType = options.dbType
136
137 cx = None
138
139 if dbType=='sqlite':
140 import sqlite3
141 cx = sqlite3.connect(options.databaseFilename)
142
143 elif dbType=='postgres':
144 import psycopg2 as psycopg
145
146 connectStr = "dbname='"+options.databaseName+"' user='"+options.databaseUser+"' host='"+options.databaseHost+"'"
147
148 if options.verbose:
149 print 'Connect string:',connectStr
150 cx = psycopg.connect(connectStr)
151
152 else:
153 sys.exit('Must specify a database type')
154
155 return cx
156