github.com/dolthub/go-mysql-server@v0.18.0/enginetest/example_test.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package enginetest_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  
    22  	sqle "github.com/dolthub/go-mysql-server"
    23  	"github.com/dolthub/go-mysql-server/memory"
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/types"
    26  )
    27  
    28  func Example() {
    29  	// Create a test memory database and register it to the default engine.
    30  	pro := createTestDatabase()
    31  	e := sqle.NewDefault(pro)
    32  
    33  	session := memory.NewSession(sql.NewBaseSession(), pro)
    34  	ctx := sql.NewContext(context.Background(), sql.WithSession(session))
    35  	ctx.SetCurrentDatabase("mydb")
    36  
    37  	_, r, err := e.Query(ctx, `SELECT name, count(*) FROM mytable
    38  	WHERE name = 'John Doe'
    39  	GROUP BY name`)
    40  	checkIfError(err)
    41  
    42  	// Iterate results and print them.
    43  	for {
    44  		row, err := r.Next(ctx)
    45  		if err == io.EOF {
    46  			break
    47  		}
    48  		checkIfError(err)
    49  
    50  		name := row[0]
    51  		count := row[1]
    52  
    53  		fmt.Println(name, count)
    54  	}
    55  
    56  	// Output: John Doe 2
    57  }
    58  
    59  func checkIfError(err error) {
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  }
    64  
    65  func createTestDatabase() *memory.DbProvider {
    66  	db := memory.NewDatabase("mydb")
    67  	pro := memory.NewDBProvider(db)
    68  	session := memory.NewSession(sql.NewBaseSession(), pro)
    69  	ctx := sql.NewContext(context.Background(), sql.WithSession(session))
    70  
    71  	table := memory.NewTable(db.BaseDatabase, "mytable", sql.NewPrimaryKeySchema(sql.Schema{
    72  		{Name: "name", Type: types.Text, Source: "mytable"},
    73  		{Name: "email", Type: types.Text, Source: "mytable"},
    74  	}), db.GetForeignKeyCollection())
    75  	db.AddTable("mytable", table)
    76  
    77  	rows := []sql.Row{
    78  		sql.NewRow("John Doe", "john@doe.com"),
    79  		sql.NewRow("John Doe", "johnalt@doe.com"),
    80  		sql.NewRow("Jane Doe", "jane@doe.com"),
    81  		sql.NewRow("Evil Bob", "evilbob@gmail.com"),
    82  	}
    83  
    84  	for _, row := range rows {
    85  		table.Insert(ctx, row)
    86  	}
    87  
    88  	return pro
    89  }