mysql

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 9 Imported by: 0

README

MySQL

Reference: https://hub.docker.com/_/mysql

docker run -p 3306:3306 --name=voter -e MYSQL_ROOT_PASSWORD='rootpassword' -d mysql/mysql-server:latest
docker exec -it voter mysql -uroot -prootpassword

Backup

Method 1: use mysqldump

$ mysqldump -u root -p voter > backup_voter.sql

Method 2: docker

$ docker exec docker_img_name sh -c 'exec mysqldump -uroot -p"password" database_name' > ./backup_voter.sql

SQL

Database
mysql> drop database voter;
mysql> create database voter;
mysql> create user 'voter_db_user'@'%' identified by 'voter_db_pwd';
mysql> grant all privileges on voter.* to 'voter_db_user'@'%';
mysql> flush privileges;
mysql> show databases;
mysql> use voter;
Users
mysql> CREATE TABLE IF NOT EXISTS users (
  id int(10) NOT NULL AUTO_INCREMENT,
  username VARCHAR(255),
  password VARCHAR(255),
  realname VARCHAR(255),
  nickname VARCHAR(255),
  user_ip INT(4) UNSIGNED UNIQUE,
  avatar_url VARCHAR(255),
  phone VARCHAR(11),
  state TINYINT(1) DEFAULT 0 COMMENT 'user state: 0=normal, 1=disable',
  deleted TINYINT(1) DEFAULT 0 COMMENT 'soft deleted: 0=undelete,1=deleted',
  create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);

mysql> show tables;
+-----------------+
| Tables_in_voter |
+-----------------+
| users           |
+-----------------+

mysql> describe users;
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
| Field       | Type         | Null | Key | Default           | Extra                                         |
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
| id          | int          | NO   | PRI | NULL              | auto_increment                                |
| username    | varchar(255) | YES  |     | NULL              |                                               |
| password    | varchar(255) | YES  |     | NULL              |                                               |
| realname    | varchar(255) | YES  |     | NULL              |                                               |
| nickname    | varchar(255) | YES  |     | NULL              |                                               |
| user_ip     | int unsigned | YES  | UNI | NULL              |                                               |
| avatar_url  | varchar(255) | YES  |     | NULL              |                                               |
| phone       | varchar(11)  | YES  |     | NULL              |                                               |
| state       | tinyint(1)   | YES  |     | 0                 |                                               |
| deleted     | tinyint(1)   | YES  |     | 0                 |                                               |
| create_time | timestamp    | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             |
| update_time | timestamp    | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
12 rows in set (0.01 sec)
Votes
mysql> CREATE TABLE IF NOT EXISTS votes (
  id int(10) NOT NULL AUTO_INCREMENT,
  title VARCHAR(255),
  is_radio TINYINT(1) DEFAULT 0 COMMENT 'is radio: 1=no, 0=yes(default)',
  a varchar(255),
  b varchar(255),
  c varchar(255),
  d varchar(255),
  e varchar(255),
  f varchar(255),
  g varchar(255),
  h varchar(255),
  has_txt_field TINYINT(1) DEFAULT 0 COMMENT 'has txt field: 1=yes, 0=no(default)',
  create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);

mysql> show tables;
+-----------------+
| Tables_in_voter |
+-----------------+
| users           |
| votes           |
+-----------------+
2 rows in set (0.01 sec)

mysql> desc votes;
+---------------+--------------+------+-----+-------------------+-----------------------------------------------+
| Field         | Type         | Null | Key | Default           | Extra                                         |
+---------------+--------------+------+-----+-------------------+-----------------------------------------------+
| id            | int          | NO   | PRI | NULL              | auto_increment                                |
| title         | varchar(255) | YES  |     | NULL              |                                               |
| is_radio      | tinyint(1)   | YES  |     | 0                 |                                               |
| a             | varchar(255) | YES  |     | NULL              |                                               |
| b             | varchar(255) | YES  |     | NULL              |                                               |
| c             | varchar(255) | YES  |     | NULL              |                                               |
| d             | varchar(255) | YES  |     | NULL              |                                               |
| e             | varchar(255) | YES  |     | NULL              |                                               |
| f             | varchar(255) | YES  |     | NULL              |                                               |
| g             | varchar(255) | YES  |     | NULL              |                                               |
| h             | varchar(255) | YES  |     | NULL              |                                               |
| has_txt_field | tinyint(1)   | YES  |     | 0                 |                                               |
| create_time   | timestamp    | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             |
| update_time   | timestamp    | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+---------------+--------------+------+-----+-------------------+-----------------------------------------------+
14 rows in set (0.01 sec)
Posts
mysql> CREATE TABLE IF NOT EXISTS posts (
  id int(10) NOT NULL AUTO_INCREMENT,
  title VARCHAR(255),
  is_closed TINYINT(1) DEFAULT 0 COMMENT 'is closed: 0=no(default), 1=yes',
  detail VARCHAR(255),
  create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);

mysql> show tables;
+-----------------+
| Tables_in_voter |
+-----------------+
| posts           |
| users           |
| votes           |
+-----------------+
3 rows in set (0.00 sec)

mysql> desc posts;
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
| Field       | Type         | Null | Key | Default           | Extra                                         |
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
| id          | int          | NO   | PRI | NULL              | auto_increment                                |
| title       | varchar(255) | YES  |     | NULL              |                                               |
| is_closed   | tinyint(1)   | YES  |     | 0                 |                                               |
| detail      | varchar(255) | YES  |     | NULL              |                                               |
| create_time | timestamp    | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             |
| update_time | timestamp    | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
6 rows in set (0.01 sec)
PostVotes

1 post with many votes

mysql> CREATE TABLE IF NOT EXISTS post_votes (
  id int(10) NOT NULL AUTO_INCREMENT,
  post_id int(10),
  vote_id int(10),
  PRIMARY KEY (id)
);

mysql> desc post_votes;
+---------+------+------+-----+---------+----------------+
| Field   | Type | Null | Key | Default | Extra          |
+---------+------+------+-----+---------+----------------+
| id      | int  | NO   | PRI | NULL    | auto_increment |
| post_id | int  | YES  |     | NULL    |                |
| vote_id | int  | YES  |     | NULL    |                |
+---------+------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
IpVotes

1 IP can vote 1 post many votes.

mysql> CREATE TABLE IF NOT EXISTS ip_votes (
  id int(10) NOT NULL AUTO_INCREMENT,
  ip int(4) UNSIGNED,
  vote_id int(10),
  opts VARCHAR(8),
  txt_field VARCHAR(255),
  post_id int(10),
  PRIMARY KEY (id)
);

mysql> desc ip_votes;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int          | NO   | PRI | NULL    | auto_increment |
| ip        | int          | YES  |     | NULL    |                |
| vote_id   | int          | YES  |     | NULL    |                |
| opts      | varchar(8)   | YES  |     | NULL    |                |
| txt_field | varchar(255) | YES  |     | NULL    |                |
| post_id   | int          | YES  |     | NULL    |                |
+-----------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
IpPosts

1 IP can only vote any post one time.

mysql> CREATE TABLE IF NOT EXISTS ip_posts (
  id int(10) NOT NULL AUTO_INCREMENT,
  ip int(4) UNSIGNED,
  post_id int(10),
  PRIMARY KEY (id)
);

mysql> desc ip_posts;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int          | NO   | PRI | NULL    | auto_increment |
| ip      | int unsigned | YES  |     | NULL    |                |
| post_id | int          | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.29 sec)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound   = errors.New("Item not found in table")
	ErrValidation = errors.New("Valid column error")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	DatabaseClient *DatabaseClient
	// contains filtered or unexported fields
}

func NewClient

func NewClient() (*Client, error)

type DatabaseClient

type DatabaseClient struct {
	// contains filtered or unexported fields
}

func (*DatabaseClient) DeleteIpPost

func (dc *DatabaseClient) DeleteIpPost(ctx context.Context, id int) error

func (*DatabaseClient) DeleteIpVote

func (dc *DatabaseClient) DeleteIpVote(ctx context.Context, id int) error

func (*DatabaseClient) DeletePost

func (dc *DatabaseClient) DeletePost(ctx context.Context, id int) error

DeletePost2 is true delete from database instead of DeletePost just update the row

func (*DatabaseClient) DeletePostVote

func (dc *DatabaseClient) DeletePostVote(ctx context.Context, id int) error

DeletePostVote2 is true delete from database instead of DeletePostVote just update the row

func (*DatabaseClient) DeleteUser

func (dc *DatabaseClient) DeleteUser(ctx context.Context, id int) error

DeleteUser is soft delete, not delete from database, but update deleted field to 1 DeleteUser is cooperate with All(ctx), that just return all rows except deleted is 1

func (*DatabaseClient) DeleteVote

func (dc *DatabaseClient) DeleteVote(ctx context.Context, id int) error

DeleteVote2 is true delete from database instead of DeleteVote just update the row

func (*DatabaseClient) InsertIpPost

func (dc *DatabaseClient) InsertIpPost(ctx context.Context, ipPost *IpPost) (int64, error)

func (*DatabaseClient) InsertIpVote

func (dc *DatabaseClient) InsertIpVote(ctx context.Context, ipVote *IpVote) (int64, error)

func (*DatabaseClient) InsertPost

func (dc *DatabaseClient) InsertPost(ctx context.Context, post *Post) (int64, error)

func (*DatabaseClient) InsertPostVote

func (dc *DatabaseClient) InsertPostVote(ctx context.Context, postVote *PostVote) error

func (*DatabaseClient) InsertUser

func (dc *DatabaseClient) InsertUser(ctx context.Context, user *User) error

func (*DatabaseClient) InsertVote

func (dc *DatabaseClient) InsertVote(ctx context.Context, vote *Vote) error

func (*DatabaseClient) PermanentlyDeleteUser

func (dc *DatabaseClient) PermanentlyDeleteUser(ctx context.Context, id int) error

PermanentlyDeleteUser is true delete from database instead of DeleteUser just update the row

func (*DatabaseClient) QueryIpPost

func (dc *DatabaseClient) QueryIpPost() *IpPostQuery

func (*DatabaseClient) QueryIpVote

func (dc *DatabaseClient) QueryIpVote() *IpVoteQuery

func (*DatabaseClient) QueryPost

func (dc *DatabaseClient) QueryPost() *PostQuery

func (*DatabaseClient) QueryPostVote

func (dc *DatabaseClient) QueryPostVote() *PostVoteQuery

func (*DatabaseClient) QueryUser

func (dc *DatabaseClient) QueryUser() *UserQuery

func (*DatabaseClient) QueryVote

func (dc *DatabaseClient) QueryVote() *VoteQuery

func (*DatabaseClient) UndeleteUser

func (dc *DatabaseClient) UndeleteUser(ctx context.Context, id int) error

func (*DatabaseClient) UpdateIpPost

func (dc *DatabaseClient) UpdateIpPost(ctx context.Context, ipPost *IpPost) error

func (*DatabaseClient) UpdateIpVote

func (dc *DatabaseClient) UpdateIpVote(ctx context.Context, ipVote *IpVote) error

func (*DatabaseClient) UpdatePost

func (dc *DatabaseClient) UpdatePost(ctx context.Context, post *Post) error

func (*DatabaseClient) UpdatePostVote

func (dc *DatabaseClient) UpdatePostVote(ctx context.Context, postVote *PostVote) error

func (*DatabaseClient) UpdateUser

func (dc *DatabaseClient) UpdateUser(ctx context.Context, user *User) error

func (*DatabaseClient) UpdateVote

func (dc *DatabaseClient) UpdateVote(ctx context.Context, vote *Vote) error

type IpPost

type IpPost struct {
	Id, PostId int
	Ip         string
}

type IpPostQuery

type IpPostQuery struct {
	// contains filtered or unexported fields
}

func (*IpPostQuery) All

func (ivq *IpPostQuery) All(ctx context.Context) (*IpPosts, error)

All will display all rows

func (*IpPostQuery) First

func (ivq *IpPostQuery) First(ctx context.Context) (*IpPost, error)

func (*IpPostQuery) Limit

func (ivq *IpPostQuery) Limit(limit int) *IpPostQuery

func (*IpPostQuery) Offset

func (ivq *IpPostQuery) Offset(offset int) *IpPostQuery

func (*IpPostQuery) Order

func (ivq *IpPostQuery) Order(condition string) *IpPostQuery

func (*IpPostQuery) Where

func (ivq *IpPostQuery) Where(cs ...[4]string) *IpPostQuery

cs: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]} the last `or` or `and` in clause will cut off after prepareQuery(). so, every clause need `or` or `and` for last element.

type IpPosts

type IpPosts struct {
	Collection []*IpPost
}

type IpVote

type IpVote struct {
	Id, VoteId, PostId int
	Ip, Opts, TxtField string
}

type IpVoteQuery

type IpVoteQuery struct {
	// contains filtered or unexported fields
}

func (*IpVoteQuery) All

func (ivq *IpVoteQuery) All(ctx context.Context) (*IpVotes, error)

All will display all rows

func (*IpVoteQuery) First

func (ivq *IpVoteQuery) First(ctx context.Context) (*IpVote, error)

func (*IpVoteQuery) Limit

func (ivq *IpVoteQuery) Limit(limit int) *IpVoteQuery

func (*IpVoteQuery) Offset

func (ivq *IpVoteQuery) Offset(offset int) *IpVoteQuery

func (*IpVoteQuery) Order

func (ivq *IpVoteQuery) Order(condition string) *IpVoteQuery

func (*IpVoteQuery) Where

func (ivq *IpVoteQuery) Where(cs ...[4]string) *IpVoteQuery

cs: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]} the last `or` or `and` in clause will cut off after prepareQuery(). so, every clause need `or` or `and` for last element.

type IpVotes

type IpVotes struct {
	Collection []*IpVote
}

type Post

type Post struct {
	Id, IsClosed           int
	Title, Detail          string
	CreateTime, UpdateTime time.Time
}

type PostQuery

type PostQuery struct {
	// contains filtered or unexported fields
}

func (*PostQuery) All

func (pq *PostQuery) All(ctx context.Context) (*Posts, error)

All will display all rows

func (*PostQuery) First

func (pq *PostQuery) First(ctx context.Context) (*Post, error)

func (*PostQuery) Limit

func (pq *PostQuery) Limit(limit int) *PostQuery

func (*PostQuery) Offset

func (pq *PostQuery) Offset(offset int) *PostQuery

func (*PostQuery) Order

func (pq *PostQuery) Order(condition string) *PostQuery

func (*PostQuery) Where

func (pq *PostQuery) Where(cs ...[4]string) *PostQuery

cs: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]} the last `or` or `and` in clause will cut off after prepareQuery(). so, every clause need `or` or `and` for last element.

type PostVote

type PostVote struct {
	Id, PostId, VoteId int
}

type PostVoteQuery

type PostVoteQuery struct {
	// contains filtered or unexported fields
}

func (*PostVoteQuery) All

func (pvq *PostVoteQuery) All(ctx context.Context) (*PostVotes, error)

All will display all rows

func (*PostVoteQuery) First

func (pvq *PostVoteQuery) First(ctx context.Context) (*PostVote, error)

func (*PostVoteQuery) Limit

func (pvq *PostVoteQuery) Limit(limit int) *PostVoteQuery

func (*PostVoteQuery) Offset

func (pvq *PostVoteQuery) Offset(offset int) *PostVoteQuery

func (*PostVoteQuery) Order

func (pvq *PostVoteQuery) Order(condition string) *PostVoteQuery

func (*PostVoteQuery) Where

func (pvq *PostVoteQuery) Where(cs ...[4]string) *PostVoteQuery

cs: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]} the last `or` or `and` in clause will cut off after prepareQuery(). so, every clause need `or` or `and` for last element.

type PostVotes

type PostVotes struct {
	Collection []*PostVote
}

type Posts

type Posts struct {
	Collection []*Post
}

type User

type User struct {
	Id, State, Deleted                                               int
	Username, Password, Realname, Nickname, AvatarUrl, Phone, UserIp string
	CreateTime, UpdateTime                                           time.Time
}

type UserQuery

type UserQuery struct {
	// contains filtered or unexported fields
}

func (*UserQuery) All

func (uq *UserQuery) All(ctx context.Context) (*Users, error)

All will display all lines that the deleted field value is 0

func (*UserQuery) All2

func (uq *UserQuery) All2(ctx context.Context) (*Users, error)

All2 will display all rows even if deleted field value is 1

func (*UserQuery) First

func (uq *UserQuery) First(ctx context.Context) (*User, error)

func (*UserQuery) Limit

func (uq *UserQuery) Limit(limit int) *UserQuery

func (*UserQuery) Offset

func (uq *UserQuery) Offset(offset int) *UserQuery

func (*UserQuery) Order

func (uq *UserQuery) Order(condition string) *UserQuery

func (*UserQuery) Where

func (uq *UserQuery) Where(cs ...[4]string) *UserQuery

cs: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]} the last `or` or `and` in clause will cut off after prepareQuery(). so, every clause need `or` or `and` for last element.

type Users

type Users struct {
	Collection []*User
}

type Vote

type Vote struct {
	Id, IsRadio, HasTxtField      int
	Title, A, B, C, D, E, F, G, H string
	CreateTime, UpdateTime        time.Time
}

type VoteQuery

type VoteQuery struct {
	// contains filtered or unexported fields
}

func (*VoteQuery) All

func (vq *VoteQuery) All(ctx context.Context) (*Votes, error)

All will display all rows

func (*VoteQuery) First

func (vq *VoteQuery) First(ctx context.Context) (*Vote, error)

func (*VoteQuery) Limit

func (vq *VoteQuery) Limit(limit int) *VoteQuery

func (*VoteQuery) Offset

func (vq *VoteQuery) Offset(offset int) *VoteQuery

func (*VoteQuery) Order

func (vq *VoteQuery) Order(condition string) *VoteQuery

func (*VoteQuery) Where

func (vq *VoteQuery) Where(cs ...[4]string) *VoteQuery

cs: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]} the last `or` or `and` in clause will cut off after prepareQuery(). so, every clause need `or` or `and` for last element.

type Votes

type Votes struct {
	Collection []*Vote
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL