1/*
2 * Copyright (c) 2000-2004,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19//
20// dlquery - search query sublanguage for DL and MDS queries
21//
22#include <security_cdsa_client/dlquery.h>
23
24
25namespace Security {
26namespace CssmClient {
27
28
29//
30// Constructing Relations
31//
32Comparison::Comparison(const Comparison &r)
33	: mName(r.mName), mOperator(r.mOperator), mFormat(r.mFormat),
34	  mValue(Allocator::standard())
35{
36	mValue.copy(r.mValue);
37}
38
39Comparison &Comparison::operator = (const Comparison &r)
40{
41	mName = r.mName;
42	mOperator = r.mOperator;
43	mFormat = r.mFormat;
44	mValue.copy(r.mValue);
45	return *this;
46}
47
48
49Comparison::Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const char *s)
50	: mName(attr.name()), mOperator(op), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_STRING),
51	mValue(Allocator::standard(), StringData(s))
52{ }
53
54Comparison::Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const std::string &s)
55	: mName(attr.name()), mOperator(op), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_STRING),
56	mValue(Allocator::standard(), StringData(s))
57{ }
58
59Comparison::Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, uint32 value)
60	: mName(attr.name()), mOperator(op), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_UINT32),
61	mValue(Allocator::standard(), CssmData::wrap(value))
62{ }
63
64Comparison::Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, bool value)
65	: mName(attr.name()), mOperator(op), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_UINT32),
66	mValue(Allocator::standard(), CssmData::wrap(uint32(value ? 1 : 0)))
67{ }
68
69Comparison::Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const CssmData &data)
70	: mName(attr.name()), mOperator(op), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_BLOB),
71	mValue(Allocator::standard(), data)
72{ }
73
74Comparison::Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const CSSM_GUID &guid)
75	: mName(attr.name()), mOperator(op), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_STRING),
76	mValue(Allocator::standard(), StringData(Guid::overlay(guid).toString()))
77{
78}
79
80
81Comparison::Comparison(const Attribute &attr)
82	: mName(attr.name()), mOperator(CSSM_DB_NOT_EQUAL), mFormat(CSSM_DB_ATTRIBUTE_FORMAT_UINT32),
83	  mValue(Allocator::standard(), CssmData::wrap(uint32(CSSM_FALSE)))
84{
85}
86
87Comparison operator ! (const Attribute &attr)
88{
89	return Comparison(attr, CSSM_DB_EQUAL, uint32(CSSM_FALSE));
90}
91
92
93//
94// Query methods
95//
96Query &Query::operator = (const Query &q)
97{
98	mRelations = q.mRelations;
99	mQueryValid = false;
100	return *this;
101}
102
103
104//
105// Form the CssmQuery from a Query object.
106// We cache this in mQuery, which we have made sure isn't copied along.
107//
108const CssmQuery &Query::cssmQuery() const
109{
110	if (!mQueryValid) {
111		// record type remains at ANY
112		mQuery.conjunctive(CSSM_DB_AND);
113		for (vector<Comparison>::const_iterator it = mRelations.begin(); it != mRelations.end(); it++) {
114			CssmSelectionPredicate pred;
115			pred.dbOperator(it->mOperator);
116			pred.attribute().info() = CssmDbAttributeInfo(it->mName.c_str(), it->mFormat);
117			pred.attribute().set(it->mValue.get());
118			mPredicates.push_back(pred);
119		}
120		mQuery.set((uint32)mPredicates.size(), &mPredicates[0]);
121		mQueryValid = true;
122	}
123	return mQuery;
124}
125
126
127} // end namespace CssmClient
128} // end namespace Security
129