taosapp.go 9.9 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package main
16

H
hzcheng 已提交
17 18 19 20 21
import (
	"database/sql"
    "time"
    "log"
    "fmt"
22
	_ "github.com/taosdata/driver-go/taosSql"
H
hzcheng 已提交
23 24 25 26 27 28 29 30 31
)

func main() {
    taosDriverName := "taosSql"
    demodb := "demodb"
    demot := "demot"

    fmt.Printf("\n======== start demo test ========\n")
    // open connect to taos server
32
    db, err := sql.Open(taosDriverName, "root:taosdata@/tcp(127.0.0.1:0)/")
H
hzcheng 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    if err != nil {
        log.Fatalf("Open database error: %s\n", err)
    }
    defer db.Close()

    drop_database(db, demodb)
    create_database(db, demodb)
    use_database(db, demodb)
    create_table(db, demot)
    insert_data(db, demot)
    select_data(db, demot)

    fmt.Printf("\n======== start stmt mode test ========\n")

    demodbStmt := "demodbStmt"
    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)

    fmt.Printf("\n======== end demo test ========\n")
}

func drop_database(db *sql.DB, demodb string) {
    st := time.Now().Nanosecond()
61 62
    res, err := db.Exec("drop database if exists " + demodb)
    checkErr(err, "drop database if exists " + demodb)
H
hzcheng 已提交
63 64

    affectd, err := res.RowsAffected()
65
    checkErr(err, "drop db, res.RowsAffected")
H
hzcheng 已提交
66 67 68 69 70 71 72 73 74 75

    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(db *sql.DB, demodb string) {
    st := time.Now().Nanosecond()
    // create database
    res, err := db.Exec("create database " + demodb)
76
    checkErr(err, "create db, db.Exec")
H
hzcheng 已提交
77 78

    affectd, err := res.RowsAffected()
79
    checkErr(err, "create db, res.RowsAffected")
H
hzcheng 已提交
80 81 82 83 84 85 86 87 88 89 90 91

    et := time.Now().Nanosecond()

    fmt.Printf("create database result:\n %d row(s) affectd (%6.6fs)\n\n", affectd, (float32(et-st))/1E9)

    return
}

func use_database(db *sql.DB, demodb string) {
    st := time.Now().Nanosecond()
    // use database
    res, err := db.Exec("use " +  demodb)  // notes: must no quote to db name
92
    checkErr(err, "use db db.Exec")
H
hzcheng 已提交
93 94

    affectd, err := res.RowsAffected()
95
    checkErr(err, "use db, res.RowsAffected")
H
hzcheng 已提交
96 97 98 99 100 101 102 103 104 105

    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(db *sql.DB, demot string) {
    st := time.Now().Nanosecond()
    // create table
    res, err := db.Exec("create table " + demot + " (ts timestamp, id int, name binary(8), len tinyint, flag bool, notes binary(8), fv float, dv double)")
106
    checkErr(err, "create table db.Exec")
H
hzcheng 已提交
107 108

    affectd, err := res.RowsAffected()
109
    checkErr(err, "create table res.RowsAffected")
H
hzcheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122

    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(db *sql.DB, demot string) {
    st := time.Now().Nanosecond()
    // insert data
    res, err := db.Exec("insert into " + demot +
                               " values (now, 100, 'beijing', 10,  true, 'one', 123.456, 123.456)" +
                                      " (now+1s, 101, 'shanghai', 11, true, 'two', 789.123, 789.123)" +
                                      " (now+2s, 102, 'shenzhen', 12,  false, 'three', 456.789, 456.789)")

123
    checkErr(err, "insert data, db.Exec")
H
hzcheng 已提交
124 125

    affectd, err := res.RowsAffected()
126
    checkErr(err, "insert data res.RowsAffected")
H
hzcheng 已提交
127 128 129 130 131 132 133 134 135

    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(db *sql.DB, demot string) {
    st := time.Now().Nanosecond()

    rows, err := db.Query("select * from ? " , demot)  // go text mode
136
    checkErr(err, "select db.Query")
H
hzcheng 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150

    fmt.Printf("%10s%s%8s %5s %9s%s %s %8s%s %7s%s %8s%s %4s%s %5s%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)
151
        checkErr(err, "select rows.Scan")
H
hzcheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

        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)
    fmt.Printf("insert data result:\n %d row(s) affectd (%6.6fs)\n\n", affectd, (float32(et-st))/1E9)
}

func drop_database_stmt(db *sql.DB,demodb string) {
    st := time.Now().Nanosecond()
    // drop test db
173 174
    res, err := db.Exec("drop database if exists " + demodb)
    checkErr(err, "drop database " + demodb)
H
hzcheng 已提交
175 176

    affectd, err := res.RowsAffected()
177
    checkErr(err, "drop db, res.RowsAffected")
H
hzcheng 已提交
178 179 180 181 182 183 184 185 186 187

    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 ?")
188
    checkErr(err, "create db, db.Prepare")
H
hzcheng 已提交
189 190 191

    //var res driver.Result
    res, err := stmt.Exec(demodb)
192
    checkErr(err, "create db, stmt.Exec")
H
hzcheng 已提交
193 194 195

    //fmt.Printf("Query OK, %d row(s) affected()", res.RowsAffected())
    affectd, err := res.RowsAffected()
196
    checkErr(err, "create db, res.RowsAffected")
H
hzcheng 已提交
197 198 199 200 201 202 203 204 205 206

    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)
207
    checkErr(err, "use db, db.Prepare")
H
hzcheng 已提交
208 209

    res, err := stmt.Exec()
210
    checkErr(err, "use db, stmt.Exec")
H
hzcheng 已提交
211 212

    affectd, err := res.RowsAffected()
213
    checkErr(err, "use db, res.RowsAffected")
H
hzcheng 已提交
214 215 216 217 218 219 220 221 222

    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)
223 224
    stmt, err := db.Prepare("create table ? (? timestamp, ? int, ? binary(10), ? tinyint, ? bool, ? binary(8), ? float, ? double)")
    checkErr(err, "create table db.Prepare")
H
hzcheng 已提交
225 226

    res, err := stmt.Exec(demot, "ts", "id", "name", "len", "flag", "notes", "fv", "dv")
227
    checkErr(err, "create table stmt.Exec")
H
hzcheng 已提交
228 229

    affectd, err := res.RowsAffected()
230
    checkErr(err, "create table res.RowsAffected")
H
hzcheng 已提交
231 232 233 234 235 236 237 238 239

    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(?, ?, ?, ?, ?, ?, ?, ?) (?, ?, ?, ?, ?, ?, ?, ?) (?, ?, ?, ?, ?, ?, ?, ?)")
240
    checkErr(err, "insert db.Prepare")
H
hzcheng 已提交
241 242 243 244

    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,)
245
    checkErr(err, "insert data, stmt.Exec")
H
hzcheng 已提交
246 247

    affectd, err := res.RowsAffected()
248
    checkErr(err, "res.RowsAffected")
H
hzcheng 已提交
249 250 251 252 253 254 255 256 257

    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
258
    checkErr(err, "db.Prepare")
H
hzcheng 已提交
259 260

    rows, err := stmt.Query("ts", "id","name","len", "flag","notes", "fv", "dv", demot)
261
    checkErr(err, "stmt.Query")
H
hzcheng 已提交
262

263
    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")
H
hzcheng 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277
    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)
278
        checkErr(err, "rows.Scan")
H
hzcheng 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

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

297
func checkErr(err error, prompt string) {
H
hzcheng 已提交
298
    if err != nil {
299
	fmt.Printf("%s\n", prompt)
H
hzcheng 已提交
300 301
        panic(err)
    }
302
}