#ifndef _SQMMDB_METADATA_HPP_ #define _SQMMDB_METADATA_HPP_ // ------------------------------------------------------------------------------------------------ #include "Handle/Database.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { /* ------------------------------------------------------------------------------------------------ * Class that can hold and be used inspect database meta-data. */ class Metadata { // -------------------------------------------------------------------------------------------- friend class Database; // Only a valid database instance can construct this type. protected: // -------------------------------------------------------------------------------------------- typedef MMDB_metadata_s Type; // The managed type. // -------------------------------------------------------------------------------------------- typedef Type* Pointer; // Pointer to the managed type. typedef const Type* ConstPtr; // Constant pointer to the managed type. // -------------------------------------------------------------------------------------------- typedef Type& Reference; // Reference to the managed type. typedef const Type& ConstRef; // Constant reference to the managed type. /* -------------------------------------------------------------------------------------------- * Validate the managed database handle and throw an error if invalid. */ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) void Validate(CCStr file, Int32 line) const; #else void Validate() const; #endif // _DEBUG private: /* -------------------------------------------------------------------------------------------- * Validate the managed database handle and throw an error if invalid. */ #if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC) Pointer GetValid(CCStr file, Int32 line) const; #else Pointer GetValid() const; #endif // _DEBUG private: // --------------------------------------------------------------------------------------------- DbRef m_Handle; // The database associated with this meta-data. Pointer m_Metadata; // The inspected meta-data structure. /* -------------------------------------------------------------------------------------------- * Construct and with a specific meta-data. */ Metadata(const DbRef & db, Pointer metadata) : m_Handle(db), m_Metadata(metadata) { /* ... */ } public: /* -------------------------------------------------------------------------------------------- * Default constructor. (null) */ Metadata() : m_Handle(), m_Metadata(nullptr) { /* ... */ } /* -------------------------------------------------------------------------------------------- * Copy constructor. */ Metadata(const Metadata &) = default; /* -------------------------------------------------------------------------------------------- * Move constructor. */ Metadata(Metadata &&) = default; /* -------------------------------------------------------------------------------------------- * Copy assignment operator. */ Metadata & operator = (const Metadata &) = default; /* -------------------------------------------------------------------------------------------- * Move assignment operator. */ Metadata & operator = (Metadata &&) = default; /* -------------------------------------------------------------------------------------------- * Retrieve the internal result structure reference. */ Pointer GetHandle() { return m_Metadata; } /* -------------------------------------------------------------------------------------------- * Retrieve the internal result structure reference. */ ConstPtr GetHandle() const { return m_Metadata; } /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */ CSStr ToString() const { return m_Metadata ? m_Metadata->database_type : _SC(""); } /* -------------------------------------------------------------------------------------------- * Used by the script engine to retrieve the name from instances of this type. */ static SQInteger Typename(HSQUIRRELVM vm); /* -------------------------------------------------------------------------------------------- * See whether this instance references a valid database and result structure. */ bool IsValid() const { return m_Handle && m_Metadata; } /* -------------------------------------------------------------------------------------------- * Retrieve the node count. */ SQInteger GetNodeCount() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->node_count); } /* -------------------------------------------------------------------------------------------- * Retrieve the record size. */ SQInteger GetRecordSize() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->record_size); } /* -------------------------------------------------------------------------------------------- * Retrieve the IP version. */ SQInteger GetIpVersion() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->ip_version); } /* -------------------------------------------------------------------------------------------- * Retrieve the database type. */ CSStr GetDatabaseType() const { return SQMOD_GET_VALID(*this)->database_type; } /* -------------------------------------------------------------------------------------------- * Retrieve the number of language names. */ SQInteger GetLanguageCount() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->languages.count); } /* -------------------------------------------------------------------------------------------- * Retrieve the name of a certain language. */ CSStr GetLanguageName(Uint32 idx) const { // Validate the specified index if (idx > SQMOD_GET_VALID(*this)->languages.count) { STHROWF("The specified language index is out of range: %u > %u", idx, m_Metadata->languages.count); } // Return the requested name return m_Metadata->languages.names[idx]; } /* -------------------------------------------------------------------------------------------- * Retrieve the major version of the binary format. */ SQInteger GetBinaryFormatMajorVersion() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->binary_format_major_version); } /* -------------------------------------------------------------------------------------------- * Retrieve the minor version of the binary format. */ SQInteger GetBinaryFormatMinorVersion() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->binary_format_minor_version); } /* -------------------------------------------------------------------------------------------- * Retrieve the build epoch. */ Object GetBuildEpoch() const { // Obtain the initial stack size const StackGuard sg; // Push a long integer instance with the requested value on the stack SqMod_PushULongObject(DefaultVM::Get(), ConvTo< Uint64 >::From(SQMOD_GET_VALID(*this)->build_epoch)); // Obtain the object from the stack and return it return Var< Object >(DefaultVM::Get(), -1).value; } /* -------------------------------------------------------------------------------------------- * Retrieve the number of available description handles. */ SQInteger GetDescriptionCount() const { return ConvTo< SQInteger >::From(SQMOD_GET_VALID(*this)->description.count); } /* -------------------------------------------------------------------------------------------- * Retrieve the handle of a certain description. */ Description GetDescriptionHandle(Uint32 idx) const; /* -------------------------------------------------------------------------------------------- * Retrieve the description of a certain description handle. */ CSStr GetDescriptionValue(Uint32 idx) const { // Validate the specified index if (idx > SQMOD_GET_VALID(*this)->description.count) { STHROWF("The specified description index is out of range: %u > %u", idx, m_Metadata->description.count); } // Return the requested description value return m_Metadata->description.descriptions[idx]->description; } /* -------------------------------------------------------------------------------------------- * Retrieve the language of a certain description handle. */ CSStr GetDescriptionLanguage(Uint32 idx) const { // Validate the specified index if (idx > SQMOD_GET_VALID(*this)->description.count) { STHROWF("The specified description index is out of range: %u > %u", idx, m_Metadata->description.count); } // Return the requested description language return m_Metadata->description.descriptions[idx]->language; } }; } // Namespace:: SqMod #endif // _SQMMDB_METADATA_HPP_