2 * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved.
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
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.
20 // mdsclient - friendly interface to CDSA MDS API
22 #include <security_cdsa_client/mdsclient.h>
26 namespace CssmClient
{
30 // DLAccess gets a virtual destructor just in case
37 // Basic Record objects (abstract)
39 Record::Record(const char * const * names
)
40 : CssmAutoData(Allocator::standard(Allocator::sensitive
))
45 void Record::addAttributes(const char * const * name
)
49 mAttributes
.add(CssmDbAttributeInfo(*name
++));
57 // Tables and their components (non-template common features)
59 TableBase::TableBase(DLAccess
&source
, CSSM_DB_RECORDTYPE type
, bool getData
/* = true */)
60 : database(source
), mRecordType(type
), mGetData(getData
)
64 TableBase::Handle::~Handle()
67 mAccess
->dlAbortQuery(query
);
70 TableBase::Uid::~Uid()
73 mAccess
->dlFreeUniqueId(uid
);
76 TableBase::Iterator::Iterator(DLAccess
*ac
, CSSM_HANDLE query
,
77 CSSM_DB_UNIQUE_RECORD
*id
, Record
*record
, bool getData
)
78 : mAccess(ac
), mQuery(new Handle(ac
, query
)),
79 mUid(new Uid(ac
, id
)), mRecord(record
), mGetData(getData
)
83 void TableBase::Iterator::advance(Record
*fill
)
85 RefPointer
<Record
> newRecord
= fill
; // hold it safely
86 CSSM_DB_UNIQUE_RECORD
*id
;
87 CssmAutoData
data(mAccess
->allocator());
88 if (mAccess
->dlGetNext(mQuery
->query
, newRecord
->attributes(),
89 mGetData
? &data
.get() : NULL
, id
)) {
91 newRecord
->recordData() = data
;
92 mUid
= new Uid(mAccess
, id
);
95 mQuery
->query
= CSSM_INVALID_HANDLE
; // was automatically aborted
96 // release all iterator resources and make me == end()
104 uint32
TableBase::erase(const CSSM_QUERY
&query
)
106 CSSM_DB_UNIQUE_RECORD
*id
;
107 CssmDbRecordAttributeData noAttributes
;
108 CSSM_HANDLE handle
= database
.dlGetFirst(query
, noAttributes
, NULL
, id
);
109 if (handle
== CSSM_INVALID_HANDLE
)
110 return 0; // no match, nothing erased
113 database
.dlDeleteRecord(id
);
115 database
.dlFreeUniqueId(id
);
116 } while (database
.dlGetNext(handle
, noAttributes
, NULL
, id
));
120 uint32
TableBase::erase(const Query
&query
)
122 return erase(query
.cssmQuery());
126 } // end namespace CssmClient
127 } // end namespace Security