HomeBlogMagic

SQL Tabellen mit JSON Beschreiben

Nachdem ich jetzt schon einige male Datenbanken neu einrichten musste, und dies über verschiedene Datenbank Typen hinweg, habe ich jetzt beschlossen die Tabellen mit JSON zu beschreiben und dann die CREATE TABLE daraus für jede Datenbank individuell zu generieren.

Es fehlen bestimmt noch eine große Menge an Optionen die man setzen könnte. Dieses Beispiel ist allerdings für mich und die Tabellen die Ich so benutze vollkommen ausreichend.

Mein CcOS wird das demnächst im C++ interpretieren können. Für PHP werde ich da auch noch etwas aufsetzen.
Damit können die Informationen systemübergreifend ausgetauscht werden.

{ 
  name     : "example"    # Name of table
  database : "optional"   # Optional Database target, if it s required to define
  type     : "InnoDB"     # Optional Type like InnoDB
  charset  : "utf8"       # Optional Charset, default should be utf8
  primary  :              # Primary key, is an array of columns, default only one
  [
    "id"
  ]
  columns :
  [
    {
      name  : "id"      # Name of column
      type  : "BIGINT"  # Type of table like INT, Timestampe, DATETIME etc.
      attributes :      # defined attributes, they should be converted to 
      [                 # SQL specific names/functions
        "NOT NULL", 
        "AUTO_INCREMENT"
      ]
    }
    {
      name  : "foreign_index"      # Name of column
      type  : "BIGINT"  # Type of table like INT, Timestampe, DATETIME etc.
      attributes :      # defined attributes, they should be converted to 
      [                 # SQL specific names/functions
        "NULL"
      ]
    }
    {
      name  : "uni_col"      # Name of column
      type  : "BIGINT"  # Type of table like INT, Timestampe, DATETIME etc.
      attributes :      # defined attributes, they should be converted to 
      [                 # SQL specific names/functions
        "NOT NULL"
      ]
    }
  ]
  unique  :              # Optional Unique keys
  [
    "uni_col"
  ]
  index  :               # Index keys for assagining foreign keys and constraints
  [
    "foreign_index"
  ]
  foreign
  [
    {
      primary :           # Primary key/keys to connect to
      [
        "table",
        "id"
      ]
      foreigen :          # index key/keys to connect from
      [
        "foreign_index"
      ]
      update : "CASCADE"  # or NULL,
      delete : "CASCADE"  # or NULL, 
    }
  ]
}
Permalink: https://adirmeier.de/Blog/ID_355
Tags: Blog, JSON, SQLvon am 2021-01-15