Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspardle committed Jan 5, 2015
1 parent 9026ec8 commit 3313282
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 153 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 22 additions & 26 deletions mssql/connection.hpp
Expand Up @@ -51,19 +51,22 @@ class Connection
//XXX string to wstring conversion
std::wstring connect_with_pass;
connect_with_pass.assign(connection_str.begin(), connection_str.end());
/*if (password && !password->empty())
{
connect_with_pass += " password=" + *password;
}*/

if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
throw mapnik::datasource_exception("Mssql Plugin: SQLAllocHandle");// goto FINISHED;

if (SQL_SUCCESS != SQLSetEnvAttr(sqlenvhandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
throw mapnik::datasource_exception("Mssql Plugin: SQLSetEnvAttr");// goto FINISHED;
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle)){
close();
throw mapnik::datasource_exception("Mssql Plugin: SQLAllocHandle");
}

if (SQL_SUCCESS != SQLSetEnvAttr(sqlenvhandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)){
close();
throw mapnik::datasource_exception("Mssql Plugin: SQLSetEnvAttr");
}

if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
throw mapnik::datasource_exception("Mssql Plugin: SQLAllocHandle");// goto FINISHED;
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle)){
close();
throw mapnik::datasource_exception("Mssql Plugin: SQLAllocHandle");
}

SQLWCHAR retconstring[1024];
SQLSMALLINT OutConnStrLen;
Expand All @@ -79,7 +82,7 @@ class Connection
{
std::string err_msg = "Mssql Plugin: ";
err_msg += status(SQL_HANDLE_DBC, sqlconnectionhandle);
err_msg += "\nConnection string: '";
err_msg += "\nSQLDriverConnect Error, Connection string: '";
err_msg += connection_str;
err_msg += "'\n";
close();
Expand Down Expand Up @@ -107,11 +110,8 @@ class Connection
#ifdef MAPNIK_STATS
mapnik::progress_timer __stats__(std::clog, std::string("mssql_connection::execute ") + sql);
#endif
throw mapnik::datasource_exception("execute!");
//PGresult *result = PQexec(conn_, sql.c_str());
//bool ok = (result && (PQresultStatus(result) == PGRES_COMMAND_OK));
//PQclear(result);
//return ok;
throw mapnik::datasource_exception("not implemented");

}

std::shared_ptr<ResultSet> executeQuery(std::string const& sql)
Expand All @@ -122,24 +122,20 @@ class Connection
debug_current_sql = sql;
SQLHANDLE hstmt = NULL;
SQLRETURN retcode;
if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &hstmt))
throw mapnik::datasource_exception("cant SQLAllocHandle");

if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &hstmt)){
throw mapnik::datasource_exception("SQLAllocHandle error");
}
retcode = SQLExecDirectA(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);


if (!(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO))
// if (! result || (PQresultStatus(result) != PGRES_TUPLES_OK))

if (!(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO))
{
std::string err_msg = status(SQL_HANDLE_STMT, hstmt);
err_msg += "\nFull sql was: '";
err_msg += sql;
err_msg += "'\n";

SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
// PQclear(result);


throw mapnik::datasource_exception(err_msg);
}

Expand Down Expand Up @@ -219,7 +215,7 @@ class Connection

if (retcode != SQL_STILL_EXECUTING)
break;
sleep(1);
//sleep(1);
}

return retcode;
Expand Down
15 changes: 10 additions & 5 deletions mssql/connection_manager.hpp
Expand Up @@ -76,7 +76,7 @@ class ConnectionCreator
inline std::string connection_string() const
{
std::string connect_str = connection_string_safe();
if (pass_ && !pass_->empty()) connect_str += " PWD=" + *pass_;
if (pass_ && !pass_->empty()) connect_str += "PWD=" + *pass_;
return connect_str;
}

Expand All @@ -97,11 +97,16 @@ class ConnectionCreator
connect_str += "DRIVER={SQL Server Native Client 11.0};";
}
if (host_ && !host_->empty()) connect_str += "SERVER=" + *host_ + ";";
if (port_ && !port_->empty()) connect_str += " port=" + *port_ + ";";
if (dbname_ && !dbname_->empty()) connect_str += " DATABASE=" + *dbname_ + ";";
if (user_ && !user_->empty()) connect_str += " UID=" + *user_ + ";";
if (port_ && !port_->empty()) connect_str += "port=" + *port_ + ";";
if (dbname_ && !dbname_->empty()) connect_str += "DATABASE=" + *dbname_ + ";";
if (user_ && !user_->empty()) connect_str += "UID=" + *user_ + ";";
if (connect_timeout_ && !connect_timeout_->empty())
connect_str += " connect_timeout=" + *connect_timeout_ + ";";
connect_str += "connect_timeout=" + *connect_timeout_ + ";";
/*if (encrypt_ && !encrypt_->empty())
connect_str += "Encrypt=true;";
if (trust_server_certificate_ && !trust_server_certificate_->empty())
connect_str += "TrustServerCertificate=no;";
*/
}

return connect_str;
Expand Down
1 change: 0 additions & 1 deletion mssql/mssql.vcxproj
Expand Up @@ -102,7 +102,6 @@
<ClInclude Include="mssql_featureset.hpp" />
<ClInclude Include="resultset.hpp" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="transition.h" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions mssql/mssql.vcxproj.filters
Expand Up @@ -18,9 +18,6 @@
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="connection.hpp">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
54 changes: 3 additions & 51 deletions mssql/mssql_datasource.cpp
Expand Up @@ -892,6 +892,8 @@ box2d<double> mssql_datasource::envelope() const

boost::optional<mapnik::datasource::geometry_t> mssql_datasource::get_geometry_type() const
{
//return boost::optional<mapnik::datasource::geometry_t>();

boost::optional<mapnik::datasource::geometry_t> result;

CnxPool_ptr pool = ConnectionManager::instance().getPool(creator_.id());
Expand All @@ -903,56 +905,7 @@ boost::optional<mapnik::datasource::geometry_t> mssql_datasource::get_geometry_t
{
std::ostringstream s;
std::string g_type;
/*try
{
s << "SELECT lower(type) as type FROM "
<< GEOMETRY_COLUMNS <<" WHERE f_table_name='"
<< mapnik::sql_utils::unquote_double(geometry_table_)
<< "'";
if (! schema_.empty())
{
s << " AND f_table_schema='"
<< mapnik::sql_utils::unquote_double(schema_)
<< "'";
}
if (! geometry_field_.empty())
{
s << " AND f_geometry_column='"
<< mapnik::sql_utils::unquote_double(geometry_field_)
<< "'";
}
shared_ptr<ResultSet> rs = conn->executeQuery(s.str());
if (rs->next())
{
g_type = rs->getValue("type");
if (boost::algorithm::contains(g_type, "line"))
{
result.reset(mapnik::datasource::LineString);
return result;
}
else if (boost::algorithm::contains(g_type, "point"))
{
result.reset(mapnik::datasource::Point);
return result;
}
else if (boost::algorithm::contains(g_type, "polygon"))
{
result.reset(mapnik::datasource::Polygon);
return result;
}
else // geometry
{
result.reset(mapnik::datasource::Collection);
return result;
}
}
}
catch (mapnik::datasource_exception const& ex) {
// let this pass on query error and use the fallback below
MAPNIK_LOG_WARN(mssql) << "mssql_datasource: metadata query failed: " << ex.what();
}*/

// fallback to querying first several features
if (g_type.empty() && !geometryColumn_.empty())
{
s.str("");
Expand All @@ -975,8 +928,7 @@ boost::optional<mapnik::datasource::geometry_t> mssql_datasource::get_geometry_t

shared_ptr<ResultSet> rs = conn->executeQuery(s.str());
while (rs->next() && !rs->isNull(0))
{
//const char* data = rs->getValue(0);
{
std::string data = rs->getString(0);

if (boost::algorithm::contains(data, "line"))
Expand Down
87 changes: 28 additions & 59 deletions mssql/resultset.hpp
Expand Up @@ -67,8 +67,7 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
}

virtual void close()
{
//PQclear(res_);
{
// SQLDisconnect(res_);
SQLFreeHandle(SQL_HANDLE_STMT, res_);
res_ = 0;
Expand Down Expand Up @@ -142,6 +141,7 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
sizeof(fname),
&name_length,
0);
//XXX validation?
return std::string(fname);
}

Expand All @@ -157,40 +157,32 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
NULL,
NULL,
&length);
//XXX validation?
return length;
//return PQgetlength(res_, pos_, index);

}

virtual int getFieldLength(const char* name) const
{

throw mapnik::datasource_exception("ResultSet getFieldLength not implemented");
/*int col = PQfnumber(res_, name);
if (col >= 0)
{
return PQgetlength(res_, pos_, col);
}*/
throw mapnik::datasource_exception("ResultSet getFieldLength not implemented");
return 0;
}

virtual int getTypeOID(int index) const
{
SQLLEN dataType;
SQLColAttribute(res_, index + 1, SQL_DESC_TYPE, NULL, 0, NULL, &dataType);
SQLRETURN retcode;
retcode = SQLColAttribute(res_, index + 1, SQL_DESC_TYPE, NULL, 0, NULL, &dataType);

//XXX validation?
return dataType;
//return PQftype(res_, index);
}

virtual int getTypeOID(const char* name) const
{
throw mapnik::datasource_exception("ResultSet getTypeOID(const char* name) not implemented");
return 0;
/* int col = PQfnumber(res_, name);
if (col >= 0)
{
return PQftype(res_, col);
}
return 0;*/
return 0;
}

virtual bool isNull(int index) const
Expand All @@ -201,6 +193,7 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
unsigned char value[1];

retcode = SQLGetData(res_, index + 1, SQL_C_BINARY, value, 0, &length);
//XXX retcode validation?

return static_cast<bool>(length == SQL_NULL_DATA);
}
Expand All @@ -209,21 +202,28 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
virtual const int getInt(int index) const
{
SQLINTEGER intvalue;
SQLGetData(res_, index + 1, SQL_C_SLONG, &intvalue, 0, NULL);
return intvalue;
// return PQgetvalue(res_, pos_, index);
SQLRETURN retcode;

retcode = SQLGetData(res_, index + 1, SQL_C_SLONG, &intvalue, 0, NULL);

//XXX retcode validation?
return intvalue;
}
virtual const double getDouble(int index) const
{
double value;
SQLGetData(res_, index + 1, SQL_C_DOUBLE, &value, 0, NULL);
SQLRETURN retcode;

retcode = SQLGetData(res_, index + 1, SQL_C_DOUBLE, &value, 0, NULL);
return value;
// return PQgetvalue(res_, pos_, index);
}
virtual const float getFloat(int index) const
{
float value;
SQLGetData(res_, index + 1, SQL_C_FLOAT, &value, 0, NULL);
SQLRETURN retcode;

retcode = SQLGetData(res_, index + 1, SQL_C_FLOAT, &value, 0, NULL);
return value;
// return PQgetvalue(res_, pos_, index);
}
Expand Down Expand Up @@ -263,10 +263,9 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
return mapnik::utf16_to_utf8(str);
#else
return str;
#endif

// return PQgetvalue(res_, pos_, index);
#endif
}

virtual const std::vector<char> getBinary(int index) const
{
SQLLEN length = 0;
Expand All @@ -277,44 +276,14 @@ class ResultSet : public IResultSet, private mapnik::noncopyable
retcode = SQLGetData(res_, index + 1, SQL_C_BINARY, bit, 0, &length);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){
if (length != SQL_NULL_DATA){
// char* binvalue = new char[length*2];


std::vector<char> binvalue(length, 0);
retcode = SQLGetData(res_, index + 1, SQL_C_BINARY, (SQLPOINTER)&binvalue[0], length, &length);

//char* binvalue = new char[80000];
//retcode = SQLGetData(res_, index + 1, SQL_C_BINARY, binvalue, length, &length);


return binvalue;
// return std::vector<const char>(binvalue, binvalue + sizeof binvalue / sizeof binvalue[0]);

return binvalue;
}
}
return std::vector<char>();

// return PQgetvalue(res_, pos_, index);
}
virtual const char* getValue4(int index) const
{
throw mapnik::datasource_exception("ResultSet getValue(intindex) not implemented");
return "novalue";
// return PQgetvalue(res_, pos_, index);
}

virtual const char* getValue5(const char* name) const
{
std::string a = "ResultSet getValue([const char* name]";
a += ((char*)name);
a += ") not implemented";
throw mapnik::datasource_exception(a.c_str());
return "novalue";
/*
int col = PQfnumber(res_, name);
if (col >= 0)
{
return getValue(col);
}
return 0;*/
}

private:
Expand Down
8 changes: 0 additions & 8 deletions mssql/targetver.h

This file was deleted.

0 comments on commit 3313282

Please sign in to comment.