func (drv *csvDriver) Open(cfg string) (driver.Conn, error) {
	c := Conn{}
	if strings.HasPrefix(cfg, "{") {
		err := json.Unmarshal([]byte(cfg), &c)
		if err != nil {
			return nil, err
		}
	} else {
		c.File = cfg
		c.setDefaults()
	}

	doImport := false
	_, err := os.Lstat(c.File)
	if err == nil {
		doImport = true
	}

	drv.mu.Lock()
	defer drv.mu.Unlock()
	if drv.dbs == nil {
		drv.dbs = make(map[string]*csvConn)
	}
	conn := drv.dbs[c.File]
	if conn == nil {
		var f *os.File
		switch {
		case strings.HasPrefix(c.File, "http://"), strings.HasPrefix(c.File, "https://"):
			// FIXME(sbinet: check that c.Mode makes sense (ie: only reading)
			resp, err := http.Get(c.File)
			if err != nil {
				return nil, err
			}
			defer resp.Body.Close()
			// FIXME(sbinet): devise a mechanism to remove that temporary file
			// when we close the connection.
			f, err = ioutil.TempFile("", "csvdriver-")
			if err != nil {
				return nil, err
			}
			_, err = io.CopyBuffer(f, resp.Body, make([]byte, 16*1024*1024))
			if err != nil {
				return nil, err
			}
			_, err = f.Seek(0, io.SeekStart)
			if err != nil {
				return nil, err
			}
			doImport = true
		default:
			// local file
			f, err = os.OpenFile(c.File, c.Mode, c.Perm)
			if err != nil {
				return nil, err
			}
		}
		conn = &csvConn{
			f:    f,
			cfg:  c,
			drv:  drv,
			refs: 0,
		}

		err = conn.initDB()
		if err != nil {
			return nil, err
		}

		if doImport {
			err = conn.importCSV()
			if err != nil {
				return nil, err
			}
		}
		drv.dbs[c.File] = conn
	}
	conn.refs++

	return conn, err
}
// Open returns a new connection to the database.
// The name is a string in a driver-specific format.
//
// Open may return a cached connection (one previously
// closed), but doing so is unnecessary; the sql package
// maintains a pool of idle connections for efficient re-use.
//
// The returned connection is only used by one goroutine at a
// time.
Open returns a new connection to the database.
The name is a string in a driver-specific format.

Open may return a cached connection (one previously
closed), but doing so is unnecessary; the sql package
maintains a pool of idle connections for efficient re-use.

The returned connection is only used by one goroutine at a
time.
// Open returns a new connection to the database . // The name is a string in a driver - specific format . // // Open may return a cached connection ( one previously // closed ) but doing so is unnecessary ; the sql package // maintains a pool of idle connections for efficient re - use . // // The returned connection is only used by one goroutine at a // time .
func (drv *csvDriver) Open(cfg string) (driver.Conn, error) {
	c := Conn{}
	if strings.HasPrefix(cfg, "{") {
		err := json.Unmarshal([]byte(cfg), &c)
		if err != nil {
			return nil, err
		}
	} else {
		c.File = cfg
		c.setDefaults()
	}
	doImport := false
	_, err := os.Lstat(c.File)
	if err == nil {
		doImport = true
	}
	drv.mu.Lock()
	defer drv.mu.Unlock()
	if drv.dbs == nil {
		drv.dbs = make(map[string]*csvConn)
	}
	conn := drv.dbs[c.File]
	if conn == nil {
		var f *os.File
		switch {
		case strings.HasPrefix(c.File, "http://"), strings.HasPrefix(c.File, "https://"):
			resp, err := http.Get(c.File)
			if err != nil {
				return nil, err
			}
			defer resp.Body.Close()
			f, err = ioutil.TempFile("", "csvdriver-")
			if err != nil {
				return nil, err
			}
			_, err = io.CopyBuffer(f, resp.Body, make([]byte, 16*1024*1024))
			if err != nil {
				return nil, err
			}
			_, err = f.Seek(0, io.SeekStart)
			if err != nil {
				return nil, err
			}
			doImport = true
		default:
			f, err = os.OpenFile(c.File, c.Mode, c.Perm)
			if err != nil {
				return nil, err
			}
		}
		conn = &csvConn{
			f:    f,
			cfg:  c,
			drv:  drv,
			refs: 0,
		}
		err = conn.initDB()
		if err != nil {
			return nil, err
		}
		if doImport {
			err = conn.importCSV()
			if err != nil {
				return nil, err
			}
		}
		drv.dbs[c.File] = conn
	}
	conn.refs++
	return conn, err
}
func ( drv * csvDriver ) Open ( cfg string ) ( driver . Conn , error ) { c := Conn { } if strings . HasPrefix ( cfg , " " ) { err := json . Unmarshal ( [ ] byte ( cfg ) , & c ) if err != nil { return nil , err } } else { c . File = cfg c . setDefaults ( ) } doImport := false _ , err := os . Lstat ( c . File ) if err == nil { doImport = true } drv . mu . Lock ( ) defer drv . mu . Unlock ( ) if drv . dbs == nil { drv . dbs = make ( map [ string ] * csvConn ) } conn := drv . dbs [ c . File ] if conn == nil { var f * os . File switch { case strings . HasPrefix ( c . File , " " ) , strings . HasPrefix ( c . File , " " ) : resp , err := http . Get ( c . File ) if err != nil { return nil , err } defer resp . Body . Close ( ) f , err = ioutil . TempFile ( " " , " " ) if err != nil { return nil , err } _ , err = io . CopyBuffer ( f , resp . Body , make ( [ ] byte , 16 * 1024 * 1024 ) ) if err != nil { return nil , err } _ , err = f . Seek ( 0 , io . SeekStart ) if err != nil { return nil , err } doImport = true default : f , err = os . OpenFile ( c . File , c . Mode , c . Perm ) if err != nil { return nil , err } } conn = & csvConn { f : f , cfg : c , drv : drv , refs : 0 , } err = conn . initDB ( ) if err != nil { return nil , err } if doImport { err = conn . importCSV ( ) if err != nil { return nil , err } } drv . dbs [ c . File ] = conn } conn . refs ++ return conn , err }
Open returns a new connection to the database.
Open returns a new connection to the database .
// FIXME(sbinet: check that c.Mode makes sense (ie: only reading) // FIXME(sbinet): devise a mechanism to remove that temporary file // when we close the connection. // local file