Beispiel #1
0
bool command::bind_args(term & t, term & resp)
{
	bool ret = false;

	// {{pid, ref}, BIND_ARGS, Connection Handle, Statement Handle, BindList}
	term & conection = t[2];
	term & statement = t[3];
	term & bind_list = t[4];
    if(conection.is_any_int() && statement.is_any_int() && bind_list.is_list()) {
		ocisession * conn_handle = (ocisession *)(conection.v.ll);
		ocistmt * statement_handle = (ocistmt*)(statement.v.ll);

		try {
			if (!conn_handle->has_statement(statement_handle)) {
				term & _t = resp.insert().tuple();
				_t.insert().atom("error");
				_t.insert().integer(0);
				_t.insert().binary("invalid statement handle");
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR invalid statement handle\n");
			} else {
				map_schema_to_bind_args(bind_list, statement_handle->get_in_bind_args());
				resp.insert().atom("ok");
			}
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
		} catch (string & str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
    } else {
		//REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

    return ret;
}
Beispiel #2
0
bool command::prep_sql(term & t, term & resp)
{
	bool ret = false;

	// {{pid, ref}, PREP_STMT, Connection Handle, SQL String}
	term & connection = t[2];
	term & sql_string = t[3];
    if(connection.is_any_int() && sql_string.is_binary()) {

        //LOG_ARGS(ARG_COUNT(command), args, "Execute SQL statement");

		ocisession * conn_handle = (ocisession *)(connection.v.ll);
		try {
	        ocistmt * statement_handle = conn_handle->prepare_stmt((unsigned char *)&sql_string.str[0], sql_string.str_len);
			term & _t = resp.insert().tuple();
			_t.insert().atom("stmt");
			_t.insert().integer((unsigned long long)statement_handle);
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			if (r.fn_ret == CONTINUE_WITH_ERROR) {
				if(resp.is_undef())
					REMOTE_LOG(INF, "Continue with ERROR Execute SQL \"%.*s;\" -> %s\n",
						t[3].str_len, &t[3].str[0], r.gerrbuf);
			} else {
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
				ret = true;
			}
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
    } else {
//		REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

	return ret;
}
Beispiel #3
0
ocisession::ocisession(const char * connect_str, const int connect_str_len,
					   const char * user_name, const int user_name_len,
					   const char * password, const int password_len)
{
	intf_ret r;
	OCIAuthInfo *authp = NULL;

	init();

	r.handle = envhp;

	// allocate error handle
	checkenv(&r, OCIHandleAlloc((OCIEnv*)envhp,	/* environment handle */
                            (void **) &_errhp,	/* returned err handle */
                            OCI_HTYPE_ERROR,	/* typ of handle to allocate */
                            (size_t) 0,			/* optional extra memory size */
                            (void **) NULL));	/* returned extra memeory */

	if(r.fn_ret != SUCCESS) {
   		REMOTE_LOG("failed OCISessionGet %s\n", r.gerrbuf);
        throw r;
	}

	// allocate auth handle
	checkenv(&r, OCIHandleAlloc((OCIEnv*)envhp,
							(void**)&authp, OCI_HTYPE_AUTHINFO,
							(size_t)0, (void **) NULL));

	r.handle = _errhp;

	// usrname and password
	checkerr(&r, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO,
								(void*) user_name, user_name_len,
								OCI_ATTR_USERNAME, (OCIError *)_errhp));
	checkerr(&r, OCIAttrSet(authp, OCI_HTYPE_AUTHINFO,
								(void*) password, password_len,
								OCI_ATTR_PASSWORD, (OCIError *)_errhp));


    /* get the database connection */
    checkerr(&r, OCISessionGet((OCIEnv*)envhp, (OCIError *)_errhp,
                               (OCISvcCtx**)&_svchp,					/* returned database connection */
                               authp,									/* initialized authentication handle */                               
                               (OraText *) connect_str, connect_str_len,/* connect string */
                               NULL, 0, NULL, NULL, NULL,				/* session tagging parameters: optional */
                               OCI_SESSGET_STMTCACHE));					/* modes */
	if(r.fn_ret != SUCCESS) {
		REMOTE_LOG("failed OCISessionGet %s\n", r.gerrbuf);
        throw r;
	}

	(void) OCIHandleFree(authp, OCI_HTYPE_AUTHINFO);

	REMOTE_LOG("got session %p\n", _svchp);

	_sessions.push_back(this);
}
Beispiel #4
0
bool command::get_session(term & t, term & resp)
{
    bool ret = false;

	// {{pid, ref}, GET_SESSN, Connection String, User name, Password, client identifier}
	term & con_str = t[2];
	term & usr_str = t[3];
	term & passwrd = t[4];
	term & clnt_id = t[5];
    if(con_str.is_binary() && usr_str.is_binary() && passwrd.is_binary()) {
		try {
			ocisession * conn_handle = new ocisession(
				&con_str.str[0], con_str.str_len,		// Connect String
				&usr_str.str[0], usr_str.str_len,		// User Name String
				&passwrd.str[0], passwrd.str_len,		// Password String
				&clnt_id.str[0], clnt_id.str_len);		// Client identifier
			REMOTE_LOG(INF, "got connection %lu id %s\n",
				(unsigned long long)conn_handle, &clnt_id.str[0]);
			resp.insert().integer((unsigned long long)conn_handle);
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
	} else {
		//REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

	return ret;
}
Beispiel #5
0
bool command::process(term & t)
{
	bool ret = false;
	term resp;
#ifdef PRINTCMD
	char * tmpbuf = print_term(command);
	REMOTE_LOG(DBG, "========================================\nCOMMAND : %s %s\n========================================\n",
		CMD_NAME_STR(ERL_INT_VALUE(cmd)),
		tmpbuf);
	delete tmpbuf;
#endif

	if(t.is_tuple() && t[1].is_integer()) {
        int cmd = t[1].v.i;
		resp.tuple();
		resp.add(t[0]);
		resp.insert().integer(cmd);
        if((t.length() - 1) != (size_t)CMD_ARGS_COUNT(cmd)) {
			term & _t = resp.insert().tuple();
	    	_t.insert().atom("error");
	    	_t.insert().atom("badarg");
	    	if(resp.is_undef())
                REMOTE_LOG(ERR, "ERROR badarg %s expected %d, got %d\n", CMD_NAME_STR(cmd)
                    , CMD_ARGS_COUNT(cmd), (t.length() - 1));
	        if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
            vector<unsigned char> respv = tc.encode(resp);
            p.write_cmd(respv);
	    } else {
		    switch(cmd) {
            case RMOTE_MSG:	ret = change_log_flag(t, resp);	break;
            case GET_SESSN:	ret = get_session(t, resp);		break;
            case PUT_SESSN:	ret = release_conn(t, resp);	break;
		    case CMT_SESSN:	ret = commit(t, resp);			break;
            case RBK_SESSN:	ret = rollback(t, resp);		break;
            case CMD_DSCRB:	ret = describe(t, resp);		break;
            case PREP_STMT:	ret = prep_sql(t, resp);		break;
            case BIND_ARGS:	ret = bind_args(t, resp);		break;
            case EXEC_STMT:	ret = exec_stmt(t, resp);		break;
            case FTCH_ROWS:	ret = fetch_rows(t, resp);		break;
            case CLSE_STMT:	ret = close_stmt(t, resp);		break;
            case GET_LOBDA:	ret = get_lob_data(t, resp);	break;
            case CMD_ECHOT:	ret = echo(t, resp);			break;
		    case SESN_PING:	ret = ping(t, resp);			break;
            default:
		    	ret = true;
                break;
            }
        }
    }

	return ret;
}
Beispiel #6
0
bool command::rollback(term & t, term & resp)
{
    bool ret = false;

	// {{pid, ref}, RBK_SESSN, Connection Handle}
	if(t[2].is_any_int()) {

		ocisession * conn_handle = (ocisession *)(t[2].v.ll);
		try {
			conn_handle->rollback();
            resp.insert().atom("ok");
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
    } else {
		//REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

	return ret;
}
Beispiel #7
0
bool command::echo(term & t, term & resp)
{
	bool ret = false;

	// {{pid, ref}, CMD_ECHOT, Term}
	try {
		resp.add(t[2]);
	} catch (intf_ret r) {
		term & _t = resp.insert().tuple();
		_t.insert().atom("error");
		term & _t1 = _t.insert().tuple();
		_t1.insert().integer(r.gerrcode);
		_t1.insert().binary(r.gerrbuf);
		ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
	} catch (string str) {
		term & _t = resp.insert().tuple();
		_t.insert().atom("error");
		term & _t1 = _t.insert().tuple();
		_t1.insert().integer(0);
		_t1.insert().binary(str.c_str());
		ret = true;
		if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
	} catch (...) {
		term & _t = resp.insert().tuple();
		_t.insert().atom("error");
		term & _t1 = _t.insert().tuple();
		_t1.insert().integer(0);
		_t1.insert().atom("unknwon");
		ret = true;
		if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
	}    

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

    return ret;
}
Beispiel #8
0
bool command::change_log_flag(term & t, term & resp)
{
    bool ret = false;

    // {undefined, RMOTE_MSG, DBG_FLAG_OFF/DBG_FLAG_ON}
    if(t[2].is_any_int()) {
		unsigned int log = t[2].v.ui;

        switch(log) {
        case DBG_FLAG_OFF:
            REMOTE_LOG(INF, "Disabling logging...");
            log_flag = false;
            REMOTE_LOG(CRT, "This line will never show up!!\n");
			resp.insert().atom("log_disabled");
            break;
        case DBG_FLAG_ON:
            log_flag = true;
			resp.insert().atom("log_enabled");
			REMOTE_LOG(INF, "Enabled logging...");
            break;
        default:
			term & _t = resp.insert();
			_t.tuple();
			_t.insert().atom("error");
			_t.insert().atom("badarg");
			REMOTE_LOG(ERR, "ERROR badarg %d\n", log);
            break;
        }
    } else {
//		REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

	return ret;
}
Beispiel #9
0
ocisession::~ocisession(void)
{
	intf_ret r;

	// delete all the statements
	for (list<ocistmt*>::const_iterator it = _statements.begin(); it != _statements.end(); it++)
		(*it)->close();
	_statements.clear();

	checkerr(&r, OCISessionRelease((OCISvcCtx*)_svchp, (OCIError*)_errhp, NULL, 0, OCI_DEFAULT));
	if(r.fn_ret != SUCCESS) {
		REMOTE_LOG("failed OCISessionRelease %s\n", r.gerrbuf);
        throw r;
	}

	(void) OCIHandleFree(_errhp, OCI_HTYPE_ERROR);

	// cleanup the environment if this is the last oci session from this environment
	_sessions.remove(this);

	//REMOTE_LOG("release session %p\n", _svchp);
}
Beispiel #10
0
bool command::get_lob_data(term & t, term & resp)
{
	bool ret = false;
	term lob;

	// {{pid, ref}, GET_LOBDA, Connectin Handle, Statement Handle, OCILobLocator Handle, Offset, Length}
	term & conection = t[2];
	term & statement = t[3];
	term & loblocator = t[4];
	term & offset = t[5];
	term & length = t[6];
	if(conection.is_any_int() && statement.is_any_int() && loblocator.is_any_int() && offset.is_any_int() && length.is_any_int()) {
		ocisession * conn_handle = (ocisession *)(conection.v.ull);
		ocistmt * statement_handle = (ocistmt*)(statement.v.ull);
		void * loblocator_handle = (void *)(loblocator.v.ull);
		try {
			if (!conn_handle->has_statement(statement_handle)) {
				term & _t = resp.insert().tuple();
				_t.insert().atom("error");
				_t.insert().integer(0);
				_t.insert().binary("invalid statement handle");
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR invalid statement handle\n");
			} else {
				intf_ret r = statement_handle->lob(&lob, loblocator_handle, offset.v.ull, length.v.ull);
				if(r.fn_ret == SUCCESS) {
					term & _t = resp.insert().tuple();
					_t.insert().atom("lob");
					_t.add(lob);
				}
			}
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			if (r.fn_ret == CONTINUE_WITH_ERROR) {
				if(resp.is_undef())
					REMOTE_LOG(INF, "Continue with ERROR fetch STMT %s\n", r.gerrbuf);
			} else {
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
				ret = true;
			}
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
	} else {
//		REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

    return ret;
}
Beispiel #11
0
bool command::fetch_rows(term & t, term & resp)
{
	bool ret = false;

	// {{pid, ref}, FTCH_ROWS, Connection Handle, Statement Handle, Rowcount}
	term & conection = t[2];
	term & statement = t[3];
	term & row_count = t[4];
	term rows;
	rows.lst();
    if(conection.is_any_int() && statement.is_any_int() && row_count.is_any_int()) {

		ocisession * conn_handle = (ocisession *)(conection.v.ll);
		ocistmt * statement_handle = (ocistmt*)(statement.v.ll);
        int rowcount = (row_count.v.i);
		try {
			if (!conn_handle->has_statement(statement_handle)) {
				term & _t = resp.insert().tuple();
				_t.insert().atom("error");
				_t.insert().integer(0);
				_t.insert().binary("invalid statement handle");
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR invalid statement handle\n");
			} else {
				intf_ret r = statement_handle->rows(&rows, rowcount);
				if (r.fn_ret == MORE || r.fn_ret == DONE) {
					term & _t = resp.insert().tuple();
					term & _t1 = _t.insert().tuple();
					_t1.insert().atom("rows");
					_t1.add(rows);
					_t.insert().atom((r.fn_ret == MORE && rows.length() > 0) ? "false" : "true");
				}
			}
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			if (r.fn_ret == CONTINUE_WITH_ERROR) {
				if(resp.is_undef())
					REMOTE_LOG(INF, "Continue with ERROR fetch STMT %s\n", r.gerrbuf);
			} else {
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
				ret = true;
			}
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
    } else {
//		REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

    return ret;
}
Beispiel #12
0
bool command::exec_stmt(term & t, term & resp)
{
	bool ret = false;

	// {{pid, ref}, EXEC_STMT, Connection Handle, Statement Handle, BindList, auto_commit}
	term & conection = t[2];
	term & statement = t[3];
	term & bind_list = t[4];
	term & auto_cmit = t[5];
    term columns, rowids, outdata;
	columns.lst();
	rowids.lst();
	outdata.lst();
    if(conection.is_any_int() && statement.is_any_int() && bind_list.is_list() && auto_cmit.is_any_int()) {
		ocisession * conn_handle = (ocisession *)(conection.v.ll);
		ocistmt * statement_handle = (ocistmt *)(statement.v.ll);
		bool auto_commit = (auto_cmit.v.i) > 0 ? true : false;
		try {
			if (!conn_handle->has_statement(statement_handle)) {
				term & _t = resp.insert().tuple();
				_t.insert().atom("error");
				_t.insert().integer(0);
				_t.insert().binary("invalid statement handle");
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR invalid statement handle\n");
			} else {
				size_t bound_count = map_value_to_bind_args(bind_list, statement_handle->get_in_bind_args());
				unsigned int exec_ret = statement_handle->execute(&columns, &rowids, &outdata, auto_commit);
				if (bound_count) REMOTE_LOG(DBG, "Bounds %u", bound_count);
				// TODO : Also return bound values from here
				term & _t = resp.insert().tuple();
				if (columns.length() == 0 && rowids.length() == 0) {
					_t.insert().atom("executed");
					_t.insert().integer(exec_ret);
					if (outdata.length() > 0)
						_t.add(outdata);
				} else if (columns.length() > 0 && rowids.length() == 0) {
					_t.insert().atom("cols");
					_t.add(columns);
				} else if (columns.length() == 0 && rowids.length() > 0) {
					_t.insert().atom("rowids");
					_t.add(rowids);
				} else {
					_t.insert().atom("cols");
					_t.add(columns);
					_t = resp.insert().tuple();
					_t.insert().atom("rowids");
					_t.add(rowids);
				}
			}
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			if (r.fn_ret == CONTINUE_WITH_ERROR) {
				if(resp.is_undef())
					REMOTE_LOG(INF, "Continue with ERROR Execute SQL \"%.*s;\" -> %s\n",
						t[3].str_len, &t[3].str[0], r.gerrbuf);
			} else {
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
				ret = true;
			}
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
	} else {
//		REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

	return ret;
}
Beispiel #13
0
ROW_FETCH oci_produce_rows(void * stmt_handle, void * row_list)
{
    function_success = SUCCESS;
    OCIStmt *stmthp	= (OCIStmt *)stmt_handle;

    if (columns == NULL || stmthp == NULL)
        return ERROR;

    sword res = OCI_NO_DATA;

    OCIDefine **defnhp = (OCIDefine **)malloc(num_cols * sizeof(OCIDefine *));
    void ** data_row = NULL;
    data_row = (void **) calloc(num_cols, sizeof(void *));

    /*
     * Fetch the data
     */
    unsigned int num_rows = 0;
    ub4 i = 0;
    void * row = NULL;

    /* Bind appropriate variables for data based on the column type */
    for (i = 0; i < num_cols; ++i)
        switch (columns[i].dtype) {
		case SQLT_DAT:
		//{
  //          data_row[i] = (text *) malloc((columns[i].dlen + 1) * sizeof(text));
  //          checkerr(errhp, OCIDefineByPos(stmthp, &(defnhp[i]), errhp, i+1, (dvoid *) (data_row[i]),
  //                                         (sword) columns[i].dlen + 1, SQLT_DAT, (dvoid *) 0, (ub2 *)0,
  //                                         (ub2 *)0, OCI_DEFAULT));
  //          if(function_success != SUCCESS) return ERROR;
  //      }
  //      break;
        case SQLT_NUM:
        case SQLT_CHR: {
            data_row[i] = (text *) malloc((columns[i].dlen + 1) * sizeof(text));
            checkerr(errhp, OCIDefineByPos(stmthp, &(defnhp[i]), errhp, i+1, (dvoid *) (data_row[i]),
                                           (sword) columns[i].dlen + 1, SQLT_STR, (dvoid *) 0, (ub2 *)0,
                                           (ub2 *)0, OCI_DEFAULT));
            if(function_success != SUCCESS) return ERROR;
        }
        break;
        default:
            break;
        }

    /* Fetch data by row */
    do {
        ++num_rows;
        res = OCIStmtFetch(stmthp, errhp, 1, 0, 0);
        row = NULL;
        for (i = 0; i < num_cols; ++i)
            if (res != OCI_NO_DATA)
                switch (columns[i].dtype) {
				case SQLT_DAT:
                case SQLT_NUM:
                case SQLT_CHR:
                    append_string_to_list((char*)data_row[i], &row);
                    break;
                }
        append_list_to_list(row, row_list);
    } while (res != OCI_NO_DATA &&
             calculate_resp_size(row_list) < MAX_RESP_SIZE);

    /* Release the bound variables memeory */
    for (i = 0; i < num_cols; ++i)
        if(data_row[i] != NULL)
            free(data_row[i]);
    free(data_row);

    /* cleanup only if data fetch is finished */
    if (res == OCI_NO_DATA)
        checkerr(errhp, OCIStmtRelease(stmthp, errhp, (OraText *) NULL, 0, OCI_DEFAULT));

    free(defnhp);

    if(function_success != SUCCESS)
        return ERROR;

    REMOTE_LOG("Port: Returning Rows...");

    return (res != OCI_NO_DATA ? MORE : DONE);
}
Beispiel #14
0
bool command::describe(term & t, term & resp)
{
	bool ret = false;

	// {{pid, ref}, CMD_DSCRB, Connection Handle, Describe Object BinString, Describe Object Type int}
	term & connection = t[2];
	term & obj_string = t[3];
	term & objct_type = t[4];
	term describes;
	describes.lst();
    if(connection.is_any_int() && t[3].is_binary() && t[4].is_any_int()) {
		ocisession * conn_handle = (ocisession *)(connection.v.ll);
		unsigned char desc_typ = (unsigned char)(objct_type.v.ll);

		try {
	        conn_handle->describe_object(&obj_string.str[0], obj_string.str_len, desc_typ, &describes);
			term & _t = resp.insert().tuple();
			_t.insert().atom("desc");
			_t.add(describes);
		} catch (intf_ret r) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(r.gerrcode);
			_t1.insert().binary(r.gerrbuf);
			if (r.fn_ret == CONTINUE_WITH_ERROR) {
				if(resp.is_undef())
					REMOTE_LOG(INF, "Continue with ERROR Execute DESCRIBE \"%.*s;\" -> %s\n",
						t[3].str_len, &t[3].str[0], r.gerrbuf);
			} else {
				if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", r.gerrbuf);
				ret = true;
			}
		} catch (string str) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().binary(str.c_str());
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR %s\n", str.c_str());
		} catch (...) {
			term & _t = resp.insert().tuple();
			_t.insert().atom("error");
			term & _t1 = _t.insert().tuple();
			_t1.insert().integer(0);
			_t1.insert().atom("unknwon");
			ret = true;
			if(resp.is_undef()) REMOTE_LOG(ERR, "ERROR unknown\n");
		}
    } else {
//		REMOTE_LOG_TERM(ERR, command, "argument type(s) missmatch\n");
	}

	if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
    vector<unsigned char> respv = tc.encode(resp);
    if(p.write_cmd(respv) <= 0)
        ret = true;

	return ret;
}
Beispiel #15
0
int main(int argc, char * argv[])
#endif
{
    bool threaded = false;
    ETERM *cmd_tuple;

#ifdef __WIN32__
    _setmode( _fileno( stdout ), _O_BINARY );
    _setmode( _fileno( stdin  ), _O_BINARY );
#endif

    erl_init(NULL, 0);
    log_flag = false;

    if (argc >= 2) {
        if (
#ifdef __WIN32__
            wcscmp(argv[1], L"true") == 0
#else
            strcmp(argv[1], "true") == 0
#endif
        ) log_flag = true;
    }

	if (argc >= 3) {
		int ListenPortNo = 
#ifdef __WIN32__
            _wtoi(argv[2]);
#else
            atoi(argv[2]);
#endif
		char * ret = connect_tcp(ListenPortNo);
		if(ret != NULL) {
			return -1;
		} else
			REMOTE_LOG("Logging over TCP, Voila!\n");
	}

    init_marshall();

    REMOTE_LOG("Port: OCI Process started...\n");

    threaded = InitializeThreadPool();
    if(threaded)
        REMOTE_LOG("Port: Thread pool created...\n");

    REMOTE_LOG("Port: Initialized Oracle OCI\n");

    while(!exit_loop && (cmd_tuple = (ETERM *)read_cmd()) != NULL) {
        if(threaded && ProcessCommand(cmd_tuple)) {
            //REMOTE_LOG("Port: Command sumitted to thread-pool for processing...");
        }
    }

    REMOTE_LOG("Port: Process oci terminating...\n");
	close_tcp();

    REMOTE_LOG("Port: Thread pool destroyed...\n");
    CleanupThreadPool();

    return 0;
}
Beispiel #16
0
int main(int argc, char * argv[])
{
    char *nls_lang;
    nls_lang = getenv("NLS_LANG");
#ifdef __WIN32__
    _setmode( _fileno( stdout ), _O_BINARY );
    _setmode( _fileno( stdin  ), _O_BINARY );
#endif

	transcoder::instance();
#if 0
		unsigned char b[] = {
			// 131,106 // term_to_binary(""). term_to_binary([]).
			// 131,104,0 //term_to_binary({}).
			131,109,0,0,0,0 // term_to_binary(<<>>).
		};
		vector<unsigned char> buf(b, b + sizeof(b) / sizeof(b[0]));
		term t;
		transcoder::instance().decode(buf, t);
		vector<unsigned char> buf1 = transcoder::instance().encode(t);
		term t1;
		t1.integer((unsigned long long)0xFFFFFFFFFFFFFFFF);
		vector<unsigned char> buf2 = transcoder::instance().encode(t1);
#endif

    log_flag = false;

	// Max term byte size
	if (argc >= 2) {
		max_term_byte_size = atol(argv[1]);
	}

	// Enable Logging
    if (argc >= 3) {
        if (strcmp(argv[2], "true") == 0)
			log_flag = true;
    }

	// Log listner port
	int log_tcp_port = 0;
	if (argc >= 4) {
		log_tcp_port = atol(argv[3]);
		const char * ret = logger::init(log_tcp_port);
		if(ret != NULL) {
			return -1;
		}
	}

    if(nls_lang == NULL)
        nls_lang = "";
	REMOTE_LOG(
        INF,
        "Port process configs : erlang term max size 0x%08X bytes, logging %s, TCP port for logs %d,"
        " NLS_LANG %s",
        max_term_byte_size, (log_flag ? "enabled" : "disabled"), log_tcp_port, nls_lang);
	threads::init();
	port& prt = port::instance();
	vector<unsigned char> read_buf;

	while(prt.read_cmd(read_buf) > 0) {
		cmd_queue::push(read_buf);
#ifndef USING_THREAD_POOL
		threads::start();
#endif
    }
	threads::run_threads = false;

	REMOTE_LOG(DBG, "erloci terminating...");
    return 0;
}
Beispiel #17
0
INTF_RET oci_exec_sql(const void *conn_handle, unsigned long & stmt_handle, const unsigned char * query_str, int query_str_len, inp_t *params_head, void * column_list)
{
    function_success = SUCCESS;

#if DEBUG < DBG_5
    fprintf(fp_log, "Executing \"%.*s;\"\n", query_str_len, query_str);
#endif

    OCISvcCtx *svchp = (OCISvcCtx *)conn_handle;
    OCIStmt *stmthp	= NULL;
    stmt_handle = 0;

    /* Get a prepared statement handle */
    checkerr(errhp, OCIStmtPrepare2(svchp,
                                    &stmthp,					/* returned statement handle */
                                    errhp,						/* error handle */
                                    (OraText *) query_str,		/* the statement text */
                                    query_str_len,				/* length of the text */
                                    NULL, 0,					/* tagging parameters: optional */
                                    OCI_NTV_SYNTAX, OCI_DEFAULT));
    if(function_success != SUCCESS) return function_success;

    /* Bind variables */
    ub2 type = SQLT_INT;
    int idx = 1;
    for(inp_t *param = params_head; param != NULL; param = param->next) {
        switch (param->dty) {
        case NUMBER:
            type = SQLT_INT;
            break;
        case STRING:
            type = SQLT_STR;
            break;
        }
        param->bndp = NULL;
        checkerr(errhp, OCIBindByPos(stmthp, (OCIBind **)&(param->bndp), errhp, idx,
                                     (dvoid *) param->valuep, (sword) param->value_sz, type,
                                     (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));
        if(function_success != SUCCESS) return function_success;
        idx++;
    }

    ub4 stmt_typ = OCI_STMT_SELECT;
    ub4 itrs = 0;
    checkerr(errhp, OCIAttrGet((dvoid*) stmthp, (ub4) OCI_HTYPE_STMT,
                               (dvoid*) &stmt_typ, (ub4 *)NULL, (ub4)OCI_ATTR_STMT_TYPE, errhp));
    if(function_success != SUCCESS) return function_success;
    if(stmt_typ != OCI_STMT_SELECT)
        itrs = 1;

    /* execute the statement and commit */
    checkerr(errhp, OCIStmtExecute(svchp, stmthp, errhp, itrs, 0,
                                   (OCISnapshot *)NULL, (OCISnapshot *)NULL,
                                   OCI_COMMIT_ON_SUCCESS));
    if(function_success != SUCCESS) return function_success;

    if(stmt_typ == OCI_STMT_SELECT) {
        OCIParam	*mypard;
        num_cols	= 1;
        sb4         parm_status;

        /* Request a parameter descriptor for position 1 in the select-list */
        parm_status = OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)&mypard,
                                  (ub4) num_cols);
        checkerr(errhp, parm_status);
        if(function_success != SUCCESS) return function_success;

        /* Loop only if a descriptor was successfully retrieved for
         * current position, starting at 1
         */
        text *col_name;
        ub4 len = 0;
        char * data_type = NULL;
        if (columns != NULL)
            free(columns);
        columns = NULL;
        while (parm_status == OCI_SUCCESS) {
            columns = (column_info *)realloc(columns, num_cols * sizeof(column_info));
            column_info * cur_clm = &(columns[num_cols-1]);

            /* Retrieve the data type attribute */
            len = 0;
            checkerr(errhp, OCIAttrGet((dvoid*) mypard, (ub4) OCI_DTYPE_PARAM,
                                       (dvoid*) &len, (ub4 *)0, (ub4)OCI_ATTR_DATA_TYPE,
                                       errhp));
            if(function_success != SUCCESS) return function_success;
            cur_clm->dtype = len;

            switch (len) {
            case SQLT_NUM:
            case SQLT_VNU:
            case SQLT_LNG:
                data_type = (char*)"number";
                break;
            case SQLT_AVC:
            case SQLT_AFC:
            case SQLT_CHR:
            case SQLT_STR:
            case SQLT_VCS:
                data_type = (char*)"string";
                break;
            case SQLT_INT:
            case SQLT_UIN:
                data_type = (char*)"integer";
                break;
            case SQLT_DAT:
                data_type = (char*)"date";
                break;
            case SQLT_FLT:
                data_type = (char*)"double";
                break;
            default:
                data_type = (char*)"undefined";
                break;
            }

            /* Retrieve the data size attribute */
            len = 0;
            checkerr(errhp, OCIAttrGet((dvoid*) mypard, (ub4) OCI_DTYPE_PARAM,
                                       (dvoid*) &len, (ub4 *)0, (ub4)OCI_ATTR_DATA_SIZE,
                                       errhp));
            if(function_success != SUCCESS) return function_success;
            cur_clm->dlen = len;

            /* Retrieve the column name attribute */
            len = 0;
            checkerr(errhp, OCIAttrGet((dvoid*) mypard, (ub4) OCI_DTYPE_PARAM,
                                       (dvoid**) &col_name, (ub4 *) &len, (ub4) OCI_ATTR_NAME,
                                       errhp));
            if(function_success != SUCCESS) return function_success;
            char * column_name = new char[len+1];
            sprintf_s(column_name, len+1, "%.*s", len, col_name);
            append_coldef_to_list(column_name, data_type, cur_clm->dlen, column_list);
            delete column_name;
            col_name = NULL;

            /* Increment counter and get next descriptor, if there is one */
            if(OCI_SUCCESS != OCIDescriptorFree(mypard, OCI_DTYPE_PARAM))
                return FAILURE;
            num_cols++;
            parm_status = OCIParamGet(stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)&mypard,
                                      (ub4) num_cols);
        }
        --num_cols;

        if(function_success != SUCCESS)
            return function_success;
        REMOTE_LOG("Port: Returning Columns");
        stmt_handle = (unsigned long)stmthp;
    } else {
        if(stmthp != NULL) {
            checkerr(errhp, OCIStmtRelease(stmthp, errhp, NULL, 0, OCI_DEFAULT));
            if(function_success != SUCCESS) return function_success;
        }
        REMOTE_LOG("Port: Executed non-select statement!");
    }

    return function_success;
}