r1671: *** empty log message ***
[clsql.git] / interfaces / mysql / clsql-mysql.c
1 /****************************************************************************
2  * FILE IDENTIFICATION
3  *
4  *   Name:          mysql-helper.cl
5  *   Purpose:       Helper functions for mysql.cl to handle 64-bit parts of API
6  *   Programmer:    Kevin M. Rosenberg
7  *   Date Started:  Mar 2002
8  *
9  * $Id: clsql-mysql.c,v 1.3 2002/03/27 05:48:22 kevin Exp $
10  *
11  * This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
12  *
13  * CLSQL users are granted the rights to distribute and use this software
14  * as governed by the terms of the Lisp Lesser GNU Public License
15  * (http://opensource.franz.com/preamble.html), also known as the LLGPL.
16  ***************************************************************************/
17
18 #ifdef WIN32
19 #include <windows.h>
20
21 BOOL WINAPI DllEntryPoint(HINSTANCE hinstdll, DWORD fdwReason,
22                           LPVOID lpvReserved)
23 {
24         return 1;
25 }
26        
27 #define DLLEXPORT __declspec(dllexport)
28
29 #else
30 #define DLLEXPORT 
31 #endif
32
33
34 #include <mysql.h>
35
36 /* Need to assemble a 64-bit integer to send to MySQL */
37 DLLEXPORT
38 void
39 clsql_mysql_data_seek (MYSQL_RES* res, unsigned int offset_high32,
40                        unsigned int offset_low32)
41 {
42   my_ulonglong offset;
43
44   offset = offset_high32;
45   offset = offset << 32;
46   offset += offset_low32;
47   
48   mysql_data_seek (res, offset);
49 }
50
51 /* The following functions are used to return 64-bit integers to Lisp.
52    They return the 32-bit low part and store in upper 32-bits in a 
53    located sent via a pointer */
54
55 const unsigned int bitmask_32bits = 0xFFFFFFFF;
56
57 #define lower_32bits(int64) ((unsigned int) int64 & bitmask_32bits)
58 #define upper_32bits(int64) ((unsigned int) (int64 >> 32))
59
60 DLLEXPORT
61 unsigned int
62 clsql_mysql_affected_rows (MYSQL* res, unsigned int* pHigh32)
63 {
64   my_ulonglong nAffected = mysql_affected_rows (res);
65   *pHigh32 = upper_32bits(nAffected);
66   return lower_32bits(nAffected);
67 }
68
69 DLLEXPORT
70 unsigned int
71 clsql_mysql_insert_id (MYSQL* mysql, unsigned int* pHigh32)
72 {
73   my_ulonglong insert_id = mysql_insert_id (mysql);
74   *pHigh32 = upper_32bits(insert_id);
75   return lower_32bits(insert_id);
76 }
77
78
79 /* Reads a 64-bit integer string, returns result as two 32-bit integers */
80
81 DLLEXPORT
82 unsigned int
83 atol64 (const unsigned char* str, int* pHigh32)
84 {
85   long long result = 0;
86   int minus = 0;
87   int first_char = *str;
88   if (first_char == '+')
89     ++str;
90   else if (first_char == '-') {
91     minus = 1;
92     ++str;
93   }
94
95   while (*str) {
96     int i = *str - '0';
97     if (i < 0 || i > 9) /* Non-numeric character -- quit */
98       break;
99     result = i + (10 * result);
100     str++;
101   }
102   if (minus)
103     result = -result;
104
105   *pHigh32 = upper_32bits(result);
106   return lower_32bits(result);
107 }
108
109   
110   
111