Exemplo n.º 1
0
    void authorize(Document & request)
    {
        if(request["data"].Size() > 0)
        {
            if(request["data"][0].HasMember("login") && request["data"][0].HasMember("password"))
            {
                try
                {
                    PreparedStatement * prepstmt;
                    prepstmt = conn->prepareStatement("select ((select pwdhash from users where login=?) = (sha1(?) collate utf8_general_ci) is true) as val;");
                    prepstmt->setString(1, request["data"][0]["login"].GetString());
                    prepstmt->setString(2, request["data"][0]["password"].GetString());
                    ResultSet * rs;
                    rs = prepstmt->executeQuery();
                    rs->next();
                    if(rs->getBoolean("val")) return;
                    throw 4;
                }
                catch(SQLException e)
                {
                    throw 2;
                }

            }
        }
        else throw 0;
    }
Exemplo n.º 2
0
int PhoneCall::DBread(Connection *sqlCon)
{
	if (!sqlCon || !id)
		return -1;
	PreparedStatement *pstmt = sqlCon->prepareStatement(
			"SELECT phone, client, translator, client_country, translator_country, lang, price, start_time, accounted, cost, error, request_time, confirm_time, accepted FROM calls WHERE id=(?)");
	pstmt->setInt(1, id);
	ResultSet *res;
	try {
		res = pstmt->executeQuery();
	} catch (SQLException &ex) {
		log(LOG_ERROR, "[%s] MySQL error(%d): %s", __func__, ex.getErrorCode(), ex.what());
		delete pstmt;
		return 0;
	}
	delete pstmt;
	if (res->rowsCount() != 1)
		return -1;
	res->first();
	if (!res->getInt("phone")) {
		delete res;
		return -1;
	}
	client = res->getInt("client");
	translator = res->getInt("translator");
	translateLang = res->getString("lang").c_str();
	price = res->getInt("price");
	const char *time = res->getString("start_time").c_str();
	if (strlen(time) > 0)
		start_time = mktime(getdate(time));
	accounted = res->getInt("accounted");
	cost = res->getInt("cost");
	if (res->getInt("error"))
		state = ERROR;
	time = res->getString("request_time").c_str();
	if (strlen(time) > 0)
		request_time = mktime(getdate(time));
	time = res->getString("confirm_time").c_str();
	if (strlen(time) > 0)
		confirm_time = mktime(getdate(time));
	accepted = res->getBoolean("accepted");
	setClientCountry(res->getString("client_country").c_str());
	setTranslatorCountry(res->getString("translator_country").c_str());
	delete res;
	return 0;
}
Exemplo n.º 3
0
 int validateToken(string token)
 {
     PreparedStatement * prepstmt;
     prepstmt = conn -> prepareStatement("select ((select expiry from tokens where token=?) > now() is true) as res, (select userid from tokens where token=?) as userid;");
     prepstmt->setString(1, token);
     prepstmt->setString(2, token);
     try
     {
         ResultSet * rs;
         rs = prepstmt->executeQuery();
         rs->next();
         if(rs->getBoolean("res")) return rs->getInt("userid");
         throw 3;
     }
     catch(exception e)
     {
         throw 2;
     }
 }
Exemplo n.º 4
0
 bool validateTokenByName(string user, string token)
 {
     PreparedStatement * prepstmt;
     prepstmt = conn -> prepareStatement("select ((select expiry from tokens where (userid=(select id from users where login=?) and token=?)) > now() is true) as res;");
     prepstmt->setString(1, user);
     prepstmt->setString(2, token);
     try
     {
         ResultSet * rs;
         rs = prepstmt->executeQuery();
         string a = rs->getMetaData()->getColumnLabel(1);
         rs->next();
         if(rs->getBoolean(a))return true;
         throw 3;
     }
     catch(exception e)
     {
         throw 2;
     }
 }
Exemplo n.º 5
0
int nuodb_resultset_next(struct nuodb *db, struct nuodb_resultset *rs,
                         int *has_values, struct nuodb_value values[]) {
    ResultSet *resultSet = reinterpret_cast<ResultSet *>(rs);
    try {
        *has_values = resultSet->next();
        if (*has_values) {
            ResultSetMetaData *resultSetMetaData = resultSet->getMetaData();
            int columnCount = resultSetMetaData->getColumnCount();
            for (int i=0; i < columnCount; ++i) {
                int64_t i64 = 0;
                int32_t i32 = 0;
                enum nuodb_value_type vt = NUODB_TYPE_NULL;
                int columnIndex = i+1;
                switch (resultSetMetaData->getColumnType(columnIndex)) {
                    case NUOSQL_NULL:
                        vt = NUODB_TYPE_NULL;
                        break;
                    case NUOSQL_TINYINT:
                    case NUOSQL_SMALLINT:
                    case NUOSQL_INTEGER:
                    case NUOSQL_BIGINT:
                        if (resultSetMetaData->getScale(columnIndex) == 0) {
                            i64 = resultSet->getLong(columnIndex);
                            if (!resultSet->wasNull()) {
                                vt = NUODB_TYPE_INT64;
                            }
                            break;
                        }
                        // fallthrough; must be fetched as a string
                    case NUOSQL_NUMERIC:
                    case NUOSQL_DECIMAL: {
                        const char *string = resultSet->getString(columnIndex);
                        if (!resultSet->wasNull()) {
                            vt = NUODB_TYPE_BYTES; // strings are returned as bytes
                            i64 = reinterpret_cast<int64_t>(string);
                            i32 = std::strlen(string);
                        }
                        break;
                    }
                    case NUOSQL_FLOAT:
                    case NUOSQL_DOUBLE: {
                        union {
                            double float64;
                            int64_t i64;
                        } value = { resultSet->getDouble(columnIndex) };
                        if (!resultSet->wasNull()) {
                            vt = NUODB_TYPE_FLOAT64;
                            i64 = value.i64;
                        }
                        break;
                    }
                    case NUOSQL_BIT:
                    case NUOSQL_BOOLEAN:
                        i64 = resultSet->getBoolean(columnIndex);
                        if (!resultSet->wasNull()) {
                            vt = NUODB_TYPE_BOOL;
                        }
                        break;
                    case NUOSQL_DATE:
                    case NUOSQL_TIME:
                    case NUOSQL_TIMESTAMP: {
                        Timestamp *ts = resultSet->getTimestamp(columnIndex);
                        if (ts && !resultSet->wasNull()) {
                            vt = NUODB_TYPE_TIME;
                            i64 = ts->getSeconds();
                            i32 = ts->getNanos();
                        }
                        break;
                    }
                    default: {
                        const Bytes b = resultSet->getBytes(columnIndex);
                        if (!resultSet->wasNull()) {
                            vt = NUODB_TYPE_BYTES;
                            i64 = reinterpret_cast<int64_t>(b.data);
                            i32 = b.length;
                        }
                        break;
                    }
                }
                values[i].i64 = i64;
                values[i].i32 = i32;
                values[i].vt = vt;
            }
        }
        return 0;
    } catch (SQLException &e) {
        return setError(db, e);
    }
}