From 9e6fc1ba7be8a4adca80557d28ef7320686bc69c Mon Sep 17 00:00:00 2001 From: David Villalobos Cambronero Date: Mon, 12 May 2008 19:01:13 +0000 Subject: [PATCH] [GB.DB.MYSQL] * NEW: Deleted some unneeded lines. * NEW: Added Info() function to _DataBase, _Table, _View, _Field that retrieves the symbol's information. * NEW: Added Insert() function to _Table, it acts as a normal INSERT INTO. * NEW: Optimizations in some functions and procedures. * NEW: Added CBool function, to convert Gambas Bolean types to MySQL types. git-svn-id: svn://localhost/gambas/trunk@1372 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- gb.db.mysql/src/gb.db.mysql/.info | 77 +++++++++++++-------- gb.db.mysql/src/gb.db.mysql/.lang/es.po | 32 ++++----- gb.db.mysql/src/gb.db.mysql/.list | 1 + gb.db.mysql/src/gb.db.mysql/_DataBase.class | 8 ++- gb.db.mysql/src/gb.db.mysql/_Field.class | 15 ++++ gb.db.mysql/src/gb.db.mysql/_MySQL.class | 13 ++++ gb.db.mysql/src/gb.db.mysql/_Table.class | 35 ++++++++++ gb.db.mysql/src/gb.db.mysql/_User.class | 2 +- gb.db.mysql/src/gb.db.mysql/_View.class | 7 ++ gb.db.mysql/src/gb.db.mysql/modMain.module | 2 +- 10 files changed, 142 insertions(+), 50 deletions(-) diff --git a/gb.db.mysql/src/gb.db.mysql/.info b/gb.db.mysql/src/gb.db.mysql/.info index ea1595d20..7e71b2ce3 100644 --- a/gb.db.mysql/src/gb.db.mysql/.info +++ b/gb.db.mysql/src/gb.db.mysql/.info @@ -22,7 +22,7 @@ m Modify m -(DataBase)s[(CharSet)s(Collation)s] +(DataBase)s[(Charset)s(Collation)s] Delete m @@ -30,7 +30,7 @@ m Add m -(DataBase)s[(CharSet)s(Collation)s] +(DataBase)s[(Charset)s(Collation)s] Dump m @@ -129,103 +129,103 @@ LONGBLOB Bit m s -[(Lenght)i] +[(Length)i] SignedFloat m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] UnsignedFloat m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] SignedDouble m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] UnsignedDouble m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] SignedDoublePrecision m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] UnsignedDoublePrecision m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] SignedReal m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] UnsignedReal m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] SignedDecimal m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] UnsignedDecimal m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] SignedDec m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] UnsignedDec m s -(Lenght)i[(Decimals)i] +(Length)i[(Decimals)i] Year m s -[(Lenght)i] +[(Length)i] Char m s -[(Lenght)i(CharSet)s(Collation)s] +[(Length)i(Charset)s(Collation)s] VarChar m s -(Lenght)i[(CharSet)s(Collation)s] +(Length)i[(Charset)s(Collation)s] Binary m s -(Lenght)i +(Length)i VarBinary m s -(Lenght)i +(Length)i TinyText m s -[(Lenght)i(CharSet)s(Collation)s] +[(Length)i(Charset)s(Collation)s] Blob m s -[(Lenght)i] +[(Length)i] Text m s -[(Lenght)i(CharSet)s(Collation)s] +[(Length)i(Charset)s(Collation)s] MediumText m s -[(CharSet)s(Collation)s] +[(Charset)s(Collation)s] LongText m s -[(CharSet)s(Collation)s] +[(Charset)s(Collation)s] Enum m s -(Values)String[];[(CharSet)s(Collation)s] +(Values)String[];[(Charset)s(Collation)s] Set m s -(Values)String[];[(CharSet)s(Collation)s] +(Values)String[];[(Charset)s(Collation)s] #_Field CA @@ -263,7 +263,7 @@ Engine p s -CharSet +Charset p s @@ -295,6 +295,10 @@ View r _View +User +r +_User + Confirm p b @@ -309,7 +313,7 @@ m Add m -(Table)s[(Engine)s(CharSet)s] +(Table)s[(Engine)s(Charset)s] Delete m @@ -318,6 +322,21 @@ LoadData m (Table)s(File)s +#_User + +CA +Delete +m + +(User)s(Host)s[(IfExists)b] +Add +m + +(User)s(Password)s(Host)s +Grant +m + +(Privileges)String[];(User)s(Password)s(Host)s(DataBase)s #_View CA diff --git a/gb.db.mysql/src/gb.db.mysql/.lang/es.po b/gb.db.mysql/src/gb.db.mysql/.lang/es.po index 16ed90689..3ffc66f98 100644 --- a/gb.db.mysql/src/gb.db.mysql/.lang/es.po +++ b/gb.db.mysql/src/gb.db.mysql/.lang/es.po @@ -10,30 +10,18 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: modMain.module:33 +#: modMain.module:41 msgid "Do you really want to execute the following statement?" msgstr "¿Realmente desea ejecutar la siguiente sentencia?" -#: modMain.module:33 +#: modMain.module:41 msgid "&Yes" msgstr "&Sí" -#: modMain.module:33 +#: modMain.module:41 msgid "&No" msgstr "&No" -#: _MySQL.class:28 -msgid "Not a MySQL connection." -msgstr "No es una conexión MySQL." - -#: _MySQL.class:67 -msgid "Character Set not valid." -msgstr "Character Set no válido." - -#: _MySQL.class:93 -msgid "Collation not valid." -msgstr "Callation no válida." - #: .project:1 msgid "mysql" msgstr "mysql" @@ -42,6 +30,14 @@ msgstr "mysql" msgid "Creates MySQL statements and executes them." msgstr "Crea sentencias MySQL y las ejecuta." -#: _Table.class:9 -msgid "Action no allowed." -msgstr "Acción no permitida." +#~ msgid "Not a MySQL connection." +#~ msgstr "No es una conexión MySQL." + +#~ msgid "Character Set not valid." +#~ msgstr "Character Set no válido." + +#~ msgid "Collation not valid." +#~ msgstr "Callation no válida." + +#~ msgid "Action no allowed." +#~ msgstr "Acción no permitida." diff --git a/gb.db.mysql/src/gb.db.mysql/.list b/gb.db.mysql/src/gb.db.mysql/.list index ce538b07e..9e56f8251 100644 --- a/gb.db.mysql/src/gb.db.mysql/.list +++ b/gb.db.mysql/src/gb.db.mysql/.list @@ -6,4 +6,5 @@ _Field _Index _MySQL _Table +_User _View diff --git a/gb.db.mysql/src/gb.db.mysql/_DataBase.class b/gb.db.mysql/src/gb.db.mysql/_DataBase.class index 2020c0ec1..a79013532 100644 --- a/gb.db.mysql/src/gb.db.mysql/_DataBase.class +++ b/gb.db.mysql/src/gb.db.mysql/_DataBase.class @@ -43,7 +43,6 @@ Public Procedure Dump(Optional File As String) sTmp = "mysqldump -u " & modMain.$Connection.User & " -h " & modMain.$Connection.Host If modMain.$Connection.Port Then sTmp &= " -P " & modMain.$Connection.Port sTmp &= " --password='" & modMain.$Connection.Password & "' -B " & modMain.$Connection.Name - 'If Tables Then sTmp &= " " & modMain.ArrayToString(Tables, True) If File Then sTmp &= " > " & File Else @@ -52,3 +51,10 @@ Public Procedure Dump(Optional File As String) Shell sTmp For Read End + +Public Function Info(DataBase As String) As String + + modMain.$hResult = modMain.$Connection.Exec("SHOW CREATE DATABASE " & DB.Quote(DataBase)) + Return modMain.$hResult["Create Database"] + +End diff --git a/gb.db.mysql/src/gb.db.mysql/_Field.class b/gb.db.mysql/src/gb.db.mysql/_Field.class index d04560a3c..b57ce58ad 100644 --- a/gb.db.mysql/src/gb.db.mysql/_Field.class +++ b/gb.db.mysql/src/gb.db.mysql/_Field.class @@ -48,3 +48,18 @@ Public Procedure ForeignKey(Field As String, RefTable As String, RefField As Str modMain.$ForeignKey.Add($sTmp) End + +Public Function Info(Table As String, Field As String) As String + + Dim sDefinition As String + + modMain.$hResult = modMain.$Connection.Exec("SHOW COLUMNS FROM " & Table & " LIKE '" & Field & "'") + sDefinition &= "Field: " & modMain.$hResult!Field & GB.NewLine + sDefinition &= "Type: " & modMain.$hResult!Type & GB.NewLine + sDefinition &= "Null: " & modMain.$hResult!Null & GB.NewLine + sDefinition &= "Key: " & modMain.$hResult!Key & GB.NewLine + sDefinition &= "Default: " & modMain.$hResult!Default & GB.NewLine + sDefinition &= "Extra: " & modMain.$hResult!Extra + Return sDefinition + +End diff --git a/gb.db.mysql/src/gb.db.mysql/_MySQL.class b/gb.db.mysql/src/gb.db.mysql/_MySQL.class index 1d134f997..a832453d2 100644 --- a/gb.db.mysql/src/gb.db.mysql/_MySQL.class +++ b/gb.db.mysql/src/gb.db.mysql/_MySQL.class @@ -152,3 +152,16 @@ Private Function User_Read() As _User Return $User End + +Public Function CBool(Value As Variant) As Integer + + Select (Value) + Case -1, "T" + Return 1 + Case 0 + Return 0 + Default + Error.Raise("Invalid value") + End Select + +End diff --git a/gb.db.mysql/src/gb.db.mysql/_Table.class b/gb.db.mysql/src/gb.db.mysql/_Table.class index 5bbafa4ec..7ab8ba41e 100644 --- a/gb.db.mysql/src/gb.db.mysql/_Table.class +++ b/gb.db.mysql/src/gb.db.mysql/_Table.class @@ -51,3 +51,38 @@ Public Procedure LoadData(Table As String, File As String) modMain.RunQuery(modMain.$Query) End + +Public Function Info(Table As String) As String + + modMain.$hResult = modMain.$Connection.Exec("SHOW CREATE TABLE " & DB.Quote(Table)) + Return modMain.$hResult["Create Table"] + +End + +Public Procedure Insert(Table As String, Fields As String[], Values As String[]) + + Dim iCounter As Integer + + modMain.$Query = "INSERT INTO " & DB.Quote(Table) + If Upper(Fields[0]) = "ALL" Then + modMain.$Query &= " VALUES (" + Else + modMain.$Query &= " (" & modMain.ArrayToString(Fields, False) & ") VALUES (" + Endif + 'If the Value is string type, we need to use the Apostrophe caracter + For iCounter = 0 To Values.Count - 1 + If (IsNumber(Val(Values[iCounter]))) Then + modMain.$Query &= Values[iCounter] & ", " + Else + If Upper(Values[iCounter]) = "NULL" Or Upper(Values[iCounter]) = "TRUE" Or Upper(Values[iCounter]) = "FALSE" Then + modMain.$Query &= Values[iCounter] & ", " + Else + modMain.$Query &= "'" & Values[iCounter] & "', " + Endif + Endif + Next + modMain.$Query = Mid(modMain.$Query, 1, Len(modMain.$Query) - 2) 'Strips the last two characters + modMain.$Query &= ")" + modMain.RunQuery(modMain.$Query) + +End diff --git a/gb.db.mysql/src/gb.db.mysql/_User.class b/gb.db.mysql/src/gb.db.mysql/_User.class index 924d4716a..b44095a01 100644 --- a/gb.db.mysql/src/gb.db.mysql/_User.class +++ b/gb.db.mysql/src/gb.db.mysql/_User.class @@ -27,7 +27,7 @@ Public Procedure Grant(Privileges As String[], User As String, Password As Strin Dim iCounter As Integer - If Privileges[0] = "All" Or Privileges[0] = "all" Or Privileges[0] = "ALL" Then + If Upper(Privileges[0]) = "ALL" Then modMain.$Query = "GRANT ALL PRIVILEGES" Else modMain.$Query = "GRANT " diff --git a/gb.db.mysql/src/gb.db.mysql/_View.class b/gb.db.mysql/src/gb.db.mysql/_View.class index 74a08dcd4..9108fa6ae 100644 --- a/gb.db.mysql/src/gb.db.mysql/_View.class +++ b/gb.db.mysql/src/gb.db.mysql/_View.class @@ -27,3 +27,10 @@ Public Procedure Delete(Views As String[], IfExists As Boolean) modMain.RunQuery(modMain.$Query) End + +Public Function Info(View As String) As String + + modMain.$hResult = modMain.$Connection.Exec("SHOW CREATE VIEW " & DB.Quote(View)) + Return modMain.$hResult["Create View"] + +End diff --git a/gb.db.mysql/src/gb.db.mysql/modMain.module b/gb.db.mysql/src/gb.db.mysql/modMain.module index acd190193..86fcf04ca 100644 --- a/gb.db.mysql/src/gb.db.mysql/modMain.module +++ b/gb.db.mysql/src/gb.db.mysql/modMain.module @@ -9,6 +9,7 @@ Public $PrimaryKey As String Public $Connection As New Connection Public $Confirm As Boolean = False Public $Process As Process +Public $hResult As Result Public Sub SetConnection(hCon As Connection) @@ -17,7 +18,6 @@ Public Sub SetConnection(hCon As Connection) End - Public Function ArrayToString(Array As String[], Quote As Boolean) As String Dim iCounter As Integer