summaryrefslogtreecommitdiff
path: root/utils/vmpi_private/mysql_wrapper/mysql_wrapper.cpp
blob: 3b441e67ad7bcf64c16869dad7970e02b8f77185 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

// mysql_wrapper.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"

extern "C"
{
	#include "mysql.h"
};

#include "imysqlwrapper.h"
#include <stdio.h>


static char* CopyString( const char *pStr )
{
	if ( !pStr )
	{
		pStr = "";
	}

	char *pRet = new char[ strlen( pStr ) + 1 ];
	strcpy( pRet, pStr );
	return pRet;
}


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}


class CCopiedRow
{
public:
	~CCopiedRow()
	{
		m_Columns.PurgeAndDeleteElements();
	}

	CUtlVector<char*> m_Columns;
};


class CMySQLCopiedRowSet : public IMySQLRowSet
{
public:
	~CMySQLCopiedRowSet()
	{
		m_Rows.PurgeAndDeleteElements();
		m_ColumnNames.PurgeAndDeleteElements();
	}

	virtual void			Release()
	{
		delete this;
	}

	virtual int				NumFields()
	{
		return m_ColumnNames.Count();
	}

	virtual const char*		GetFieldName( int iColumn )
	{
		return m_ColumnNames[iColumn];
	}

	virtual bool			NextRow()
	{
		++m_iCurRow;
		return m_iCurRow < m_Rows.Count();
	}

	virtual bool			SeekToFirstRow()
	{
		m_iCurRow = 0;
		return m_iCurRow < m_Rows.Count();
	}

	virtual CColumnValue	GetColumnValue( int iColumn )
	{
		return CColumnValue( this, iColumn );
	}

	virtual CColumnValue	GetColumnValue( const char *pColumnName )
	{
		return CColumnValue( this, GetColumnIndex( pColumnName ) );
	}

	virtual const char*		GetColumnValue_String( int iColumn )
	{
		if ( iColumn < 0 || iColumn >= m_ColumnNames.Count() )
			return "<invalid column specified>";
		else if ( m_iCurRow < 0 || m_iCurRow >= m_Rows.Count() )
			return "<invalid row specified>";
		else
			return m_Rows[m_iCurRow]->m_Columns[iColumn];
	}

	virtual long			GetColumnValue_Int( int iColumn )
	{
		return atoi( GetColumnValue_String( iColumn ) );
	}

	virtual int				GetColumnIndex( const char *pColumnName )
	{
		for ( int i=0; i < m_ColumnNames.Count(); i++ )
		{
			if ( stricmp( m_ColumnNames[i], pColumnName ) == 0 )
				return i;
		}
		return -1;
	}


public:
	int m_iCurRow;
	CUtlVector<CCopiedRow*> m_Rows;
	CUtlVector<char*> m_ColumnNames;
};


// -------------------------------------------------------------------------------------------------------- //
// CMySQL class.
// -------------------------------------------------------------------------------------------------------- //

class CMySQL : public IMySQL
{
public:
							CMySQL();
	virtual					~CMySQL();

	virtual bool			InitMySQL( const char *pDBName, const char *pHostName, const char *pUserName, const char *pPassword );
	virtual void			Release();
	virtual int				Execute( const char *pString );
	virtual IMySQLRowSet*	DuplicateRowSet();
	virtual unsigned long	InsertID();
	virtual int				NumFields();
	virtual const char*		GetFieldName( int iColumn );
	virtual bool			NextRow();
	virtual bool			SeekToFirstRow();
	virtual CColumnValue	GetColumnValue( int iColumn );
	virtual CColumnValue	GetColumnValue( const char *pColumnName );
	virtual const char*		GetColumnValue_String( int iColumn );
	virtual long			GetColumnValue_Int( int iColumn );
	virtual int				GetColumnIndex( const char *pColumnName );

	// Cancels the storage of the rows from the latest query.
	void					CancelIteration();

	virtual const char *	GetLastError( void );


public:

	MYSQL		*m_pSQL;
	MYSQL_RES	*m_pResult;
	MYSQL_ROW	m_Row;
	CUtlVector<MYSQL_FIELD>	m_Fields;

	char m_szLastError[128];
};


EXPOSE_INTERFACE( CMySQL, IMySQL, MYSQL_WRAPPER_VERSION_NAME );


// -------------------------------------------------------------------------------------------------------- //
// CMySQL implementation.
// -------------------------------------------------------------------------------------------------------- //

CMySQL::CMySQL()
{
	m_pSQL = NULL;
	m_pResult = NULL;
	m_Row = NULL;
}


CMySQL::~CMySQL()
{
	CancelIteration();

	if ( m_pSQL )
	{
		mysql_close( m_pSQL );
		m_pSQL = NULL;
	}
}


bool CMySQL::InitMySQL( const char *pDBName, const char *pHostName, const char *pUserName, const char *pPassword )
{
	MYSQL *pSQL = mysql_init( NULL );
	if ( !pSQL )
		return NULL;

	if ( !mysql_real_connect( pSQL, pHostName, pUserName, pPassword, pDBName, 0, NULL, 0 ) )
	{
		Q_strncpy( m_szLastError, mysql_error( pSQL ), sizeof(m_szLastError) );

		mysql_close( pSQL );
		return false;
	}

	m_pSQL = pSQL;
	return true;
}


void CMySQL::Release()
{
	delete this;
}


int CMySQL::Execute( const char *pString )
{
	CancelIteration();

	int result = mysql_query( m_pSQL, pString );
	if ( result == 0 )
	{
		// Is this a query with a result set?
		m_pResult = mysql_store_result( m_pSQL );
		if ( m_pResult )
		{
			// Store the field information.
			int count = mysql_field_count( m_pSQL );
			MYSQL_FIELD *pFields = mysql_fetch_fields( m_pResult );
			m_Fields.CopyArray( pFields, count );
			return 0;
		}
		else
		{
			// No result set. Was a set expected?
			if ( mysql_field_count( m_pSQL ) != 0 )
				return 1;	// error! The query expected data but didn't get it.
		}
	}
	else
	{
		const char *pError = mysql_error( m_pSQL );
		pError = pError;
	}

	return result;
}


IMySQLRowSet* CMySQL::DuplicateRowSet()
{
	CMySQLCopiedRowSet *pSet = new CMySQLCopiedRowSet;

	pSet->m_iCurRow = -1;

	pSet->m_ColumnNames.SetSize( m_Fields.Count() );
	for ( int i=0; i < m_Fields.Count(); i++ )
		pSet->m_ColumnNames[i] = CopyString( m_Fields[i].name );

	while ( NextRow() )
	{
		CCopiedRow *pRow = new CCopiedRow;
		pSet->m_Rows.AddToTail( pRow );
		pRow->m_Columns.SetSize( m_Fields.Count() );

		for ( int i=0; i < m_Fields.Count(); i++ )
		{
			pRow->m_Columns[i] = CopyString( m_Row[i] );
		}
	}

	return pSet;
}


unsigned long CMySQL::InsertID()
{
	return mysql_insert_id( m_pSQL );
}


int CMySQL::NumFields()
{
	return m_Fields.Count();
}


const char* CMySQL::GetFieldName( int iColumn )
{
	return m_Fields[iColumn].name;
}


bool CMySQL::NextRow()
{
	if ( !m_pResult )
		return false;

	m_Row = mysql_fetch_row( m_pResult );
	if ( m_Row == 0 )
	{
		return false;
	}
	else
	{
		return true;
	}
}


bool CMySQL::SeekToFirstRow()
{
	if ( !m_pResult )
		return false;

	mysql_data_seek( m_pResult, 0 );
	return true;
}


CColumnValue CMySQL::GetColumnValue( int iColumn )
{
	return CColumnValue( this, iColumn );
}


CColumnValue CMySQL::GetColumnValue( const char *pColumnName )
{
	return CColumnValue( this, GetColumnIndex( pColumnName ) );
}


const char* CMySQL::GetColumnValue_String( int iColumn )
{
	if ( m_Row && iColumn >= 0 && iColumn < m_Fields.Count() && m_Row[iColumn] )
		return m_Row[iColumn];
	else
		return "";
}


long CMySQL::GetColumnValue_Int( int iColumn )
{
	return atoi( GetColumnValue_String( iColumn ) );
}


int CMySQL::GetColumnIndex( const char *pColumnName )
{
	for ( int i=0; i < m_Fields.Count(); i++ )
	{
		if ( stricmp( pColumnName, m_Fields[i].name ) == 0 )
		{
			return i;
		}
	}
	
	return -1;
}


void CMySQL::CancelIteration()
{
	m_Fields.Purge();
	
	if ( m_pResult )
	{
		mysql_free_result( m_pResult );
		m_pResult = NULL;
	}

	m_Row = NULL;
}

const char *CMySQL::GetLastError( void )
{
	// Default to the last error if m_pSQL was not successfully initialized
	const char *pszLastError = m_szLastError;

	if ( m_pSQL )
	{
		pszLastError = mysql_error( m_pSQL );
	}

	return pszLastError;
}