mariadb

package
v0.0.0-...-2172325 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 9 Imported by: 0

README

1. MariaDB

Refer: https://hub.docker.com/_/mariadb

1.1. docker

docker pull mariadb
docker network create hfcms-mariadb
docker run --detach \
--publish 3306:3306 \
--env MARIADB_ROOT_PASSWORD=my-secret-pw \
--network hfcms-mariadb \
--name hfcms-mariadb \
mariadb:latest

docker exec -it hfcms-mariadb mysql -u root -pmy-secret-pw

MARIADB_ROOT_PASSWORD=my-secret-pw

1.2 mariadb

  1. Change root password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '[newpassword]';
  1. Creating database dumps
$ docker exec hfcms-mariadb sh -c 'exec mysqldump -uroot -p"$MARIADB_ROOT_PASSWORD" hfcms_articles' > ./hfcms_articles.sql
  1. Restoring data from dump files
$ docker exec -i hfcms-mariadb sh -c 'exec mysql -uroot -p"$MARIADB_ROOT_PASSWORD" hfcms_articles' < ./hfcms_articles.sql
  1. Create tables:
    Database
CREATE database hfcms_articles;
CREATE USER 'hfcms_articles_user'@localhost IDENTIFIED BY 'hfcms_articles_user_passwd';
GRANT ALL PRIVILEGES ON hfcms_articles.* TO 'hfcms_articles_user'@localhost;
FLUSH PRIVILEGES;
USE hfcms_articles;

Articles

DROP TABLE articles;
CREATE TABLE articles (id VARCHAR(24) NOT NULL, title VARCHAR(255), content TEXT(65535), category_id INT(10), user_id INT(10), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
describe articles;
+-------------+--------------+------+-----+---------------------+-------------------------------+
| Field       | Type         | Null | Key | Default             | Extra                         |
+-------------+--------------+------+-----+---------------------+-------------------------------+
| id          | varchar(24)  | NO   | PRI | NULL                |                               |
| title       | varchar(255) | YES  |     | NULL                |                               |
| content     | mediumtext   | YES  |     | NULL                |                               |
| category_id | int(10)      | YES  |     | NULL                |                               |
| user_id     | int(10)      | YES  |     | NULL                |                               |
| update_time | timestamp    | NO   |     | current_timestamp() | on update current_timestamp() |
+-------------+--------------+------+-----+---------------------+-------------------------------+

Categories

DROP TABLE categories;
CREATE TABLE categories (id INT(10) NOT NULL AUTO_INCREMENT, name VARCHAR(255), code VARCHAR(10), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
describe categories;
+-------------+--------------+------+-----+---------------------+-------------------------------+
| Field       | Type         | Null | Key | Default             | Extra                         |
+-------------+--------------+------+-----+---------------------+-------------------------------+
| id          | int(10)      | NO   | PRI | NULL                | auto_increment                |
| name        | varchar(255) | YES  |     | NULL                |                               |
| code        | varchar(10)  | YES  |     | NULL                |                               |
| update_time | timestamp    | NO   |     | current_timestamp() | on update current_timestamp() |
+-------------+--------------+------+-----+---------------------+-------------------------------+

Tags

DROP TABLE tags;
CREATE TABLE tags (id INT(10) NOT NULL AUTO_INCREMENT, name VARCHAR(255), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
describe tags;
+-------------+--------------+------+-----+---------------------+-------------------------------+
| Field       | Type         | Null | Key | Default             | Extra                         |
+-------------+--------------+------+-----+---------------------+-------------------------------+
| id          | int(10)      | NO   | PRI | NULL                | auto_increment                |
| name        | varchar(255) | YES  |     | NULL                |                               |
| update_time | timestamp    | NO   |     | current_timestamp() | on update current_timestamp() |
+-------------+--------------+------+-----+---------------------+-------------------------------+

Attributes

DROP TABLE attributes;
CREATE TABLE attributes (id INT(16) NOT NULL AUTO_INCREMENT, path VARCHAR(255), description VARCHAR(255), user_id INT(10), article_id VARCHAR(24), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
describe attributes;
+-------------+--------------+------+-----+---------------------+-------------------------------+
| Field       | Type         | Null | Key | Default             | Extra                         |
+-------------+--------------+------+-----+---------------------+-------------------------------+
| id          | int(16)      | NO   | PRI | NULL                | auto_increment                |
| path        | varchar(255) | YES  |     | NULL                |                               |
| description | varchar(255) | YES  |     | NULL                |                               |
| user_id     | int(10)      | YES  |     | NULL                |                               |
| article_id  | varchar(24)  | YES  |     | NULL                |                               |
| update_time | timestamp    | NO   |     | current_timestamp() | on update current_timestamp() |
+-------------+--------------+------+-----+---------------------+-------------------------------+

ArticleTags

DROP TABLE article_tags;
CREATE TABLE article_tags (id INT(16) NOT NULL AUTO_INCREMENT, article_id VARCHAR(24), tag_id INT(10), UNIQUE KEY (id));
describe article_tags;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(16)     | NO   | PRI | NULL    | auto_increment |
| article_id | varchar(24) | YES  |     | NULL    |                |
| tag_id     | int(10)     | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

ArticleAttributes

DROP TABLE article_attributes;
CREATE TABLE article_attributes (id INT(16) NOT NULL AUTO_INCREMENT, article_id VARCHAR(24), attribute_id INT(16), UNIQUE KEY (id));
describe article_attributes;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(16)     | NO   | PRI | NULL    | auto_increment |
| article_id   | varchar(24) | YES  |     | NULL    |                |
| attribute_id | int(16)     | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

SQL All in one

CREATE database hfcms_articles;
CREATE USER 'hfcms_articles_user'@'%' IDENTIFIED BY 'hfcms_articles_user_passwd';
GRANT ALL PRIVILEGES ON hfcms_articles.* TO 'hfcms_articles_user'@'%';
FLUSH PRIVILEGES;
USE hfcms_articles;
CREATE TABLE articles (id VARCHAR(24) NOT NULL, title VARCHAR(255), content TEXT(65535), category_id INT(10), user_id INT(10), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
CREATE TABLE categories (id INT(10) NOT NULL AUTO_INCREMENT, name VARCHAR(255), code VARCHAR(10), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
CREATE TABLE tags (id INT(10) NOT NULL AUTO_INCREMENT, name VARCHAR(255), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
CREATE TABLE attributes (id INT(16) NOT NULL AUTO_INCREMENT, path VARCHAR(255), description VARCHAR(255), user_id INT(10), article_id VARCHAR(24), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY (id));
CREATE TABLE article_tags (id INT(16) NOT NULL AUTO_INCREMENT, article_id VARCHAR(24), tag_id INT(10), UNIQUE KEY (id));
CREATE TABLE article_attributes (id INT(16) NOT NULL AUTO_INCREMENT, article_id VARCHAR(24), attribute_id INT(16), UNIQUE KEY (id));

Validate

select host,
       user as username,
       password,
       password_expired
from mysql.user
order by user;
SHOW GRANTS FOR hfcms_articles_user;
describe articles;
describe categories;
describe tags;
describe attributes;
describe article_tags;
describe article_attributes;

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 Article

type Article struct {
	Id, Name, Title, Content string
	UserId, CategoryId       int
	UpdateTime               time.Time
}

type ArticleAttribute

type ArticleAttribute struct {
	Id, AttributeId int
	ArticleId       string
}

type ArticleAttributeQuery

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

func (*ArticleAttributeQuery) All

func (*ArticleAttributeQuery) First

func (*ArticleAttributeQuery) Limit

func (aaq *ArticleAttributeQuery) Limit(limit int) *ArticleAttributeQuery

func (*ArticleAttributeQuery) Offset

func (aaq *ArticleAttributeQuery) Offset(offset int) *ArticleAttributeQuery

func (*ArticleAttributeQuery) Order

func (aaq *ArticleAttributeQuery) Order(condition string) *ArticleAttributeQuery

func (*ArticleAttributeQuery) Where

func (aaq *ArticleAttributeQuery) Where(ps ...[4]string) *ArticleAttributeQuery

ps: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]}

type ArticleAttributes

type ArticleAttributes struct {
	Collection []*ArticleAttribute
}

type ArticleQuery

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

func (*ArticleQuery) All

func (aq *ArticleQuery) All(ctx context.Context) (*Articles, error)

func (*ArticleQuery) First

func (aq *ArticleQuery) First(ctx context.Context) (*Article, error)

func (*ArticleQuery) Limit

func (aq *ArticleQuery) Limit(limit int) *ArticleQuery

func (*ArticleQuery) Offset

func (aq *ArticleQuery) Offset(offset int) *ArticleQuery

func (*ArticleQuery) Order

func (aq *ArticleQuery) Order(condition string) *ArticleQuery

func (*ArticleQuery) Where

func (aq *ArticleQuery) Where(cs ...[4]string) *ArticleQuery

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 ArticleTag

type ArticleTag struct {
	Id, TagId int
	ArticleId string
}

type ArticleTagQuery

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

func (*ArticleTagQuery) All

func (atq *ArticleTagQuery) All(ctx context.Context) (*ArticleTags, error)

func (*ArticleTagQuery) First

func (atq *ArticleTagQuery) First(ctx context.Context) (*ArticleTag, error)

func (*ArticleTagQuery) Limit

func (atq *ArticleTagQuery) Limit(limit int) *ArticleTagQuery

func (*ArticleTagQuery) Offset

func (atq *ArticleTagQuery) Offset(offset int) *ArticleTagQuery

func (*ArticleTagQuery) Order

func (atq *ArticleTagQuery) Order(condition string) *ArticleTagQuery

func (*ArticleTagQuery) Where

func (atq *ArticleTagQuery) Where(ps ...[4]string) *ArticleTagQuery

ps: {["name", "=", "jack", "and"], ["title", "like", "anything", ""]}

type ArticleTags

type ArticleTags struct {
	Collection []*ArticleTag
}

type Articles

type Articles struct {
	Collection []*Article
}

type Attribute

type Attribute struct {
	Id, UserId                   int
	Path, Description, ArticleId string
	UpdateTime                   time.Time
}

type AttributeQuery

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

func (*AttributeQuery) All

func (aq *AttributeQuery) All(ctx context.Context) (*Attributes, error)

func (*AttributeQuery) First

func (aq *AttributeQuery) First(ctx context.Context) (*Attribute, error)

func (*AttributeQuery) Limit

func (aq *AttributeQuery) Limit(limit int) *AttributeQuery

func (*AttributeQuery) Offset

func (aq *AttributeQuery) Offset(offset int) *AttributeQuery

func (*AttributeQuery) Order

func (aq *AttributeQuery) Order(condition string) *AttributeQuery

func (*AttributeQuery) Where

func (aq *AttributeQuery) Where(cs ...[4]string) *AttributeQuery

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 Attributes

type Attributes struct {
	Collection []*Attribute
}

type Categories

type Categories struct {
	Collection []*Category
}

type Category

type Category struct {
	Id         int
	Name, Code string
	UpdateTime time.Time
}

type CategoryQuery

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

func (*CategoryQuery) All

func (cq *CategoryQuery) All(ctx context.Context) (*Categories, error)

func (*CategoryQuery) First

func (cq *CategoryQuery) First(ctx context.Context) (*Category, error)

func (*CategoryQuery) Limit

func (cq *CategoryQuery) Limit(limit int) *CategoryQuery

func (*CategoryQuery) Offset

func (cq *CategoryQuery) Offset(offset int) *CategoryQuery

func (*CategoryQuery) Order

func (cq *CategoryQuery) Order(condition string) *CategoryQuery

func (*CategoryQuery) Where

func (cq *CategoryQuery) Where(cs ...[4]string) *CategoryQuery

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 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) DeleteArticle

func (dc *DatabaseClient) DeleteArticle(ctx context.Context, id string) error

func (*DatabaseClient) DeleteArticleAttribute

func (dc *DatabaseClient) DeleteArticleAttribute(ctx context.Context, id string) error

func (*DatabaseClient) DeleteArticleTag

func (dc *DatabaseClient) DeleteArticleTag(ctx context.Context, id string) error

func (*DatabaseClient) DeleteAttribute

func (dc *DatabaseClient) DeleteAttribute(ctx context.Context, id string) error

func (*DatabaseClient) DeleteCategory

func (dc *DatabaseClient) DeleteCategory(ctx context.Context, id string) error

func (*DatabaseClient) DeleteTag

func (dc *DatabaseClient) DeleteTag(ctx context.Context, id string) error

func (*DatabaseClient) InsertArticle

func (dc *DatabaseClient) InsertArticle(ctx context.Context, article *Article) error

func (*DatabaseClient) InsertArticleAttribute

func (dc *DatabaseClient) InsertArticleAttribute(ctx context.Context, articleAttribute *ArticleAttribute) error

func (*DatabaseClient) InsertArticleTag

func (dc *DatabaseClient) InsertArticleTag(ctx context.Context, articleTag *ArticleTag) error

func (*DatabaseClient) InsertAttribute

func (dc *DatabaseClient) InsertAttribute(ctx context.Context,
	attribute *Attribute) error

func (*DatabaseClient) InsertCategory

func (dc *DatabaseClient) InsertCategory(ctx context.Context, category *Category) error

func (*DatabaseClient) InsertTag

func (dc *DatabaseClient) InsertTag(ctx context.Context, tag *Tag) error

func (*DatabaseClient) QueryArticle

func (dc *DatabaseClient) QueryArticle() *ArticleQuery

func (*DatabaseClient) QueryArticleAttribute

func (dc *DatabaseClient) QueryArticleAttribute() *ArticleAttributeQuery

func (*DatabaseClient) QueryArticleTag

func (dc *DatabaseClient) QueryArticleTag() *ArticleTagQuery

func (*DatabaseClient) QueryAttribute

func (dc *DatabaseClient) QueryAttribute() *AttributeQuery

func (*DatabaseClient) QueryCategory

func (dc *DatabaseClient) QueryCategory() *CategoryQuery

func (*DatabaseClient) QueryTag

func (dc *DatabaseClient) QueryTag() *TagQuery

func (*DatabaseClient) UpdateArticle

func (dc *DatabaseClient) UpdateArticle(ctx context.Context, article *Article) error

func (*DatabaseClient) UpdateArticleAttribute

func (dc *DatabaseClient) UpdateArticleAttribute(ctx context.Context, ArticleAttribute *ArticleAttribute) error

func (*DatabaseClient) UpdateArticleTag

func (dc *DatabaseClient) UpdateArticleTag(ctx context.Context, ArticleTag *ArticleTag) error

func (*DatabaseClient) UpdateAttribute

func (dc *DatabaseClient) UpdateAttribute(ctx context.Context,
	attribute *Attribute) error

func (*DatabaseClient) UpdateCategory

func (dc *DatabaseClient) UpdateCategory(ctx context.Context, category *Category) error

func (*DatabaseClient) UpdateTag

func (dc *DatabaseClient) UpdateTag(ctx context.Context, tag *Tag) error

type Tag

type Tag struct {
	Id         int
	Name       string
	UpdateTime time.Time
}

type TagQuery

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

func (*TagQuery) All

func (tq *TagQuery) All(ctx context.Context) (*Tags, error)

func (*TagQuery) First

func (tq *TagQuery) First(ctx context.Context) (*Tag, error)

func (*TagQuery) Limit

func (tq *TagQuery) Limit(limit int) *TagQuery

func (*TagQuery) Offset

func (tq *TagQuery) Offset(offset int) *TagQuery

func (*TagQuery) Order

func (tq *TagQuery) Order(condition string) *TagQuery

func (*TagQuery) Where

func (tq *TagQuery) Where(cs ...[4]string) *TagQuery

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 Tags

type Tags struct {
	Collection []*Tag
}

Jump to

Keyboard shortcuts

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