XSDSQL

 view release on metacpan or  search on metacpan

bin/xsd2sql.pl  view on Meta::CPAN

use blx::xsdsql::schema_repository;
use blx::xsdsql::connection;
use DBI;

use constant {
 	DEFAULT_VIEW_PREFIX				=> 'V'
	,DEFAULT_ROOT_TABLE_NAME 		=> 'ROOT'
 	,DEFAULT_SEQUENCE_PREFIX			=> 'S'
 	,DEFAULT_TABLE_DICTIONARY		=> 'table_dictionary'
 	,DEFAULT_COLUMN_DICTIONARY		=> 'column_dictionary'
	,DEFAULT_RELATION_DICTIONARY 	=> 'relation_dictionary'
	,O_KEYS							=>  [ qw(MAX_VIEW_COLUMNS MAX_VIEW_JOINS EMIT_SCHEMA_DUMPER) ]
	
};

sub get_dbconn_cs {
	my ($connstr,%params)=@_;
	$connstr=$ENV{DB_CONNECT_STRING} unless defined $connstr;
	unless (defined $connstr) {
		print STDERR "no connection string specify - set the option 'c' or define the env var DB_CONNECT_STRING\n";
		return ();
	}

	my $connection=blx::xsdsql::connection->new;
	unless ($connection->do_connection_list($connstr)) {
		print STDERR $connection->get_last_error,"\n";
		return ();
	}
	my @a=$connection->get_connection_list;
	return ( { 
				OUTPUT_NAMESPACE => $connection->get_output_namespace
				,DB_NAMESPACE	 => $connection->get_db_namespace
			 },@a
	);
}


my %Opt=();
unless (getopts ('hc:r:p:t:v:do:',\%Opt)) {
	print STDERR "invalid option or option not set\n";
	exit 1;
}

if ($Opt{h}) {
	print STDOUT 
$0,q(  [<options>]  [<xsdfile>] [<command>...]
    <options>: 
        -h  - this help
        -d  - emit debug info 
		-c <connstr> - connect string to database - the default is the value of the env var DB_CONNECT_STRING
			otherwise is an error
			the form is  [<output_namespace>::]<dbtype>:<user>/<password>@<dbname>[:hostname[:port]][;<attribute>[,<attribute>...]
			<output_namespace>::<dbtype> - see the output of namespaces command 
				the default for <output_namespace> is 'sql'
			<dbname> - database name 
			<hostname> - remote host name or ip address 
			<port> - remote host port
			<attribute> - extra attribute
			Examples: 
				sql::pg:user/pwd@mydb:127.0.0.1;RaiseError => 1,AutoCommit => 0,pg_enable_utf8 => 1
				sql::mysql:user/pwd@mydb:127.0.0.1;RaiseError => 1,AutoCommit => 0,mysql_enable_utf8 => 1
				sql::oracle:user/pwd@orcl:neutrino:1522;RaiseError => 1,AutoCommit => 0
			
        -r <root_table_name> - set the root table name  (default '".DEFAULT_ROOT_TABLE_NAME."')
        -p <table_prefix_name> - set the prefix for the tables name (default none)
        -v <view_prefix_name>  - set the prefix for views name (default '".DEFAULT_VIEW_PREFIX."')
                WARNING - This option can influence table names
        -t <table_name>|<path_name>[,<table_name>|<path_name>...] - generate view starting only from <table_name> (default all tables)
            if the first <table_name>|<path_name> begin with comma then <table_name>|<path_name> (without comma) is a file name containing a list of <table_name>|<path_name>
        -o <name>=<value>[,<name>=<value>...]
            set extra params - valid names are:
                MAX_VIEW_COLUMNS     =>  produce view code only for views with columns number <= MAX_VIEW_COLUMNS
                    -1 is a system limit (database depend)
                    false is no limit (the default)
                MAX_VIEW_JOINS         =>  produce view code only for views with join number <= MAX_VIEW_JOINS 
                    -1 is a system limit (database depend)
                    false is no limit (the default)
    <commands>
        display_namespaces - display on stdout the namespaces (type+db) founded (Es: sql::pg)
        drop_table  - generate a drop tables on stdout
        create_table - generate a create tables on stdout
        addpk - generate primary keys on stdout
        drop_sequence - generate a drop sequence on stdout
        create_sequence - generate a create sequence on stdout
        drop_view       - generate a drop view on stdout
        create_view     - generate a create view on stdout
        drop_dictionary - generate a drop dictionary on stdout
        create_dictionary - generate a create dictionary on stdout
        insert_dictionary - generate an insert dictionary on stdout
        display_path_relation - display on stdout the relation from path and table/column
),"\n";
    exit 0;
}


my @cl_namespaces=blx::xsdsql::generator::get_namespaces;

if (nvl($ARGV[0]) eq 'display_namespaces') {	
	for my $n(@cl_namespaces) {
		print STDOUT $n,"\n";
	}
	exit 0;
}

my ($ns,@conn)=get_dbconn_cs($Opt{c});
exit 1 unless defined $ns;
$Opt{output_namespace}=$ns->{OUTPUT_NAMESPACE};
$Opt{db_namespace}=$ns->{DB_NAMESPACE};

unless (grep($_ eq $ns->{OUTPUT_NAMESPACE}.'::'.$ns->{DB_NAMESPACE},@cl_namespaces)) {
	print STDERR $ns->{OUTPUT_NAMESPACE}.'::'.$ns->{DB_NAMESPACE}.": namespace not know";
	exit 1;
}


if (defined (my $t=$Opt{t})) {
	if ($t=~/^,(.*)$/) {  # is a file name
		if (open(my $fd,'<',$1)) {
			my @lines=grep(!/^\s*$/,
				map {
					my $v=$_;



( run in 0.838 second using v1.01-cache-2.11-cpan-39bf76dae61 )