1/*
2 * Copyright (c) 2004 Apple Computer, 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
23#ifndef _H_CDSA_CLIENT_DLQUERY
24#define _H_CDSA_CLIENT_DLQUERY
25
26#include <security_cdsa_utilities/cssmdb.h>
27#include <string>
28#include <vector>
29
30
31namespace Security {
32namespace CssmClient {
33
34
35//
36// A DL record attribute
37//
38class Attribute {
39public:
40	Attribute(const std::string &name) : mName(name) { }
41	Attribute(const char *name) : mName(name) { }
42
43	const std::string &name() const { return mName; }
44
45private:
46	std::string mName;
47};
48
49
50//
51// A comparison (attribute ~rel~ constant-value)
52//
53class Comparison {
54	friend class Query;
55public:
56	Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const char *s);
57	Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const std::string &s);
58	Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, uint32 v);
59	Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, bool v);
60	Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const CSSM_GUID &guid);
61	Comparison(const Attribute &attr, CSSM_DB_OPERATOR op, const CssmData &data);
62
63	Comparison(const Attribute &attr);
64	friend Comparison operator ! (const Attribute &attr);
65
66	Comparison(const Comparison &r);
67	Comparison &operator = (const Comparison &r);
68
69private:
70	std::string mName;
71	CSSM_DB_OPERATOR mOperator;
72	CSSM_DB_ATTRIBUTE_FORMAT mFormat;
73	CssmAutoData mValue;
74};
75
76template <class Value>
77Comparison operator == (const Attribute &attr, const Value &value)
78{ return Comparison(attr, CSSM_DB_EQUAL, value); }
79
80template <class Value>
81Comparison operator != (const Attribute &attr, const Value &value)
82{ return Comparison(attr, CSSM_DB_NOT_EQUAL, value); }
83
84template <class Value>
85Comparison operator < (const Attribute &attr, const Value &value)
86{ return Comparison(attr, CSSM_DB_LESS_THAN, value); }
87
88template <class Value>
89Comparison operator > (const Attribute &attr, const Value &value)
90{ return Comparison(attr, CSSM_DB_GREATER_THAN, value); }
91
92template <class Value>
93Comparison operator % (const Attribute &attr, const Value &value)
94{ return Comparison(attr, CSSM_DB_CONTAINS, value); }
95
96
97//
98// A Query
99//
100class Query {
101public:
102	Query() : mQueryValid(false) { }
103	Query(const Comparison r) : mQueryValid(false) { mRelations.push_back(r); }
104	Query(const Attribute &attr) : mQueryValid(false) { mRelations.push_back(attr); }
105
106	Query(const Query &q) : mRelations(q.mRelations), mQueryValid(false) { }
107
108	Query &operator = (const Query &q);
109
110	Query &add(const Comparison &r)
111	{ mRelations.push_back(r); return *this; }
112
113	const CssmQuery &cssmQuery() const;
114
115private:
116	std::vector<Comparison> mRelations;
117
118	// cached CssmQuery equivalent of this object
119	mutable bool mQueryValid;   // mQuery has been constructed
120	mutable vector<CssmSelectionPredicate> mPredicates; // holds lifetimes for mQuery
121	mutable CssmQuery mQuery;
122};
123
124inline Query operator && (Query c, const Comparison &r)
125{ return c.add(r); }
126
127
128} // end namespace CssmClient
129} // end namespace Security
130
131#endif // _H_CDSA_CLIENT_DLQUERY
132