Documentation ¶
Overview ¶
Package dbcommon provides common database functionality
Index ¶
- Constants
- Variables
- func EnumScan(src interface{}) (uint8, error)
- func EnumValue(enum EnumInter) (driver.Value, error)
- func GetGormLogger(zapLogger *log.ZapEventLogger) gormLogger.Interface
- func GetModelName(db *gorm.DB, model interface{}) (string, error)
- func GetTestConnString() string
- func MysqlRealEscapeString(value string) stringdeprecated
- type DBType
- type Enum
- type EnumInter
- type Namer
Examples ¶
Constants ¶
const ( // EnableMysqlTestVar is the environment variable to enable mysql tests. EnableMysqlTestVar = "ENABLE_MYSQL_TEST" // MysqlDatabaseVar is the environment variable for the mysql database dsn. MysqlDatabaseVar = "MYSQL_DATABASE" // MysqlUserVar is the environment variable for the mysql user. MysqlUserVar = "MYSQL_USER" // MysqlPasswordVar is the environment variable for the mysql password. MysqlPasswordVar = "MYSQL_PASSWORD" // MysqlHostVar is the environment variable for the mysql host. MysqlHostVar = "MYSQL_HOST" // MysqlPortVar is the environment variable for the mysql port. MysqlPortVar = "MYSQL_PORT" )
const EnumDataType = "integer"
EnumDataType is exported here to be passed as GormDataType. TODO: support string types.
Variables ¶
var AllDBTypes []DBType
AllDBTypes is a list of all contract types. Since we use stringer and this is a testing library, instead of manually copying all these out we pull the names out of stringer. In order to make sure stringer is updated, we panic on any method called where the index is higher than the stringer array length.
Functions ¶
func EnumScan ¶ added in v0.0.37
EnumScan converts the enum to a value. the returned uint 8 should be used. Note this should only be used for a non-nullable enum. TODO: consider using generics to make this more type safe.
func GetGormLogger ¶
func GetGormLogger(zapLogger *log.ZapEventLogger) gormLogger.Interface
GetGormLogger gets a gorm logger at the correct level TODO investigate https://github.com/moul/zapgorm, we want to use the same write group. TODO: otel logging.
func GetModelName ¶ added in v0.0.52
GetModelName returns the name of the model.
func GetTestConnString ¶ added in v0.0.58
func GetTestConnString() string
GetTestConnString returns the connection string for the mysql test database. this is derived from environment variables. TODO: test this in ci.
func MysqlRealEscapeString
deprecated
added in
v0.0.80
MysqlRealEscapeString mimics the behavior of the PHP function mysql_real_escape_string, which is used to escape special characters in a string before sending it to a MySQL database. This can help prevent SQL injection attacks.
The PHP function itself has a checkered history. While its primary intention was to make strings safe for MySQL, relying solely on it is considered bad practice. This is because the best way to prevent SQL injection is by using prepared statements or parameterized queries, which don't require manual string escaping. Additionally, mysql_real_escape_string in PHP relies on the current character set, and without the proper character set, it can fail to protect against SQL injection.
It's worth noting that using this Go implementation is, in a way, an acknowledgement of an anti-pattern from PHP. While it can escape some characters and may prevent some naive SQL injection attempts, it is not a substitute for prepared statements or parameterized queries. This implementation is provided as a convenience for some legacy code and should not be considered a robust security solution. Furthermore, it is only used on blockchain data which is all public. Do not use this in any situation where you are dealing w/ private data.
Deprecated: Use prepared statements or parameterized queries instead going forward.
Types ¶
type DBType ¶
type DBType int
DBType is the database driver to use.
func DBTypeFromString ¶
DBTypeFromString parses a database type from a string.
type Enum ¶ added in v0.0.37
Enum is a type used to define interfaces db enums must conform to. it is kept here so it can be used throughout the app.
Example ¶
ExampleEnum demonstrates example use of the enum interface. this implementation can be confusing, so there's an example below.
res, err := RunEnumExample(context.Background(), fmt.Sprintf("%s/sql.db", os.TempDir())) if err != nil { panic(err) } for _, res := range res { fmt.Printf("got result %s \n", res.Fruit.String()) }
Output:
type EnumInter ¶ added in v0.0.37
type EnumInter interface {
Int() uint8
}
EnumInter ensures an enum has a type to access an integer.
type Namer ¶
type Namer struct {
// contains filtered or unexported fields
}
Namer is used to pull consistent names for fields from models. This prevents inconsistency by panicing on breaking changes. It also allows us to avoid using xModelFieldName, yModelFieldName by taking advantage of introspection.
func (Namer) GetConsistentName ¶
GetConsistentName makes sure a `ColumnName` has a common column tag against all models we use this so we don't have to define bridgeRedeemModelFieldName, originChainFieldName, etc. panic's on an inconsistency. Note: this should only be called from init.
after this is called, you can safely call getGormFieldName(AnyModelWithField, fieldName) if all fields cannot make this guarantee, they should be separated out to avoid developer confusion.
this returns the result of getGormFieldName for a valid model. It also panics if no model is valid todo consider generating getters for all field names.
Example ¶
namer := dbcommon.NewNamer(getTestModels()) fmt.Println(namer.GetConsistentName("CreatedAt"))
Output: created_at