Advertisement
Guest User

Untitled

a guest
May 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ''''''''''''''''''''
  2. '' DATABASE CLASS ''
  3. ''''''''''''''''''''
  4.  
  5. Class DBMySQLClass
  6.  
  7.   Private ConnectionString
  8.   Private DB
  9.   Public QueryCount
  10.   Public OpenTime
  11.  
  12.   Private Sub Class_Initialize()
  13.     Set DB = CreateObject("ADODB.Connection")
  14.   End Sub
  15.  
  16.   Private Sub Class_Terminate()
  17.     Me.Close
  18.     Set DB = Nothing
  19.   End Sub
  20.  
  21.   ' Create
  22.  '
  23.  Public Sub Setting(Hostname,Database,Username,Password)
  24.     ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver};SERVER="&Hostname&";DB="&Database&";USER="&Username&";PASSWORD="&Password&";OPTION=3;"
  25.   End Sub
  26.  
  27.   ' Reset
  28.  ' Reset connection to database
  29.  Public Sub Reset()
  30.     Me.Close
  31.     Set DB = Nothing
  32.     Set DB = CreateObject("ADODB.Connection")
  33.   End Sub
  34.  
  35.   ' Connect
  36.  ' Setup connection to SQL server.
  37.  Private Sub Connect()
  38.     If DB.ConnectionString = "" Then
  39.       DB.ConnectionString = ConnectionString
  40.     End If
  41.   End Sub
  42.  
  43.   ' Open
  44.  ' Open the MySQL connection
  45.  Public Sub Open()
  46.     If DateDiff("s",OpenTime,Now()) > 180 Then Close
  47.     If Not CBool(DB.State) Then
  48.       Connect
  49.       DB.Open
  50.       OpenTime = Now
  51.     End If
  52.   End Sub
  53.  
  54.   ' Close
  55.  ' Close the MySQL connection
  56.  Public Sub Close()
  57.     If CBool(DB.State) Then
  58.       DB.Close
  59.     End If
  60.   End Sub
  61.  
  62.   ' Query
  63.  ' Execute a SQL query
  64.  Public Function Query(SQL)
  65.     StartTimer("query")
  66.     Open
  67.     Set Query = DB.Execute(SQL)
  68.     Call EndTimer("query",SQL)
  69.   End Function
  70.  
  71.   ' Count
  72.  ' Returns result count.
  73.  ' Note: resets pointer, so besure to save result count to a variable
  74.  Public Function Count(Result)
  75.     If Result.EOF AND Result.BOF Then
  76.       Count = 0
  77.     Else
  78.       Result.MoveFirst
  79.       While Not Result.EOF
  80.         Count = Count + 1
  81.         Result.MoveNext
  82.       Wend
  83.       Result.MoveFirst
  84.     End If
  85.   End Function
  86.  
  87.   ' EscapeString
  88.  ' Converts a string into a SQL escaped string.
  89.  ' Important to use this to reduce SQL injection exploitations.
  90.  Public Function EscapeString(ES)
  91.     EscapeString = Array_Replace(ES,Array("\","'","""",vbNewLine,vbCr,vbLf,vbTab),Array("\\","\'","\""","\n","","\n","\t"),1,-1,vbBinaryCompare)
  92.   End Function
  93.  
  94.   ' WildCard
  95.  ' Convert VB's usual wildcard to MySQL wildcard.
  96.  Public Function Wildcard(WC)
  97.     Wildcard = Replace(WC,"*","%")
  98.   End Function
  99.  
  100. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement