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)  );
}
Exemplo n.º 2
0
void SG_treendx__get_all_paths(SG_context* pCtx, SG_treendx* pTreeNdx, const char* psz_gid, SG_stringarray ** ppResults)
{
	sqlite3_stmt* pStmt = NULL;
	SG_stringarray * pResults = NULL;
	int rc;

	SG_ERR_CHECK_RETURN(  SG_gid__argcheck(pCtx, psz_gid)  );

	SG_ERR_CHECK(  SG_STRINGARRAY__ALLOC(pCtx,&pResults, 1)  );
	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pTreeNdx->psql, &pStmt, "SELECT strpath FROM treendx WHERE gid='%s' ORDER BY strpath;", psz_gid)  );
	while ((rc = sqlite3_step(pStmt)) == SQLITE_ROW)
	{
		const char* pszPath = (const char*) sqlite3_column_text(pStmt, 0);
		SG_ERR_CHECK(  SG_stringarray__add(pCtx, pResults, pszPath)   );
	}
	if (rc != SQLITE_DONE)
	{
		SG_ERR_THROW(SG_ERR_SQLITE(rc));
	}

	SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );
	*ppResults = pResults;

	return;
fail:
	if (pStmt)
	{
		SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
	}

}
void sg_wc_db__gid__delete_all_tmp(SG_context * pCtx,
								   sg_wc_db * pDb)
{
	sqlite3_stmt * pStmt = NULL;

#if TRACE_WC_DB
	SG_ERR_IGNORE(  SG_console(pCtx, SG_CS_STDERR,
							   "sg_wc_db__gid__delete_all_tmp: had %d tmp gids.\n",
							   pDb->nrTmpGids)  );
#endif

	if (pDb->nrTmpGids == 0)
		return;

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("DELETE FROM tbl_gid WHERE tmp != 0"))  );
	SG_ERR_CHECK(  sg_sqlite__step(pCtx, pStmt, SQLITE_DONE)  );
	SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );

	pDb->nrTmpGids = 0;

	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}
void sg_wc_db__gid__prepare_toggle_tmp_stmt(SG_context * pCtx,
											sg_wc_db * pDb,
											SG_uint64 uiAliasGid,
											SG_bool bIsTmp,
											sqlite3_stmt ** ppStmt)
{
	sqlite3_stmt * pStmt = NULL;

#if TRACE_WC_DB
	{
		SG_int_to_string_buffer bufui64;

		SG_ERR_IGNORE(  SG_console(pCtx, SG_CS_STDERR, "sg_wc_db__gid__prepare_toggle_tmp_stmt: %s --> %c\n",
								   SG_uint64_to_sz(uiAliasGid, bufui64),
								   ((bIsTmp) ? 'T' : 'F'))  );
	}
#endif

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("UPDATE tbl_gid SET tmp = ? WHERE alias_gid = ?"))  );
	SG_ERR_CHECK(  sg_sqlite__bind_int( pCtx, pStmt, 1, bIsTmp)  );
	SG_ERR_CHECK(  sg_sqlite__bind_int64(pCtx, pStmt, 2, uiAliasGid)  );

	*ppStmt = pStmt;
	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)  );
}
void sg_wc_db__branch__detach(SG_context * pCtx,
                              sg_wc_db * pDb)
{
    sqlite3_stmt * pStmt = NULL;

#if TRACE_WC_DB
    SG_ERR_IGNORE(  SG_console(pCtx, SG_CS_STDERR,
                               "sg_wc_db__branch__detach:\n")  );
#endif

    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)  );
    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)  );
    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);
}
/**
 * QUEUE a SQL statment to insert a row into the named "pc" table.
 * Note that this DOES NOT alter the associated LVI or even refer
 * to it.  This is important since the main-line MERGE has probably
 * already done that.  And we are being called to build an alternate
 * "pc" table after the fact.
 *
 */
void sg_wc_tx__journal__append__insert_fake_pc_stmt(SG_context * pCtx,
													SG_wc_tx * pWcTx,
													const sg_wc_db__cset_row * pCSetRow,
													const sg_wc_db__pc_row * pPcRow)
{
	sqlite3_stmt * pStmt = NULL;

	SG_ERR_CHECK(  sg_wc_db__pc__prepare_insert_stmt(pCtx, pWcTx->pDb, pCSetRow, pPcRow, &pStmt)  );
	SG_ERR_CHECK(  SG_vector__append(pCtx, pWcTx->pvecJournalStmts, (void *)pStmt, NULL)  );
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}
void sg_wc_db__gid__get_gid_from_alias2(SG_context * pCtx,
										sg_wc_db * pDb,
										SG_uint64 uiAliasGid,
										char ** ppszGid,
										SG_bool * pbIsTmp)
{
	sqlite3_stmt * pStmt = NULL;
	char * pszGid = NULL;
	int rc;
	SG_bool bIsTmp;

	// TODO 2011/08/01 Should this check for "*null*" and offer to return NULL ?

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("SELECT gid,tmp FROM tbl_gid WHERE alias_gid = ?"))  );
	SG_ERR_CHECK(  sg_sqlite__bind_int64(pCtx, pStmt, 1, uiAliasGid)  );

	if ((rc=sqlite3_step(pStmt)) != SQLITE_ROW)
	{
		SG_int_to_string_buffer bufui64;
		SG_ERR_THROW2(  SG_ERR_SQLITE(rc),
						(pCtx, "sg_wc_db:tbl_gid can't find alias %s.",
						 SG_uint64_to_sz(uiAliasGid, bufui64))  );
	}

	SG_ERR_CHECK(  SG_STRDUP(pCtx, (const char *)sqlite3_column_text(pStmt, 0), &pszGid)  );
	bIsTmp = (sqlite3_column_int(pStmt, 1) != 0);
	SG_ERR_CHECK(  sg_sqlite__finalize(pCtx, pStmt)  );

	*ppszGid = pszGid;
	*pbIsTmp = bIsTmp;
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
	SG_NULLFREE(pCtx, pszGid);
}
void sg_wc_db__tne__prepare_delete_stmt(SG_context * pCtx,
										sg_wc_db * pDb,
										sg_wc_db__cset_row * pCSetRow,
										SG_uint64 uiAliasGid,
										sqlite3_stmt ** ppStmt)
{
	sqlite3_stmt * pStmt = NULL;

	SG_ERR_CHECK_RETURN(  sg_wc_db__tx__assert(pCtx, pDb)  );

	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("DELETE FROM %s WHERE alias_gid = ?"),
									  pCSetRow->psz_tne_table_name)  );
	SG_ERR_CHECK(  sg_sqlite__bind_int64(pCtx, pStmt, 1, uiAliasGid)  );

	*ppStmt = pStmt;
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}
void sg_wc_db__tne__prepare_insert_stmt(SG_context * pCtx,
										sg_wc_db * pDb,
										sg_wc_db__cset_row * pCSetRow,
										SG_uint64 uiAliasGid,
										SG_uint64 uiAliasGid_Parent,
										const SG_treenode_entry * pTNE,
										sqlite3_stmt ** ppStmt)
{
	sqlite3_stmt * pStmt = NULL;

	SG_ERR_CHECK_RETURN(  sg_wc_db__tx__assert(pCtx, pDb)  );

	// Note: This pStmt is used by wc5queue/wc5apply so we can't
	//       cache this statement and reset/re-bind the fields
	//       like we do during a __load_named_cset().
	SG_ERR_CHECK(  sg_sqlite__prepare(pCtx, pDb->psql, &pStmt,
									  ("INSERT OR REPLACE INTO %s"
									   "  ("
									   "    alias_gid,"
									   "    alias_gid_parent,"
									   "    hid,"
									   "    type,"
									   "    attrbits,"
									   "    entryname"
									   "  )"
									   "  VALUES"
									   "  (?, ?, ?, ?, ?, ?)"),
									  pCSetRow->psz_tne_table_name)  );
	SG_ERR_CHECK(  sg_wc_db__tne__bind_insert(pCtx,
											  pStmt,
											  uiAliasGid,
											  uiAliasGid_Parent,
											  pTNE)  );

	*ppStmt = pStmt;
	return;

fail:
	SG_ERR_IGNORE(  sg_sqlite__finalize(pCtx, pStmt)  );
}