Exemple #1
0
// TestClob
func TestClob(t *testing.T) {
	conn := getConnection(t)
	defer conn.Close()
	text := "abcdefghijkl"
	stmt, err := conn.Prepare("SELECT TO_CLOB('" + text + "') FROM DUAL")
	if err != nil {
		t.Errorf("error preparing query1: %v", err)
		t.FailNow()
	}
	defer stmt.Close()

	var clob *oracle.ExternalLobVar
	if err = stmt.QueryRow().Scan(&clob); err != nil {
		t.Errorf("Error scanning clob: %v", err)
	}
	defer clob.Close()
	t.Logf("clob: %v", clob)

	got, err := clob.ReadAll()
	if err != nil {
		t.Errorf("error reading clob: %v", err)
		t.FailNow()
	}
	if string(got) != text {
		t.Errorf("clob: got %q, awaited %q", got, text)
	}
}
Exemple #2
0
func TestGetLobConcurrent(t *testing.T) {
	conn := getConnection(t)
	defer conn.Close()

	text := "abcdefghijkl"

	var wg sync.WaitGroup
	for i := 0; i < 4; i++ {
		wg.Add(1)
		go func(text string) {
			defer wg.Done()
			stmt, err := conn.Prepare("SELECT TO_CLOB('" + text + "') FROM DUAL")
			if err != nil {
				t.Errorf("error preparing query1: %v", err)
				return
			}
			defer stmt.Close()

			var clob *oracle.ExternalLobVar
			rows, err := stmt.Query()
			if err != nil {
				t.Errorf("query: %v", err)
				return
			}
			defer rows.Close()
			_ = rows.Next()
			if err = rows.Scan(&clob); err != nil {
				t.Errorf("Error scanning clob: %v", err)
				return
			}
			defer clob.Close()

			t.Logf("clob=%v", clob)
			got, err := clob.ReadAll()
			if err != nil {
				t.Errorf("error reading clob: %v", err)
				return
			}
			t.Logf("got=%q", got)
			if string(got) != text {
				t.Errorf("clob: got %q, awaited %q", got, text)
				return
			}
		}(text)
		//}(text + "-" + strconv.Itoa(i))
	}
	wg.Wait()
}
Exemple #3
0
func insertClob(t *testing.T, conn *sql.Tx, text string) bool {
	qry := "INSERT INTO " + tbl + ` (F_int, F_clob) 
                  VALUES (-2, EMPTY_CLOB())`

	if _, err := conn.Exec(qry); err != nil {
		t.Errorf("cannot insert into "+tbl+" (%q): %v", qry, err)
		return false
	}
	var clob *oracle.ExternalLobVar
	qry = "SELECT F_clob FROM " + tbl + " WHERE F_int = -2 AND ROWNUM < 2"
	if err := conn.QueryRow(qry).Scan(&clob); err != nil {
		t.Errorf("cannot select empty clob: %v", err)
		return false
	}
	t.Logf("clob=%v", clob)

	if n, err := clob.WriteAt([]byte(text), 0); err != nil {
		t.Errorf("WriteAt clob: %v", err)
	} else if n != len([]rune(text)) {
		t.Errorf("written %d chars, awaited %d", n, len([]rune(text)))
	} else {
		t.Logf("written %d chars", n)
	}
	clob.Close()

	if err := conn.QueryRow(qry).Scan(&clob); err != nil {
		t.Errorf("cannot select clob: %v", err)
		return false
	}
	t.Logf("clob=%v", clob)
	defer clob.Close()

	got, err := clob.ReadAll()
	if err != nil {
		t.Errorf("reading clob: %v", err)
		return false
	}
	if string(got) != text {
		t.Errorf("got %q, awaited %q", got, text)
		return false
	}
	return true
}