In the last section, we learned about the basic XORM framework and basic configuration information. In this lesson, we will continue to learn related knowledge and related operations.

Name mapping rule

Name mapping rules are mainly responsible for structure name to table name and structure field to table field name mapping. In the frame of the xorm by core. IMapper the implementers of the interface to manage, xorm built three IMapper implementation: core. SnakeMapper, core. SameMapper and core GonicMapper.

  • SnakeMapper: Supports conversions between structs for humped names and underlined names in table structures. This rule is the default Maper of XORM.

  • SameMapper: Mapping rules support structure names and corresponding table names, and structure field names and corresponding table field names the same naming;

  • GonicMapper: This mapping rule is similar to camel naming, but is more supportive for specific words, such as ID will be translated to ID instead of camel i_d.

The default name mapping rule is SnakeMapper. If developers need to change it, they can use the created database engine object to set it as follows:

engine.SetMapper(core.SameMapper{})Copy the code

In addition, you can set the table name and table field to different mapping rules:

engine.SetTableMapper(core.SameMapper{})
engine.SetColumnMapper(core.SnakeMapper{})Copy the code

Use Tag mapping rules

It would be ideal if all the naming was done according to IMapper’s mapping. However, if a table name or field name does not match the mapping rule, we need another mechanism to change it. Xorm provides several ways to do this:

  • If a structure has a member method of TableName() string, the return value of this method is the database TableName corresponding to the structure.

  • The engine.table () method can be used to change the name of the database Table corresponding to the struct, and xorm:”‘column_name'” can be used to change the name of the Column corresponding to the field in the sturct. The Column name is enclosed by two single quotation marks to prevent name collisions, because we can define more of the Column in the Tag. If the names do not conflict, single quotation marks may not be used.

Column attribute definition

We define attributes for Column in the Tag corresponding to field to set and qualify database table fields in our project. The definition method is basically the same as when we write SQL to define the table structure. As follows:

type User struct {
    Id   int64
    Name string  `xorm:"varchar(25) notnull unique 'usr_name'"`}Copy the code

Xorm has its own definition for data types. The specific Tag rules are as follows. Keywords in the Tag are case-insensitive:

name The name of the field corresponding to the current field
pk Primary Key
name The name of the field corresponding to the current field
pk Primary Key
autoincr Is it self-increasing
[not] null or notnull Whether it can be null
unique Is it unique
index Index or not

| extends | structure used in an anonymous member or not an anonymous member structure above | | – | this Field will not be for Field mapping | – > | Field will only be written to the database and not read from the database | | < – | Field will only be read from the database, Not written to the database | | created | Field will be automatically assigned to the current time when Insert | | updated | Field will automatically when the Insert or Update assignment to the current time | | does | The Field will be set to the current time when the Delete, and the current record does not Delete | | version | Field will be the default is 1, when insert each update automatically add 1 | | default 0 or default (0) | set a default value, Following the content if it is Varchar needs combined with single quotes | | json | | first said the content will be converted to json format

Field mapping rule

In addition to the above table name mapping rules and the use of tags to set the fields, the basic Go language structure data types also correspond to the fields in the database table. Specific data types correspond to the following rules:

Go language data type Types in XORM
implemented Conversion Text
int, int8, int16, int32, uint, uint8, uint16, uint32 Int
int64, uint64 BigInt
float32 Float
float64 Double
complex64, complex128 Varchar(64)
[]uint8 Blob
array, slice, map except []uint8 Text
bool Bool
string Varchar(255)
time.Time DateTime
cascade struct BigInt
struct Text
Others Text

Table basic operations

  • Create a table: CreateTables() with one or more empty Pointers to the corresponding Struct.

  • Check whether the table is empty: IsTableEmpty()

  • Check whether the table exists: IsTableExist()

  • DropTables: DropTables() takes one or more empty Pointers to the corresponding Struct or the name of the table.

Basic and statistical operations

Use Get, Find, Count, Rows, Iterate, as well as the conditional query Where.

We will give you detailed use of specific programming methods in the video and program, which is the focus of our actual combat project.