Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
2450c0e7
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
2450c0e7
编写于
4月 22, 2021
作者:
L
liuyq-617
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[TD-3879]add stmt mode for taodemo go
上级
f4a0c1d9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
148 addition
and
1 deletion
+148
-1
tests/examples/go/taosdemo.go
tests/examples/go/taosdemo.go
+148
-1
未找到文件。
tests/examples/go/taosdemo.go
浏览文件 @
2450c0e7
...
...
@@ -18,6 +18,7 @@ import (
"database/sql"
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime"
...
...
@@ -26,7 +27,6 @@ import (
"time"
_
"github.com/taosdata/driver-go/taosSql"
//"golang.org/x/sys/unix"
)
...
...
@@ -48,6 +48,7 @@ type config struct {
dbName
string
supTblName
string
tablePrefix
string
mode
string
numOftables
int
numOfRecordsPerTable
int
numOfRecordsPerReq
int
...
...
@@ -70,6 +71,7 @@ func init() {
flag
.
StringVar
(
&
configPara
.
password
,
"P"
,
"taosdata"
,
"The password to use when connecting to the server."
)
flag
.
StringVar
(
&
configPara
.
dbName
,
"d"
,
"test"
,
"Destination database."
)
flag
.
StringVar
(
&
configPara
.
tablePrefix
,
"m"
,
"d"
,
"Table prefix name."
)
flag
.
StringVar
(
&
configPara
.
mode
,
"M"
,
"r"
,
"mode,r:raw,s:stmt"
)
flag
.
IntVar
(
&
configPara
.
numOftables
,
"t"
,
2
,
"The number of tables."
)
flag
.
IntVar
(
&
configPara
.
numOfRecordsPerTable
,
"n"
,
10
,
"The number of records per table."
)
flag
.
IntVar
(
&
configPara
.
numOfRecordsPerReq
,
"r"
,
3
,
"The number of records per request."
)
...
...
@@ -94,6 +96,7 @@ func printAllArgs() {
fmt
.
Printf
(
"usr: %v
\n
"
,
configPara
.
user
)
fmt
.
Printf
(
"password: %v
\n
"
,
configPara
.
password
)
fmt
.
Printf
(
"dbName: %v
\n
"
,
configPara
.
dbName
)
fmt
.
Printf
(
"mode: %v
\n
"
,
configPara
.
mode
)
fmt
.
Printf
(
"tablePrefix: %v
\n
"
,
configPara
.
tablePrefix
)
fmt
.
Printf
(
"numOftables: %v
\n
"
,
configPara
.
numOftables
)
fmt
.
Printf
(
"numOfRecordsPerTable: %v
\n
"
,
configPara
.
numOfRecordsPerTable
)
...
...
@@ -119,6 +122,24 @@ func main() {
//defer db.Close()
rand
.
Seed
(
time
.
Now
()
.
Unix
())
if
configPara
.
mode
==
"s"
{
fmt
.
Printf
(
"
\n
======== start stmt mode test ========
\n
"
)
db
,
err
:=
sql
.
Open
(
"taosSql"
,
url
)
if
err
!=
nil
{
log
.
Fatalf
(
"Open database error: %s
\n
"
,
err
)
}
defer
db
.
Close
()
demodbStmt
:=
configPara
.
dbName
demotStmt
:=
"demotStmt"
drop_database_stmt
(
db
,
demodbStmt
)
create_database_stmt
(
db
,
demodbStmt
)
use_database_stmt
(
db
,
demodbStmt
)
create_table_stmt
(
db
,
demotStmt
)
insert_data_stmt
(
db
,
demotStmt
)
select_data_stmt
(
db
,
demotStmt
)
return
}
createDatabase
(
configPara
.
dbName
,
configPara
.
supTblName
)
fmt
.
Printf
(
"======== create database success! ========
\n\n
"
)
...
...
@@ -407,6 +428,132 @@ func selectTest(dbName string, tbPrefix string, supTblName string) {
checkErr
(
err
,
"rows next iteration error"
)
}
}
func
drop_database_stmt
(
db
*
sql
.
DB
,
demodb
string
)
{
st
:=
time
.
Now
()
.
Nanosecond
()
// drop test db
res
,
err
:=
db
.
Exec
(
"drop database if exists "
+
demodb
)
checkErr
(
err
,
"drop database "
+
demodb
)
affectd
,
err
:=
res
.
RowsAffected
()
checkErr
(
err
,
"drop db, res.RowsAffected"
)
et
:=
time
.
Now
()
.
Nanosecond
()
fmt
.
Printf
(
"drop database result:
\n
%d row(s) affectd (%6.6fs)
\n\n
"
,
affectd
,
(
float32
(
et
-
st
))
/
1e9
)
}
func
create_database_stmt
(
db
*
sql
.
DB
,
demodb
string
)
{
st
:=
time
.
Now
()
.
Nanosecond
()
// create database
//var stmt interface{}
stmt
,
err
:=
db
.
Prepare
(
"create database ?"
)
checkErr
(
err
,
"create db, db.Prepare"
)
//var res driver.Result
res
,
err
:=
stmt
.
Exec
(
demodb
)
checkErr
(
err
,
"create db, stmt.Exec"
)
//fmt.Printf("Query OK, %d row(s) affected()", res.RowsAffected())
affectd
,
err
:=
res
.
RowsAffected
()
checkErr
(
err
,
"create db, res.RowsAffected"
)
et
:=
time
.
Now
()
.
Nanosecond
()
fmt
.
Printf
(
"create database result:
\n
%d row(s) affectd (%6.6fs)
\n\n
"
,
affectd
,
(
float32
(
et
-
st
))
/
1e9
)
}
func
use_database_stmt
(
db
*
sql
.
DB
,
demodb
string
)
{
st
:=
time
.
Now
()
.
Nanosecond
()
// create database
//var stmt interface{}
stmt
,
err
:=
db
.
Prepare
(
"use "
+
demodb
)
checkErr
(
err
,
"use db, db.Prepare"
)
res
,
err
:=
stmt
.
Exec
()
checkErr
(
err
,
"use db, stmt.Exec"
)
affectd
,
err
:=
res
.
RowsAffected
()
checkErr
(
err
,
"use db, res.RowsAffected"
)
et
:=
time
.
Now
()
.
Nanosecond
()
fmt
.
Printf
(
"use database result:
\n
%d row(s) affectd (%6.6fs)
\n\n
"
,
affectd
,
(
float32
(
et
-
st
))
/
1e9
)
}
func
create_table_stmt
(
db
*
sql
.
DB
,
demot
string
)
{
st
:=
time
.
Now
()
.
Nanosecond
()
// create table
// (ts timestamp, id int, name binary(8), len tinyint, flag bool, notes binary(8), fv float, dv double)
stmt
,
err
:=
db
.
Prepare
(
"create table ? (? timestamp, ? int, ? binary(10), ? tinyint, ? bool, ? binary(8), ? float, ? double)"
)
checkErr
(
err
,
"create table db.Prepare"
)
res
,
err
:=
stmt
.
Exec
(
demot
,
"ts"
,
"id"
,
"name"
,
"len"
,
"flag"
,
"notes"
,
"fv"
,
"dv"
)
checkErr
(
err
,
"create table stmt.Exec"
)
affectd
,
err
:=
res
.
RowsAffected
()
checkErr
(
err
,
"create table res.RowsAffected"
)
et
:=
time
.
Now
()
.
Nanosecond
()
fmt
.
Printf
(
"create table result:
\n
%d row(s) affectd (%6.6fs)
\n\n
"
,
affectd
,
(
float32
(
et
-
st
))
/
1e9
)
}
func
insert_data_stmt
(
db
*
sql
.
DB
,
demot
string
)
{
st
:=
time
.
Now
()
.
Nanosecond
()
// insert data into table
stmt
,
err
:=
db
.
Prepare
(
"insert into ? values(?, ?, ?, ?, ?, ?, ?, ?) (?, ?, ?, ?, ?, ?, ?, ?) (?, ?, ?, ?, ?, ?, ?, ?)"
)
checkErr
(
err
,
"insert db.Prepare"
)
res
,
err
:=
stmt
.
Exec
(
demot
,
"now"
,
1000
,
"'haidian'"
,
6
,
true
,
"'AI world'"
,
6987.654
,
321.987
,
"now+1s"
,
1001
,
"'changyang'"
,
7
,
false
,
"'DeepMode'"
,
12356.456
,
128634.456
,
"now+2s"
,
1002
,
"'chuangping'"
,
8
,
true
,
"'database'"
,
3879.456
,
65433478.456
)
checkErr
(
err
,
"insert data, stmt.Exec"
)
affectd
,
err
:=
res
.
RowsAffected
()
checkErr
(
err
,
"res.RowsAffected"
)
et
:=
time
.
Now
()
.
Nanosecond
()
fmt
.
Printf
(
"insert data result:
\n
%d row(s) affectd (%6.6fs)
\n\n
"
,
affectd
,
(
float32
(
et
-
st
))
/
1e9
)
}
func
select_data_stmt
(
db
*
sql
.
DB
,
demot
string
)
{
st
:=
time
.
Now
()
.
Nanosecond
()
stmt
,
err
:=
db
.
Prepare
(
"select ?, ?, ?, ?, ?, ?, ?, ? from ?"
)
// go binary mode
checkErr
(
err
,
"db.Prepare"
)
rows
,
err
:=
stmt
.
Query
(
"ts"
,
"id"
,
"name"
,
"len"
,
"flag"
,
"notes"
,
"fv"
,
"dv"
,
demot
)
checkErr
(
err
,
"stmt.Query"
)
fmt
.
Printf
(
"%10s%s%8s %5s %8s%s %s %10s%s %7s%s %8s%s %11s%s %14s%s
\n
"
,
" "
,
"ts"
,
" "
,
"id"
,
" "
,
"name"
,
" "
,
"len"
,
" "
,
"flag"
,
" "
,
"notes"
,
" "
,
"fv"
,
" "
,
" "
,
"dv"
)
var
affectd
int
for
rows
.
Next
()
{
var
ts
string
var
name
string
var
id
int
var
len
int8
var
flag
bool
var
notes
string
var
fv
float32
var
dv
float64
err
=
rows
.
Scan
(
&
ts
,
&
id
,
&
name
,
&
len
,
&
flag
,
&
notes
,
&
fv
,
&
dv
)
//fmt.Println("start scan fields from row.rs, &fv:", &fv)
//err = rows.Scan(&fv)
checkErr
(
err
,
"rows.Scan"
)
fmt
.
Printf
(
"%s
\t
"
,
ts
)
fmt
.
Printf
(
"%d
\t
"
,
id
)
fmt
.
Printf
(
"%10s
\t
"
,
name
)
fmt
.
Printf
(
"%d
\t
"
,
len
)
fmt
.
Printf
(
"%t
\t
"
,
flag
)
fmt
.
Printf
(
"%s
\t
"
,
notes
)
fmt
.
Printf
(
"%06.3f
\t
"
,
fv
)
fmt
.
Printf
(
"%09.6f
\n
"
,
dv
)
affectd
++
}
et
:=
time
.
Now
()
.
Nanosecond
()
fmt
.
Printf
(
"insert data result:
\n
%d row(s) affectd (%6.6fs)
\n\n
"
,
affectd
,
(
float32
(
et
-
st
))
/
1e9
)
}
func
checkErr
(
err
error
,
prompt
string
)
{
if
err
!=
nil
{
fmt
.
Printf
(
"%s
\n
"
,
prompt
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录