📄 cmysql.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cMySQL"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'################################################################################'
'# VBMySQL APi version .01 #
'# Copyright (C) 2000 Jim Banasiak #
'# #
'# This program is free software; you can redistribute it and/or #
'# modify it under the terms of the GNU General Public License #
'# as published by the Free Software Foundation; either version 2 #
'# of the License, or (at your option) any later version. #
'# #
'# This program is distributed in the hope that it will be useful, #
'# but WITHOUT ANY WARRANTY; without even the implied warranty of #
'# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
'# GNU General Public License for more details. #
'# #
'# You should have received a copy of the GNU General Public License #
'# along with this program; if not, write to the Free Software #
'# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
'# #
'################################################################################
'/****************************************************************************/'
' Class Wrapper: cMySQL
' We have to 'wrap' around pointers to various mysql API calls
' to hide pointers and that other garbage for our Vb heros
' This will use the API's from libmysql and translate
'/****************************************************************************/'
Option Explicit
Private myConHandle As Long 'valid connection pointer for this class
Private myRec As API_MYSQL 'valid record to hold ourselves
Private myRec_res As API_MYSQL_RES 'this is we we will store results for ourselves
Private myRec_field As API_MYSQL_FIELD
Private myRec_rows As API_MYSQL_ROWS
Private mUser As String
Private mPassword As String
Private mHost As String
Private mDb As String
Private mUnix_socket As String
Private mClientFlag As Long
Private mPort As Long
'mysql_init() --handled upon creation
Private Sub Class_Initialize()
myConHandle = API_mysql_init(myRec)
If myConHandle Then
CopyMemory myRec, ByVal myConHandle, LenB(myRec) 'move the memory back from pointer
Else
Err.Raise 512, "cMysql::Initialize", "Cannot Initialize a mysql record::Fatal"
End If
End Sub
'mysql_close() --handled upon destruction, unconditionally close
Private Sub Class_Terminate()
If myConHandle <> 0 Then Call API_mysql_close(myRec)
End Sub
Public Sub real_connect(Optional host As String = vbNullString, Optional user As String = vbNullString, Optional Passwd As String = vbNullString, Optional DB As String = vbNullString, Optional Port As Long = MYSQL_PORT, Optional Unix_Socket As String = vbNullString, Optional clientflag As Long = 0)
If Len(host) > 0 Then mHost = host
If Len(user) > 0 Then mUser = user
If Len(Passwd) > 0 Then mPassword = Passwd
If Len(DB) > 0 Then mDb = DB
If Len(Unix_Socket) > 0 Then mUnix_socket = Unix_Socket
If Port <> MYSQL_PORT Then mPort = Port
If clientflag <> 0 Then mClientFlag = clientflag
myConHandle = API_mysql_real_connect(myRec, StrPtr(StrConv(mHost, vbFromUnicode)), StrPtr(StrConv(mUser, vbFromUnicode)), StrPtr(StrConv(mPassword, vbFromUnicode)), StrPtr(StrConv(mDb, vbFromUnicode)), Port, StrPtr(StrConv(mUnix_socket, vbFromUnicode)), mClientFlag)
If myConHandle Then
CopyMemory myRec, ByVal myConHandle, LenB(myRec)
Else
Err.Raise API_mysql_errno(myRec), "cMysql::real_connect", ptr2str(API_mysql_error(myRec))
End If
End Sub
Public Sub connect(Optional host As String = vbNullString, Optional user As String = vbNullString, Optional Passwd As String = vbNullString)
If Len(host) > 0 Then mHost = host
If Len(user) > 0 Then mUser = user
If Len(Passwd) > 0 Then mPassword = Passwd
myConHandle = API_mysql_connect(myRec, StrPtr(StrConv(mHost, vbFromUnicode)), StrPtr(StrConv(mUser, vbFromUnicode)), StrPtr(StrConv(mPassword, vbFromUnicode)))
If myConHandle Then
CopyMemory myRec, ByVal myConHandle, LenB(myRec) 'again we have to mooo-ve the memory
Else
Err.Raise API_mysql_errno(myRec), "cMysql::connect", ptr2str(API_mysql_error(myRec))
End If
End Sub
Public Function affected_rows() As Long
affected_rows = convert642l(API_mysql_affected_rows(myRec))
End Function
Public Sub create_db(DB As String)
Dim ret As Long
ret = API_mysql_create_db(myRec, StrPtr(StrConv(DB, vbFromUnicode)))
If ret <> 0 Then Err.Raise API_mysql_errno(myRec), "cMysql::create_db", ptr2str(API_mysql_error(myRec))
End Sub
Public Sub debug_push(DBUG_PUSH As String) ' does a DBUG_PUSH with the given string.
Call API_mysql_debug(StrPtr(StrConv(DBUG_PUSH, vbFromUnicode)))
End Sub
Public Sub drop_db(DB As String)
Dim ret As Long
ret = API_mysql_drop_db(myRec, StrPtr(StrConv(DB, vbFromUnicode)))
If ret <> 0 Then Err.Raise API_mysql_errno(myRec), "cMysql::drop_db", ptr2str(API_mysql_error(myRec))
End Sub
Public Sub dump_debug_info() 'forces server to dump debug info
Dim ret As Long
ret = API_mysql_dump_debug_info(myRec)
If ret <> 0 Then Err.Raise API_mysql_errno(myRec), "cMysql::dump_debug_info", ptr2str(API_mysql_error(myRec))
End Sub
Public Function eof() As Boolean
eof = API_mysql_eof(myRec_res)
End Function
Public Function err_no() As Long
err_no = API_mysql_errno(myRec)
End Function
Public Function err_description() As String
err_description = ptr2str(API_mysql_error(myRec))
End Function
Public Sub escape_string(to_ As String, from_ As String, from_length As Long)
Dim ret As Long
ret = API_mysql_escape_string(myRec, StrPtr(StrConv(to_, vbFromUnicode)), StrPtr(StrConv(from_, vbFromUnicode)), from_length)
End Sub
Public Sub free_result()
'free the memeory
Dim ret As Long
ret = API_mysql_free_result(myRec_res)
If ret <> 0 Then Err.Raise API_mysql_errno(myRec), "cMysql::free_result", ptr2str(API_mysql_error(myRec))
End Sub
Public Function get_client_info() As String
Dim ret As Long
ret = API_mysql_get_client_info
If ret <> 0 Then get_client_info = ptr2str(ret) Else Err.Raise API_mysql_errno(myRec), "cMysql::get_client_info", ptr2str(API_mysql_error(myRec))
End Function
Public Function get_host_info() As String
Dim ret As Long
ret = API_mysql_get_host_info(myRec)
If ret <> 0 Then get_host_info = ptr2str(ret) Else Err.Raise API_mysql_errno(myRec), "cMysql::get_host_info", ptr2str(API_mysql_error(myRec))
End Function
Public Function get_proto_info() As String
Dim ret As Long
ret = API_mysql_get_proto_info(myRec)
If ret <> 0 Then get_proto_info = ptr2str(ret) Else Err.Raise API_mysql_errno(myRec), "cMysql::get_proto_info", ptr2str(API_mysql_error(myRec))
End Function
Public Function get_server_info()
Dim ret As Long
ret = API_mysql_get_server_info(myRec)
If ret <> 0 Then get_server_info = ptr2str(ret) Else Err.Raise API_mysql_errno(myRec), "cMysql::get_server_info", ptr2str(API_mysql_error(myRec))
End Function
Public Function info() As String
Dim ret As Long
ret = API_mysql_info(myRec)
If ret <> 0 Then info = ptr2str(ret) Else Err.Raise API_mysql_errno(myRec), "cMysql::info", ptr2str(API_mysql_error(myRec))
End Function
Public Function Insert_ID() As Long
Insert_ID = convert642l(API_mysql_insert_id(myRec))
End Function
Public Sub Kill(PID As Long)
Dim ret As Long
ret = API_mysql_kill(myRec, PID)
If ret <> 0 Then Err.Raise API_mysql_errno(myRec), "cMysql::kill", ptr2str(API_mysql_error(myRec))
End Sub
Public Function list_dbs(Optional wild As String = "%") As Recordset
Dim ret As Long
ret = API_mysql_list_dbs(myRec, StrPtr(StrConv(wild, vbFromUnicode)))
If ret Then
CopyMemory myRec_res, ByVal ret, LenB(myRec_res)
Set list_dbs = ConvertResult(myRec_res)
End If
End Function
Public Function list_fields(Optional table As String = vbNullString, Optional wild As String = "%") As Recordset
Dim ret As Long
ret = API_mysql_list_fields(myRec, StrPtr(StrConv(table, vbFromUnicode)), StrPtr(StrConv(wild, vbFromUnicode)))
If ret Then
CopyMemory myRec_res, ByVal ret, LenB(myRec_res)
Set list_fields = ConvertResult(myRec_res)
End If
End Function
Public Function list_processes() As Recordset
Dim ret As Long
ret = API_mysql_list_processes(myRec)
If ret Then
CopyMemory myRec_res, ByVal ret, LenB(myRec_res)
Set list_processes = ConvertResult(myRec_res)
End If
End Function
Public Function list_tables(Optional wild As String = "%") As Recordset
Dim ret As Long
ret = API_mysql_list_tables(myRec, StrPtr(StrConv(wild, vbFromUnicode)))
If ret Then
CopyMemory myRec_res, ByVal ret, LenB(myRec_res)
Set list_tables = ConvertResult(myRec_res)
End If
End Function
Public Function num_fields() As Long
num_fields = API_mysql_num_fields(myRec_res)
End Function
Public Function num_rows() As Long
num_rows = convert642l(API_mysql_num_rows(myRec_res))
End Function
'#############################################################
' These are things I left out because they are mostly
' used behind the scenes when converting to a recordset (i'm lazy...)
'#############################################################
'don't know if we need to set options
'Public Sub options(l As API_mysql_option, Optional arg As String = vbNullString)
'Dim ret As Long
'ret = API_mysql_options(myRec, , StrPtr(StrConv(arg, vbFromUnicode)))
'If ret <> 0 Then Err.Raise API_mysql_errno(myRec), "cMysql::options", ptr2str(API_mysql_error(myRec))
'End Sub
'Public Sub row_seek() 'Seeks to a row in a result set, using value returned from mysql_row_tell().
' API_mysql_row_seek
' myRec_rows.data
'End Sub
'Public Sub row_tell() 'current_cursor position
'Dim ret As Long
' ret = API_mysql_row_tell(myRec_res) 'pointer to mysql_rows
' CopyMemory myRec_rows, ret, LenB(myRec_rows)
'End Sub
'Public Sub fetch_field()
'Public Sub fetch_field_direct()
'Public Sub fetch_fields()
'Public Sub fetch_lengths()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -