コード例 #1
0
ファイル: Connection.cs プロジェクト: xuanxinhuiqing/fastdb
        /// <summary>
        /// Insert object in the database. There is should be table in the database with
        /// name equal to the full class name of the inserted object (comparison is
        /// case sensitive). GigaBASE will store to the database all non-static and
        /// non-transient fields from the class. All Java primitive types,
        /// <code>java.lang.string</code>, arrays of primitive types or strings, <code>java.util.Date</code>
        /// are supported by GigaBASE. If <code>int</code> field is marked as <code>volatile</code>, it
        /// is assumed to be autoincremented field - unique value to this field is assigned automatically
        /// by GigaBASE.
        /// </summary>
        /// <param name="obj">object to be inserted inthe database</param>
        /// <returns>reference to the inserted object</returns>
        ///
        public Reference insert(object obj)
        {
            if (socket == null)
            {
                throw new CliError("Session is not opened");
            }
            Type            clazz     = obj.GetType();
            string          className = clazz.Name;
            TableDescriptor tableDesc;

            lock (tableHash) {
                tableDesc = (TableDescriptor)tableHash[className];
                if (tableDesc == null)
                {
                    tableDesc            = new TableDescriptor(clazz);
                    tableHash[className] = tableDesc;
                }
            }
            ComBuffer buf = new ComBuffer(CLICommand.cli_cmd_prepare_and_insert);

            buf.putAsciiz("insert into " + className);
            buf.putByte(tableDesc.nColumns);
            tableDesc.writeColumnDefs(buf);
            tableDesc.writeColumnValues(buf, obj);
            send(buf);
            receive(buf, 12);
            CLIStatus rc = (CLIStatus)buf.getInt();

            if (rc == CLIStatus.cli_ok)
            {
                int rowid = buf.getInt();
                int oid   = buf.getInt();
                if (tableDesc.autoincrement)
                {
                    for (int i = tableDesc.nColumns; --i >= 0;)
                    {
                        if (tableDesc.types[i] == CLIType.cli_autoincrement)
                        {
                            tableDesc.columns[i].SetValue(obj, rowid);
                        }
                    }
                }
                return((oid != 0) ? new Reference(oid) : null);
            }
            else
            {
                throw new CliError("Insert object operation failed with status " + rc);
            }
        }
コード例 #2
0
ファイル: Statement.cs プロジェクト: nakijun/FusionDB
 internal Statement(Connection con, string sql, int stmtId)
 {
     this.stmtId = stmtId;
     int src = 0, dst = 0, len = sql.Length;
     int p = sql.IndexOf("from");
     if (p < 0 && (p  = sql.IndexOf("FROM")) < 0) {
     throw new CliError("Bad statment: SELECT FROM expected");
     }
     p += 5;
     while (p < len && sql[p] == ' ') {
     p += 1;
     }
     int q = p;
     while (++q < len && sql[q] != ' ');
     if (p+1 == q) {
     throw new CliError("Bad statment: table name expected after FROM");
     }
     string tableName = sql.Substring(p, q-p);
     lock (Connection.tableHash) {
     tableDesc = (TableDescriptor)Connection.tableHash[tableName];
     if (tableDesc == null) {
         Type tableClass = Type.GetType(tableName);
         if (tableClass == null) {
             if (con.pkgs != null)
             {
                 foreach (string pkg in con.pkgs) {
                     tableClass = Type.GetType(pkg + '.' + tableName);
                     if (tableClass != null)
                     {
                         break;
                     }
                 }
             }
             if (tableClass == null)
             {
                 foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
                 {
                     foreach (Module mod in ass.GetModules())
                     {
                         foreach (Type t in mod.FindTypes(new TypeFilter(FindTypeByName), tableName))
                         {
                             if (tableClass != null)
                             {
                                 throw new CliError("Class " + tableName + " exists in more than one scope");
                             }
                             tableClass = t;
                         }
                     }
                 }
             }
             if (tableClass == null) {
                 throw new CliError("Class " + tableName + " not found");
             }
         }
         tableDesc = new TableDescriptor(tableClass);
         Connection.tableHash[tableName] = tableDesc;
     }
     }
     byte[] buf = new byte[len];
     while (src < len) {
     char ch = sql[src];
     if (ch == '\'') {
         do {
             do {
                 buf[dst++] = (byte)sql[src++];
                 if (src == len) {
                     throw new CliError("Unterminated string constant in query");
                 }
             } while (sql[src] != '\'');
             buf[dst++] = (byte)'\'';
         } while (++src < len && sql[src] == '\'');
     } else if (ch == '%') {
         int begin = src;
         do {
             if (++src == len) {
                 break;
             }
             ch = sql[src];
         } while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
                  || (ch >= '0' && ch <= '9') || ch == '_');
         if (ch == '%') {
             throw new CliError("Invalid parameter name");
         }
         Parameter param = new Parameter(sql.Substring(begin, src-begin));
         if (lastParam == null) {
             parameters = param;
         } else {
             lastParam.next = param;
         }
         lastParam = param;
         nParams += 1;
         buf[dst++] = (byte)'\0';
     } else {
         buf[dst++]= (byte)sql[src++];
     }
     }
     stmt = buf;
     stmtLen = dst;
     this.con = con;
 }
コード例 #3
0
ファイル: Connection.cs プロジェクト: armano2/fastdb
 /// <summary>
 /// Insert object in the database. There is should be table in the database with
 /// name equal to the full class name of the inserted object (comparison is 
 /// case sensitive). GigaBASE will store to the database all non-static and 
 /// non-transient fields from the class. All Java primitive types, 
 /// <code>java.lang.string</code>, arrays of primitive types or strings, <code>java.util.Date</code>
 /// are supported by GigaBASE. If <code>int</code> field is marked as <code>volatile</code>, it
 /// is assumed to be autoincremented field - unique value to this field is assigned automatically 
 /// by GigaBASE. 
 /// </summary>
 /// <param name="obj">object to be inserted inthe database</param>
 /// <returns>reference to the inserted object</returns>
 ///
 public Reference insert(object obj)
 {
     if (socket == null) {
     throw new CliError("Session is not opened");
     }
     Type clazz = obj.GetType();
     string className = clazz.Name;
     TableDescriptor tableDesc;
     lock (tableHash) {
     tableDesc = (TableDescriptor)tableHash[className];
     if (tableDesc == null) {
     tableDesc = new TableDescriptor(clazz);
     tableHash[className] = tableDesc;
     }
     }
     ComBuffer buf = new ComBuffer(CLICommand.cli_cmd_prepare_and_insert);
     buf.putAsciiz("insert into " + className);
     buf.putByte(tableDesc.nColumns);
     tableDesc.writeColumnDefs(buf);
     tableDesc.writeColumnValues(buf, obj);
     send(buf);
     receive(buf, 12);
     CLIStatus rc = (CLIStatus)buf.getInt();
     if (rc == CLIStatus.cli_ok) {
     int rowid = buf.getInt();
     int oid = buf.getInt();
     if (tableDesc.autoincrement) {
     for (int i = tableDesc.nColumns; --i >= 0;) {
     if (tableDesc.types[i] == CLIType.cli_autoincrement) {
     tableDesc.columns[i].SetValue(obj, rowid);
     }
     }
     }
     return (oid != 0) ? new Reference(oid) : null;
     } else {
     throw new CliError("Insert object operation failed with status " + rc);
     }
 }
コード例 #4
0
ファイル: Statement.cs プロジェクト: xuanxinhuiqing/fastdb
        internal Statement(Connection con, string sql, int stmtId)
        {
            this.stmtId = stmtId;
            int src = 0, dst = 0, len = sql.Length;
            int p = sql.IndexOf("from");

            if (p < 0 && (p = sql.IndexOf("FROM")) < 0)
            {
                throw new CliError("Bad statment: SELECT FROM expected");
            }
            p += 5;
            while (p < len && sql[p] == ' ')
            {
                p += 1;
            }
            int q = p;

            while (++q < len && sql[q] != ' ')
            {
                ;
            }
            if (p + 1 == q)
            {
                throw new CliError("Bad statment: table name expected after FROM");
            }
            string tableName = sql.Substring(p, q - p);

            lock (Connection.tableHash) {
                tableDesc = (TableDescriptor)Connection.tableHash[tableName];
                if (tableDesc == null)
                {
                    Type tableClass = Type.GetType(tableName);
                    if (tableClass == null)
                    {
                        if (con.pkgs != null)
                        {
                            foreach (string pkg in con.pkgs)
                            {
                                tableClass = Type.GetType(pkg + '.' + tableName);
                                if (tableClass != null)
                                {
                                    break;
                                }
                            }
                        }
                        if (tableClass == null)
                        {
                            foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
                            {
                                foreach (Module mod in ass.GetModules())
                                {
                                    foreach (Type t in mod.FindTypes(new TypeFilter(FindTypeByName), tableName))
                                    {
                                        if (tableClass != null)
                                        {
                                            throw new CliError("Class " + tableName + " exists in more than one scope");
                                        }
                                        tableClass = t;
                                    }
                                }
                            }
                        }
                        if (tableClass == null)
                        {
                            throw new CliError("Class " + tableName + " not found");
                        }
                    }
                    tableDesc = new TableDescriptor(tableClass);
                    Connection.tableHash[tableName] = tableDesc;
                }
            }
            byte[] buf = new byte[len];
            while (src < len)
            {
                char ch = sql[src];
                if (ch == '\'')
                {
                    do
                    {
                        do
                        {
                            buf[dst++] = (byte)sql[src++];
                            if (src == len)
                            {
                                throw new CliError("Unterminated string constant in query");
                            }
                        } while (sql[src] != '\'');
                        buf[dst++] = (byte)'\'';
                    } while (++src < len && sql[src] == '\'');
                }
                else if (ch == '%')
                {
                    int begin = src;
                    do
                    {
                        if (++src == len)
                        {
                            break;
                        }
                        ch = sql[src];
                    } while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
                             (ch >= '0' && ch <= '9') || ch == '_');
                    if (ch == '%')
                    {
                        throw new CliError("Invalid parameter name");
                    }
                    Parameter param = new Parameter(sql.Substring(begin, src - begin));
                    if (lastParam == null)
                    {
                        parameters = param;
                    }
                    else
                    {
                        lastParam.next = param;
                    }
                    lastParam  = param;
                    nParams   += 1;
                    buf[dst++] = (byte)'\0';
                }
                else
                {
                    buf[dst++] = (byte)sql[src++];
                }
            }
            stmt     = buf;
            stmtLen  = dst;
            this.con = con;
        }