github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/mocks/mock.go (about)

     1  package mocks
     2  
     3  import (
     4  	"github.com/volatiletech/sqlboiler/v4/drivers"
     5  	"github.com/volatiletech/sqlboiler/v4/importers"
     6  	"github.com/volatiletech/strmangle"
     7  )
     8  
     9  func init() {
    10  	drivers.RegisterFromInit("mock", &MockDriver{})
    11  }
    12  
    13  // MockDriver is a mock implementation of the bdb driver Interface
    14  type MockDriver struct{}
    15  
    16  // Templates returns the overriding templates for the driver
    17  func (m *MockDriver) Templates() (map[string]string, error) {
    18  	return nil, nil
    19  }
    20  
    21  // Imports return the set of imports that should be merged
    22  func (m *MockDriver) Imports() (importers.Collection, error) {
    23  	return importers.Collection{
    24  		BasedOnType: importers.Map{
    25  			"null.Int": {
    26  				ThirdParty: importers.List{`"github.com/volatiletech/null/v8"`},
    27  			},
    28  			"null.String": {
    29  				ThirdParty: importers.List{`"github.com/volatiletech/null/v8"`},
    30  			},
    31  			"null.Time": {
    32  				ThirdParty: importers.List{`"github.com/volatiletech/null/v8"`},
    33  			},
    34  			"null.Bytes": {
    35  				ThirdParty: importers.List{`"github.com/volatiletech/null/v8"`},
    36  			},
    37  
    38  			"time.Time": {
    39  				Standard: importers.List{`"time"`},
    40  			},
    41  		},
    42  	}, nil
    43  }
    44  
    45  // Assemble the DBInfo
    46  func (m *MockDriver) Assemble(config drivers.Config) (dbinfo *drivers.DBInfo, err error) {
    47  	dbinfo = &drivers.DBInfo{
    48  		Dialect: drivers.Dialect{
    49  			LQ: '"',
    50  			RQ: '"',
    51  
    52  			UseIndexPlaceholders: true,
    53  			UseLastInsertID:      false,
    54  			UseTopClause:         false,
    55  		},
    56  	}
    57  
    58  	defer func() {
    59  		if r := recover(); r != nil && err == nil {
    60  			dbinfo = nil
    61  			err = r.(error)
    62  		}
    63  	}()
    64  
    65  	schema := config.MustString(drivers.ConfigSchema)
    66  	whitelist, _ := config.StringSlice(drivers.ConfigWhitelist)
    67  	blacklist, _ := config.StringSlice(drivers.ConfigBlacklist)
    68  
    69  	dbinfo.Tables, err = drivers.TablesConcurrently(m, schema, whitelist, blacklist, 1)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	return dbinfo, err
    75  }
    76  
    77  // TableNames returns a list of mock table names
    78  func (m *MockDriver) TableNames(schema string, whitelist, blacklist []string) ([]string, error) {
    79  	if len(whitelist) > 0 {
    80  		return whitelist, nil
    81  	}
    82  	tables := []string{"pilots", "jets", "airports", "licenses", "hangars", "languages", "pilot_languages"}
    83  	return strmangle.SetComplement(tables, blacklist), nil
    84  }
    85  
    86  // Columns returns a list of mock columns
    87  func (m *MockDriver) Columns(schema, tableName string, whitelist, blacklist []string) ([]drivers.Column, error) {
    88  	return map[string][]drivers.Column{
    89  		"pilots": {
    90  			{Name: "id", Type: "int", DBType: "integer"},
    91  			{Name: "name", Type: "string", DBType: "character"},
    92  		},
    93  		"airports": {
    94  			{Name: "id", Type: "int", DBType: "integer"},
    95  			{Name: "size", Type: "null.Int", DBType: "integer", Nullable: true},
    96  		},
    97  		"jets": {
    98  			{Name: "id", Type: "int", DBType: "integer"},
    99  			{Name: "pilot_id", Type: "int", DBType: "integer", Nullable: true, Unique: true},
   100  			{Name: "airport_id", Type: "int", DBType: "integer"},
   101  			{Name: "name", Type: "string", DBType: "character", Nullable: false},
   102  			{Name: "color", Type: "null.String", DBType: "character", Nullable: true},
   103  			{Name: "uuid", Type: "string", DBType: "uuid", Nullable: true},
   104  			{Name: "identifier", Type: "string", DBType: "uuid", Nullable: false},
   105  			{Name: "cargo", Type: "[]byte", DBType: "bytea", Nullable: false},
   106  			{Name: "manifest", Type: "[]byte", DBType: "bytea", Nullable: true, Unique: true},
   107  		},
   108  		"licenses": {
   109  			{Name: "id", Type: "int", DBType: "integer"},
   110  			{Name: "pilot_id", Type: "int", DBType: "integer"},
   111  		},
   112  		"hangars": {
   113  			{Name: "id", Type: "int", DBType: "integer"},
   114  			{Name: "name", Type: "string", DBType: "character", Nullable: true, Unique: true},
   115  		},
   116  		"languages": {
   117  			{Name: "id", Type: "int", DBType: "integer"},
   118  			{Name: "language", Type: "string", DBType: "character", Nullable: false, Unique: true},
   119  		},
   120  		"pilot_languages": {
   121  			{Name: "pilot_id", Type: "int", DBType: "integer"},
   122  			{Name: "language_id", Type: "int", DBType: "integer"},
   123  		},
   124  	}[tableName], nil
   125  }
   126  
   127  // ForeignKeyInfo returns a list of mock foreignkeys
   128  func (m *MockDriver) ForeignKeyInfo(schema, tableName string) ([]drivers.ForeignKey, error) {
   129  	return map[string][]drivers.ForeignKey{
   130  		"jets": {
   131  			{Table: "jets", Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", ForeignColumnUnique: true},
   132  			{Table: "jets", Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id"},
   133  		},
   134  		"licenses": {
   135  			{Table: "licenses", Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
   136  		},
   137  		"pilot_languages": {
   138  			{Table: "pilot_languages", Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
   139  			{Table: "pilot_languages", Name: "jet_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id"},
   140  		},
   141  	}[tableName], nil
   142  }
   143  
   144  // TranslateColumnType converts a column to its "null." form if it is nullable
   145  func (m *MockDriver) TranslateColumnType(c drivers.Column) drivers.Column {
   146  	if c.Nullable {
   147  		switch c.DBType {
   148  		case "bigint", "bigserial":
   149  			c.Type = "null.Int64"
   150  		case "integer", "serial":
   151  			c.Type = "null.Int"
   152  		case "smallint", "smallserial":
   153  			c.Type = "null.Int16"
   154  		case "decimal", "numeric", "double precision":
   155  			c.Type = "null.Float64"
   156  		case `"char"`:
   157  			c.Type = "null.Byte"
   158  		case "bytea":
   159  			c.Type = "null.Bytes"
   160  		case "boolean":
   161  			c.Type = "null.Bool"
   162  		case "date", "time", "timestamp without time zone", "timestamp with time zone":
   163  			c.Type = "null.Time"
   164  		default:
   165  			c.Type = "null.String"
   166  		}
   167  	} else {
   168  		switch c.DBType {
   169  		case "bigint", "bigserial":
   170  			c.Type = "int64"
   171  		case "integer", "serial":
   172  			c.Type = "int"
   173  		case "smallint", "smallserial":
   174  			c.Type = "int16"
   175  		case "decimal", "numeric", "double precision":
   176  			c.Type = "float64"
   177  		case `"char"`:
   178  			c.Type = "types.Byte"
   179  		case "bytea":
   180  			c.Type = "[]byte"
   181  		case "boolean":
   182  			c.Type = "bool"
   183  		case "date", "time", "timestamp without time zone", "timestamp with time zone":
   184  			c.Type = "time.Time"
   185  		default:
   186  			c.Type = "string"
   187  		}
   188  	}
   189  
   190  	return c
   191  }
   192  
   193  // PrimaryKeyInfo returns mock primary key info for the passed in table name
   194  func (m *MockDriver) PrimaryKeyInfo(schema, tableName string) (*drivers.PrimaryKey, error) {
   195  	return map[string]*drivers.PrimaryKey{
   196  		"pilots": {
   197  			Name:    "pilot_id_pkey",
   198  			Columns: []string{"id"},
   199  		},
   200  		"airports": {
   201  			Name:    "airport_id_pkey",
   202  			Columns: []string{"id"},
   203  		},
   204  		"jets": {
   205  			Name:    "jet_id_pkey",
   206  			Columns: []string{"id"},
   207  		},
   208  		"licenses": {
   209  			Name:    "license_id_pkey",
   210  			Columns: []string{"id"},
   211  		},
   212  		"hangars": {
   213  			Name:    "hangar_id_pkey",
   214  			Columns: []string{"id"},
   215  		},
   216  		"languages": {
   217  			Name:    "language_id_pkey",
   218  			Columns: []string{"id"},
   219  		},
   220  		"pilot_languages": {
   221  			Name:    "pilot_languages_pkey",
   222  			Columns: []string{"pilot_id", "language_id"},
   223  		},
   224  	}[tableName], nil
   225  }
   226  
   227  // UseLastInsertID returns a database mock LastInsertID compatibility flag
   228  func (m *MockDriver) UseLastInsertID() bool { return false }
   229  
   230  // UseTopClause returns a database mock SQL TOP clause compatibility flag
   231  func (m *MockDriver) UseTopClause() bool { return false }
   232  
   233  // Open mimics a database open call and returns nil for no error
   234  func (m *MockDriver) Open() error { return nil }
   235  
   236  // Close mimics a database close call
   237  func (m *MockDriver) Close() {}
   238  
   239  // RightQuote is the quoting character for the right side of the identifier
   240  func (m *MockDriver) RightQuote() byte {
   241  	return '"'
   242  }
   243  
   244  // LeftQuote is the quoting character for the left side of the identifier
   245  func (m *MockDriver) LeftQuote() byte {
   246  	return '"'
   247  }
   248  
   249  // UseIndexPlaceholders returns true to indicate fake support of indexed placeholders
   250  func (m *MockDriver) UseIndexPlaceholders() bool {
   251  	return false
   252  }