dgmike
2/6/2010 - 8:01 PM

Exports a database to dia file using PDO to connect on server

Exports a database to dia file using PDO to connect on server

<?php

$file = dirname(__FILE__).'/tabelas.dia';
$host = 'mysql:host=localhost;dbname=';
$user = '';
$pass = '';

$export = array();

$pdo = new PDO($host, $user, $pass);
$tables = $pdo->query('show tables');
while ($table = $tables->fetch()) {
    $sql = 'describe '.$table[0];
    $columns = $pdo->query($sql);
    $export[$table[0]] = array();
    while ($column = $columns->fetch()) {
        $export[$table[0]][$column[0]] = $column[1];
    }
}

$saida = '<?xml version="1.0" encoding="UTF-8"?>
<dia:diagram xmlns:dia="http://www.lysator.liu.se/~alla/dia/">
  <dia:diagramdata>
    <dia:attribute name="background"><dia:color val="#ffffff"/></dia:attribute>
    <dia:attribute name="pagebreak"><dia:color val="#000099"/></dia:attribute>
    <dia:attribute name="paper">
      <dia:composite type="paper">
        <dia:attribute name="name"><dia:string>#A4#</dia:string></dia:attribute>
        <dia:attribute name="tmargin"><dia:real val="2.8222000598907471"/></dia:attribute>
        <dia:attribute name="bmargin"><dia:real val="2.8222000598907471"/></dia:attribute>
        <dia:attribute name="lmargin"><dia:real val="2.8222000598907471"/></dia:attribute>
        <dia:attribute name="rmargin"><dia:real val="2.8222000598907471"/></dia:attribute>
        <dia:attribute name="is_portrait"><dia:boolean val="true"/></dia:attribute>
        <dia:attribute name="scaling"><dia:real val="1"/></dia:attribute>
        <dia:attribute name="fitto"><dia:boolean val="false"/></dia:attribute>
      </dia:composite>
    </dia:attribute>
    <dia:attribute name="grid">
      <dia:composite type="grid">
        <dia:attribute name="width_x"><dia:real val="1"/></dia:attribute>
        <dia:attribute name="width_y"><dia:real val="1"/></dia:attribute>
        <dia:attribute name="visible_x"><dia:int val="1"/></dia:attribute>
        <dia:attribute name="visible_y"><dia:int val="1"/></dia:attribute>
        <dia:composite type="color"/>
      </dia:composite>
    </dia:attribute>
    <dia:attribute name="color"><dia:color val="#d8e5e5"/></dia:attribute>
    <dia:attribute name="guides">
      <dia:composite type="guides">
        <dia:attribute name="hguides"/>
        <dia:attribute name="vguides"/>
      </dia:composite>
    </dia:attribute>
  </dia:diagramdata>
  <dia:layer name="First Plan" visible="true" active="true">

';

$x = 0;
foreach ($export as $table => $columns) {
    $x++;
    $saida .= '
 
    <dia:object type="Database - Table" version="0" id="O0">
      <dia:attribute name="name"><dia:string>#'.$table.'#</dia:string></dia:attribute>
      <dia:attribute name="elem_corner"><dia:point val="0.5,'.$x.'"/></dia:attribute>
 
      <dia:attribute name="obj_pos"><dia:point val="2.2,1.2"/></dia:attribute>
      <dia:attribute name="obj_bb"><dia:rectangle val="1.2,1.2;7.12,3.1"/></dia:attribute>
      <dia:attribute name="meta"><dia:composite type="dict"/></dia:attribute>
      <dia:attribute name="elem_width"><dia:real val="5.9199999999999999"/></dia:attribute>
      <dia:attribute name="elem_height"><dia:real val="1.8999999999999999"/></dia:attribute>
      <dia:attribute name="text_colour"><dia:color val="#000000"/></dia:attribute>
      <dia:attribute name="line_colour"><dia:color val="#000000"/></dia:attribute>
      <dia:attribute name="fill_colour"><dia:color val="#ffffff"/></dia:attribute>
      <dia:attribute name="line_width"><dia:real val="0.10000000000000001"/></dia:attribute>
      <dia:attribute name="comment"><dia:string>##</dia:string></dia:attribute>
      <dia:attribute name="visible_comment"><dia:boolean val="false"/></dia:attribute>
      <dia:attribute name="tagging_comment"><dia:boolean val="false"/></dia:attribute>
      <dia:attribute name="underline_primary_key"><dia:boolean val="true"/></dia:attribute>
      <dia:attribute name="bold_primary_keys"><dia:boolean val="false"/></dia:attribute>
      <dia:attribute name="normal_font"><dia:font family="monospace" style="0" name="Courier"/></dia:attribute>
      <dia:attribute name="name_font"><dia:font family="sans" style="80" name="Helvetica-Bold"/></dia:attribute>
      <dia:attribute name="comment_font"><dia:font family="sans" style="0" name="Helvetica"/></dia:attribute>
      <dia:attribute name="normal_font_height"><dia:real val="0.80000000000000004"/></dia:attribute>
      <dia:attribute name="name_font_height"><dia:real val="0.69999999999999996"/></dia:attribute>
      <dia:attribute name="comment_font_height"><dia:real val="0.69999999999999996"/></dia:attribute>
 
      <dia:attribute name="attributes">
 
    ';
     
    foreach ($columns as $column => $type) {
        $saida .= '
            <dia:composite type="table_attribute">
                <dia:attribute name="name"><dia:string>#'.$column.'#</dia:string></dia:attribute>
                <dia:attribute name="type"><dia:string>#'.$type.'#</dia:string></dia:attribute>
                <dia:attribute name="comment"><dia:string>##</dia:string></dia:attribute>
                <dia:attribute name="primary_key"><dia:boolean val="false"/></dia:attribute>
                <dia:attribute name="nullable"><dia:boolean val="false"/></dia:attribute>
                <dia:attribute name="unique"><dia:boolean val="false"/></dia:attribute>
            </dia:composite>
        ';
    }

    $saida .= '

        </dia:attribute>
    </dia:object>

    ';
}

$saida .= '
  </dia:layer>
</dia:diagram>  
';

file_put_contents($file, $saida);