summaryrefslogtreecommitdiff
path: root/utils/vmpi/mysql_wrapper.h
blob: 01ca9d988f96604d6e51127733cfce7c6bda8f1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef MYSQL_WRAPPER_H
#define MYSQL_WRAPPER_H
#ifdef _WIN32
#pragma once
#endif


#include "utlvector.h"


class IMySQL;


// This is a helper class to build queries like the stream IO.
class CMySQLQuery
{
friend class CMySQL;

public:
	// This is like a sprintf, but it will grow the string as necessary.
	void				Format( PRINTF_FORMAT_STRING const char *pFormat, ... );

private:
	CUtlVector<char>	m_QueryText;
};


class CColumnValue
{
public:

					CColumnValue( CMySQL *pSQL, int iColumn );

	const char*		String();
	long			Int32();
	unsigned long	UInt32();

private:
	CMySQL		*m_pSQL;
	int			m_iColumn;
};


class IMySQL
{
public:
	virtual void			Release() = 0;

	// These execute SQL commands. They return 0 if the query was successful.
	virtual int				Execute( const char *pString ) = 0;
	virtual int				Execute( CMySQLQuery &query ) = 0;

	// If you just inserted rows into a table with an AUTO_INCREMENT column,
	// then this returns the (unique) value of that column.
	virtual unsigned long	InsertID() = 0;

	// If you just executed a select statement, then you can use these functions to
	// iterate over the result set.

	// Get the number of columns in the data returned from the last query (if it was a select statement).	
	virtual int				NumFields() = 0;
	
	// Get the name of each column returned by the last query.
	virtual const char*		GetFieldName( int iColumn ) = 0;

	// Call this in a loop until it returns false to iterate over all rows the query returned.
	virtual bool			NextRow() = 0;

	// You can call this to start iterating over the result set from the start again.
	// Note: after calling this, you have to call NextRow() to actually get the first row's value ready.
	virtual bool			SeekToFirstRow() = 0;

	virtual CColumnValue	GetColumnValue( int iColumn ) = 0;
	virtual CColumnValue	GetColumnValue( const char *pColumnName ) = 0;

	// You can call this to get the index of a column for faster lookups with GetColumnValue( int ).
	// Returns -1 if the column can't be found.
	virtual int				GetColumnIndex( const char *pColumnName ) = 0;
};


IMySQL* InitMySQL( const char *pDBName, const char *pHostName="", const char *pUserName="", const char *pPassword="" );



// ------------------------------------------------------------------------------------------------ //
// Inlines.
// ------------------------------------------------------------------------------------------------ //

inline CColumnValue::CColumnValue( CMySQL *pSQL, int iColumn )
{
	m_pSQL = pSQL;
	m_iColumn = iColumn;
}


#endif // MYSQL_WRAPPER_H