github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/dtables/commits_table.go (about)

     1  // Copyright 2020 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 dtables
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
    24  	"github.com/dolthub/dolt/go/store/types"
    25  )
    26  
    27  var _ sql.Table = (*CommitsTable)(nil)
    28  
    29  // CommitsTable is a sql.Table that implements a system table which
    30  // shows the combined commit log for all branches in the repo.
    31  type CommitsTable struct {
    32  	dbName string
    33  	ddb    *doltdb.DoltDB
    34  }
    35  
    36  // NewCommitsTable creates a CommitsTable
    37  func NewCommitsTable(_ *sql.Context, ddb *doltdb.DoltDB) sql.Table {
    38  	return &CommitsTable{ddb: ddb}
    39  }
    40  
    41  // Name is a sql.Table interface function which returns the name of the table.
    42  func (dt *CommitsTable) Name() string {
    43  	return doltdb.CommitsTableName
    44  }
    45  
    46  // String is a sql.Table interface function which returns the name of the table.
    47  func (dt *CommitsTable) String() string {
    48  	return doltdb.CommitsTableName
    49  }
    50  
    51  // Schema is a sql.Table interface function that gets the sql.Schema of the commits system table.
    52  func (dt *CommitsTable) Schema() sql.Schema {
    53  	return []*sql.Column{
    54  		{Name: "commit_hash", Type: sql.Text, Source: doltdb.CommitsTableName, PrimaryKey: true},
    55  		{Name: "committer", Type: sql.Text, Source: doltdb.CommitsTableName, PrimaryKey: false},
    56  		{Name: "email", Type: sql.Text, Source: doltdb.CommitsTableName, PrimaryKey: false},
    57  		{Name: "date", Type: sql.Datetime, Source: doltdb.CommitsTableName, PrimaryKey: false},
    58  		{Name: "message", Type: sql.Text, Source: doltdb.CommitsTableName, PrimaryKey: false},
    59  	}
    60  }
    61  
    62  // Partitions is a sql.Table interface function that returns a partition
    63  // of the data. Currently the data is unpartitioned.
    64  func (dt *CommitsTable) Partitions(*sql.Context) (sql.PartitionIter, error) {
    65  	return sqlutil.NewSinglePartitionIter(types.Map{}), nil
    66  }
    67  
    68  // PartitionRows is a sql.Table interface function that gets a row iterator for a partition.
    69  func (dt *CommitsTable) PartitionRows(sqlCtx *sql.Context, _ sql.Partition) (sql.RowIter, error) {
    70  	return NewCommitsRowItr(sqlCtx, dt.ddb)
    71  }
    72  
    73  // CommitsRowItr is a sql.RowItr which iterates over each commit as if it's a row in the table.
    74  type CommitsRowItr struct {
    75  	ctx context.Context
    76  	itr doltdb.CommitItr
    77  }
    78  
    79  // NewCommitsRowItr creates a CommitsRowItr from the current environment.
    80  func NewCommitsRowItr(sqlCtx *sql.Context, ddb *doltdb.DoltDB) (CommitsRowItr, error) {
    81  	itr, err := doltdb.CommitItrForAllBranches(sqlCtx, ddb)
    82  	if err != nil {
    83  		return CommitsRowItr{}, err
    84  	}
    85  
    86  	return CommitsRowItr{ctx: sqlCtx, itr: itr}, nil
    87  }
    88  
    89  // Next retrieves the next row. It will return io.EOF if it's the last row.
    90  // After retrieving the last row, Close will be automatically closed.
    91  func (itr CommitsRowItr) Next() (sql.Row, error) {
    92  	h, cm, err := itr.itr.Next(itr.ctx)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	meta, err := cm.GetCommitMeta()
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	return sql.NewRow(h.String(), meta.Name, meta.Email, meta.Time(), meta.Description), nil
   103  }
   104  
   105  // Close closes the iterator.
   106  func (itr CommitsRowItr) Close(*sql.Context) error {
   107  	return nil
   108  }