Premshree Pillai ([info]premshree) wrote,
@ 2005-01-30 22:13:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Current music:Dido - White Flag

No XML here

So I’m at office on a Sunday, designing a database schema. I started out with writing the structures as SQL queries; I figured I’d write a parser—to generate formatted documentation. Bad idea. For one, I wouldn’t have been able to add any metadata.

I’m using YAML now, instead. I’m using a simple schema to design the tables:

tables:
   table_name:
      -field:
          type: type
          isnotnull: 0|1
          default:
          description:

Constructing the queries and generating formatted documentation is simple now. I’m using yaml4r:

#!/usr/local/bin/ruby

require 'yaml'
tree = YAML::parse(File.open('tables.yaml'))

table_name = 'foo'

types = tree.select('/tables/' + table_name + '/*/type').transform
isnotnulls = tree.select('/tables/' + table_name + '/*/isnotnull').transform
defaults = tree.select('/tables/' + table_name + '/*/default').transform
descriptions = tree.select('/tables/' + table_name + '/*/description').transform

all_data = [types, isnotnulls, defaults, descriptions]
all_data.each { |data|
	# do stuff
}

Less work than XML. A similar format, based on Python, is SLiP. My problem with SLiP is that blocks are separated using indentation—I’m somehow not happy with the idea of block separation using indentation for the purpose of representing semantic information. YAML, on the other hand, gives you the option of using block styles or flow styles.



(Post a new comment)


sriramb
2005-01-30 06:55 pm UTC (link)
this ruby stuff looks interesting. reminds me of my smalltalk days. have started playing with v1.82. Will get into YaML once i start dealing with object serialization.

(Reply to this)(Thread)


[info]premshree
2005-01-30 08:32 pm UTC (link)
Go, Ruby, go!

(Reply to this)(Parent)


[info]teemus
2005-01-30 07:08 pm UTC (link)
WoW!

(Reply to this)


[info]code_martial
2005-01-30 08:21 pm UTC (link)
What "stuff" do you do with it? I was looking for something that could reverse-engineer diagrams out of "CREATE TABLE" statements.

(Reply to this)(Thread)


[info]premshree
2005-01-30 08:23 pm UTC (link)
Generate queries and documentation.

(Reply to this)(Parent)(Thread)


[info]code_martial
2005-01-30 08:26 pm UTC (link)
Pass me a sample of the documentation you generate. It might turn out to be useful for me :)

(Reply to this)(Parent)


[info]code_martial
2005-01-30 08:25 pm UTC (link)
I'm not sure whether you intended to wrap SLiP in an 'abbr' tag (the tooltip doesn't look like an expansion) but if you want to show a tooltip, you can put its text in 'title' attribute anywhere (like in the 'a' tag here, for example). If you don't already have an element wrapping the content, you can use 'span'.

(Reply to this)(Thread)


[info]premshree
2005-01-30 08:30 pm UTC (link)
I intended to. It is an expansion.

(Reply to this)(Parent)


[info]teemus
2005-01-30 10:09 pm UTC (link)
Can you make a provision for referential constraints, i.e. foreign keys?

(Reply to this)(Thread)


[info]premshree
2005-01-31 08:01 am UTC (link)
keys:
   primary:
      -table_name:
          field: field
          name: name
   refs:
      -ref_name:
          field: field
          refer: refer (from list of primary)
          name: name

(Reply to this)(Parent)(Thread)


(Anonymous)
2005-02-01 09:12 pm UTC (link)
Assuming that the original example was,

tables:
- name: mytable
  comment: in this table I store wibbles
  fields:
  - name: field_one
    type: text
  - name: field_two
    type: integer
  - name: field_three
    type: text
    size: 254


then you could specify a primary key, by using,


tables:
- &T1
  name: primary-table
  comment: in this table I store wibbles
  fields:
  - &F1 
    name: field_one
    type: text
  - &F2
    name: field_two
    type: integer
  - &F3
    name: field_three
    type: text
    size: 254
  primary-key: [ *F1 ]
  secondary-key: [ *F2, *F3 ]
- 
  name: subordinate-table
  comment: refers to the primary table 
  fields:
  - &FX
    name: link-column
    type: text
  - 
    name: some-data
    type: boolean
  foreign-keys:
   - 
     from: [ *FX ]
     to: [ *F1 ]


(Reply to this)(Parent)

I'm not sure if this is what you meant...
(Anonymous)
2005-02-01 08:59 pm UTC (link)
The construct,


tables:
 table_name:
  - field:
      type: integer


doesn't probably do what you want -- it returns a sequence of mappings, where each mapping only has a single key with the field name. e.g.

 
{ 'tables':  
 { 'table_name': 
   [
     { 'field':
        { 'type': 'integer',
          'default': '' } 
      }
   ]
 }
}


Perhaps you were thinking of something like...

tables:
- name: mytable
  comment: in this table I store wibbles
  fields:
  - name: field_one
    type: text
  - name: field_two
    type: integer

(Reply to this)(Thread)

Re: I'm not sure if this is what you meant...
[info]premshree
2005-02-02 06:17 am UTC (link)
No, I intended field to be a sequence.

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…