Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Sep 2001 10:37:39 +0300
From:      "Dimitar Peikov" <mitko@rila.bg>
To:        binup@freebsd.org
Subject:   Perl code for XMLing database creation
Message-ID:  <200109250737.f8P7bda53417@earth.rila.bg>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
Hi,
This script uses XML::Parser to parse and generate SQL code from XML as the 
project requires to move configuration from hardcoded SQL statements to XML 
configuration. Attached is a simple XML file that is needed to generate the 
current MySQL data schema with database creation.



[-- Attachment #2 --]
<?xml version="1.0"?>
<updated>
  <database type="mysql" username="user" password="pass" db="updates">
    <table name="actions">
      <column name="reltag" type="int"/>
      <column name="component" type="int"/>
      <column name="distribution" type="varchar(255)"/>
      <column name="action" type="varchar(255)"/>
      <column name="undo" type="varchar(255)"/>
    </table>
    <table name="dists">
      <column name="reltag" type="int"/>
      <column name="timestamp" type="datetime"/>
      <column name="distribution" type="varchar(128)"/>
    </table>
    <table name="dirs">
      <column name="reltag" type="int"/>
      <column name="component" type="int"/>
      <column name="timestamp" type="datetime"/>
      <column name="mtree" type="varchar(255)"/>
    </table>
    <table name="files">
      <column name="reltag" type="int"/>
      <column name="timestamp" type="datetime"/>
      <column name="distribution" type="varchar(128)"/>
      <column name="filename" type="varchar(255)"/>
      <column name="location" type="varchar(255)"/>
      <column name="mode" type="int"/>
      <column name="uid" type="int"/>
      <column name="gid" type="int"/>
    </table>
    <table name="profile">
      <column name="name" type="varchar(128)"/>
      <column name="reltag" type="int"/>
      <column name="description" type="varchar(255)"/>
    </table>
    <table name="releases">
      <column name="reltag" type="int" PK="yes" extra="auto_increment"/>
      <column name="name" type="varchar(128)"/>
      <column name="floating" type="int"/>
    </table>
  </database>
</updated>
[-- Attachment #3 --]
#!/usr/bin/perl

use XML::Parser;

my $arg = shift;
unless (defined $arg) { 
    print "XML file needed.\n"; 
    exit();
};

my $sql_code = "";
my $database = "";
my $table_sql = "";
my $column_id = 0;

my $parser = new XML::Parser(Style=>'Subs');

$parser->parsefile($arg);

print $sql_code;

sub updated {
    my $expat = shift;
    my $element = shift;
    my @attributes = @_;

    if (defined $DEBUG) {
	print "Enter updated @attributes\n";
    }
}

sub updated_ {
    my $expat = shift;
    my $element = shift;

    if (defined $DEBUG) {
	print "Exit updated\n";
    }
}

sub database {
    my $expat = shift;
    my $element = shift;
    my @attributes = @_;
    my %attr = @attributes;

    # MySQL
    $sql_code .= "create database " . $attr{db} . ";\n";

    if (defined $DEBUG) {
	print "Enter database @attributes\n";
    }
}

sub database_ {
    my $expat = shift;
    my $element = shift;

    if (defined $DEBUG) {
	print "Exit database\n";
    }
}

sub table {
    my $expat = shift;
    my $element = shift;
    my @attributes = @_;
    my %attr = @attributes;

    $table_sql = "create table " . $attr{name} . " (";
    $column_id = 0;

    if (defined $DEBUG) {
	print "Enter table @attributes\n";
    }
}

sub table_ {
    my $expat = shift;
    my $element = shift;

    $table_sql .= ");\n";
    $sql_code .= $table_sql;

    if (defined $DEBUG) {
	print "Exit table\n";
    }
}

sub column {
    my $expat = shift;
    my $element = shift;
    my @attributes = @_;
    my %attr = @attributes;

    $table_sql .= ", " if ($column_id > 0);
    $column_id ++;
    $table_sql .= $attr{name} . " " . $attr{type};
    $table_sql .= " primary key" if (defined $attr{PK} && uc $attr{PK} == "YES");
    $table_sql .= " " . $attr{extra} if (defined $attr{extra});

    if (defined $DEBUG) {
	print "Enter column @attributes\n";
    }
}

sub column_ {
    my $expat = shift;
    my $element = shift;

    if (defined $DEBUG) {
	print "Exit column\n";
    }
}

[-- Attachment #4 --]
Dimitar Peikov
Programmer Analyst
Globalization Group
"We Build e-Business"  

RILA Solutions  
27 Building, Acad.G.Bonchev Str.  
1113 Sofia, Bulgaria  

phone: (+359 2) 9797320 
phone: (+359 2) 9797300 
fax:   (+359 2) 9733355  
http://www.rila.com 

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200109250737.f8P7bda53417>