Skip to content

Commit

Permalink
updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
darthbear committed Apr 29, 2015
1 parent e539f95 commit 754b7d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ Options:
name VARCHAR(20) DEFAULT '(no name)' NOT NULL,
dept CHAR(2),
age INT,
height DOUBLE(2,1)
height DOUBLE(2,1),
created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

mysql> INSERT INTO employee VALUES (1, 'John Doe', 'IT', 28, 6.3),(1, 'Mary Gray', 'IT', 30, 6.8);
mysql> INSERT INTO employee(id, name, dept, age, height) VALUES (1, 'John Doe', 'IT', 28, 6.3),(1, 'Mary Gray', 'IT', 30, 6.8);

**Export the DDL definition**

```json
$ catdb ddl -d my_testdb -t employee -e /tmp/employee.json
$ cat /tmp/employee.json
{
Expand Down Expand Up @@ -144,12 +146,19 @@ Options:
"scale": 1,
"size": 2,
"type": "real"
},
{
"column": "created_on",
"default": "current_timestamp",
"nullable": false,
"type": "timestamp"
}
],
"name": "employee"
}
]
}
```

**Convert DDL definition to CREATE TABLE statement for Postgres**

Expand All @@ -160,23 +169,24 @@ Options:
name character varying(20) DEFAULT '(no name)',
dept character(2),
age integer,
height real
height real,
created_on timestamp without time zone DEFAULT now()
);

**Export data**

$ catdb data -d my_testdb -t employee -e /tmp/export.csv
$ cat /tmp/export.csv
id|name|dept|age|height
1|John Doe|IT|28|6.3
1|Mary Gray|IT|30|6.8
id|name|dept|age|height|created_on
1|John Doe|IT|28|6.3|2015-04-28 22:17:57
1|Mary Gray|IT|30|6.8|2015-04-28 22:17:57

**Import data (dry-run)**

$ catdb data -d pg_testdb -t employee -i /tmp/export.csv -dr
INSERT INTO employee (id,name,dept,age,height)
VALUES('1','John Doe','IT','28','6.3'),
('1','Mary Gray','IT','30','6.8');
INSERT INTO employee (id,name,dept,age,height,created_on)
VALUES('1','John Doe','IT','28','6.3','2015-04-28 22:17:57'),
('1','Mary Gray','IT','30','6.8','2015-04-28 22:17:57');

### TODO

Expand All @@ -201,4 +211,5 @@ Elastic Search | :x:
MongoDB | :x:
Export to S3 | :x:
Import from S3 | :x:
Compatible with Turbine XML format | :x:
Common console | :x:
2 changes: 2 additions & 0 deletions catdb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class CatDbException(Exception):
pass
11 changes: 10 additions & 1 deletion catdb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib
import pkg_resources
from pyhocon import ConfigFactory
from catdb import CatDbException


class Db(object):
Expand Down Expand Up @@ -41,14 +42,22 @@ def get_ddl(self, schema=None, table_filter=None):
def get_default(col_type, value):
return self._rev_mappings[col_type]['defaults'].get(value, value)

def get_rev_mapping(col_type):
rev_mapping = self._rev_mappings.get(col_type)
if rev_mapping is None:
raise CatDbException('Cannot find reverse mapping {col_type}. ' \
'Please submit an issue at https://github.com/chimpler/catdb/issues.'.format(col_type=col_type))
else:
return rev_mapping

def get_column_def(entry):
row = {
'column': entry['column'],
'nullable': entry['nullable'],
'radix': entry['radix'],
'scale': entry['scale'],
'size': entry['size'],
'type': self._rev_mappings[entry['type']]['type'],
'type': get_rev_mapping(entry['type'])['type'],
'default': get_default(entry['type'], entry['default'])
}

Expand Down
2 changes: 1 addition & 1 deletion catdb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():
data_parser = subparsers.add_parser('data', help='data', parents=[parent_parser])
data_parser.add_argument('-e', '--export', dest='export_file', help='export', required=False, action='store')
data_parser.add_argument('-i', '--import', dest='import_file', help='import', required=False)
list_parser = subparsers.add_parser('list', help='list', parents=[parent_parser])
subparsers.add_parser('list', help='list', parents=[parent_parser])

args = argparser.parse_args()
config = ConfigFactory.parse_file('.catdb')
Expand Down

0 comments on commit 754b7d0

Please sign in to comment.