Ejemplo n.º 1
0
/* StoreSchema
 */
rc_t VDatabaseStoreSchema ( VDatabase *self )
{
    /* open schema node */
    KMDataNode *node;
    rc_t rc = KMetadataOpenNodeUpdate ( self -> meta, & node, "schema" );
    if ( rc == 0 )
    {
        size_t num_writ;
        char expr [ 256 ];
        rc = VSchemaToText ( self -> schema, expr, sizeof expr - 1, & num_writ,
            "%N%V", self -> sdb -> name, self -> sdb -> version );
        if ( rc != 0 )
            LOGERR ( klogInt, rc, "failed to determine database schema" );
        else
        {
            expr [ num_writ ] = 0;
            rc = KMDataNodeWriteAttr ( node, "name", expr );
            if ( rc != 0 )
                PLOGERR (klogInt, ( klogInt, rc, "failed to write database type '$(expr)'", "expr=%s", expr ));
            else
            {
                /* truncate existing schema */
                rc = KMDataNodeWrite ( node, "", 0 );
                if ( rc == 0 )
                {
                    rc = VSchemaDump ( self -> schema, sdmCompact, expr,
                        ( rc_t ( CC * ) ( void*, const void*, size_t ) ) KMDataNodeAppend, node );
                }
                if ( rc != 0 )
                    PLOGERR (klogInt, ( klogInt, rc, "failed to write database schema '$(expr)'", "expr=%s", expr ));
            }
        }

        KMDataNodeRelease ( node );
    }
    return rc;
}
Ejemplo n.º 2
0
/* StoreSchema
 *  stores schema definition in metadata
 *
 *  <schema name="">...</schema>
 */
LIB_EXPORT rc_t VTableStoreSchema ( VTable *self )
{
    /* open schema node */
    KMDataNode *node;
    rc_t rc = KMetadataOpenNodeUpdate ( self -> meta, & node, "schema" );
    if ( rc == 0 )
    {
        size_t num_writ;
        char expr [ 256 ];
        rc = VSchemaToText ( self -> schema, expr, sizeof expr - 1, & num_writ,
            "%N%V", self -> stbl -> name, self -> stbl -> version );
        if ( rc != 0 )
            LOGERR ( klogInt, rc, "failed to determine table schema" );
        else
        {
            expr [ num_writ ] = 0;

            /* if table has a default view declaration,
               store the table information under a new attribute */
            if ( self -> stbl -> dflt_view != NULL )
            {
                uint32_t type;
                const SNameOverload *name;
                const STable *view = VSchemaFind ( self -> schema, & name, & type,
                    self -> stbl -> dflt_view-> addr, __func__, false );
                if ( view == NULL )
                {
                    rc = RC ( rcVDB, rcTable, rcUpdating, rcSchema, rcNotFound );
                    PLOGERR ( klogInt, ( klogInt, rc, "failed to locate default view schema '$(expr)'",
                                         "expr=%S", self -> stbl -> dflt_view ));
                }
                else
                {
                    rc = KMDataNodeWriteAttr ( node, "table", expr );
                    if ( rc != 0 )
                        PLOGERR ( klogInt, ( klogInt, rc, "failed to write table type '$(expr)'", "expr=%s", expr ));
                    else
                    {
                        rc = VSchemaToText ( self -> schema, expr, sizeof expr - 1, & num_writ,
                            "%N%V", view -> name, view -> version );
                        if ( rc != 0 )
                            LOGERR ( klogInt, rc, "failed to determine table default view schema" );
                        else
                            expr [ num_writ ] = 0;
                    }
                }
            }

            if ( rc == 0 )
            {
                rc = KMDataNodeWriteAttr ( node, "name", expr );
                if ( rc != 0 )
                    PLOGERR ( klogInt, ( klogInt, rc, "failed to write table name '$(expr)'", "expr=%s", expr ));
            }
            if ( rc == 0 )
            {
                /* truncate existing schema */
                rc = KMDataNodeWrite ( node, "", 0 );
                if ( rc == 0 )
                {
                    rc = VSchemaDump ( self -> schema, sdmCompact, expr,
                        ( rc_t ( CC * ) ( void*, const void*, size_t ) ) KMDataNodeAppend, node );
                }
                if ( rc != 0 )
                    PLOGERR ( klogInt, ( klogInt, rc, "failed to write table schema '$(expr)'", "expr=%s", expr ));
            }
        }

        KMDataNodeRelease ( node );
    }
    return rc;
}