Exemplo n.º 1
0
func TestDate(t *testing.T) {
	myConnect(t, true, 0)
	query("drop table D") // Drop test table if exists
	checkResult(t,
		query("create table D (id int, dd date, dt datetime, tt time)"),
		cmdOK(0, false, true),
	)

	test := []struct {
		dd, dt string
		tt     time.Duration
	}{
		{
			"2011-11-13",
			"2010-12-12 11:24:00",
			-time.Duration((128*3600 + 3*60 + 2) * 1e9),
		}, {
			"0000-00-00",
			"0000-00-00 00:00:00",
			time.Duration(0),
		},
	}

	ins, err := my.Prepare("insert D values (?, ?, ?, ?)")
	checkErr(t, err, nil)

	sel, err := my.Prepare("select id, tt from D where dd = ? && dt = ?")
	checkErr(t, err, nil)

	for i, r := range test {
		_, err = ins.Run(i, r.dd, r.dt, r.tt)
		checkErr(t, err, nil)

		sdt, err := mysql.ParseTime(r.dt, time.Local)
		checkErr(t, err, nil)
		sdd, err := mysql.ParseDate(r.dd)
		checkErr(t, err, nil)

		rows, _, err := sel.Exec(sdd, sdt)
		checkErr(t, err, nil)
		if rows == nil {
			t.Fatal("nil result")
		}
		if rows[0].Int(0) != i {
			t.Fatal("Bad id", rows[0].Int(1))
		}
		if rows[0][1].(time.Duration) != r.tt {
			t.Fatal("Bad tt", rows[0].Duration(1))
		}
	}

	checkResult(t, query("drop table D"), cmdOK(0, false, true))
	myClose(t)
}
Exemplo n.º 2
0
func TestPrepared(t *testing.T) {
	myConnect(t, true, 0)
	query("drop table P") // Drop test table if exists
	checkResult(t,
		query(
			"create table P ("+
				"   ii int not null, ss varchar(20), dd datetime"+
				") default charset=utf8",
		),
		cmdOK(0, false, true),
	)

	exp := Stmt{
		fields: []*mysql.Field{
			&mysql.Field{
				Catalog: "def", Db: "test", Table: "P", OrgTable: "P",
				Name:    "i",
				OrgName: "ii",
				DispLen: 11,
				Flags:   _FLAG_NO_DEFAULT_VALUE | _FLAG_NOT_NULL,
				Type:    MYSQL_TYPE_LONG,
				Scale:   0,
			},
			&mysql.Field{
				Catalog: "def", Db: "test", Table: "P", OrgTable: "P",
				Name:    "s",
				OrgName: "ss",
				DispLen: 3 * 20, // varchar(20)
				Flags:   0,
				Type:    MYSQL_TYPE_VAR_STRING,
				Scale:   0,
			},
			&mysql.Field{
				Catalog: "def", Db: "test", Table: "P", OrgTable: "P",
				Name:    "d",
				OrgName: "dd",
				DispLen: 19,
				Flags:   _FLAG_BINARY,
				Type:    MYSQL_TYPE_DATETIME,
				Scale:   0,
			},
		},
		fc_map:        map[string]int{"i": 0, "s": 1, "d": 2},
		field_count:   3,
		param_count:   2,
		warning_count: 0,
		status:        0x2,
	}

	sel := prepare("select ii i, ss s, dd d from P where ii = ? and ss = ?")
	checkStmt(t, sel, &StmtErr{&exp, nil})

	all := prepare("select * from P")
	checkErr(t, all.err, nil)

	ins := prepare("insert into P values (?, ?, ?)")
	checkErr(t, ins.err, nil)

	parsed, err := mysql.ParseTime("2012-01-17 01:10:10", time.Local)
	checkErr(t, err, nil)
	parsedZero, err := mysql.ParseTime("0000-00-00 00:00:00", time.Local)
	checkErr(t, err, nil)
	if !parsedZero.IsZero() {
		t.Fatalf("time '%s' isn't zero", parsedZero)
	}
	exp_rows := []mysql.Row{
		mysql.Row{
			2, "Taki tekst", time.Unix(123456789, 0),
		},
		mysql.Row{
			5, "Pąk róży", parsed,
		},
		mysql.Row{
			-3, "基础体温", parsed,
		},
		mysql.Row{
			11, "Zero UTC datetime", time.Unix(0, 0),
		},
		mysql.Row{
			17, mysql.Blob([]byte("Zero datetime")), parsedZero,
		},
		mysql.Row{
			23, []byte("NULL datetime"), (*time.Time)(nil),
		},
		mysql.Row{
			23, "NULL", nil,
		},
	}

	for _, row := range exp_rows {
		checkErrWarn(t,
			exec(ins.stmt, row[0], row[1], row[2]),
			cmdOK(1, true, true),
		)
	}

	// Convert values to expected result types
	for _, row := range exp_rows {
		for ii, col := range row {
			val := reflect.ValueOf(col)
			// Dereference pointers
			if val.Kind() == reflect.Ptr {
				val = val.Elem()
			}
			switch val.Kind() {
			case reflect.Invalid:
				row[ii] = nil

			case reflect.String:
				row[ii] = []byte(val.String())

			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
				reflect.Int64:
				row[ii] = int32(val.Int())

			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
				reflect.Uint64:
				row[ii] = int32(val.Uint())

			case reflect.Slice:
				if val.Type().Elem().Kind() == reflect.Uint8 {
					bytes := make([]byte, val.Len())
					for ii := range bytes {
						bytes[ii] = val.Index(ii).Interface().(uint8)
					}
					row[ii] = bytes
				}
			}
		}
	}

	checkErrWarn(t, exec(sel.stmt, 2, "Taki tekst"), selectOK(exp_rows, true))
	checkErrWarnRows(t, exec(all.stmt), selectOK(exp_rows, true))

	checkResult(t, query("drop table P"), cmdOK(0, false, true))

	checkErr(t, sel.stmt.Delete(), nil)
	checkErr(t, all.stmt.Delete(), nil)
	checkErr(t, ins.stmt.Delete(), nil)

	myClose(t)
}