Exemplo n.º 1
0
// TODO consider the possible perf benefits of changing this routine
// to accept lots of changeset ids instead of just one, so it
// can handle them all at once.
void SG_treendx__update__multiple(
        SG_context* pCtx,
        SG_treendx* pTreeNdx,
        SG_stringarray* psa
        )
{
    SG_changeset* pcs = NULL;
	sqlite3_stmt* pStmt = NULL;
    SG_vhash* pvh_treepaths = NULL;
    SG_uint32 count_treepaths = 0;
    SG_uint32 count_changesets = 0;
    SG_uint32 ics = 0;

	SG_NULLARGCHECK_RETURN(psa);
	SG_NULLARGCHECK_RETURN(pTreeNdx);

    SG_ERR_CHECK(  SG_stringarray__count(pCtx, psa, &count_changesets)  );

    SG_ERR_CHECK(  sg_sqlite__exec__va(pCtx, pTreeNdx->psql, "BEGIN TRANSACTION; ")  );
    SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pTreeNdx->psql, &pStmt, "INSERT OR IGNORE INTO treendx (gid, strpath) VALUES (?, ?)")  );
    for (ics=0; ics<count_changesets; ics++)
    {
        const char* psz_hid = NULL;
        SG_uint32 i = 0;

        SG_ERR_CHECK(  SG_stringarray__get_nth(pCtx, psa, ics, &psz_hid)  );
        SG_ERR_CHECK(  SG_changeset__load_from_repo(pCtx, pTreeNdx->pRepo, psz_hid, &pcs)  );
        SG_ERR_CHECK(  SG_changeset__get_treepaths(pCtx, pcs, &pvh_treepaths)  );

        if (pvh_treepaths)
        {
            SG_ERR_CHECK(  SG_vhash__count(pCtx, pvh_treepaths, &count_treepaths)  );


            for (i=0; i<count_treepaths; i++)
            {
                const char* psz_gid = NULL;
                const SG_variant* pv = NULL;
                const char* psz_path = NULL;

                SG_ERR_CHECK(  SG_vhash__get_nth_pair(pCtx, pvh_treepaths, i, &psz_gid, &pv)  );
                SG_ERR_CHECK(  SG_variant__get__sz(pCtx, pv, &psz_path)  );

                SG_ERR_CHECK(  sg_sqlite__reset(pCtx, pStmt)  );
                SG_ERR_CHECK(  sg_sqlite__clear_bindings(pCtx, pStmt)  );
                SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx, pStmt, 1, psz_gid)  );
                SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx, pStmt, 2, psz_path)  );
                SG_ERR_CHECK(  sg_sqlite__step(pCtx, pStmt, SQLITE_DONE)  );
            }
        }
        SG_CHANGESET_NULLFREE(pCtx, pcs);
    }
    SG_ERR_CHECK(  sg_sqlite__nullfinalize(pCtx, &pStmt)  );
    SG_ERR_CHECK(  sg_sqlite__exec__va(pCtx, pTreeNdx->psql, "COMMIT TRANSACTION; ")  );

fail:
    SG_CHANGESET_NULLFREE(pCtx, pcs);
}
void sg_wc_db__gid__get_alias_from_gid(SG_context * pCtx,
									   sg_wc_db * pDb,
									   const char * pszGid,
									   SG_uint64 * puiAliasGid)
{
	sqlite3_stmt * pStmt = NULL;
	SG_uint64 uiAliasGid = 0;
	int rc;

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("SELECT alias_gid FROM tbl_gid WHERE gid = ?"))  );
	SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx, pStmt, 1,
										((pszGid && *pszGid) ? pszGid : SG_WC_DB__GID__NULL_ROOT))  );

	if ((rc=sqlite3_step(pStmt)) != SQLITE_ROW)
		SG_ERR_THROW2(  SG_ERR_SQLITE(rc),
						(pCtx, "sg_wc_db:tbl_gid can't find gid %s.", pszGid)  );
	
	uiAliasGid = (SG_uint64)sqlite3_column_int64(pStmt, 0);
	SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );

	*puiAliasGid = uiAliasGid;
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}
/**
 * Insert a row into the "tbl_gid" table WITH A FIXED AND KNOWN ALIAS.
 * We only use this for the special pseudo-gids.  This overrides the
 * auto-increment feature on the alias_gid field.
 */
void sg_wc_db__gid__insert_known(SG_context * pCtx,
								 sg_wc_db * pDb,
								 SG_uint64 uiAliasGid,
								 const char * pszGid)
{
	sqlite3_stmt * pStmt = NULL;

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("INSERT OR REPLACE INTO tbl_gid ( alias_gid, gid, tmp ) VALUES (?, ?, 0)"))  );
	SG_ERR_CHECK(  sg_sqlite__bind_int64(pCtx, pStmt, 1, uiAliasGid)  );
	SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx,  pStmt, 2, pszGid)  );
	SG_ERR_CHECK(  sg_sqlite__step(pCtx, pStmt, SQLITE_DONE)  );
	SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}
static void _sg_wc_db__gid__insert(SG_context * pCtx,
								   sg_wc_db * pDb,
								   const char * pszGid,
								   SG_bool bIsTmp)
{
	sqlite3_stmt * pStmt = NULL;

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("INSERT OR IGNORE INTO tbl_gid ( gid, tmp ) VALUES ( ?, ? )"))  );
	SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx, pStmt, 1, pszGid)  );
	SG_ERR_CHECK(  sg_sqlite__bind_int( pCtx, pStmt, 2, bIsTmp)  );
	SG_ERR_CHECK(  sg_sqlite__step(pCtx, pStmt, SQLITE_DONE)  );
	SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}
/**
 * Attach/tie the WD to the given branch.
 *
 * We optionally validate the name (so that they can attach to
 * old pre-2.0 branches that allowed punctuation characters).
 *
 * We optionally verify that the branch name does/does not exist
 * (corresponding to --attach or --attach-new).
 *
 * We don't care one way or the other and only provide these
 * options as a service to the caller.  You probably don't need
 * to use them since the caller should have already validated/verified
 * all this before while they were parsing the user's input.
 *
 */
void sg_wc_db__branch__attach(SG_context * pCtx,
                              sg_wc_db * pDb,
                              const char * pszBranchName,
                              SG_vc_branches__check_attach_name__flags flags,
                              SG_bool bValidateName)
{
    sqlite3_stmt * pStmt = NULL;
    char * pszNormalizedBranchName = NULL;	// however, we always normalize the name

    SG_NONEMPTYCHECK_RETURN( pszBranchName );

#if TRACE_WC_DB
    SG_ERR_IGNORE(  SG_console(pCtx, SG_CS_STDERR,
                               "sg_wc_db__branch__attach: [flags %d][validate %d] %s\n",
                               (SG_uint32)flags, bValidateName, pszBranchName)  );
#endif

    SG_ERR_CHECK(  SG_vc_branches__check_attach_name(pCtx, pDb->pRepo, pszBranchName,
                   flags, bValidateName,
                   &pszNormalizedBranchName)  );

    SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
                                      ("INSERT OR REPLACE INTO tbl_branch ( id, name ) VALUES ( ?, ? )"))  );
    SG_ERR_CHECK(  sg_sqlite__bind_int(pCtx, pStmt, 1, ID_KEY)  );
    // probably unnecessary since __check_attach_name should have thrown.
    if (pszNormalizedBranchName && *pszNormalizedBranchName)
        SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx, pStmt, 2, pszNormalizedBranchName)  );
    else
        SG_ERR_CHECK(  sg_sqlite__bind_null(pCtx, pStmt, 2)  );

    SG_ERR_CHECK(  sg_sqlite__step(pCtx, pStmt, SQLITE_DONE)  );
    SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );
    SG_NULLFREE(pCtx, pszNormalizedBranchName);
    return;

fail:
    SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
    SG_NULLFREE(pCtx, pszNormalizedBranchName);
}
/**
 * Get the TNE Row for the (uiAliasGirParent, pszEntryname) pair.
 * This should not have any of the entryname ambiguity problems
 * that such a request on the live view might have.
 *
 */
void sg_wc_db__tne__get_row_by_parent_alias_and_entryname(SG_context * pCtx,
														  sg_wc_db * pDb,
														  const sg_wc_db__cset_row * pCSetRow,
														  SG_uint64 uiAliasGidParent,
														  const char * pszEntryname,
														  SG_bool * pbFound,
														  sg_wc_db__tne_row ** ppTneRow)
{
	sqlite3_stmt * pStmt = NULL;
	sg_wc_db__tne_row * pTneRow = NULL;
	int rc;

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("SELECT"
									   "    alias_gid,"			// 0
									   "    hid,"				// 1
									   "    type,"				// 2
									   "    attrbits"			// 3
									   "  FROM %s"
									   "  WHERE alias_gid_parent = ?"
									   "    AND entryname = ?"),
									  pCSetRow->psz_tne_table_name)  );
	SG_ERR_CHECK(  sg_sqlite__bind_int64(pCtx, pStmt, 1, uiAliasGidParent)  );
	SG_ERR_CHECK(  sg_sqlite__bind_text(pCtx,  pStmt, 2, pszEntryname)  );

	if ((rc=sqlite3_step(pStmt)) != SQLITE_ROW)
	{
		SG_int_to_string_buffer bufui64;

		if ((rc == SQLITE_DONE) && pbFound)
		{
			*pbFound = SG_FALSE;
			*ppTneRow = NULL;
			goto done;
		}

		SG_ERR_THROW2(  SG_ERR_SQLITE(rc),
						(pCtx, "sg_wc_db:%s can't find tne row for parent alias %s and entryname '%s'.",
						 pCSetRow->psz_tne_table_name,
						 SG_uint64_to_sz(uiAliasGidParent, bufui64),
						 pszEntryname)  );
	}

	SG_ERR_CHECK(  sg_wc_db__tne_row__alloc(pCtx, &pTneRow)  );

	pTneRow->p_s->uiAliasGid       = (SG_uint64)sqlite3_column_int64(pStmt, 0);
	pTneRow->p_s->uiAliasGidParent = uiAliasGidParent;
	SG_ERR_CHECK(  SG_STRDUP(pCtx, (const char *)sqlite3_column_text(pStmt, 1), &pTneRow->p_d->pszHid)  );
	pTneRow->p_s->tneType          = (SG_uint32)sqlite3_column_int(pStmt, 2);
	pTneRow->p_d->attrbits         = (SG_uint64)sqlite3_column_int64(pStmt, 3);
	SG_ERR_CHECK(  SG_STRDUP(pCtx, pszEntryname, &pTneRow->p_s->pszEntryname)  );

    SG_ERR_CHECK(  sg_sqlite__nullfinalize(pCtx, &pStmt)  );

#if TRACE_WC_DB
	SG_ERR_IGNORE(  SG_console(pCtx, SG_CS_STDERR, "sg_wc_db__tne__get_row_by_parent_alias_and_entryname: found:\n")  );
	SG_ERR_IGNORE(  sg_wc_db__debug__tne_row__print(pCtx, pTneRow)  );
#endif

	if (pbFound)
		*pbFound = SG_TRUE;
	*ppTneRow = pTneRow;
	return;

fail:
	SG_WC_DB__TNE_ROW__NULLFREE(pCtx, pTneRow);
done:
    SG_ERR_IGNORE(  sg_sqlite__nullfinalize(pCtx, &pStmt)  );
}