示例#1
0
        public Connection ConnectInMemory(NativeBindings bindings, string name = null)
        {
            lock (_locker)
            {
                if (name == null)
                {
                    name = "";
                }

                // check if there is already opened database for this file
                if (!_namesToDatabases.TryGetValue(name, out var database))
                {
                    // open database if there are none opened
                    var databaseState = bindings.DuckdbOpen(null, out var databasePtr);
                    database = new Database(databasePtr);

                    switch (databaseState)
                    {
                    case DuckdbState.DuckDBSuccess:
                        _namesToDatabases.Add(name, database);
                        break;

                    case DuckdbState.DuckDBError:
                        throw new DuckDbException($"Could not open in-memory database");
                    }
                }

                // create new connection to the database
                var connectionState = bindings.DuckdbConnect(database.Pointer, out var connectionPtr);
                var connection      = new Connection(connectionPtr);
                switch (connectionState)
                {
                case DuckdbState.DuckDBSuccess:
                    _connectionsToDatabases.Add(connection, database);
                    break;

                case DuckdbState.DuckDBError:
                    throw new DuckDbException($"Could not connect to in-memory database");
                }

                return(connection);
            }
        }
示例#2
0
        public Connection Connect(NativeBindings bindings, string filename)
        {
            lock (_locker)
            {
                var file = Path.GetFullPath(filename);

                // check if there is already opened database for this file
                if (!_filesToDatabases.TryGetValue(file, out var database))
                {
                    // open database if there are none opened
                    var databaseState = bindings.DuckdbOpen(filename, out var databasePtr);
                    database = new Database(databasePtr);

                    switch (databaseState)
                    {
                    case DuckdbState.DuckDBSuccess:
                        _filesToDatabases.Add(file, database);
                        break;

                    case DuckdbState.DuckDBError:
                        throw new DuckDbException($"Could not open database at: {filename}");
                    }
                }

                // create new connection to the database
                var connectionState = bindings.DuckdbConnect(database.Pointer, out var connectionPtr);
                var connection      = new Connection(connectionPtr);
                switch (connectionState)
                {
                case DuckdbState.DuckDBSuccess:
                    _connectionsToDatabases.Add(connection, database);
                    break;

                case DuckdbState.DuckDBError:
                    throw new DuckDbException($"Could not connect to database at: {filename}");
                }

                return(connection);
            }
        }