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;
}
Ejemplo n.º 3
0
static rc_t DumpSchema_to( VSchema *my_schema, const context *ctx )
{
    struct schema_dumper_state_t st;
    rc_t rc = 0;
    unsigned i;

    static const char *preamble[] = {
        "/*===========================================================================",
        "*",
        "*                             PUBLIC DOMAIN NOTICE",
        "*                National Center for Biotechnology Information",
        "*",
        "*   This software/database is a \"United States Government Work\" under the",
        "*   terms of the United States Copyright Act.  It was written as part of",
        "*   the author's official duties as a United States Government employee and",
        "*   thus cannot be copyrighted.  This software/database is freely available",
        "*   to the public for use. The National Library of Medicine and the U.S.",
        "*   Government have not placed any restriction on its use or reproduction.",
        "*",
        "*   Although all reasonable efforts have been taken to ensure the accuracy",
        "*   and reliability of the software and data, the NLM and the U.S.",
        "*   Government do not and cannot warrant the performance or results that",
        "*   may be obtained by using this software or data. The NLM and the U.S.",
        "*   Government disclaim all warranties, express or implied, including",
        "*   warranties of performance, merchantability or fitness for any particular",
        "*   purpose.",
        "*",
        "*   Please cite the author in any work or product based on this material.",
        "*",
        "* ===========================================================================",
        "*",
        "* THIS IS AN AUTO-GENERATED FILE - DO NOT EDIT",
        "*/",

        "#include \"sraschema-priv.h\"",
        "const char sra_schema_text[] = {"
    };
    static const char *postamble[] = {
        "};",
        "const size_t sra_schema_size =",
    };
    
    memset(&st, 0, sizeof(st));
    
    st.fp = ctx->output_file ? fopen(ctx->output_file, "w") : stdout;
    
    if (st.fp == NULL)
        return RC(rcSRA, rcSchema, rcFormatting, rcFile, rcNotOpen);

    for ( i = 0; i != sizeof(preamble) / sizeof(preamble[0]); ++i)
        fprintf(st.fp, "%s\n", preamble[i]);
    
    rc = VSchemaDump( my_schema, sdmCompact, NULL, my_flush, &st );
    if (rc == 0) {
        if (st.line_pos != 0)
            schema_dumper_state_write_line(&st);
        fprintf(st.fp, "%s\n", postamble[0]);
        fprintf(st.fp, "%s %u;\n", postamble[1], st.out_size);
    }
    if (st.fp != stdout)
        fclose(st.fp);
    return rc;
}