Giter Club home page Giter Club logo

cmd's Introduction

中文

xorm tools

xorm tools is a set of tools for database operation.

Source Install

go get github.com/go-xorm/cmd/xorm

and you should install the depends below:

** For sqlite support, you need build via go build -tags sqlite3 because of this driver ask cgo.

Commands

All the commands below.

  • reverse reverse a db to codes
  • shell a general shell to operate all kinds of database
  • dump dump database all table struct's and data to standard output
  • source execute a sql from std in
  • driver list all supported drivers

Reverse

Reverse command is a tool to convert your database struct to all kinds languages of structs or classes. After you installed the tool, you can type

xorm help reverse

to get help

example:

cd $GOPATH/src/github.com/go-xorm/cmd/xorm

sqlite: xorm reverse sqite3 test.db templates/goxorm

mysql: xorm reverse mysql root:@/xorm_test?charset=utf8 templates/goxorm

mymysql: xorm reverse mymysql xorm_test2/root/ templates/goxorm

postgres: xorm reverse postgres "dbname=xorm_test sslmode=disable" templates/goxorm

mssql: xorm reverse mssql "server=test;user id=testid;password=testpwd;database=testdb" templates/goxorm

will generated go files in ./model directory

Template and Config

Now, xorm tool supports go and c++ two languages and have go, goxorm, c++ three of default templates. In template directory, we can put a config file to control how to generating.

lang=go
genJson=1

lang must be go or c++ now. genJson can be 1 or 0, if 1 then the struct will have json tag.

Shell

Shell command provides a tool to operate database. For example, you can create table, alter table, insert data, delete data and etc.

xorm shell sqlite3 test.db will connect to the sqlite3 database and you can type help to list all the shell commands.

Dump

Dump command provides a tool to dump all database structs and data as SQL to your standard output.

xorm dump sqlite3 test.db could dump sqlite3 database test.db to standard output. If you want to save to file, just type xorm dump sqlite3 test.db > test.sql.

Source

xorm source sqlite3 test.db < test.sql will execute sql file on the test.db.

Driver

List all supported drivers since default build will not include sqlite3.

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

cmd's People

Contributors

admpub avatar bjzhang03 avatar lunny avatar melotusme avatar mkideal avatar nashtsai avatar saraka avatar shonminh avatar uzimith avatar xiaobeicn avatar yang09701194 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmd's Issues

something wrong when using xorm reverse on unique index

error message

[Error] reverse.go:178 Unknown col is_default) WHERE in index userc_addr_userc_id_is_default_key of table userc_addr, columns [id userc_id name phone city area detail is_default create_time area_id city_id]

the table

create table userc(
  id bigserial primary key,
  phone varchar(11),
  create_time timestamp with time zone,
  UNIQUE(phone)
);


create table userc_addr(
  id bigserial primary key,
  userc_id bigint REFERENCES userc(id),
  name varchar(128),
  phone varchar(11),
  city varchar(128),
  area varchar(128),
  detail text,
  is_default boolean DEFAULT FALSE,
  create_time timestamp with time zone,
  area_id int,
  city_id int
);
CREATE UNIQUE index userc_addr_userc_id_is_default_key on userc_addr(userc_id, is_default) where is_default=true;
CREATE index userc_addr_userc_id_idx on userc_addr(userc_id);

expected ';', found 'IDENT' string (and 1 more errors)-package model

>xorm reverse mysql root:@(127.0.0.1:3306)/nongji?charset=utf8 .\templates\goxorm
2017/09/22 12:07:41 [Error] reverse.go:298 16:17: expected ';', found 'IDENT' string (and 1 more errors)-package model





type MyInformation29 struct {

        Iid     int `xorm:"not null pk autoincr MEDIUMINT(7)"`
        Id      int `xorm:"not null default 0 index INT(10)"`
        Content string `xorm:"MEDIUMTEXT"`
        YumiOperationType       int `xorm:"not null default 0 TINYINT(1)"`
        PlotsType       int `xorm:"not null default 0 TINYINT(1)"`
        DateRangeEnd    string `xorm:"not null default 0.00 DECIMAL(10,2)"`
        DateRangeStart  string `xorm:"not null default 0.00 DECIMAL(10,2)"`
        Expected price  string `xorm:"not null default 0.00 DECIMAL(10,2)"`
        Address string `xorm:"not null VARCHAR(250)"`
        Area    string `xorm:"not null default 0.00 DECIMAL(10,2)"`
        Source  string `xorm:"not null VARCHAR(250)"`

}

如上,reverse 报这个错误。
已经删除了 go-xorm 所有代码并重新 go get ,并重新将 go-xorm/cmd/templates 重新拷贝到当前目录。还是报这个错误。

我检查下代码看看能不能找到原因。

go build 报错

D:\gopath\src\github.com\go-xorm\cmd\xorm>go build

errors

compile: version "go1.10.3" does not match go tool version "go1.12.4"

internal/race

compile: version "go1.10.3" does not match go tool version "go1.12.4"

runtime/internal/atomic

flag provided but not defined: -gensymabis
usage: asm [options] file.s ...
Flags:
-D value
predefined symbol with optional simple value -D=identifier=value; can be
set multiple times
-I value
include directory; can be set multiple times
-S print assembly and machine code
-V print version and exit
-debug
dump instructions as they are parsed
-dynlink
support references to Go symbols defined in other shared libraries
-e no limit on number of errors reported
-o string
output file; default foo.o for /a/b/c/foo.s as first argument
-shared
generate code that can be linked into a shared library
-trimpath string
remove prefix from recorded source file paths

runtime/internal/sys

compile: version "go1.10.3" does not match go tool version "go1.12.4"

sync/atomic

flag provided but not defined: -gensymabis
usage: asm [options] file.s ...
Flags:
-D value
predefined symbol with optional simple value -D=identifier=value; can be
set multiple times
-I value
include directory; can be set multiple times
-S print assembly and machine code
-V print version and exit
-debug
dump instructions as they are parsed
-dynlink
support references to Go symbols defined in other shared libraries
-e no limit on number of errors reported
-o string
output file; default foo.o for /a/b/c/foo.s as first argument
-shared
generate code that can be linked into a shared library
-trimpath string
remove prefix from recorded source file paths

internal/cpu

flag provided but not defined: -gensymabis
usage: asm [options] file.s ...
Flags:
-D value
predefined symbol with optional simple value -D=identifier=value; can be
set multiple times
-I value
include directory; can be set multiple times

xorm reverse

这我就不是很懂了 ,我把密码改成这样就没事了 1701&CahE ->111

D:\coding\go_code\SmartMigrate\src\github.com\go-xorm\cmd\xorm>xorm reverse mysql zhyb:1701&CahE@(192.168.29.194:3306)/iebm_platform_v2_db_dev?charset=utf8 D:/coding/go_code/SmartMigrate/src/tmp
params error, please see xorm help reverse
'CahE@' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

D:\coding\go_code\SmartMigrate\src\github.com\go-xorm\cmd\xorm>xorm reverse mysql zhyb:11@(192.168.29.194:3306)/iebm_platform_v2_db_dev?charset=utf8 D:/coding/go_code/SmartMigrate/src/tmp
2018/12/22 12:15:20 [Error] reverse.go:178 Error 1045: Access denied for user 'zhyb'@'192.168.29.158' (using password: YES)

go get fail

λ go get -u github.com/go-xorm/cmd/xorm

github.com/go-xorm/cmd/xorm

E:\gopath\src\github.com\go-xorm\cmd\xorm\dump.go:45:26: cannot use "github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".LogLevel) as type "github.com/go-xorm/core".LogLevel in argument to engine.Logger().SetLevel
E:\gopath\src\github.com\go-xorm\cmd\xorm\reverse.go:228:34: cannot use tables (type []"github.com/go-xorm/core".Table) as type []"github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".Table in argument to langTmpl.GenImports
E:\gopath\src\github.com\go-xorm\cmd\xorm\reverse.go:236:18: cannot use table (type *"github.com/go-xorm/core".Table) as type *"github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".Table in append
E:\gopath\src\github.com\go-xorm\cmd\xorm\reverse.go:273:26: cannot use table (type *"github.com/go-xorm/core".Table) as type *"github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".Table in array or slice literal
E:\gopath\src\github.com\go-xorm\cmd\xorm\shell.go:60:26: cannot use "github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".LogLevel) as type "github.com/go-xorm/core".LogLevel in argument to engine.Logger().SetLevel
E:\gopath\src\github.com\go-xorm\cmd\xorm\source.go:45:26: cannot use "github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".LOG_UNKNOWN (type "github.com/go-xorm/cmd/xorm/vendor/github.com/go-xorm/core".LogLevel) as type "github.com/go-xorm/core".LogLevel in argument to engine.Logger().SetLevel

C:\Users\Administrator
λ go version
go version go1.9.3 windows/amd64

reverse example 有问题?

xorm reverse mysql root:@/xorm_test?charset=utf8 templates/goxorm
这个example跑不通?会报no matches found的错误
去掉charset=utf8竟然就跑通了……

Add example link in README.md - Template and Config

In README.md - Template and Config,

I suggest add example direction to https://github.com/go-xorm/cmd/tree/master/xorm/templates

such as the bold word in the end below,

because when I see this paragraph at the first time, I can't know what's the name of config file, what's the name of template file, etc.


Template and Config

Now, xorm tool supports go and c++ two languages and have go, goxorm, c++ three of default templates. In template directory, we can put a config file to control how to generating.

lang=go
genJson=1

lang must be go or c++ now. genJson can be 1 or 0, if 1 then the struct will have json tag.

Complete example see https://github.com/go-xorm/cmd/tree/master/xorm/templates

Unknown colType SMALLDATETIME - mssql

I'm using reverse function on MSSQL database, and get error on SMALLDATETIME

>xorm reverse mssql "server=XXX;user id=XXX;password=XXX;database=XXX" templates/goxorm

2018/05/04 14:14:38 [Error] reverse.go:178 Unknown colType SMALLDATETIME
 for XXTanble - XXColumn

Does xorm/cmd consider to support smalldatetime ? Thank you.


(I'm begginer at go and I think I am not able to make PR now like #27 )

是不是对 Sqlite 的支持并不是那么好

首先,官方文档中 sqlite: xorm reverse sqite3 test.db templates/goxorm 这个就提示没有 sqite3 驱动,应该是拼写问题。

我的SQL如下:

create table ta_user (
user_id              INTEGER      PRIMARY KEY AUTOINCREMENT,
user_name            VARCHAR(100),
user_org             VARCHAR(300),
user_dept            VARCHAR(300)
);

但是生成的内容如下:

package models

type TaUser struct {
	UserId   int    `xorm:"pk autoincr INTEGER"`
	UserName string `xorm:"VARCHAR(100)"`
	UserOrg  string `xorm:"VARCHAR(300)"`
	UserDept string `xorm:"VARCHAR(300)"`
	string   `xorm:""`
}

最后多了一个 string

If mysql table name is a number,there will be a error occured

如果mysql数据库表名是个数字 它将报错. 详情如下:
If mysql table name is a number,there will be a error occured. details below:

I run command : xorm reverse mysql sukhoi:123456@(127.0.0.1:3306)/crawling?charset=utf8 ./cmd/xorm/templates/go
return :
[Error] reverse.go:298 8:6: expected 'IDENT', found 'INT' 000001-package model

Unknown colType NCHAR - mssql

I'm using reverse function on MSSQL database, and get error on NCHAR

>xorm reverse mssql "server=XXX;user id=XXX;password=XXX;database=XXX" templates/goxorm

2018/05/04 14:58:29 [Error] reverse.go:178 Unknown colType NCHAR for XXTanble - XXColumn

Does xorm/cmd consider to support NCHAR ? Thank you.


(I'm begginer at go and I think I am not able to make PR now like #27 )

xorm reverse的"-m"参数用不了。

$ xorm help reverse
usage: xorm reverse [-m] driverName datasourceName tmplPath [generatedPath]

according database's tables and columns to generate codes for Go, C++ and etc.

-m                 Generated one go file for every table
driverName        Database driver name, now supported four: mysql mymysql sqlite3 postgres
datasourceName    Database connection uri, for detail infomation please visit driver's project page
tmplPath        Template dir for generated. the default templates dir has provide 1 template
generatedPath    This parameter is optional, if blank, the default value is model, then will
                generated all codes in model dir

看了一下源码,应该是"-s"这个参数才对。

form 支持

是否有生成像json tag那样的 from tag?

postgresql数据库的josnb是空类型,映射string后有bug

postgresql数据库的josnb字段是允许为空
映射string后,插入数据时,字段不赋值,插入式给的是'' 不是nil 导致插入不进去,报错

模型
type Test struct { Id int xorm:"pk autoincr INT"Name stringxorm:"JSONB" }

访问数据库代码
test := model.Test{Id: 2} id, err := db.Insert(&test)

出错信息
[xorm] [info] 2017/07/27 14:28:04.194950 [SQL] INSERT INTO "test" ("id","name") VALUES ($1, $2) RETURNING "id" []interface {}{2, ""} pq: json类型使用了无效的输入语法

name是个jsonb 允许为空,会出错,插""是不行的,求更新!

dump 命令导出mysql的数据不正确

表结构:
CREATE TABLE IF NOT EXISTS cos_member_profile (uid INT(10) PRIMARY KEY AUTO_INCREMENT NOT NULL, nickname VARCHAR(30) NOT NULL DEFAULT '', sex TINYINT(3) NOT NULL DEFAULT 0, birthday DATE NOT NULL DEFAULT 0000-00-00, qq VARCHAR(15) NOT NULL DEFAULT '', score INT(10) NOT NULL DEFAULT 0, credit INT(10) NOT NULL DEFAULT 0, balance DECIMAL(20) NOT NULL DEFAULT 0.000, frozen_funds DECIMAL(20) NOT NULL DEFAULT 0.000, timezone INT(3) NOT NULL DEFAULT 0, language VARCHAR(30) NOT NULL DEFAULT '', currency VARCHAR(30) NOT NULL DEFAULT '', login INT(10) NOT NULL DEFAULT 0, reg_ip BIGINT(20) NOT NULL DEFAULT 0, reg_time INT(10) NOT NULL DEFAULT 0, last_login_ip BIGINT(20) NOT NULL DEFAULT 0, last_login_time INT(10) NOT NULL DEFAULT 0, status TINYINT(4) NOT NULL DEFAULT 0) DEFAULT CHARSET utf8;
导出的数据:
INSERT INTO cos_member_profile (uid, nickname, sex, birthday, qq, score, credit, balance, frozen_funds, timezone, language, currency, login, reg_ip, reg_time, last_login_ip, last_login_time, status) VALUES ([49], 'admin', [48], '0000-00-00', '', [49 48], [49 48], [48 46 48 48 48], [48 46 48 48 48], [45 56], 'zh-cn', 'CNY', [50], [48], [49 51 56 57 52 56 55 57 56 50], [50 49 51 48 55 48 54 52 51 51], [49 51 56 57 52 56 56 48 53 48], [49]);

正确的数据应该是:
INSERT INTO cos_member_profile (uid, nickname, sex, birthday, qq, score, credit, balance, frozen_funds, timezone, language, currency, login, reg_ip, reg_time, last_login_ip, last_login_time, status) VALUES
(1, 'admin', 0, '0000-00-00', '', 10, 10, 0.000, 0.000, -8, 'zh-cn', 'CNY', 2, 0, 1389487982, 2130706433, 1389488050, 1);

go get 请求不到cloud.google.com怎么办

Fetching https://cloud.google.com/go/civil?go-get=1
https fetch failed: Get https://cloud.google.com/go/civil?go-get=1: dial tcp 216.58.200.238:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
package cloud.google.com/go/civil: unrecognized import path "cloud.google.com/go/civil" (https fetch: Get https://cloud.google.com/go/civil?go-get=1: dial tcp 216.58.200.238:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

tag missing on reverse

For the columns as follow in MySql :

`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

the reverse tool will generate fields as follow:

CreateTime        time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' TIMESTAMP"`
UpdateTime        time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' TIMESTAMP"`

In this case, CreateTime and UpdateTime fields will not have a correct behavior when user leave it unset (that is, it's value is zero.).

Does this problem is because of missing tag like created, updated when reverse?

xorm reverse

I am trying to use xorm reverse but it does not connect my mysql database. Does it not support ip addresses ? I can ping the address without problems.

I tried:
xorm reverse mysql user:password@tcp(192.168.0.4:3306)/database templates/goxorm
But it gives an error:
-bash: syntax error near unexpected token ('`

I also tried:
xorm reverse mysql user:[email protected]/database templates/goxorm
But that gives another error:
2017/03/26 13:37:11 [Error] reverse.go:176 Default addr for network '192.168.0.4' unknown

Unknown colType DATETIME2 - mssql

I'm using reverse function on MSSQL database, and get error on DATETIME2

>xorm reverse mssql "server=XXX;user id=XXX;password=XXX;database=XXX" templates/goxorm

2018/05/04 15:22:40 [Error] reverse.go:178 Unknown colType DATETIME2 for XXTanble - XXColumn

Does xorm/cmd consider to support DATETIME2 ? Thank you.

(I'm begginer at go and I think I am not able to make PR now like #27 )

Failed to go get

Hi, I installed the dependecies, and while tried "go get github.com/go-xorm/cmd/xorm", it fails with this error:

# github.com/go-xorm/cmd/xorm
go-xorm/cmd/xorm/reverse.go:201: cannot use tables (type []*xorm.Table) as type []*core.Table in function argument
go-xorm/cmd/xorm/reverse.go:209: cannot use table (type *xorm.Table) as type *core.Table in append
go-xorm/cmd/xorm/reverse.go:246: cannot use table (type *xorm.Table) as type *core.Table in array element

reverse Sqlite3 error , reverse.go:179 Unknown col "GroupGID" in index "IX_..."

use a exist sqlite database, index sql like

CREATE  INDEX "IX_Users_GroupGID" ON "Users" ("GroupGID")

DbMetas() func throw a error Unknown col "GroupGID", because at dialect_sqlite3.go func GetIndexes()
get column name is "GroupGID" ,it's include quotation.

索引定义中表名和字段名包含引号,这个数据库是其他程序生成的,所以 在获取数据库元数据时发生了异常.
无法找到对应的字段名.

目前的解决办法是 在 dialect_sqlite3.go 中增加一个 normalizeName 方法,去掉两边的引号和中间的空格

//normalize tabale/index/col name remove quotation
func normalizeName(name string) string {
	if name[0] == '"' {
		name = name[1:]
	}
	l := len(name)
	if name[l-1] == '"' {
		name = name[:l-1]
	}
	spaceIndex := strings.Index(name, " ")
	if spaceIndex > 1 { //name include space,Use firse token
		return name[:spaceIndex]
	}
	return name
}

SqlServer 中 Unknown colType UNIQUEIDENTIFIER

xorm reverse mssql "sever=127.0.0.1;user id=sa;password=123;port=49783;database=WQMonData" templates\goxorm

2018/01/03 17:16:17 [Error] reverse.go:176 Unknown colType UNIQUEIDENTIFIER for WQ_Mon_T_VerifierInfo - id

SqlServer 中不识别 全局唯一标识符 (GUID)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.