PHP code example of maghead / sqlite-parser

1. Go to this page and download the library: Download maghead/sqlite-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maghead / sqlite-parser example snippets



use Maghead\SqliteParser\CreateTableParser;

$sql = 'CREATE TEMP TABLE `foo` (`a` INT DEFAULT 0, name VARCHAR, address VARCHAR, CONSTRAINT address_idx UNIQUE(name, address))';

$parser = new CreateTableParser;
$def = $parser->parse($sql);

foreach ($def->columns as $c) {
    echo $c->name;
    echo $c->type;
    echo $c->primary;
}

$this->assertCount(1, $def->constraints);
$this->assertInstanceOf('Maghead\\SqliteParser\\Constraint', $def->constraints[0]);
$this->assertEquals('address_idx', $def->constraints[0]->name);
$this->assertCount(2, $def->constraints[0]->unique);

class Maghead\SqliteParser\Table#2 (5) {
  public $columns =>
  array(1) {
    [0] =>
    class Maghead\SqliteParser\Column#6 (13) {
      public $name =>
      string(1) "a"
      public $type =>
      string(3) "INT"
      public $length =>
      NULL
      public $decimals =>
      NULL
      public $unsigned =>
      bool(false)
      public $primary =>
      NULL
      public $ordering =>
      NULL
      public $autoIncrement =>
      NULL
      public $unique =>
      NULL
      public $notNull =>
      NULL
      public $default =>
      int(-20)
      public $collate =>
      NULL
      public $references =>
      NULL
    }
  }
  public $temporary =>
  bool(true)
  public $ifNotExists =>
  bool(false)
  public $tableName =>
  string(3) "foo"
  public $constraints =>
  array(1) {
    [0] =>
    class Maghead\SqliteParser\Constraint#8 (4) {
      public $name =>
      string(2) "aa"
      public $primaryKey =>
      NULL
      public $unique =>
      array(1) {
        [0] =>
        class stdClass#13 (2) {
          public $name =>
          string(1) "a"
          public $ordering =>
          string(3) "ASC"
        }
      }
      public $foreignKey =>
      NULL
    }
  }
}