本文整理汇总了Golang中github.com/lib/pq.CopyIn函数的典型用法代码示例。如果您正苦于以下问题:Golang CopyIn函数的具体用法?Golang CopyIn怎么用?Golang CopyIn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CopyIn函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestCopyOne
// TestCopyOne verifies that only one COPY can run at once.
func TestCopyOne(t *testing.T) {
defer leaktest.AfterTest(t)()
t.Skip("https://github.com/lib/pq/issues/494")
params, _ := createTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
if _, err := db.Exec(`
CREATE DATABASE d;
SET DATABASE = d;
CREATE TABLE t (
i INT PRIMARY KEY
);
`); err != nil {
t.Fatal(err)
}
txn, err := db.Begin()
if err != nil {
t.Fatal(err)
}
if _, err := txn.Prepare(pq.CopyIn("t", "i")); err != nil {
t.Fatal(err)
}
if _, err := txn.Prepare(pq.CopyIn("t", "i")); err == nil {
t.Fatal("expected error")
}
}
开发者ID:BramGruneir,项目名称:cockroach,代码行数:32,代码来源:copy_in_test.go
示例2: copyFrom
func (qs *QuadStore) copyFrom(tx *sql.Tx, in []graph.Delta) error {
stmt, err := tx.Prepare(pq.CopyIn("quads", "subject", "predicate", "object", "label", "id", "ts", "subject_hash", "predicate_hash", "object_hash", "label_hash"))
if err != nil {
return err
}
for _, d := range in {
_, err := stmt.Exec(
d.Quad.Subject,
d.Quad.Predicate,
d.Quad.Object,
d.Quad.Label,
d.ID.Int(),
d.Timestamp,
hashOf(d.Quad.Subject),
hashOf(d.Quad.Predicate),
hashOf(d.Quad.Object),
hashOf(d.Quad.Label),
)
if err != nil {
glog.Errorf("couldn't prepare COPY statement: %v", err)
return err
}
}
_, err = stmt.Exec()
if err != nil {
return err
}
return stmt.Close()
}
开发者ID:WeiZhang555,项目名称:clair,代码行数:29,代码来源:quadstore.go
示例3: bulkDirect
func bulkDirect(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(pq.CopyIn("foo", "id", "val"))
if err != nil {
tx.Rollback()
return err
}
// Build up the COPY FROM by repeated Exec calls
for i := 0; i < *Count; i++ {
val := *Start + i
strVal := fmt.Sprintf("%07d", val)
if _, err := stmt.Exec(val, strVal); err != nil {
fmt.Printf("error while building up COPY: %s\n", err)
tx.Rollback()
return err
}
}
// Flush the COPY FROM to postgres
if _, err := stmt.Exec(); err != nil {
fmt.Printf("error while copying data: %s\n", err)
return err
}
if err := stmt.Close(); err != nil {
fmt.Printf("error during stmt.Close(): %s\n", err)
return err
}
if err := tx.Commit(); err != nil {
fmt.Printf("could not commit transaction: %s\n", err)
return err
}
return nil
}
开发者ID:jameinel,项目名称:scripts,代码行数:35,代码来源:benchmark-pg.go
示例4: UpdateDivision
func (db *Database) UpdateDivision(divisionId int, units []Unit) {
tx, err := db.db.Begin()
if err != nil {
panic(err)
}
_, err = tx.Query("DELETE FROM unit WHERE unit.division = $1", divisionId)
if err != nil {
panic(err)
}
stmt, err := tx.Prepare(pq.CopyIn("unit", "division", "unit_type", "amount"))
for _, unit := range units {
_, err = stmt.Exec(divisionId, unit.TypeNum, unit.Amount)
if err != nil {
panic(err)
}
}
_, err = stmt.Exec()
if err != nil {
panic(err)
}
err = stmt.Close()
if err != nil {
panic(err)
}
err = tx.Commit()
if err != nil {
panic(err)
}
}
开发者ID:GaroBrik,项目名称:crisis,代码行数:35,代码来源:divisiondao.go
示例5: CopyDataIntoTable
func (ops *DBOps) CopyDataIntoTable(t []Stock) {
txn, err := ops.Db.Begin()
if err != nil {
log.Fatal(err)
}
defer txn.Rollback()
stmt, err := txn.Prepare(pq.CopyIn("stock", "dealtime", "price", "volume", "amount", "climax"))
if err != nil {
log.Fatal(err)
}
for _, item := range t {
_, err = stmt.Exec(item.Dealtime, item.Price, item.Volume, item.Amount, item.Climax)
if err != nil {
log.Fatal(err)
}
}
_, err = stmt.Exec()
if err != nil {
log.Fatal(err)
}
err = stmt.Close()
if err != nil {
log.Fatal(err)
}
err = txn.Commit()
if err != nil {
log.Fatal(err)
}
}
开发者ID:saiyaw,项目名称:example,代码行数:34,代码来源:dbops.go
示例6: insertShooters
func insertShooters(db *sql.DB, m *Match) {
txn, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := txn.Prepare(pq.CopyIn("users", "name", "countryCode", "mos", "flags"))
if err != nil {
log.Fatal(err)
}
for _, results := range m.Results {
for _, result := range results {
_, err = stmt.Exec(result.Name, result.CountryCode, result.MosNumber, result.Flags)
if err != nil {
log.Fatal(err)
}
}
}
_, err = stmt.Exec()
if err != nil {
log.Fatal(err)
}
err = stmt.Close()
if err != nil {
log.Fatal(err)
}
err = txn.Commit()
if err != nil {
log.Fatal(err)
}
}
开发者ID:flowlo,项目名称:quali,代码行数:35,代码来源:db.go
示例7: createSubscriptions
func createSubscriptions(ids []int) error {
info := getDBSettings()
db, errCon := sql.Open("postgres", fmt.Sprintf("host=%v user=%v password=%v dbname=%v sslmode=require", info.Host, info.Username, info.Password, info.Database))
defer db.Close()
if errCon != nil {
log.Fatal(errCon)
}
txn, errT := db.Begin()
if errT != nil {
log.Println(errT)
return errT
}
stmt, errPrep := txn.Prepare(pq.CopyIn("subscription", "topicid", "userid"))
if errPrep != nil {
log.Fatal(errPrep)
}
log.Println("Start For...")
var wg sync.WaitGroup
for _, id := range ids {
userID := id
wg.Add(1)
go func(id int) {
defer wg.Done()
mr.Seed(time.Now().UnixNano())
numSubs := mr.Intn(5) + 1
for i := 0; i < numSubs; i++ {
topicID := i + 1
_, errA := stmt.Exec(topicID, userID)
if errA != nil {
log.Fatal(errA)
}
}
}(userID)
}
wg.Wait()
log.Println("End For")
log.Println("Start Exec")
_, errEX := stmt.Exec()
if errEX != nil {
log.Fatal(errEX)
}
log.Println("End Exec")
errClose := stmt.Close()
if errClose != nil {
log.Fatal(errClose)
}
log.Println("Start Commit")
errCommit := txn.Commit()
if errCommit != nil {
log.Fatal(errCommit)
}
log.Println("End Commit")
return nil
}
开发者ID:genghisjahn,项目名称:kinesis-lambda-demo,代码行数:59,代码来源:download.go
示例8: executeCopyLine
func (dfr *DataFileReader) executeCopyLine(line string, c *spiffy.DbConnection, tx *sql.Tx) (*sql.Stmt, error) {
pieces := dfr.extractCopyLine(line)
if len(pieces) < 3 {
return nil, exception.New("Invalid `COPY ...` line, cannot continue.")
}
tableName := pieces[1]
columnCSV := pieces[2]
columns := strings.Split(columnCSV, ", ")
return tx.Prepare(pq.CopyIn(tableName, columns...))
}
开发者ID:blendlabs,项目名称:spiffy,代码行数:10,代码来源:data_file_reader.go
示例9: createLang
func createLang(ids []int) error {
info := getDBSettings()
db, errCon := sql.Open("postgres", fmt.Sprintf("host=%v user=%v password=%v dbname=%v sslmode=require", info.Host, info.Username, info.Password, info.Database))
defer db.Close()
if errCon != nil {
log.Fatal(errCon)
}
txn, errT := db.Begin()
if errT != nil {
log.Println(errT)
return errT
}
stmt, errPrep := txn.Prepare(pq.CopyIn("userinfo", "userid", "langcode"))
if errPrep != nil {
log.Fatal(errPrep)
}
log.Println("Start For...")
var wg sync.WaitGroup
for _, id := range ids {
userID := id
wg.Add(1)
go func(id int) {
defer wg.Done()
_, errA := stmt.Exec(userID, "en")
if errA != nil {
log.Fatal(errA)
}
}(userID)
}
wg.Wait()
log.Println("End For")
log.Println("Start Exec")
_, errEX := stmt.Exec()
if errEX != nil {
log.Fatal(errEX)
}
log.Println("End Exec")
errClose := stmt.Close()
if errClose != nil {
log.Fatal(errClose)
}
log.Println("Start Commit")
errCommit := txn.Commit()
if errCommit != nil {
log.Fatal(errCommit)
}
log.Println("End Commit")
return nil
}
开发者ID:genghisjahn,项目名称:kinesis-lambda-demo,代码行数:53,代码来源:download.go
示例10: commitData
func commitData(db *sql.DB, data map[string]string, columns ...string) {
var dataitems = []string{}
columns = append(columns, "symbol")
for k, v := range columns {
if k == 0 && len(data[v]) == 0 {
break
}
dataitems = append(dataitems, data[v])
}
if len(dataitems) > 0 {
fmt.Printf("%v{%d}\n", dataitems, len(dataitems))
txn, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := txn.Prepare(pq.CopyIn("balancesheet", columns...))
if err != nil {
log.Fatal(err)
}
new := make([]interface{}, len(dataitems))
for i, v := range dataitems {
new[i] = v
}
_, err = stmt.Exec(new...)
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec()
if err != nil {
log.Fatal(err)
}
err = stmt.Close()
if err != nil {
log.Fatal(err)
}
err = txn.Commit()
if err != nil {
log.Fatal(err)
}
}
}
开发者ID:casbeebc,项目名称:yahoo_financials,代码行数:52,代码来源:balancesheet.go
示例11: insert
// insert operates on a list of dumpFormat and inserts them to the provided Postgres
// database.
func (data dataset) insert(db *sql.DB) error {
transaction, err := db.Begin()
if err != nil {
return fmt.Errorf("Error starting PG txn => %s", err.Error())
}
// PG's COPY FROM used for fast mass insertions. Syntax is table followed by columns.
// http://godoc.org/github.com/lib/pq#hdr-Bulk_imports
stmt, err := transaction.Prepare(pq.CopyIn(
"density_data", // table
"dump_time", // columns.....
"group_id",
"group_name",
"parent_id",
"parent_name",
"client_count",
))
if err != nil {
return fmt.Errorf("Error prepping PG txn => %s", err.Error())
}
defer stmt.Close()
// Add all data from the set
for _, d := range data {
_, err = stmt.Exec(
d.DumpTime,
d.GroupID,
d.GroupName,
d.ParentID,
d.ParentName,
d.ClientCount,
)
if err != nil {
return fmt.Errorf("Failed to add to bulk insert => %s", err.Error())
}
}
// execute the transaction
if _, err = stmt.Exec(); err != nil {
return fmt.Errorf("Failed to execute bulk insert => %s", err.Error())
}
// commit the transaction if there's been no errors
if err = transaction.Commit(); err != nil {
log.Printf("ERROR: Failed to commit txn => %s", err.Error())
if err = transaction.Rollback(); err != nil {
log.Printf("ERROR: Failed to rollback txn => %s", err.Error())
}
}
return nil
}
开发者ID:vorkady,项目名称:wireless_data_processor,代码行数:53,代码来源:data.go
示例12: TestCopyError
func TestCopyError(t *testing.T) {
defer leaktest.AfterTest(t)()
params, _ := createTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
if _, err := db.Exec(`
CREATE DATABASE d;
SET DATABASE = d;
CREATE TABLE t (
i INT PRIMARY KEY
);
`); err != nil {
t.Fatal(err)
}
txn, err := db.Begin()
if err != nil {
t.Fatal(err)
}
stmt, err := txn.Prepare(pq.CopyIn("t", "i"))
if err != nil {
t.Fatal(err)
}
// Insert conflicting primary keys.
for i := 0; i < 2; i++ {
_, err = stmt.Exec(1)
if err != nil {
t.Fatal(err)
}
}
err = stmt.Close()
if err == nil {
t.Fatal("expected error")
}
// Make sure we can query after an error.
var i int
if err := db.QueryRow("SELECT 1").Scan(&i); err != nil {
t.Fatal(err)
} else if i != 1 {
t.Fatalf("expected 1, got %d", i)
}
if err := txn.Rollback(); err != nil {
t.Fatal(err)
}
}
开发者ID:BramGruneir,项目名称:cockroach,代码行数:51,代码来源:copy_in_test.go
示例13: InsertMultiDataValues
func InsertMultiDataValues(db *sql.DB, table_name string, table_columns []string, data [][]interface{}) error {
// Transaction Begins and must end with a commit or rollback
transaction, err := db.Begin()
if err != nil {
transaction.Rollback()
return err
}
// Preparing statement with the table name and columns passed
statement, err := transaction.Prepare(pq.CopyIn(table_name, table_columns...))
if err != nil {
statement.Close()
transaction.Rollback()
return err
}
// Looping though all the data rows passed
for _, data_row := range data {
// Inserting Single Data row into the statement
_, err = statement.Exec(data_row...)
if err != nil {
statement.Close()
transaction.Rollback()
return err
}
}
/*
_, err = stmt.Exec()
if err != nil {
return err
}
*/
// Closing the connection of the statement
err = statement.Close()
if err != nil {
statement.Close()
transaction.Rollback()
return err
}
// Commiting and closing the transaction saving changes we have made in the database
err = transaction.Commit()
if err != nil {
transaction.Rollback()
return err
}
return nil
}
开发者ID:emculber,项目名称:database_access,代码行数:50,代码来源:postgresql_access.go
示例14: TestCopyTransaction
// TestCopyTransaction verifies that COPY data can be used after it is done
// within a transaction.
func TestCopyTransaction(t *testing.T) {
defer leaktest.AfterTest(t)()
params, _ := createTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
if _, err := db.Exec(`
CREATE DATABASE d;
SET DATABASE = d;
CREATE TABLE t (
i INT PRIMARY KEY
);
`); err != nil {
t.Fatal(err)
}
txn, err := db.Begin()
if err != nil {
t.Fatal(err)
}
stmt, err := txn.Prepare(pq.CopyIn("t", "i"))
if err != nil {
t.Fatal(err)
}
const val = 2
_, err = stmt.Exec(val)
if err != nil {
t.Fatal(err)
}
if err = stmt.Close(); err != nil {
t.Fatal(err)
}
var i int
if err := txn.QueryRow("SELECT i FROM d.t").Scan(&i); err != nil {
t.Fatal(err)
} else if i != val {
t.Fatalf("expected 1, got %d", i)
}
if err := txn.Commit(); err != nil {
t.Fatal(err)
}
}
开发者ID:BramGruneir,项目名称:cockroach,代码行数:50,代码来源:copy_in_test.go
示例15: NewInserter
func NewInserter(
tx *sql.Tx,
driver string,
table string,
columns ...string,
) (ins *Inserter, err error) {
defer Safe(&err)
ins = &Inserter{tx, table, columns, driver, nil}
if driver == "postgres" {
ins.stmt = Prepare(tx, pq.CopyIn(table, columns...))
} else {
ins.stmt = ins.preparedInsert()
}
return
}
开发者ID:BurntSushi,项目名称:csql,代码行数:16,代码来源:insert.go
示例16: loadEventsPage
func loadEventsPage(page Page, db *sql.DB) error {
startPage := time.Now()
tx, err := db.Begin()
if err != nil {
return err
}
statement, err := tx.Prepare(pq.CopyIn("charges",
"id", "amount", "created", "sequence"))
if err != nil {
return err
}
for _, event := range page.Data {
switch event.Type {
case "charge.created":
_, err = statement.Exec(
// TODO: deserialize to proper charge object
event.Data.Obj["id"].(string),
uint64(event.Data.Obj["amount"].(float64)),
time.Unix(int64(event.Data.Obj["created"].(float64)), 0),
// TODO: should actually be in its own table
event.Sequence,
)
}
}
_, err = statement.Exec()
if err != nil {
return err
}
err = statement.Close()
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
log.Printf("Loaded page of %v event(s) in %v.",
len(page.Data), time.Now().Sub(startPage))
return nil
}
开发者ID:brandur,项目名称:stripe-warehouse,代码行数:46,代码来源:main.go
示例17: saveDailyQuotes
func saveDailyQuotes(in <-chan (*Response), db *sql.DB) {
for res := range in {
if res != nil && len(res.Query.Results.DailyTicks) > 0 {
fmt.Printf("Saving quotes for %s...\n", res.Query.Results.DailyTicks[0].Symbol)
tx, err := db.Begin()
if err != nil {
fmt.Println("error creating db tx")
return
}
stmt, err := tx.Prepare(pq.CopyIn("daily_quotes_hst", "symbol", "trade_dt", "open", "high", "low", "close", "volume", "adj_close"))
if err != nil {
fmt.Println(err.Error())
return
}
for i := 0; i < len(res.Query.Results.DailyTicks); i++ {
q := res.Query.Results.DailyTicks[i]
_, err = stmt.Exec(q.Symbol, q.Date, q.Open, q.High, q.Low, q.Close, q.Volume, q.AdjClose)
if err != nil {
fmt.Printf("error saving quotes to db tx: %s\n", err.Error())
break
}
}
_, err = stmt.Exec()
if err != nil {
fmt.Printf("error saving quotes to db tx: %s\n", err.Error())
}
err = stmt.Close()
if err != nil {
fmt.Printf("error closing stmt: %s\n", err.Error())
}
err = tx.Commit()
if err != nil {
fmt.Printf("error commiting tx\n")
return
}
fmt.Printf("Total daily ticks for %s : %d\n", res.Query.Results.DailyTicks[0].Symbol, len(res.Query.Results.DailyTicks))
}
}
}
开发者ID:kvattikuti,项目名称:mktdata-hist,代码行数:43,代码来源:main.go
示例18: copyFrom
func (qs *QuadStore) copyFrom(tx *sql.Tx, in []graph.Delta, opts graph.IgnoreOpts) error {
panic("broken")
stmt, err := tx.Prepare(pq.CopyIn("quads", "subject", "predicate", "object", "label", "id", "ts", "subject_hash", "predicate_hash", "object_hash", "label_hash"))
if err != nil {
glog.Errorf("couldn't prepare COPY statement: %v", err)
return err
}
for _, d := range in {
s, p, o, l, err := marshalQuadDirections(d.Quad)
if err != nil {
glog.Errorf("couldn't marshal quads: %v", err)
return err
}
_, err = stmt.Exec(
s,
p,
o,
l,
d.ID.Int(),
d.Timestamp,
hashOf(d.Quad.Subject),
hashOf(d.Quad.Predicate),
hashOf(d.Quad.Object),
hashOf(d.Quad.Label),
)
if err != nil {
err = convInsertError(err)
glog.Errorf("couldn't execute COPY statement: %v", err)
return err
}
}
//if _, err = stmt.Exec(); err != nil {
// glog.Errorf("couldn't execute COPY statement 2: %v", err)
// return err
//}
if err = stmt.Close(); err != nil {
glog.Errorf("couldn't close COPY statement: %v", err)
err = convInsertError(err)
return err
}
return nil
}
开发者ID:dennwc,项目名称:cayley,代码行数:42,代码来源:quadstore.go
示例19: CreateDivision
func (db *Database) CreateDivision(coords Coords, units []Unit, name string, factionId int) int {
tx, err := db.db.Begin()
if err != nil {
panic(err)
}
row := tx.QueryRow("INSERT INTO division (faction, division_name, route) "+
"VALUES($1, $2, ARRAY[($3, $4)]) RETURNING id", factionId, name, coords.X, coords.Y)
var divisionId int
err = row.Scan(&divisionId)
if err != nil {
panic(err)
}
stmt, err := tx.Prepare(pq.CopyIn("unit", "division", "unit_type", "amount"))
for _, unit := range units {
_, err = stmt.Exec(divisionId, unit.TypeNum, unit.Amount)
if err != nil {
panic(err)
}
}
_, err = stmt.Exec()
if err != nil {
panic(err)
}
err = stmt.Close()
if err != nil {
panic(err)
}
err = tx.Commit()
if err != nil {
panic(err)
}
return divisionId
}
开发者ID:GaroBrik,项目名称:crisis,代码行数:41,代码来源:divisiondao.go
示例20: AddOneSymbol
func (ops *DBOps) AddOneSymbol(symbol string, name string, valid bool) error {
txn, err := ops.Db.Begin()
if err != nil {
log.Println(err.Error())
return err
}
defer txn.Rollback()
stmt, err := txn.Prepare(pq.CopyIn("symbol", "value", "name", "valid", "updatetime"))
if err != nil {
log.Println(err.Error())
return err
}
_, err = stmt.Exec(symbol, name, valid, time.Now())
if err != nil {
log.Println(err.Error())
return err
}
_, err = stmt.Exec()
if err != nil {
log.Println(err.Error())
return err
}
err = stmt.Close()
if err != nil {
log.Println(err.Error())
return err
}
err = txn.Commit()
if err != nil {
log.Println(err.Error())
return err
}
return nil
}
开发者ID:saiyaw,项目名称:example,代码行数:39,代码来源:dbops.go
注:本文中的github.com/lib/pq.CopyIn函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论