-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfttables.py
executable file
·53 lines (48 loc) · 1.3 KB
/
fttables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import pg
import json
import mypass
import types
if len(sys.argv) < 2:
print "Missing table name"
sys.exit()
tablename = sys.argv[1]
description = ""
if len(sys.argv) > 2:
description = sys.argv[2]
pgconn = mypass.getConn()
#r = pgconn.query("SELECT * FROM %s LIMIT 1" % tablename).dictresult()
#r = pgconn.get_attnames(tablename)
sql = "select column_name, udt_name from information_schema.columns WHERE table_name = '%s' order by ordinal_position" % tablename
res = pgconn.query(sql).getresult()
out = {
"kind": "fusiontables#table",
"name": tablename,
"columns": [
],
"description": description,
"isExportable": False,
"attribution": "Cédric Sam",
"attributionLink": "http://cedric.sam.name/"
}
i = 0
#print res
for r in res:
k = r[0]
v = r[1]
if v in ["numeric"] or v.startswith("int"):
typ = "NUMBER"
elif v in ["date"] or v.startswith("time"):
typ = "DATETIME"
elif v in ["varchar"]:
typ = "STRING"
elif v in ["geometry"] or k in ["lat", "lng", "latitude", "longitude", "kml", "bounds", "point"]:
typ = "LOCATION"
else:
typ = "STRING"
col = { "columnId": i, "name": k, "type": typ }
i += 1
out["columns"].append(col)
print json.dumps(out)