mirror of https://gitee.com/bigwinds/arangodb
unique constraint
This commit is contained in:
parent
264e12daff
commit
675c5d0476
|
@ -173,7 +173,7 @@ actions.defineHttp({
|
|||
}
|
||||
|
||||
if (req.requestType != actions.PUT) {
|
||||
actions.unsupported(req, res);
|
||||
actions.resultUnsupported(req, res);
|
||||
}
|
||||
else {
|
||||
var limit = body.limit;
|
||||
|
@ -290,7 +290,7 @@ actions.defineHttp({
|
|||
}
|
||||
|
||||
if (req.requestType != actions.PUT) {
|
||||
actions.unsupported(req, res);
|
||||
actions.resultUnsupported(req, res);
|
||||
}
|
||||
else {
|
||||
var limit = body.limit;
|
||||
|
@ -396,7 +396,7 @@ actions.defineHttp({
|
|||
}
|
||||
|
||||
if (req.requestType != actions.PUT) {
|
||||
actions.unsupported(req, res);
|
||||
actions.resultUnsupported(req, res);
|
||||
}
|
||||
else {
|
||||
var limit = body.limit;
|
||||
|
@ -482,7 +482,7 @@ actions.defineHttp({
|
|||
}
|
||||
|
||||
if (req.requestType != actions.PUT) {
|
||||
actions.unsupported(req, res);
|
||||
actions.resultUnsupported(req, res);
|
||||
}
|
||||
else {
|
||||
var example = body.example;
|
||||
|
@ -511,6 +511,55 @@ actions.defineHttp({
|
|||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief returns all documents of a collection matching a given example
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
actions.defineHttp({
|
||||
url : API + "BY-EXAMPLE-HASH",
|
||||
context : "api",
|
||||
|
||||
callback : function (req, res) {
|
||||
var body = actions.getJsonBody(req, res);
|
||||
|
||||
if (body === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.requestType != actions.PUT) {
|
||||
actions.resultUnsupported(req, res);
|
||||
}
|
||||
else {
|
||||
var limit = body.limit;
|
||||
var skip = body.skip;
|
||||
var name = body.collection;
|
||||
var example = body.example;
|
||||
var index = body.index;
|
||||
|
||||
var name = body.collection;
|
||||
var id = parseInt(name) || name;
|
||||
var collection = internal.db._collection(id);
|
||||
|
||||
if (collection == null) {
|
||||
actions.collectionNotFound(req, res, name);
|
||||
}
|
||||
else if (typeof example !== "object") {
|
||||
actions.badParameter(req, res, "example");
|
||||
}
|
||||
else {
|
||||
try {
|
||||
var result = collection.BY_EXAMPLE_HASH(index, example, skip, limit);
|
||||
|
||||
actions.resultOk(req, res, actions.HTTP_OK, result);
|
||||
}
|
||||
catch (err) {
|
||||
actions.resultException(req, res, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @}
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -1233,6 +1233,56 @@ ArangoCollection.prototype.ensureCapConstraint = function (size) {
|
|||
return requestResult;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief adds a unique constraint
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ArangoCollection.prototype.ensureUniqueConstraint = function () {
|
||||
var body;
|
||||
var fields = [];
|
||||
|
||||
for (var i = 0; i < arguments.length; ++i) {
|
||||
fields.push(arguments[i]);
|
||||
}
|
||||
|
||||
body = { type : "hash", unique : true, fields : fields };
|
||||
|
||||
var requestResult = this._database._connection.POST("/_api/index?collection=" + encodeURIComponent(this._id), JSON.stringify(body));
|
||||
|
||||
TRI_CheckRequestResult(requestResult);
|
||||
|
||||
return requestResult;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief queries by example
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ArangoCollection.prototype.BY_EXAMPLE_HASH = function (index, example, skip, limit) {
|
||||
var body;
|
||||
|
||||
limit = limit || null;
|
||||
skip = skip || null;
|
||||
|
||||
if (index.hasOwnProperty("id")) {
|
||||
index = index.id;
|
||||
}
|
||||
|
||||
body = { collection : this._id, index : index, skip : skip, limit : limit, example : {} };
|
||||
|
||||
for (var key in example) {
|
||||
if (example.hasOwnProperty(key)) {
|
||||
body.example[key] = example[key];
|
||||
}
|
||||
}
|
||||
|
||||
var requestResult = this._database._connection.PUT("/_api/simple/BY-EXAMPLE-HASH", JSON.stringify(body));
|
||||
|
||||
TRI_CheckRequestResult(requestResult);
|
||||
|
||||
return requestResult;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief adds an geo index
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -78,8 +78,6 @@ function UniqueConstraintSuite() {
|
|||
var idx = collection.ensureUniqueConstraint("a");
|
||||
var id = idx.id;
|
||||
|
||||
print(idx);
|
||||
|
||||
assertNotEqual(0, id);
|
||||
assertEqual("hash", idx.type);
|
||||
assertEqual(true, idx.unique);
|
||||
|
|
Loading…
Reference in New Issue