Class ::nsmcp::SQLiteDatabase (public)

 ::nx::Class ::nsmcp::SQLiteDatabase[i]

Defined in /usr/local/ns/tcl/nsmcp/database.tcl

Testcases:
No testcase defined.
Source code:
    :property pool:required
    :property {maxBytes:integer 1073741824}
    :variable path

    :method init {} {
        if {${:maxBytes}<4194304} {
            error "database maxBytes must be at least 4 MiB"
        }
        ns_db withhandle -pool ${:pool} -timeout 2s handle {
            set db [:session $handle]
            $db close
        }
    }

    :public method connect {} {
        #
        # Check out a session from the dedicated SQLite cache pool.
        #
        # Validate the persistent datasource, apply private session settings and
        # enforce the configured page budget; release resources on failure.
        #
        # @return A DatabaseSession object owning the checked-out handle.
        #

        if {![llength [info commands ns_db]]} {
            error "databasePool requires nsdb/nsdbsqlite loaded before nsmcp"
        }
        set handle [ns_db gethandle -timeout 2 ${:pool}]
        if {$handle eq ""} {
            error "MCP database pool timed out"
        }
        try {
            set db [:session $handle]
            $db configure -ownsHandle true
            return $db
        } on error {message options} {
            ns_db releasehandle $handle
            return -options $options $message
        }
    }

    :public method session {handle} {
        #
        # Adapt a caller-owned handle without taking ownership of checkout.
        #
        # Use within ns_db withhandle; close the adapter before leaving that
        # scope. Validation and page-budget errors leave checkout to the caller.
        #
        # @param handle Checked-out SQLite handle from this database's pool.
        # @return A borrowed DatabaseSession, local to the caller's operation.
        #

        if {[ns_db poolname $handle] ne ${:pool} || [ns_db dbtype $handle] ne "sqlite"} {
            error "MCP databasePool must use nsdbsqlite and its configured pool"
        }
        set source [ns_db datasource $handle]
        if {$source eq "" || $source eq ":memory:" || [string match file:* $source]} {
            error "MCP cache requires a persistent file datasource"
        }
        set :path [file normalize $source]
        file attributes ${:path} -permissions 0600
        set db [::nsmcp::DatabaseSession new -handle $handle -ownsHandle false]
        try {
            set pages [expr {int(${:maxBytes}*0.45)/[$db scalar {PRAGMA page_size}]}]
            set current [$db scalar {PRAGMA page_count}]
            if {$current>$pages} {
                error "existing database exceeds configured byte budget"
            }
            $db execute "PRAGMA max_page_count=$pages"
            return $db
        } on error {message options} {
            $db close
            return -options $options $message
        }
    }

    :public method path {} {
        #
        # Return the persistent database path discovered during connection.
        #
        # Read the normalized datasource path recorded by the pool adapter.
        #
        # @return The normalized database filename.
        #

        return ${:path}
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: