Skip to content

Commit d9f07d8

Browse files
committed
stream
1 parent af68eb5 commit d9f07d8

File tree

12 files changed

+65
-10
lines changed

12 files changed

+65
-10
lines changed

examples/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/mstgnz/sqlmapper/postgres"
1212
"github.com/mstgnz/sqlmapper/sqlite"
1313
"github.com/mstgnz/sqlmapper/sqlserver"
14+
"github.com/mstgnz/sqlmapper/stream"
1415
)
1516

1617
// DatabaseType represents supported database types
@@ -92,7 +93,55 @@ func (c *Converter) Convert(sourceType, targetType DatabaseType, content string)
9293
return result, nil
9394
}
9495

96+
// StreamExample demonstrates stream parsing functionality
97+
func StreamExample() {
98+
// Create MySQL stream parser
99+
parser := mysql.NewMySQLStreamParser()
100+
101+
// Open source file
102+
file, err := os.Open("examples/files/mysql.sql")
103+
if err != nil {
104+
log.Fatalf("Failed to open file: %v", err)
105+
}
106+
defer file.Close()
107+
108+
// Parse stream and handle objects
109+
err = parser.ParseStream(file, func(obj stream.SchemaObject) error {
110+
switch obj.Type {
111+
case stream.TableObject:
112+
if table, ok := obj.Data.(*sqlmapper.Table); ok {
113+
fmt.Printf("Found table: %s\n", table.Name)
114+
}
115+
case stream.ViewObject:
116+
if view, ok := obj.Data.(*sqlmapper.View); ok {
117+
fmt.Printf("Found view: %s\n", view.Name)
118+
}
119+
case stream.FunctionObject:
120+
if function, ok := obj.Data.(*sqlmapper.Function); ok {
121+
fmt.Printf("Found function: %s\n", function.Name)
122+
}
123+
case stream.ProcedureObject:
124+
if procedure, ok := obj.Data.(*sqlmapper.Procedure); ok {
125+
fmt.Printf("Found procedure: %s\n", procedure.Name)
126+
}
127+
case stream.TriggerObject:
128+
if trigger, ok := obj.Data.(*sqlmapper.Trigger); ok {
129+
fmt.Printf("Found trigger: %s\n", trigger.Name)
130+
}
131+
}
132+
return nil
133+
})
134+
135+
if err != nil {
136+
log.Fatalf("Stream parsing error: %v", err)
137+
}
138+
}
139+
95140
func main() {
141+
// Stream parsing example
142+
fmt.Println("\n=== Stream Parsing Example ===")
143+
StreamExample()
144+
96145
converter := NewConverter()
97146

98147
// Example conversions

mysql/mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type MySQL struct {
2020

2121
// NewMySQL creates and initializes a new MySQL parser instance.
2222
// It returns a parser that can handle MySQL specific SQL syntax and schema structures.
23-
func NewMySQL() *MySQL {
23+
func NewMySQL() sqlmapper.Database {
2424
return &MySQL{
2525
schema: &sqlmapper.Schema{},
2626
}

mysql/mysql_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type MySQLStreamParser struct {
1818
// NewMySQLStreamParser creates a new MySQL stream parser
1919
func NewMySQLStreamParser() *MySQLStreamParser {
2020
return &MySQLStreamParser{
21-
mysql: NewMySQL(),
21+
mysql: NewMySQL().(*MySQL),
2222
}
2323
}
2424

oracle/oracle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Oracle struct {
2020

2121
// NewOracle creates and initializes a new Oracle parser instance.
2222
// It returns a parser that can handle Oracle specific SQL syntax and schema structures.
23-
func NewOracle() *Oracle {
23+
func NewOracle() sqlmapper.Database {
2424
return &Oracle{
2525
schema: &sqlmapper.Schema{},
2626
}

oracle/oracle_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type OracleStreamParser struct {
1818
// NewOracleStreamParser creates a new Oracle stream parser
1919
func NewOracleStreamParser() *OracleStreamParser {
2020
return &OracleStreamParser{
21-
oracle: NewOracle(),
21+
oracle: NewOracle().(*Oracle),
2222
}
2323
}
2424

postgres/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type PostgreSQL struct {
2020

2121
// NewPostgreSQL creates and initializes a new PostgreSQL parser instance.
2222
// It returns a parser that can handle PostgreSQL specific SQL syntax and schema structures.
23-
func NewPostgreSQL() *PostgreSQL {
23+
func NewPostgreSQL() sqlmapper.Database {
2424
return &PostgreSQL{
2525
schema: &sqlmapper.Schema{},
2626
}

postgres/postgres_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type PostgreSQLStreamParser struct {
1818
// NewPostgreSQLStreamParser creates a new PostgreSQL stream parser
1919
func NewPostgreSQLStreamParser() *PostgreSQLStreamParser {
2020
return &PostgreSQLStreamParser{
21-
postgres: NewPostgreSQL(),
21+
postgres: NewPostgreSQL().(*PostgreSQL),
2222
}
2323
}
2424

schema.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package sqlmapper
33
// DatabaseType represents the supported database types
44
type DatabaseType string
55

6+
// Database represents the common interface for all database implementations
7+
type Database interface {
8+
Parse(content string) (*Schema, error)
9+
Generate(schema *Schema) (string, error)
10+
}
11+
612
const (
713
MySQL DatabaseType = "mysql"
814
PostgreSQL DatabaseType = "postgresql"

sqlite/sqlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type SQLite struct {
2121

2222
// NewSQLite creates and initializes a new SQLite parser instance.
2323
// It returns a parser that can handle SQLite specific SQL syntax and schema structures.
24-
func NewSQLite() *SQLite {
24+
func NewSQLite() sqlmapper.Database {
2525
return &SQLite{
2626
schema: &sqlmapper.Schema{},
2727
buf: &bytes.Buffer{},

sqlite/sqlite_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type SQLiteStreamParser struct {
1818
// NewSQLiteStreamParser creates a new SQLite stream parser
1919
func NewSQLiteStreamParser() *SQLiteStreamParser {
2020
return &SQLiteStreamParser{
21-
sqlite: NewSQLite(),
21+
sqlite: NewSQLite().(*SQLite),
2222
}
2323
}
2424

0 commit comments

Comments
 (0)