Git Product home page Git Product logo

sqlite3createtableparser's Introduction

Sqlite3CreateTableParser

๐Ÿ“œ Advanced PRAGMA table_info through DDL parsing

GOLANG port of C library done by

@marcobambini.

SQLite CREATE TABLE parser

A parser for sqlite create table sql statements.

SQLite is a very powerful software but it lacks an easy way to extract complete information about table and columns constraints. The built-in sql pragma:

PRAGMA schema.table_info(table-name);
PRAGMA foreign_key_list(table-name);

provide incomplete information and a manual parsing is required in order to extract more useful information.

CREATE TABLE syntax diagrams can be found on the official sqlite website.

Usage

package main

import "github.com/lempiy/Sqlite3CreateTableParser/parser"

//some fancy DDL
const ddl = `
CREATE TABLE contact_groups (
 contact_id integer,
 group_id integer,
 PRIMARY KEY (contact_id, group_id),
 FOREIGN KEY (contact_id) REFERENCES contacts (contact_id)
 ON DELETE CASCADE ON UPDATE NO ACTION,
 FOREIGN KEY (group_id) REFERENCES groups (group_id)
 ON DELETE CASCADE ON UPDATE NO ACTION
);
`

func main() {
    table, errCode := parser.ParseTable(sql, 0)
    if errCode != parser.ERROR_NONE {
        panic("Error during parsing sql")
    }
    // do stuff with received data
    fmt.Printf("%+v\n", table)
}

Table info structs

type Table struct {
	Name           string
	Schema         string
	IsTemporary    bool
	IsIfNotExists  bool
	IsWithoutRowid bool
	NumColumns     int
	Columns        []Column
	NumConstraint  int
	Constraints    []TableConstraint
}

type Column struct {
	Name                  string
	Type                  string
	Length                string
	ConstraintName        string
	IsPrimaryKey          bool
	IsAutoincrement       bool
	IsNotnull             bool
	IsUnique              bool
	PkOrder               OrderClause
	PkConflictClause      ConflictClause
	NotNullConflictClause ConflictClause
	UniqueConflictClause  ConflictClause
	CheckExpr             string
	DefaultExpr           string
	CollateName           string
	ForeignKeyClause      *ForeignKey
}

type TableConstraint struct {
	Type             ConstraintType
	Name             string
	NumIndexed       int
	IndexedColumns   []IdxColumn
	ConflictClause   ConflictClause
	CheckExpr        string
	ForeignKeyNum    int
	ForeignKeyName   []string
	ForeignKeyClause *ForeignKey
}

type ForeignKey struct {
	Table      string
	NumColumns int
	ColumnName []string
	OnDelete   FkAction
	OnUpdate   FkAction
	Match      string
	Deferrable FkDefType
}

type IdxColumn struct {
	Name        string
	CollateName string
	Order       OrderClause
}

Limitations

  • CREATE TABLE AS select-stmt syntax is not supported (SQL3ERROR_UNSUPPORTEDSQL is returned).
  • EXPRESSIONS in column constraints (CHECK and DEFAULT constraint) and table constraint (CHECK constraint) are not supported (SQL3ERROR_UNSUPPORTEDSQL is returned).

sqlite3createtableparser's People

Contributors

allam76 avatar lempiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

iwot allam76 clh021

sqlite3createtableparser's Issues

module wrong in `go.mod`

Hi. I was looking for sqlite ddl parser, so I found this package.
I wanted to try it out. But go get github.com/lempiy/Sqlite3CreateTableParser reported an error.

-> ~ go get github.com/lempiy/Sqlite3CreateTableParser
go: github.com/lempiy/[email protected]: parsing go.mod:
        module declares its path as: github.com/Allam76/Sqlite3CreateTableParser
                but was required as: github.com/lempiy/Sqlite3CreateTableParser

Would you please check your go.mod and fix this? @lempiy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.