Table of Contents
Gamma code can be run in two ways: in interactive mode, and as an executable program. This first tutorial demonstrates an interactive session and introduces some of the basic Gamma/MySQL methods and functions needed to get started.
[sh]$ gamma This software is free for non-commercial use, and no valid commercial license is installed. For more information, please contact info@cogent.ca. Gamma(TM) Advanced Programming Language Copyright (C) Cogent Real-Time Systems Inc., 1995-2005. All rights reserved. Gamma Version 5.2 Build 5 at Feb 11 2005 10:21:37 Gamma> require ("MySQLSupport"); "/usr/cogent/require/MySQLSupport.g"
Gamma> mysql = new MYSQL(); {MYSQL (client_flag . 0) (connector_fd) (db) (field_alloc . ... rest of class definition}
Gamma> mysql.connect ("localhost", "test", "");
{MYSQL (client_flag . 8325) (connector_fd) (db) (field_alloc .
...rest of class definition}Gamma> mysql.query(string("CREATE DATABASE ", "test_interactive"));
0
Gamma> mysql.select_db ("test_interactive");
0
Gamma> try mysql.create_table("friends"); catch;
nilGamma> mysql.add_column ("friends", "name", "VARCHAR(20)", nil, nil);
nil
Gamma> mysql.add_column ("friends", "phone", "VARCHAR(20)", nil, nil);
nil
Gamma> mysql.add_column ("friends", "email", "VARCHAR(20)", nil, nil);
nilGamma> mysql.class_from_table (#Friend, nil, "friends");
(defclass Friend nil [(__columns . #0=[id name phone email]) (__primary_key . id)
(__table . #1="friends")][email id name phone])Gamma> newrow = new(Friend); {Friend (email) (id) (name) (phone)} Gamma> newrow.name = "Kisbet Grimes"; "Kisbet Grimes" Gamma> newrow.phone = "414-595-3389"; "414-595-3389" Gamma> newrow.email = "kisbetg@netmail.com"; "kisbetg@netmail.com" Gamma> mysql.insert(newrow); nil Gamma> newrow = new(Friend); {Friend (email) (id) (name) (phone)} Gamma> newrow.name = "Merle Royer"; "Merle Royer" Gamma> newrow.phone = "605-783-2045"; "605-783-2045" Gamma> newrow.email = "mark55@interisp.com"; "mark55@interisp.com" Gamma> mysql.insert(newrow); nil
Gamma> all_friends = mysql.query_and_store("SELECT * FROM friends");
{MYSQL_RES (current_field . 0) (eof . 1) (field_count . 4)
(fields . [{MYSQL_FIELD (decimals . 0) (def) (flags . 49667)
(length . 11) (max_length . 1) (name . "id")
(table . "friends") (type . 3)}
{MYSQL_FIELD (decimals . 0) (def) (flags . 1) (length . 20)
(max_length . 13) (name . "name") (table . "friends")
(type . 253)} {MYSQL_FIELD (decimals . 0) (def)
(flags . 1) (length . 20) (max_length . 12) (name . "phone")
(table . "friends") (type . 253)}
{MYSQL_FIELD (decimals . 0) (def) (flags . 1) (length . 20)
(max_length . 19) (name . "email") (table . "friends")
(type . 253)}])}Gamma> mysql_pretty_print(stdout, all_friends);
id name phone email
-- ------------- ------------ -------------------
1 Kisbet Grimes 414-595-3389 kisbetg@netmail.com
2 Merle Royer 605-783-2045 mark55@interisp.com
nilGamma> mysql.query(string("DROP DATABASE ", "test_interactive"));
0
Gamma> These are the fundamentals of using Gamma/MySQL. The next tutorial (Section 2.2, “Tutorial 2: create_pets.g”) is an executable program that follows a similar sequence, and introduces more Gamma/MySQL methods and functions. As a review and for your convenience, all the input for the tutorial you have just completed is as follows:
require ("MySQLSupport");
mysql = new MYSQL();
mysql.connect ("localhost", "test", "");
mysql.query(string("CREATE DATABASE ", "test_interactive"));
mysql.select_db ("test_interactive");
try mysql.create_table("friends"); catch;
mysql.add_column ("friends", "name", "VARCHAR(20)", nil, nil);
mysql.add_column ("friends", "phone", "VARCHAR(20)", nil, nil);
mysql.add_column ("friends", "email", "VARCHAR(20)", nil, nil);
mysql.class_from_table (#Friend, nil, "friends");
newrow = new(Friend);
newrow.name = "Kisbet Grimes";
newrow.phone = "414-595-3389";
newrow.email = "kisbetg@netmail.com";
mysql.insert(newrow);
newrow = new(Friend);
newrow.name = "Merle Royer";
newrow.phone = "605-783-2045";
newrow.email = "mark55@interisp.com";
mysql.insert(newrow);
all_friends = mysql.query_and_store("SELECT * FROM friends");
mysql_pretty_print(stdout, all_friends);
mysql.query(string("DROP DATABASE ", "test_interactive"));
Copyright © 1995-2010 by Cogent Real-Time Systems, Inc. All rights reserved.