Exemplo n.º 1
0
//存储一个已有的模型
BOOL CDiagRunView::save_one_model(CString model_name)
{
	CString Datasource;
	HRESULT hr;
	char sql_cmd[500];
	int i;
	int updated;

	m_sqlserver_db.dbname=model_name;
	Datasource.Format("Provider=SQLOLEDB.1; Data Source=%s; Initial Catalog=%s; User ID=%s; PWD=%s", m_sqlserver_db.dbsrc,m_sqlserver_db.dbname,m_sqlserver_db.user,m_sqlserver_db.pass);
	hr = m_pConnection->Open(_bstr_t(LPCTSTR(Datasource)),"","",-1);///连接数据库
	
	//1. 处理OBJECT_Table
/*
	m_pRecordset.CreateInstance("ADODB.Recordset");
	strcpy(sql_cmd,"delete from OBJECT_Table");
	exec_sql(sql_cmd);
	m_pRecordset.Release();

	//插入数据
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("OBJECT_Table",_variant_t((IDispatch*)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdTable);
	m_pRecordset->AddNew();
	m_pRecordset->PutCollect("对象号", _variant_t(long(2)));
	m_pRecordset->PutCollect("任务号", _variant_t(long(2)));
	m_pRecordset->PutCollect("对象名称", _variant_t(""));
	m_pRecordset->PutCollect("对象描述", _variant_t(""));
	//对象名称 对象描述
	
	m_pRecordset->Update();
	m_pRecordset.Release();
*/

	//2. 处理NODES_Table
	m_pRecordset.CreateInstance("ADODB.Recordset");
	strcpy(sql_cmd,"delete from NODES_Table");
	exec_sql(sql_cmd);
	m_pRecordset.Release();

	//插入数据
	updated=0;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("NODES_Table",_variant_t((IDispatch*)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdTable);
	int count=NodeArray.GetSize();
	for (i=0;i<count;i++)
	{
		Node *pNode=NodeArray.GetAt(i);
		if (!pNode->IsDeleted())
		{
			m_pRecordset->AddNew();
			updated=1;
			m_pRecordset->PutCollect("结点号", _variant_t(long(pNode->GetID())));
			//m_pRecordset->PutCollect("任务号", _variant_t(long(2)));
			//m_pRecordset->PutCollect("对象序号", _variant_t(long(2)));
			m_pRecordset->PutCollect("层号", _variant_t(long(pNode->layer)));
			//m_pRecordset->PutCollect("层中序号", _variant_t(long(2)));
			if (pNode->inLine==NULL)
				m_pRecordset->PutCollect("父结点号", _variant_t(long(-1)));
			else
				m_pRecordset->PutCollect("父结点号", _variant_t(long(pNode->inLine->parentNode->GetID())));
			m_pRecordset->PutCollect("结点名称", _variant_t(pNode->GetName()));
			//m_pRecordset->PutCollect("结点中文名称", _variant_t(pNode->GetName()));
			m_pRecordset->PutCollect("结点描述", _variant_t(pNode->comment));
			m_pRecordset->PutCollect("X坐标", _variant_t(long(pNode->GetTopLeft().x + pNode->GetScrPos().x)));
			m_pRecordset->PutCollect("Y坐标", _variant_t(long(pNode->GetTopLeft().y + pNode->GetScrPos().y)));
			m_pRecordset->PutCollect("图形形状", _variant_t(short(Get_Com_ID(pNode))));
			//m_pRecordset->PutCollect("图形线型", _variant_t(short(1)));
			//m_pRecordset->PutCollect("图形线宽", _variant_t(short(1)));
			m_pRecordset->PutCollect("比例", _variant_t(m_model_scale));
			//m_pRecordset->PutCollect("图形颜色", _variant_t(short(1)));
			//m_pRecordset->PutCollect("字体", _variant_t(short(1)));
			//m_pRecordset->PutCollect("字号", _variant_t(short(1)));
			//m_pRecordset->PutCollect("字体颜色", _variant_t(short(1)));
			//m_pRecordset->PutCollect("填充类型", _variant_t(short(1)));
			//m_pRecordset->PutCollect("填充颜色", _variant_t(short(1)));
			//m_pRecordset->PutCollect("正在运行", _variant_t(short(1)));
			//m_pRecordset->PutCollect("分析标志", _variant_t(short(1)));
			m_pRecordset->PutCollect("删除标志",_variant_t(short(0)));
		}
	}
	if (updated==1)
		m_pRecordset->Update();
	m_pRecordset.Release();


	//3. 处理LINE_Table
	m_pRecordset.CreateInstance("ADODB.Recordset");
	strcpy(sql_cmd,"delete from LINE_Table");
	exec_sql(sql_cmd);
	m_pRecordset.Release();

	//插入数据
	updated=0;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("LINE_Table",_variant_t((IDispatch*)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdTable);
	count=LineArray.GetSize();
	CPoint tempPoint;
	for (i=0;i<count;i++)
	{
		Line *pLine=LineArray.GetAt(i);
		if (!pLine->IsDeleted())
		{
			m_pRecordset->AddNew();
			updated=1;
			m_pRecordset->PutCollect("连线号", _variant_t(long(pLine->GetID())));
			//m_pRecordset->PutCollect("任务号", _variant_t(long(2)));
			//m_pRecordset->PutCollect("对象序号", _variant_t(long(2)));
			m_pRecordset->PutCollect("父结点号", _variant_t(long(pLine->parentNode->GetID())));
			//m_pRecordset->PutCollect("父结点类型", _variant_t(short(1)));
			m_pRecordset->PutCollect("父结点层号", _variant_t(long(pLine->parentNode->layer)));
			//m_pRecordset->PutCollect("父结点层中号", _variant_t(long(2)));
			m_pRecordset->PutCollect("子结点号", _variant_t(long(pLine->sonNode->GetID())));
			//m_pRecordset->PutCollect("子结点类型", _variant_t(short(1)));
			m_pRecordset->PutCollect("子结点层号", _variant_t(long(pLine->sonNode->layer)));
			//m_pRecordset->PutCollect("子结点层中号", _variant_t(long(2)));
			m_pRecordset->PutCollect("连线名称", _variant_t(pLine->GetName()));
			//m_pRecordset->PutCollect("连线中文名称", _variant_t(pLine->GetName()));
			m_pRecordset->PutCollect("连线描述", _variant_t(pLine->comment));

			pLine->parentNode->GetCom()->GetBottomCenter((unsigned long)&tempPoint);
			m_pRecordset->PutCollect("连线起点X", _variant_t(long(tempPoint.x+pLine->parentNode->GetScrPos().x)));
			m_pRecordset->PutCollect("连线起点Y", _variant_t(long(tempPoint.y+pLine->parentNode->GetScrPos().y)));
			pLine->sonNode->GetCom()->GetTopCenter((unsigned long)&tempPoint);
			m_pRecordset->PutCollect("连线终点X", _variant_t(long(tempPoint.x+pLine->sonNode->GetScrPos().x)));
			m_pRecordset->PutCollect("连线终点Y", _variant_t(long(tempPoint.y+pLine->sonNode->GetScrPos().y)));

			m_pRecordset->PutCollect("连线类型", _variant_t(short(pLine->GetGraphInfo()->normal_penStyle)));
			m_pRecordset->PutCollect("连线宽度", _variant_t(short(pLine->GetGraphInfo()->normal_linewidth)));
			m_pRecordset->PutCollect("连线颜色", _variant_t(short(pLine->GetGraphInfo()->normal_linecolor)));
			//m_pRecordset->PutCollect("箭头类型", _variant_t(short(1)));
			//m_pRecordset->PutCollect("分析标志", _variant_t(short(1)));
			m_pRecordset->PutCollect("删除标志",_variant_t(short(0)));
		}
	}
	if (updated==1)
		m_pRecordset->Update();
	m_pRecordset.Release();

	if(m_pConnection->State)
		m_pConnection->Close();
	return true;
}
Exemplo n.º 2
0
int main ( int argc, char **argv )
{
	int ret = check_args(argc, argv);
	if (ret) {
		printf("invalid argument error!\n");
		return -1;
	}

	if (new_db) {
		unlink(db_name);
	}
	soft_heap_limit = sqlite3_soft_heap_limit64(soft_heap_limit);
	if ( soft_heap_limit < 0 ) {
		ret = soft_heap_limit;
		printf("sqlite3_soft_heap_limit() error: %x", ret);
	} else {
		printf("sqlite3_soft_heap_limit64(): prev: %ld (byte) is set to: %lld (byte).\n", soft_heap_limit, sqlite3_soft_heap_limit64(-1));
	}


	sqlite3 *db = NULL;
	ret = sqlite3_open_v2 (db_name, &db, 
		SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE, 
		NULL);
	if ( ret != SQLITE_OK )
	{
		printf ( "Can't open database: %s\n", sqlite3_errmsg ( db ) );
		return -1;
	}

	if (new_db) {
		// create table, transaction is useful for speed optimization
		printf("== execute sql_create_table ==\n");
		exec_sql_list_in_transaction(db, sql_create_table);
	} else {
		printf("== use existing db: %s ==\n", db_name);
	}

	int sql_count = 0;
	int trans_count = 0;
	uint64_t total_time = 0;
	int64_t max_highwater = 0;
	uint64_t total_highwater = 0;
	do {
		uint64_t start_time = sceKernelGetProcessTime();
		ret = start_transaction(db);
		if (ret) {
			printf("start_transaction() err: %x\n", ret);
			break;
		}
		int trans_sql_count = 0;
		trans_count++;
		do {
			fill_sql(sql_count);
			ret = exec_sql(db, sql_buf);
			if (ret) {
				break;
			}
			sql_count++;
			trans_sql_count++;
		} while (sql_count < total_sql_num 
				 && trans_sql_count < max_sql_in_trans);

		ret = end_transaction(db, ret);
		if (ret) {
			printf("end_transaction() err: %x\n", ret);
			break;
		}
		uint64_t end_time = sceKernelGetProcessTime();
		total_time += end_time - start_time;

		printf("[%d] th transaction is end. operated sql num: %d, time: %lu (us)", trans_count, sql_count, end_time - start_time);
		printf("\t: sqlite3_memory_used(): %lld", sqlite3_memory_used());
		int64_t highwater = sqlite3_memory_highwater(true);
		if (max_highwater < highwater) {
			max_highwater = highwater;
		}
		total_highwater += highwater;
		printf("\t: sqlite3_memory_highwater(true): %ld\n", highwater);
	} while (sql_count < total_sql_num);
	printf("operated sql count: %d, transaction count: %d\n", sql_count, trans_count);
	printf("total elapsed time: %lu (us)\n", total_time);
	if (trans_count != 0) {
		printf("maximum memory highwater: %ld, average high water / transaction: %lu\n", max_highwater, total_highwater / trans_count);
	}

/*
	// select
	const char *sql_select[] =
	{
		"SELECT * FROM tbl1;", // select all
		"SELECT id, c1_int FROM tbl1 WHERE 1 < id;", // select 1 < id
		NULL, // terminater
	};
	printf("\n== execute sql_select ==\n");
	exec_sql(db, sql_select);
*/
	sqlite3_close ( db );

	return 0;
}
Exemplo n.º 3
0
//======================================================================================
int get_cost_result(char *month,cost_collect_stru **cost_collect17, long long* plant_num)
{
	
	long long i,j,k;
	long long row_num;
	char sql_string[512];
	char plant_name[256];
	long plant_name_size;
	char check_cost[20];
	long check_cost_size;
	int  flag;
	char field[22];
	long field_size;

	//===================================================================================	
	/*取得计划考核结果
	*/
	//================================================================================

	printf("\n===================================================get_cost_result()开始=======\n\n");
	sprintf(sql_string,"select sname,sum(result_num) "
					   "from plan.plan.plan_check_result " 
					   "where sstyle='厂站' and "
					   "sdate like '%s-%%%%' group by sname "
					   "union all "
					   "select sstation,sum(result_num) from "
					   "plan.plan.unit_info, plan.plan.plan_check_result "
					   "where plan.plan.unit_info.sname=plan.plan.plan_check_result.sname "
				       "and sdate like '%s-%%%%' and sstyle = '机组' "
					   "group by plan.plan.unit_info.sstation",month,month);
	/*sprintf(sql_string,"select sstation,sum(result_num) from\
		plan.plan.unit_info,plan.plan.plan_check_result\
		where plan.plan.unit_info.sname=plan.plan.plan_check_result.sname\
		and ('%s-00'< sdate and sdate < '%s-32')\
		group by plan.plan.unit_info.sstation",month,month);*/
	printf("sql_string: %s\n",sql_string);
	
	if (exec_sql(sql_string) != 0)	
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中exec_sql()")
	
	if(DM_FALSE==dm_bind_column(hsmt,1,DM_DATA_CHAR,TYPE_DEFAULT,
		plant_name,sizeof(plant_name),&plant_name_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")

	if(DM_FALSE==dm_bind_column(hsmt,2,DM_DATA_DOUBLE,TYPE_DEFAULT,
		check_cost,sizeof(check_cost),&check_cost_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")

	if(DM_FALSE==dm_describe_row_num(hsmt,&row_num))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_describe_row_num()")
	
	if(0 >= row_num)
	{
		printf("my_Worning:get_cost_result()中返回结果集(曲线考核,扣分电场为0)出错,结果条目为0\n");
	}
	for(i=0;i<row_num;i++)
	{
		flag = dm_fetch_scroll(hsmt,DM_FETCH_NEXT,0);
		if(DM_FALSE==flag)
			PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_fetch_scroll()")

		else if(DM_NO_DATA_FOUND==flag)
		{
			PERROR_CLOSEDATABASE_EXIT(-1,"执行sql时,数据表中没有数据,get_cost_result()中dm_fetch_scroll()")
		}
		for(j=0;j<*plant_num;j++)
		{
			if(0 == strcmp(plant_name,(*cost_collect17)[j].plant_name))
			{								
				(*cost_collect17)[j].value[11] += (atof(check_cost))*COST_PER_MARK;
				break;
			}
		}
		
	}
	printf("只有计划曲线考核时:工厂的数目%lld\n",*plant_num);
	print_info(11, cost_collect17, plant_num);
	print_curtline("曲线违约考核计算完毕");
//===================================================================================	
/*取得非停考核结果
*/
//================================================================================
	print_curtline("非计划停机考核开始");
	sprintf(sql_string,"select sstation,sum(result_num) from\
		plan.plan.unit_info,plan.plan.unplan_shutdown_result\
		where plan.plan.unit_info.sname=plan.plan.unplan_shutdown_result.sname\
		and ('%s-00'< sdate and sdate < '%s-32')\
		group by plan.plan.unit_info.sstation",month,month);

	printf("\nsql_string: %s\n",sql_string);
	if (exec_sql(sql_string) != 0)
		PERROR_CLOSEDATABASE_EXIT(-1,"get_unplan_shutdown_result()中exec_sql()")

	if(DM_FALSE==dm_bind_column(hsmt,1,DM_DATA_CHAR,TYPE_DEFAULT,
		plant_name,sizeof(plant_name),&plant_name_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")

	if(DM_FALSE==dm_bind_column(hsmt,2,DM_DATA_DOUBLE,TYPE_DEFAULT,
		check_cost,sizeof(check_cost),&check_cost_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")

	if(DM_FALSE==dm_describe_row_num(hsmt,&row_num))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_describe_row_num()")

	if(0 >= row_num)
	{
		printf("my_Worning:get_cost_result()中非停考核结果集返回条目为0\n");
	}

	for(i=0;i<row_num;i++)
	{
		flag = dm_fetch_scroll(hsmt,DM_FETCH_NEXT,0);
		if(DM_FALSE==flag)
			PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_fetch_scroll()")
		else if(DM_NO_DATA_FOUND==flag)
		{
			PERROR_CLOSEDATABASE_EXIT(-1,"执行sql时,数据表中没有数据,get_cost_result()中dm_fetch_scroll()")
		}
		for(j=0;j<*plant_num;j++)
		{
			if(0==strcmp(plant_name,(*cost_collect17)[j].plant_name))
			{	
				(*cost_collect17)[j].value[5] += (atof(check_cost))*COST_PER_MARK;
				break;
			}
		}
		
	}
	printf("加入非停考核结果后:工厂的数目%lld\n",*plant_num);
	
	print_info(5, cost_collect17, plant_num);
	print_curtline("非计划停机考核计算完毕");
	
	
	//=========================================================================================
	//获得运行管理评分中十五项,并写cost_collect_stru结构数组
	//select sname ,sum(svalue)from plan.plan.run_manage_check where sdate<'2013-05-32'and sdate>'2013-05-00' group by sname
	//=========================================================================================
	print_curtline("运行管理评分中十五项计算开始");
	sprintf(sql_string,
			"select sname,sfield,sum(svalue)" 
			" from plan.plan.run_manage_check "			
			"where sdate < '%s-32' and sdate> '%s-00' "			
			"group by sname,sfield",month,month);

	printf("\n\nsql_string: %s\n",sql_string);
	
	if (exec_sql(sql_string) != 0)
		PERROR_CLOSEDATABASE_EXIT(-1,"执行sql时,数据表中没有数据,get_cost_result()中exec_sql()")

	if(DM_FALSE==dm_bind_column(hsmt,1,DM_DATA_CHAR,TYPE_DEFAULT,
		plant_name,sizeof(plant_name),&plant_name_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")

	if(DM_FALSE==dm_bind_column(hsmt,2,DM_DATA_CHAR,TYPE_DEFAULT,
		field,sizeof(field),&field_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")

	if(DM_FALSE==dm_bind_column(hsmt,3,DM_DATA_DOUBLE,TYPE_DEFAULT,
		check_cost,sizeof(check_cost),&check_cost_size,DM_DESC_PTR_LEN_DEFAULT))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_bind_column()")


	if(DM_FALSE==dm_describe_row_num(hsmt,&row_num))
		PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_describe_row_num()")

	if(0 >= row_num)
		printf("my_Wornding:运送考核汇总时运行管理考核费用返回条目为0\n");
	for(i=0;i<row_num;i++)
	{
		flag=dm_fetch_scroll(hsmt,DM_FETCH_NEXT,0);
		if(DM_FALSE==flag)
			PERROR_CLOSEDATABASE_EXIT(-1,"get_cost_result()中dm_fetch_scroll()")

		else if(DM_NO_DATA_FOUND==flag)
		{
			PERROR_CLOSEDATABASE_EXIT(-1,"执行sql时,数据表中没有数据,get_cost_result()中dm_fetch_scroll()")
		}

	printf("%-20s	%-20s	%-16.6f\n",plant_name,field,atof(check_cost));
		for(j=0;j<*plant_num;j++)
		{
			if(0==strcmp(plant_name,(*cost_collect17)[j].plant_name))
			{	
				for(k=0;k<COLLECT_NUM;k++)
				{
					if(!strcmp(cost_collect_order[k],field))
					{
						(*cost_collect17)[j].value[k] += atof(check_cost)*COST_PER_MARK;
					}
				}
				break;
			}
		}
		
		
	}

	print_curtline("运行管理考核十五项显示开始");
	
	printf("加入非停考核结果和计划曲线考核结果和十五项后,工厂的数目%lld\n",*plant_num);
	print_info(-2, cost_collect17,plant_num);

	print_curtline("运行管理考核十五项显示结束");
	print_curtline("运行管理考核十五项计算结束");
	return 0;
}
Exemplo n.º 4
0
svn_error_t *
svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
                 svn_sqlite__mode_t mode, const char * const statements[],
                 int latest_schema, const char * const *upgrade_sql,
                 apr_pool_t *result_pool, apr_pool_t *scratch_pool)
{
  SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
                                init_sqlite, NULL, scratch_pool));

  *db = apr_pcalloc(result_pool, sizeof(**db));

  SVN_ERR(internal_open(&(*db)->db3, path, mode, scratch_pool));

#ifdef SQLITE3_DEBUG
  sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
#endif
#ifdef SQLITE3_PROFILE
  sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
#endif

  /* Work around a bug in SQLite 3.7.7.  The bug was fixed in SQLite 3.7.7.1.

     See:

       Date: Sun, 26 Jun 2011 18:52:14 -0400
       From: Richard Hipp <*****@*****.**>
       To: General Discussion of SQLite Database <*****@*****.**>
       Cc: [email protected]
       Subject: Re: [sqlite] PRAGMA bug in 3.7.7 (but fine in 3.7.6.3)
       Message-ID: <*****@*****.**>
   */
  {
    int ignored_err = SQLITE_OK;
#if !SQLITE_VERSION_AT_LEAST(3,7,8) && defined(SQLITE_SCHEMA)
    if (!strcmp(sqlite3_libversion(), "3.7.7"))
      ignored_err = SQLITE_SCHEMA;
#endif

    SVN_ERR(exec_sql2(*db, "PRAGMA case_sensitive_like=1;", ignored_err));
  }

  SVN_ERR(exec_sql(*db,
              /* Disable synchronization to disable the explicit disk flushes
                 that make Sqlite up to 50 times slower; especially on small
                 transactions.

                 This removes some stability guarantees on specific hardware
                 and power failures, but still guarantees atomic commits on
                 application crashes. With our dependency on external data
                 like pristine files (Wc) and revision files (repository),
                 we can't keep up these additional guarantees anyway.

                 ### Maybe switch to NORMAL(1) when we use larger transaction
                     scopes */
              "PRAGMA synchronous=OFF;"
              /* Enable recursive triggers so that a user trigger will fire
                 in the deletion phase of an INSERT OR REPLACE statement.
                 Requires SQLite >= 3.6.18  */
              "PRAGMA recursive_triggers=ON;"));

#if SQLITE_VERSION_AT_LEAST(3,6,19) && defined(SVN_DEBUG)
  /* When running in debug mode, enable the checking of foreign key
     constraints.  This has possible performance implications, so we don't
     bother to do it for production...for now. */
  SVN_ERR(exec_sql(*db, "PRAGMA foreign_keys=ON;"));
#endif

  /* Store temporary tables in RAM instead of in temporary files, but don't
     fail on this if this option is disabled in the sqlite compilation by
     setting SQLITE_TEMP_STORE to 0 (always to disk) */
  svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));

  /* Validate the schema, upgrading if necessary. */
  if (upgrade_sql != NULL)
    SVN_ERR(check_format(*db, latest_schema, upgrade_sql, scratch_pool));

  /* Store the provided statements. */
  if (statements)
    {
      (*db)->statement_strings = statements;
      (*db)->nbr_statements = 0;
      while (*statements != NULL)
        {
          statements++;
          (*db)->nbr_statements++;
        }
      (*db)->prepared_stmts = apr_pcalloc(result_pool, (*db)->nbr_statements
                                                * sizeof(svn_sqlite__stmt_t *));
    }
  else
    (*db)->nbr_statements = 0;

  (*db)->state_pool = result_pool;
  apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);

  return SVN_NO_ERROR;
}
Exemplo n.º 5
0
svn_error_t *
svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
                 svn_sqlite__mode_t mode, const char * const statements[],
                 int unused1, const char * const *unused2,
                 apr_int32_t timeout,
                 apr_pool_t *result_pool, apr_pool_t *scratch_pool)
{
  SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
                                init_sqlite, NULL, scratch_pool));

  *db = apr_pcalloc(result_pool, sizeof(**db));

  SVN_ERR(internal_open(*db, path, mode, timeout, scratch_pool));

#if SQLITE_VERSION_NUMBER >= 3008000 && SQLITE_VERSION_NUMBER < 3009000
  /* disable SQLITE_ENABLE_STAT3/4 from 3.8.1 - 3.8.3 (but not 3.8.3.1+)
   * to prevent using it when it's buggy.
   * See: https://www.sqlite.org/src/info/4c86b126f2 */
  if (sqlite3_libversion_number() > 3008000 &&
      sqlite3_libversion_number() < 3008004 &&
      strcmp(sqlite3_sourceid(),"2014-02-11")<0)
    {
      sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, (*db)->db3, 0x800);
    }
#endif

#ifdef SVN_UNICODE_NORMALIZATION_FIXES
  /* Create extension buffers with space for 200 UCS-4 characters. */
  svn_membuf__create(&(*db)->sqlext_buf1, 800, result_pool);
  svn_membuf__create(&(*db)->sqlext_buf2, 800, result_pool);
  svn_membuf__create(&(*db)->sqlext_buf3, 800, result_pool);

  /* Register collation and LIKE and GLOB operator replacements. */
  SQLITE_ERR_CLOSE(sqlite3_create_collation((*db)->db3,
                                            "svn-ucs-nfd", SQLITE_UTF8,
                                            *db, collate_ucs_nfd),
                   db, scratch_pool);
  /* ### Is it really necessary to override these functions?
         I would assume the default implementation to be collation agnostic?
         And otherwise our implementation should be...

         The default implementation is in some cases index backed, while our
         implementation can't be. With an index based on the collation it could
         be. */
  SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "glob", 2,
                                           SQLITE_UTF8 | SQLITE_DETERMINISTIC,
                                           *db, glob_ucs_nfd, NULL, NULL),
                   db, scratch_pool);
  SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 2,
                                           SQLITE_UTF8 | SQLITE_DETERMINISTIC,
                                           *db, like_ucs_nfd, NULL, NULL),
                   db, scratch_pool);
  SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 3,
                                           SQLITE_UTF8 | SQLITE_DETERMINISTIC,
                                           *db, like_ucs_nfd, NULL, NULL),
                   db, scratch_pool);
#endif /* SVN_UNICODE_NORMALIZATION_FIXES */

#ifdef SQLITE3_DEBUG
  sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
#endif
#ifdef SQLITE3_PROFILE
  sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
#endif

  SVN_ERR_CLOSE(exec_sql(*db,
              /* The default behavior of the LIKE operator is to ignore case
                 for ASCII characters. Hence, by default 'a' LIKE 'A' is true.
                 The case_sensitive_like pragma installs a new application-
                 defined LIKE function that is either case sensitive or
                 insensitive depending on the value of the case_sensitive_like
                 pragma. */
              "PRAGMA case_sensitive_like=1;"
              /* Disable synchronization to disable the explicit disk flushes
                 that make Sqlite up to 50 times slower; especially on small
                 transactions.

                 This removes some stability guarantees on specific hardware
                 and power failures, but still guarantees atomic commits on
                 application crashes. With our dependency on external data
                 like pristine files (Wc) and revision files (repository),
                 we can't keep up these additional guarantees anyway.

                 ### Maybe switch to NORMAL(1) when we use larger transaction
                     scopes */
              "PRAGMA synchronous=OFF;"
              /* Enable recursive triggers so that a user trigger will fire
                 in the deletion phase of an INSERT OR REPLACE statement.
                 Requires SQLite >= 3.6.18  */
              "PRAGMA recursive_triggers=ON;"
              /* Enforce current Sqlite default behavior. Some distributions
                 might change the Sqlite defaults without realizing how this
                 affects application(read: Subversion) performance/behavior. */
              "PRAGMA foreign_keys=OFF;"      /* SQLITE_DEFAULT_FOREIGN_KEYS*/
              "PRAGMA locking_mode = NORMAL;" /* SQLITE_DEFAULT_LOCKING_MODE */
              /* Testing shows TRUNCATE is faster than DELETE on Windows. */
              "PRAGMA journal_mode = TRUNCATE;"
              ),
                *db);

#if defined(SVN_DEBUG)
  /* When running in debug mode, enable the checking of foreign key
     constraints.  This has possible performance implications, so we don't
     bother to do it for production...for now. */
  SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA foreign_keys=ON;"),
                *db);
#endif

#ifdef SVN_SQLITE_REVERSE_UNORDERED_SELECTS
  /* When enabled, this PRAGMA causes SELECT statements without an ORDER BY
     clause to emit their results in the reverse order of what they normally
     would.  This can help detecting invalid assumptions about the result
     order.*/
  SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA reverse_unordered_selects=ON;"),
                *db);
#endif

  /* Store temporary tables in RAM instead of in temporary files, but don't
     fail on this if this option is disabled in the sqlite compilation by
     setting SQLITE_TEMP_STORE to 0 (always to disk) */
  svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));

  /* Store the provided statements. */
  if (statements)
    {
      (*db)->statement_strings = statements;
      (*db)->nbr_statements = 0;
      while (*statements != NULL)
        {
          statements++;
          (*db)->nbr_statements++;
        }

      (*db)->prepared_stmts = apr_pcalloc(
                                  result_pool,
                                  ((*db)->nbr_statements + STMT_INTERNAL_LAST)
                                                * sizeof(svn_sqlite__stmt_t *));
    }
  else
    {
      (*db)->nbr_statements = 0;
      (*db)->prepared_stmts = apr_pcalloc(result_pool,
                                          (0 + STMT_INTERNAL_LAST)
                                                * sizeof(svn_sqlite__stmt_t *));
    }

  (*db)->state_pool = result_pool;
  apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);

  return SVN_NO_ERROR;
}