11function createSchema(package ,parentdir ,db )
2- % DJ.CREATESCHEMA - interactively create a new DataJoint schema
3- %
4- % INPUT:
5- % (optional) package - name of the package to be associated with the schema
6- % (optional) parentdir - name of the dirctory where to create new package
7- % (optional) db - database name to associate with the schema
8-
9- if nargin < 3
10- dbname = input(' Enter database name >> ' ,' s' );
11- else
12- dbname = db ;
13- end
14-
15- if ~dbname
16- disp ' No database name entered. Quitting.'
17- elseif isempty(regexp(dbname ,' ^[a-z][a-z0-9_]*$' ,' once' ))
18- error ' Invalid database name. Begin with a letter, only lowercase alphanumerical and underscores.'
19- else
20- % create database
21- s = query(dj .conn , ...
22- sprintf(' SELECT schema_name FROM information_schema.schemata WHERE schema_name = "%s "' , dbname ));
23-
24- if ~isempty(s .schema_name )
25- disp ' database already exists'
2+ % DJ.CREATESCHEMA - interactively create a new DataJoint schema
3+ %
4+ % INPUT:
5+ % (optional) package - name of the package to be associated with the schema
6+ % (optional) parentdir - name of the dirctory where to create new package
7+ % (optional) db - database name to associate with the schema
8+
9+ if nargin < 3
10+ dbname = input(' Enter database name >> ' ,' s' );
2611 else
27- query(dj .conn , sprintf(' create schema %s ' ,dbname ))
28- disp ' database created'
12+ dbname = db ;
2913 end
30-
31- if nargin < 1
32- if usejava(' desktop' )
33- disp ' Please select a package folder. Opening UI...'
34- folder = uigetdir(' ./' ,' Select a package folder' );
14+
15+ if ~dbname
16+ disp ' No database name entered. Quitting.'
17+ elseif isempty(regexp(dbname ,' ^[a-z][a-z0-9_]*$' ,' once' ))
18+ error([' Invalid database name. Begin with a letter, only lowercase alphanumerical and ' ...
19+ ' underscores.' ])
20+ else
21+ % create database
22+ s = query(dj .conn , ...
23+ sprintf([' SELECT schema_name as `schema_name` ' ...
24+ ' FROM information_schema.schemata WHERE schema_name = "%s "' ], dbname ));
25+
26+ if ~isempty(s .schema_name )
27+ disp ' database already exists'
3528 else
36- folder = input(' Enter package folder path >> ' ,' s' );
29+ query(dj .conn , sprintf(' create schema %s ' ,dbname ))
30+ disp ' database created'
3731 end
38- else
39- if nargin < 3
32+
33+ if nargin < 1
4034 if usejava(' desktop' )
41- fprintf( ' Please select folder to create package %s in . Opening UI...\n ' , [ ' + ' , package ])
42- folder = uigetdir(' ./' , sprintf( ' Select folder to create package %s in ' , [ ' + ' , package ]) );
35+ disp ' Please select a package folder . Opening UI...'
36+ folder = uigetdir(' ./' ,' Select a package folder ' );
4337 else
44- folder = input(' Enter parent folder path >> ' ,' s' );
38+ folder = input(' Enter package folder path >> ' ,' s' );
4539 end
4640 else
47- folder = parentdir ;
48- end
49-
50- if folder
51- folder = fullfile(folder , [' +' , package ]);
52- mkdir(folder )
53- end
54- end
55-
56- if ~folder
57- disp ' No package selected. Cancelled.'
58- else
59- [filepath ,package ] = fileparts(folder );
60- if package(1 )~=' +'
61- error ' Package folders must start with a +'
41+ if nargin < 3
42+ if usejava(' desktop' )
43+ fprintf(' Please select folder to create package %s in. Opening UI...\n ' , ...
44+ [' +' , package ])
45+ folder = uigetdir(' ./' , sprintf(' Select folder to create package %s in' , ...
46+ [' +' , package ]));
47+ else
48+ folder = input(' Enter parent folder path >> ' ,' s' );
49+ end
50+ else
51+ folder = parentdir ;
52+ end
53+
54+ if folder
55+ folder = fullfile(folder , [' +' , package ]);
56+ mkdir(folder )
57+ end
6258 end
63- package = package(2 : end ); % discard +
64-
65- % create the getSchema function
66- schemaFile = fullfile(folder ,' getSchema.m' );
67- if exist(schemaFile ,' file' )
68- fprintf(' %s .getSchema.m already exists\n ' , package )
59+
60+ if ~folder
61+ disp ' No package selected. Cancelled.'
6962 else
70- f = fopen(schemaFile ,' wt' );
71- assert(-1 ~= f , ' Could not open %s ' , f )
72-
73- fprintf(f ,' function obj = getSchema\n ' );
74- fprintf(f ,' persistent schemaObject\n ' );
75- fprintf(f ,' if isempty(schemaObject)\n ' );
76- fprintf(f ,' schemaObject = dj.Schema(dj.conn, ''%s'' , ''%s'' );\n ' , package , dbname );
77- fprintf(f ,' end\n ' );
78- fprintf(f ,' obj = schemaObject;\n ' );
79- fprintf(f ,' end\n ' );
80- fclose(f );
81- end
82-
83- % test that getSchema is on the path
84- whichpath = which(sprintf(' %s .getSchema' ,package ));
85- if isempty(whichpath )
86- warning(' Could not open %s .getSchema. Ensure that %s is on the path' , package , filepath )
63+ [filepath ,package ] = fileparts(folder );
64+ if package(1 )~=' +'
65+ error ' Package folders must start with a +'
66+ end
67+ package = package(2 : end ); % discard +
68+
69+ % create the getSchema function
70+ schemaFile = fullfile(folder ,' getSchema.m' );
71+ if exist(schemaFile ,' file' )
72+ fprintf(' %s .getSchema.m already exists\n ' , package )
73+ else
74+ f = fopen(schemaFile ,' wt' );
75+ assert(-1 ~= f , ' Could not open %s ' , f )
76+
77+ fprintf(f ,' function obj = getSchema\n ' );
78+ fprintf(f ,' persistent schemaObject\n ' );
79+ fprintf(f ,' if isempty(schemaObject)\n ' );
80+ fprintf(f ,' schemaObject = dj.Schema(dj.conn, ''%s'' , ''%s'' );\n ' , ...
81+ package , dbname );
82+ fprintf(f ,' end\n ' );
83+ fprintf(f ,' obj = schemaObject;\n ' );
84+ fprintf(f ,' end\n ' );
85+ fclose(f );
86+ end
87+
88+ % test that getSchema is on the path
89+ whichpath = which(sprintf(' %s .getSchema' ,package ));
90+ if isempty(whichpath )
91+ warning(' Could not open %s .getSchema. Ensure that %s is on the path' , ...
92+ package , filepath )
93+ end
8794 end
8895 end
89- end
96+ end
0 commit comments