Go实现MySQL

343 阅读1分钟

仓库地址:github.com/dolthub/go-…


package main

import (
   "fmt"
   "time"

   "github.com/dolthub/go-mysql-server/sql/information_schema"

   sqle "github.com/dolthub/go-mysql-server"
   "github.com/dolthub/go-mysql-server/memory"
   "github.com/dolthub/go-mysql-server/server"
   "github.com/dolthub/go-mysql-server/sql"
)

var (
   dbName    = "mydb"
   tableName = "mytable"
   address   = "localhost"
   port      = 3306
)

func main() {
   ctx := sql.NewEmptyContext()
   engine := sqle.NewDefault(
      sql.NewDatabaseProvider(
         createTestDatabase(ctx),
         information_schema.NewInformationSchemaDatabase(),
      ))

   config := server.Config{
      Protocol: "tcp",
      Address:  fmt.Sprintf("%s:%d", address, port),
   }
   s, err := server.NewDefaultServer(config, engine)
   if err != nil {
      panic(err)
   }
   if err = s.Start(); err != nil {
      panic(err)
   }
}

func createTestDatabase(ctx *sql.Context) *memory.Database {
   db := memory.NewDatabase(dbName)
   table := memory.NewTable(tableName, sql.NewPrimaryKeySchema(sql.Schema{
      {Name: "name", Type: sql.Text, Nullable: false, Source: tableName, PrimaryKey: true},
      {Name: "email", Type: sql.Text, Nullable: false, Source: tableName, PrimaryKey: true},
      {Name: "phone_numbers", Type: sql.JSON, Nullable: false, Source: tableName},
      {Name: "created_at", Type: sql.Datetime, Nullable: false, Source: tableName},
   }), db.GetForeignKeyCollection())
   db.AddTable(tableName, table)

   creationTime := time.Unix(0, 1667304000000001000).UTC()
   _ = table.Insert(ctx, sql.NewRow("Jane Deo", "janedeo@gmail.com", sql.MustJSON(`["556-565-566", "777-777-777"]`), creationTime))
   _ = table.Insert(ctx, sql.NewRow("Jane Doe", "jane@doe.com", sql.MustJSON(`[]`), creationTime))
   _ = table.Insert(ctx, sql.NewRow("John Doe", "john@doe.com", sql.MustJSON(`["555-555-555"]`), creationTime))
   _ = table.Insert(ctx, sql.NewRow("John Doe", "johnalt@doe.com", sql.MustJSON(`[]`), creationTime))
   return db
}
> mysql --host=localhost --port=3306 --user=root mydb --execute="SELECT * FROM mytable;"
+----------+-------------------+-------------------------------+----------------------------+
| name     | email             | phone_numbers                 | created_at                 |
+----------+-------------------+-------------------------------+----------------------------+
| Jane Deo | janedeo@gmail.com | ["556-565-566","777-777-777"] | 2022-11-01 12:00:00.000001 |
| Jane Doe | jane@doe.com      | []                            | 2022-11-01 12:00:00.000001 |
| John Doe | john@doe.com      | ["555-555-555"]               | 2022-11-01 12:00:00.000001 |
| John Doe | johnalt@doe.com   | []                            | 2022-11-01 12:00:00.000001 |
+----------+-------------------+-------------------------------+----------------------------+
SELECT count(name) FROM mytable
+---------------------+
| COUNT(mytable.name) |
+---------------------+
|                   4 |
+---------------------+
SELECT name,year(created_at) FROM mytable
+----------+--------------------------+
| name     | YEAR(mytable.created_at) |
+----------+--------------------------+
| John Doe |                     2018 |
| John Doe |                     2018 |
| Jane Doe |                     2018 |
| Evil Bob |                     2018 |
+----------+--------------------------+
SELECT email FROM mytable WHERE name = 'Evil Bob'
+-------------------+
| email             |
+-------------------+
| evilbob@gmail.com |
+-------------------+