xo::db::require proc table (public)

 xo::db::require[i] table name definition [ populate ]

Defined in /var/www/openacs/packages/xotcl-core/tcl/05-db-procs.tcl

Create a table if needed, or add missing columns to an existing table.

Parameters:
name (required)
Table name.
definition (required)
Dictionary mapping column names to SQL definitions.
populate (optional)
Optional SQL to run after creating the table.

Testcases:
xotcl_core_tutorial_2
Source code:
#:log "==== require table $name exists: [:exists_table $name]\n$definition"
if {![:exists_table $name]} {
  set lines {}
  foreach col [dict keys $definition] {lappend lines "$col [dict get $definition $col]"}
  set definition [join $lines ",\n"]
  # :log "--table $name does not exist, creating with definition: $definition"
  ::xo::dc dml create-table-$name "create table $name ($definition)"
  if {$populate ne ""} {
    ::xo::dc dml populate-table-$name $populate
  }
} else {
  # The table exists already. Check the columns, whether we have to
  # add columns. We do not alter attribute types, and we do not
  # delete columns.
  foreach col [dict keys $definition] {
    if {![:exists_column $name $col]} {
      ns_log notice "xodb: adding column <alter table $name add column $col [dict get $definition $col]>"
      ::xo::dc dml alter-table-$name  "alter table $name add column $col [dict get $definition $col]"
    }
  }
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: